Python math 模塊與 cmath 模塊

Python math 模塊提供了許多對浮點數的數學運算函數。

Python cmath 模塊包含了一些用於複數運算的函數。

cmath 模塊的函數跟 math 模塊函數基本一致,區別是 cmath 模塊運算的是複數,math 模塊運算的是數學運算。

要使用 math 或 cmath 函數必須先導入:

import math

1、math 模塊

查看 math 查看包中的內容:

>>> import math
>>> dir(math)
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']
>>>

math模塊常用函數:

pi                數字常量,圓周率

e                 表示一個常量

sqrt(x)         求x的平方根

fabs(x)         返回x的絕對值

factorial(x)    取x的階乘的值

fmod(x)        得到x/y的餘數,其值是一個浮點數

pow(x, y)     返回x的y次方,即x**y

isfinite(x)    如果x是正無窮大或負無窮大,則返回True,否則返回False

isinf(x)        如果x是正無窮大或負無窮大,則返回True,否則返回False

hypot(x)      如果x是不是無窮大的數字,則返回True,否則返回False

isnan(x)      如果x不是數字,則返回True,否則返回False

ldexp(x)      返回x*(2**i)的值////

log(x, a)      返回x的自然對數,以a爲基數(不寫則默認以e爲基數,a參數給定時,將x的對數返回給定的a,計算式爲:log(x)/log(a)

log10(x)       返回x的以10爲底的對數

log1p(x)       返回x+1的自然對數(基數爲e)的值

log2(x)         返回x的基2對數

modf(x)        返回由x的小數部分和整數部分組成的元組

trunc(x)        返回x的整數部分

ceil(x)           取大於等於x的最小的整數值,如果x是一個整數,則返回x

floor(x)        取小於等於x的最大的整數值,如果x是一個整數,則返回自身

radians(x)    把角度x轉換成弧度,與degrees 爲反函數

degrees(x)    把x從弧度轉換成角度

sin(x)          求x(x爲弧度)的正弦值

sinh(x)        求x(x爲弧度)的雙曲正弦值

cos(x)          求x的餘弦,x必須是弧度

tan(x)          返回x(x爲弧度)的正切值

tanh(x)        返回x(x爲弧度)的雙曲正切值

copysign(x, y)把y的正負號加到x前面,可以使用0

exp(x)        返回math.e,也就是2.71828的x次方

expm1(x)    返回math.e的x(其值爲2.71828)次方的值減1

frexp(x)        返回一個元組(m,e),其計算方式爲:x分別除0.5和1,得到一個值的範圍

fsum(x)        對迭代器裏的每個元素進行求和操作

gcd(x)          返回x和y的最大公約數


2、cmath模塊

  Python 提供對於複數運算的支持,複數在 Python 中的表達式爲 C==c.real+c.imag*j,複數 C 由他的實部和虛部組成。

對於複數,Python 支持它的加減乘除運算,同時提供了 cmath 模塊對其他複雜運算進行支持。cmath 模塊和 Python 中的 math 模塊對應, math提供對於實數的支持, 在這裏主要討論 cmath 模塊中的幾個函數的用法。

查看 cmath 查看包中的內容

>>> import cmath
>>> dir(cmath)
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atanh', 'cos', 'cosh', 'e', 'exp', 'inf', 'infj', 'isclose', 'isfinite', 'isinf', 'isnan', 'log', 'log10', 'nan', 'nanj', 'phase', 'pi', 'polar', 'rect', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau']
>>>

1).極座標和笛卡爾座標表示的轉換。

C==c.real+c.imag*j 的複數表示方法爲複數的笛卡爾表示法, cmath 模塊中的 polar() 方法和 rect() 方法可以對複數進行極座標表示和笛卡爾表示方法的轉換。 例:

>>> import cmath
>>> Z=1+2j
>>> print cmath.polar(Z)
(2.23606797749979, 1.1071487177940904)
>>> a,b=cmath.polar(Z)
>>> print cmath.rect(a,b)
(1+2j)
>>>

