从实数创建复数。可以使用直接赋值语句或使用complex()函数来创建Python复数。
复数通常在我们使用两个实数的地方使用。例如,由电压(V)和电流(C)定义的电路被用于几何学,科学计算和微积分中。
complex([real[, imag]])
>>> c = 3 +6j >>> print(type(c)) <class 'complex'> >>> print(c) (3+6j) >>> >>> c1 = complex(3,6) >>> print(type(c1)) <class 'complex'> >>> print(c1) (3+6j)
从以上结果中,我们可以看到python复数是complex类型。每个复数由一个实部和一个虚部组成。
>>> #Complex Number: >>> c = (3 + 6j) >>> >>> #Real Part of complex number >>> print('Complex Number: Real Part is = ', c. real) Complex Number: Real Part is = 3.0 >>> >>> #Imaginary Part of complex number >>> print('Complex Number: Imaginary Part is = ', c. imag) Complex Number: Imaginary Part is = 6.0 >>> >>> #Conjugate of complex number >>> print('Complex Number: conjugate Part = ', c. conjugate()) Complex Number: conjugate Part = (3-6j)
我们可以对复数进行简单的数学计算:
>>> #first complex number >>> c1 = 3 + 6j >>> #Second complex number >>> c2 = 6 + 15j >>> >>> #Addition >>> print("Addition of two complex number =", c1 + c2) Addition of two complex number = (9+21j) >>> >>> #Subtraction >>> print("Subtraction of two complex number =", c1 - c2) Subtraction of two complex number = (-3-9j) >>> >>> #Multiplication >>> print("Multiplication of two complex number =", c1 * c2) Multiplication of two complex number = (-72+81j) >>> >>> #Division >>> print("Division of two complex number =", c1 / c2) Division of two complex number = (0.4137931034482759-0.03448275862068964j)
但是,复数不支持<,>,<=,=>之类的比较运算符,它将通过TypeError消息显示:
>>> c2 <= c2 Traceback (most recent call last): File "<pyshell#40>", line 1, in <module> c2 <= c2 TypeError: '<=' not supported between instances of 'complex' and 'complex'
Python cmath模块提供对复数数学函数的访问。让我们使用数学模块功能查看复数的一些重要功能。
复数的相位是实轴与代表虚部的向量之间的夹角。
math和cmath模块返回的相位以弧度表示,我们使用numpy.degrees()函数将其转换为度。
import cmath, math, numpy c = 4+ 4j # phase phase = cmath.phase(c) print('4+ 4j Phase =', phase) print('Phase in Degrees =', numpy.degrees(phase)) print('-4-4j Phase =', cmath.phase(-4-4j), 'radians. Degrees =', numpy.degrees(cmath.phase(-4-4j))) # we can get phase using math.atan2() function too print('Complex number phase using math.atan2() =', math.atan2(2, 1))
4+ 4j Phase = 0.7853981633974483 Phase in Degrees = 45.0 -4-4j Phase = -2.356194490192345 radians. Degrees = -135.0 Complex number phase using math.atan2() = 1.1071487177940904
cmath模块中有几个常量可用于复数计算:
import cmath print('π =', cmath.pi) print('e =', cmath.e) print('tau =', cmath.tau) print('Positive infinity =', cmath.inf) print('Positive Complex infinity =', cmath.infj) print('NaN =', cmath.nan) print('NaN Complex =', cmath.nanj)
π = 3.141592653589793 e = 2.718281828459045 tau = 6.283185307179586 Positive infinity = inf Positive Complex infinity = infj NaN = nan NaN Complex = nanj
该cmath()
模块为对数和幂运算提供了一些有用的功能:
import cmath c = 1 + 2j print('e^c =', cmath.exp(c)) print('log2(c) =', cmath.log(c, 2)) print('log10(c) =', cmath.log10(c)) print('sqrt(c) =', cmath.sqrt(c))
e^c = (-1.1312043837568135+2.4717266720048188j) log2(c) = (1.1609640474436813+1.5972779646881088j) log10(c) = (0.3494850021680094+0.480828578784234j) sqrt(c) = (1.272019649514069+0.7861513777574233j)
import cmath c = 2 + 4j print('arc sine value:\n ', cmath.asin(c)) print('arc cosine value :\n', cmath.acos(c)) print('arc tangent value of complex number c :\n', cmath.atan(c)) print('sine value:\n', cmath.sin(c)) print('cosine value:\n', cmath.cos(c)) print('tangent value:\n', cmath.tan(c))
arc sine value: (0.4538702099631225+2.198573027920936j) arc cosine value : (1.1169261168317741-2.198573027920936j) arc tangent value of complex number c : (1.4670482135772953+0.20058661813123432j) sine value: (24.83130584894638-11.356612711218174j) cosine value: (-11.36423470640106-24.814651485634187j) tangent value: (-0.0005079806234700387+1.0004385132020523j)
import cmath c = 2 + 4j print('Inverse hyperbolic sine value: \n', cmath.asinh(c)) print('Inverse hyperbolic cosine value: \n', cmath.acosh(c)) print('Inverse hyperbolic tangent value: \n', cmath.atanh(c)) print('Hyperbolic sine value: \n', cmath.sinh(c)) print('Hyperbolic cosine value: \n', cmath.cosh(c)) print('Hyperbolic tangent value: \n', cmath.tanh(c))
Inverse hyperbolic sine value: (2.183585216564564+1.096921548830143j) Inverse hyperbolic cosine value: (2.198573027920936+1.1169261168317741j) Inverse hyperbolic tangent value: (0.09641562020299617+1.3715351039616865j) Hyperbolic sine value: (-2.370674169352002-2.8472390868488278j) Hyperbolic cosine value: (-2.4591352139173837-2.744817006792154j) Hyperbolic tangent value: (1.0046823121902348+0.03642336924740368j)