Python复数– cmath

时间:2020-02-23 14:42:32  来源:igfitidea点击:

从两个实数创建一个复数。
可以使用complex()函数以及直接赋值语句创建Python复数。

复数通常用在我们使用两个实数定义事物的地方。
例如,由电压(V)和电流(I)定义的电路元件。
它们主要用于几何,微积分和科学计算。

Python复数

首先,让我们学习如何在python中创建复数。

c = 1 + 2j
print(type(c))
print(c)

c1 = complex(2, 4)
print(type(c1))
print(c1)

输出:

<class 'complex'>
(1+2j)
<class 'complex'>
(2+4j)

Python复数类型为complex
每个复数包含一个实部和一个虚部。

Python复数的属性和函数

让我们看一下复数的一些属性和实例函数。

c = 1 + 2j
print('Real Part =', c.real)
print('Imaginary Part =', c.imag)
print('Complex conjugate =', c.conjugate())

输出:

Real Part = 1.0
Imaginary Part = 2.0
Complex conjugate = (1-2j)

复数数学计算

复数支持数学计算,例如加法,减法,乘法和除法。

c = 1 + 2j
c1 = 2 + 4j
print('Addition =', c + c1)
print('Subtraction =', c - c1)
print('Multiplication =', c * c1)
print('Division =', c1/c)

输出:

Addition = (3+6j)
Subtraction = (-1-2j)
Multiplication = (-6+8j)
Division = (2+0j)

复数不支持比较运算符。
如果我们尝试执行c &lt;c1,那么错误消息将被抛出为'TypeError:'<'不支持'complex'和'complex'之间的错误消息。

Python cmath模块

Python cmath模块提供对复数数学函数的访问。
让我们看一下复数的一些重要特征,以及如何使用cmath模块函数来计算它们。

复数相位

复数的相位是实轴与代表虚部的向量之间的夹角。
下图说明了复数的相位以及如何使用cmath和math模块获得该值。

注意,math和cmath模块返回的相位以弧度为单位,我们可以使用numpy.degrees()函数将其转换为度。
相位范围是从-π到+π(-pi到+ pi)弧度,它等效于-180到+180度。

import cmath, math, numpy

c = 2 + 2j

# phase
phase = cmath.phase(c)
print('2 + 2j Phase =', phase)
print('Phase in Degrees =', numpy.degrees(phase))
print('-2 - 2j Phase =', cmath.phase(-2 - 2j), 'radians. Degrees =', numpy.degrees(cmath.phase(-2 - 2j)))

# we can get phase using math.atan2() function too
print('Complex number phase using math.atan2() =', math.atan2(2, 1))

输出:

2 + 2j Phase = 0.7853981633974483
Phase in Degrees = 45.0
-2 - 2j Phase = -2.356194490192345 radians. Degrees = -135.0
Complex number phase using math.atan2() = 1.1071487177940904

极坐标和直角坐标

我们可以在极坐标中写一个复数,它是复数的模数和相位的元组。

我们可以使用cmath.rect()函数通过传递模数和相位作为参数来创建矩形格式的复数。

c = 1 + 2j

modulus = abs(c)
phase = cmath.phase(c)
polar = cmath.polar(c)

print('Modulus =', modulus)
print('Phase =', phase)
print('Polar Coordinates =', polar)

print('Rectangular Coordinates =', cmath.rect(modulus, phase))

输出:

Modulus = 2.23606797749979
Phase = 1.1071487177940904
Polar Coordinates = (2.23606797749979, 1.1071487177940904)
Rectangular Coordinates = (1.0000000000000002+2j)

cmath模块常量

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

电源和对数功能

对数和幂运算有一些有用的功能。

c = 2 + 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 = (-3.074932320639359+6.71884969742825j)
log2(c) = (1.5000000000000002+1.1330900354567985j)
log10(c) = (0.4515449934959718+0.3410940884604603j)
sqrt(c) = (1.5537739740300374+0.6435942529055826j)

三角函数

c = 2 + 2j
print('arc sine =', cmath.asin(c))
print('arc cosine =', cmath.acos(c))
print('arc tangent =', cmath.atan(c))

print('sine =', cmath.sin(c))
print('cosine =', cmath.cos(c))
print('tangent =', cmath.tan(c))

输出:

arc sine = (0.7542491446980459+1.7343245214879666j)
arc cosine = (0.8165471820968505-1.7343245214879666j)
arc tangent = (1.311223269671635+0.2388778612568591j)
sine = (3.4209548611170133-1.5093064853236156j)
cosine = (-1.5656258353157435-3.2978948363112366j)
tangent = (-0.028392952868232294+1.0238355945704727j)

双曲函数

c = 2 + 2j
print('inverse hyperbolic sine =', cmath.asinh(c))
print('inverse hyperbolic cosine =', cmath.acosh(c))
print('inverse hyperbolic tangent =', cmath.atanh(c))

print('hyperbolic sine =', cmath.sinh(c))
print('hyperbolic cosine =', cmath.cosh(c))
print('hyperbolic tangent =', cmath.tanh(c))

输出:

inverse hyperbolic sine = (1.7343245214879666+0.7542491446980459j)
inverse hyperbolic cosine = (1.7343245214879666+0.8165471820968505j)
inverse hyperbolic tangent = (0.2388778612568591+1.311223269671635j)
hyperbolic sine = (-1.5093064853236156+3.4209548611170133j)
hyperbolic cosine = (-1.5656258353157435+3.2978948363112366j)
hyperbolic tangent = (1.0238355945704727-0.028392952868232294j)

分类函数

有一些其他函数可以检查复数是有限的,无限的还是nan。
还有一个功能可以检查两个复数是否接近。

print(cmath.isfinite(2 + 2j))  # True
print(cmath.isfinite(cmath.inf + 2j))  # False

print(cmath.isinf(2 + 2j))  # False
print(cmath.isinf(cmath.inf + 2j))  # True
print(cmath.isinf(cmath.nan + 2j))  # False

print(cmath.isnan(2 + 2j))  # False
print(cmath.isnan(cmath.inf + 2j))  # False
print(cmath.isnan(cmath.nan + 2j))  # True

print(cmath.isclose(2+2j, 2.01+1.9j, rel_tol=0.05))  # True
print(cmath.isclose(2+2j, 2.01+1.9j, abs_tol=0.005))  # False