polar 函數對一個輸入的笛卡爾形勢的複數進行計算,輸出爲一個二元組,第一個值爲Z的模值, 第二個爲幅度值。 rect() 函數對輸入的模和幅度值進行計算輸出笛卡爾表示。

如果需要單獨對一個複數進行幅度值的求解,可以調用 cmath.phrase(x) 函數,返回幅度值。

2).複數的冪指數與對數函數

複數的指數函數爲 cmath.exp(x), 用來求解 e^x 表達式。

cmath.log(x[,base]) 用來求以 Base 爲底的 x 的對數。

cmath.log10(x) 用來求以 10 爲底 x 的對數

cmath.sqrt(x) 用來求 x 的平方根。

3).複數的三角函數方程

包括所有的三角函數計算 acos(x) asin(x) atan(x) sin(x) cos(x) tan(x)

4). 參數類判斷

cmath.isinf(x) 如果x的實部或者虛部爲無窮大,則返回true。

cmath.isnan(x)如果x的實部或者虛步不是數字則返回true。

5). 常量支持

cmat.pi 浮點值, 表示圓周率的大小

cmat.e 浮點值, 表示自然對數的底


3、math常用函數用法

ceil(x)      取整數值(向上取);如果x是一個整數,則返回x

>>> math.ceil(4.01)
5
>>> math.ceil(4.99)
5
>>> math.ceil(-3.99)
-3
>>> math.ceil(-3.01)
-3

copysign(x, y)    把y的正負號加到x前面。可以使用0,即copysign(1.0, -0.0)返回 -1.0

>>> math.copysign(2,3)
2.0
>>> math.copysign(2,-3)
-2.0
>>> math.copysign(3,8)
3.0
>>> math.copysign(3,-8)
-3.0

cos(x)    求x的餘弦,x必須是弧度

>>> math.cos(math.pi/4)        #math.pi/4    表示弧度,轉換成角度爲45
0.7071067811865476
>>> math.cos(math.pi/3)        #math.pi/3表示弧度,轉換成角度爲60
0.5000000000000001
>>> math.cos(math.pi/6)        #math.pi/6表示弧度,轉換成角度爲30
0.8660254037844387

degrees(x)    把x從弧度轉換成角度

>>> math.degrees(math.pi/4)
45.0
>>> math.degrees(math.pi)
180.0
>>> math.degrees(math.pi/6)
29.999999999999996
>>> math.degrees(math.pi/3)
59.99999999999999

e        表示一個常量

>>> math.e
2.718281828459045

exp(x)    返回math.e,即 2.71828的x次方

>>> math.exp(1)
2.718281828459045
>>> math.exp(2)
7.38905609893065
>>> math.exp(3)
20.085536923187668

expm1(x)    返回exp(x)-1,即 math.e 的x(其值爲2.71828)次方的值減 1

>>> math.expm1(1)
1.718281828459045
>>> math.expm1(2)
6.38905609893065
>>> math.expm1(3)
19.085536923187668

fabs(x)    返回x的絕對值

>>> math.fabs(-0.003)
0.003
>>> math.fabs(-110)
110.0
>>> math.fabs(100)
100.0

factorial(x)    取x的階乘的值 x!

>>> math.factorial(1)
1
>>> math.factorial(2)
2
>>> math.factorial(3)
6
>>> math.factorial(5)
120
>>> math.factorial(10)
3628800

floor(x)    取 ≤ x 的最大的整數值,如果x是一個整數,則返回自身

>>> math.floor(4.1)
4
>>> math.floor(4.999)
4
>>> math.floor(-4.999)
-5
>>> math.floor(-4.01)
-5

fmod(x, y)     得到x/y的餘數,其值是一個浮點數

>>> math.fmod(20,3)
2.0
>>> math.fmod(20,7)
6.0

frexp(x)

#返回一個元組(m,e),其計算方式爲:x分別除0.5和1,得到一個值的範圍,
#2**e的值在這個範圍內,e取符合要求的最大整數值,然後x/(2**e),得到m的值
#如果x等於0,則m和e的值都爲0,m的絕對值的範圍爲(0.5,1)之間,不包括0.5和1
frexp(x)
Return the mantissa and exponent of x, as pair (m, e).
m is a float and e is an int, such that x = m * 2.**e.
If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
>>> math.frexp(10)
(0.625, 4)
>>> math.frexp(75)
(0.5859375, 7)
>>> math.frexp(-40)
(-0.625, 6)
>>> math.frexp(-100)
(-0.78125, 7)
>>> math.frexp(100)
(0.78125, 7)

fsum(iterable)    對迭代器裏的每個元素進行求和操作

>>> math.fsum([1,2,3,4])
10.0
>>> math.fsum((1,2,3,4))
10.0
>>> math.fsum((-1,-2,-3,-4))
-10.0
>>> math.fsum([-1,-2,-3,-4])
-10.0

gcd(x, y)     返回x和y的最大公約數

>>> math.gcd(8,6)
2
>>> math.gcd(40,20)
20
>>> math.gcd(8,12)
4

hypot(x, y)    返回(x**2+y**2)平方根,即 sqrt(x*x + y*y)

>>> math.hypot(3,4)
5.0
>>> math.hypot(6,8)
10.0

isfinite(x)     如果x是不是無窮大的數字,則返回True,否則返回False

>>> math.isfinite(100)
True
>>> math.isfinite(0)
True
>>> math.isfinite(0.1)
True
>>> math.isfinite("a")
>>> math.isfinite(0.0001)
True

isinf(x)    如果x是正無窮大或負無窮大,則返回True,否則返回False

>>> math.isinf(234)
False
>>> math.isinf(0.1)
False

isnan(x)    如果x不是數字True,否則返回False

>>> math.isnan(23)
False
>>> math.isnan(0.01)
False

ldexp(x, i)    返回值x*(2**i)

>>> math.ldexp(5,5)
160.0
>>> math.ldexp(3,5)
96.0

log(x[, base])    返回x的自然對數,默認以e爲基數,base參數給定時,將x的對數返回給定的base,計算式爲:log(x)/log(base)

>>> math.log(10)
2.302585092994046
>>> math.log(11)
2.3978952727983707
>>> math.log(20)
2.995732273553991

log10(x)    返回x的以10爲基數的對數

>>> math.log10(10)
1.0
>>> math.log10(100)
2.0
#即10的1.3次方的結果爲20
>>> math.log10(20)
1.3010299956639813

log1p(x)    返回x+1的自然對數(基數爲e)的值

>>> math.log(10)
2.302585092994046
>>> math.log1p(10)
2.3978952727983707
>>> math.log(11)
2.3978952727983707

log2(x)    返回x的基2對數

>>> math.log2(32)
5.0
>>> math.log2(20)
4.321928094887363
>>> math.log2(16)
4.0

modf(x)    返回由x的小數部分和整數部分組成的元組

>>> math.modf(math.pi)
(0.14159265358979312, 3.0)
>>> math.modf(12.34)
(0.33999999999999986, 12.0)

pi        數字常量,圓周率

>>> print(math.pi)
3.141592653589793

pow(x, y)     返回x的y次方,即x**y

>>> math.pow(3,4)
81.0
>>> 
>>> math.pow(2,7)
128.0

radians(x)    把角度x轉換成弧度

>>> math.radians(45)
0.7853981633974483
>>> math.radians(60)
1.0471975511965976

sin(x)    求x(x爲弧度)的正弦值

>>> math.sin(math.pi/4)
0.7071067811865475
>>> math.sin(math.pi/2)
1.0
>>> math.sin(math.pi/3)
0.8660254037844386

sqrt(x)    求x的平方根

>>> math.sqrt(100)
10.0
>>> math.sqrt(16)
4.0
>>> math.sqrt(20)
4.47213595499958

tan(x)        返回x(x爲弧度)的正切值

>>> math.tan(math.pi/4)
0.9999999999999999
>>> math.tan(math.pi/6)
0.5773502691896257
>>> math.tan(math.pi/3)
1.7320508075688767

trunc(x:Real)    返回x的整數部分

>>> math.trunc(6.789)
6
>>> math.trunc(math.pi)
3
>>> math.trunc(2.567)
2

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章