cmath --- 关于复数的数学函数


This module provides access to mathematical functions for complex numbers. The functions in this module accept integers, floating-point numbers or complex numbers as arguments. They will also accept any Python object that has either a __complex__() or a __float__() method: these methods are used to convert the object to a complex or floating-point number, respectively, and the function is then applied to the result of the conversion.

备注

For functions involving branch cuts, we have the problem of deciding how to define those functions on the cut itself. Following Kahan's "Branch cuts for complex elementary functions" paper, as well as Annex G of C99 and later C standards, we use the sign of zero to distinguish one side of the branch cut from the other: for a branch cut along (a portion of) the real axis we look at the sign of the imaginary part, while for a branch cut along the imaginary axis we look at the sign of the real part.

For example, the cmath.sqrt() function has a branch cut along the negative real axis. An argument of complex(-2.0, -0.0) is treated as though it lies below the branch cut, and so gives a result on the negative imaginary axis:

>>> cmath.sqrt(complex(-2.0, -0.0))
-1.4142135623730951j

But an argument of complex(-2.0, 0.0) is treated as though it lies above the branch cut:

>>> cmath.sqrt(complex(-2.0, 0.0))
1.4142135623730951j

到极坐标和从极坐标的转换

使用 矩形坐标笛卡尔坐标 在内部存储 Python 复数 z。 这完全取决于它的 实部 z.real虚部 z.imag。 换句话说:

z == z.real + z.imag*1j

极坐标 提供了另一种复数的表示方法。在极坐标中,一个复数 z 由模量 r 和相位角 phi 来定义。模量 r 是从 z 到坐标原点的距离,而相位角 phi 是以弧度为单位的,逆时针的,从正X轴到连接原点和 z 的线段间夹角的角度。

下面的函数可用于原生直角坐标与极坐标的相互转换。

cmath.phase(x)

Return the phase of x (also known as the argument of x), as a float. phase(x) is equivalent to math.atan2(x.imag, x.real). The result lies in the range [-π, π], and the branch cut for this operation lies along the negative real axis. The sign of the result is the same as the sign of x.imag, even when x.imag is zero:

>>> phase(complex(-1.0, 0.0))
3.141592653589793
>>> phase(complex(-1.0, -0.0))
-3.141592653589793

备注

一个复数 x 的模数(绝对值)可以通过内置函数 abs() 计算。没有单独的 cmath 模块函数用于这个操作。

cmath.polar(x)

在极坐标中返回 x 的表达方式。返回一个数对 (r, phi)rx 的模数,phix 的相位角。 polar(x) 相当于 (abs(x), phase(x))

cmath.rect(r, phi)

通过极坐标的 rphi 返回复数 x。相当于 r * (math.cos(phi) + math.sin(phi)*1j)

幂函数与对数函数

cmath.exp(x)

返回 ex 次方,e 是自然对数的底数。

cmath.log(x[, base])

Returns the logarithm of x to the given base. If the base is not specified, returns the natural logarithm of x. There is one branch cut, from 0 along the negative real axis to -∞.

cmath.log10(x)

返回底数为 10 的 x 的对数。它具有与 log() 相同的支割线。

cmath.sqrt(x)

返回 x 的平方根。 它具有与 log() 相同的支割线。

三角函数

cmath.acos(x)

Return the arc cosine of x. There are two branch cuts: One extends right from 1 along the real axis to ∞. The other extends left from -1 along the real axis to -∞.

cmath.asin(x)

返回 x 的反正弦。它与 acos() 有相同的支割线。

cmath.atan(x)

Return the arc tangent of x. There are two branch cuts: One extends from 1j along the imaginary axis to ∞j. The other extends from -1j along the imaginary axis to -∞j.

cmath.cos(x)

返回 x 的余弦。

cmath.sin(x)

返回 x 的正弦。

cmath.tan(x)

返回 x 的正切。

双曲函数

cmath.acosh(x)

Return the inverse hyperbolic cosine of x. There is one branch cut, extending left from 1 along the real axis to -∞.

cmath.asinh(x)

Return the inverse hyperbolic sine of x. There are two branch cuts: One extends from 1j along the imaginary axis to ∞j. The other extends from -1j along the imaginary axis to -∞j.

cmath.atanh(x)

Return the inverse hyperbolic tangent of x. There are two branch cuts: One extends from 1 along the real axis to . The other extends from -1 along the real axis to -∞.

cmath.cosh(x)

返回 x 的双曲余弦值。

cmath.sinh(x)

返回 x 的双曲正弦值。

cmath.tanh(x)

返回 x 的双曲正切值。

分类函数

cmath.isfinite(x)

如果 x 的实部和虚部都是有限的,则返回 True,否则返回 False

在 3.2 版本加入.

cmath.isinf(x)

如果 x 的实部或者虚部是无穷大的,则返回 True,否则返回 False

cmath.isnan(x)

如果 x 的实部或者虚部是 NaN,则返回 True ,否则返回 False

cmath.isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0)

ab 的值比较接近则返回 True,否则返回 False

根据给定的绝对和相对容差确定两个值是否被认为是接近的。

rel_tol 是相对容差 —— 它是 ab 之间允许的最大差值,相对于 ab 的较大绝对值。例如,要设置5%的容差,请传递 rel_tol=0.05 。默认容差为 1e-09,确保两个值在大约9位十进制数字内相同。 rel_tol 必须大于零。

abs_tol 是最小绝对容差 —— 对于接近零的比较很有用。 abs_tol 必须至少为零。

如果没有错误发生,结果将是: abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)

IEEE 754特殊值 NaNinf-inf 将根据IEEE规则处理。具体来说, NaN 不被认为接近任何其他值,包括 NaNinf-inf 只被认为接近自己。

在 3.5 版本加入.

参见

PEP 485 —— 用于测试近似相等的函数

常量

cmath.pi

数学常数 π ,作为一个浮点数。

cmath.e

数学常数 e ,作为一个浮点数。

cmath.tau

数学常数 τ ,作为一个浮点数。

在 3.6 版本加入.

cmath.inf

浮点正无穷大。相当于 float('inf')

在 3.6 版本加入.

cmath.infj

具有零实部和正无穷虚部的复数。相当于 complex(0.0, float('inf'))

在 3.6 版本加入.

cmath.nan

浮点“非数字”(NaN)值。相当于 float('nan')

在 3.6 版本加入.

cmath.nanj

具有零实部和 NaN 虚部的复数。相当于 complex(0.0, float('nan'))

在 3.6 版本加入.

请注意,函数的选择与模块 math 中的函数选择相似,但不完全相同。 拥有两个模块的原因是因为有些用户对复数不感兴趣,甚至根本不知道它们是什么。它们宁愿 math.sqrt(-1) 引发异常,也不想返回一个复数。 另请注意,被 cmath 定义的函数始终会返回一个复数,尽管答案可以表示为一个实数(在这种情况下,复数的虚数部分为零)。

关于支割线的注释:它们是沿着给定函数无法连续的曲线。它们是许多复变函数的必要特征。 假设您需要使用复变函数进行计算,您将会了解支割线的概念。 请参阅几乎所有关于复变函数的(不太基本)的书来获得启发。 对于如何正确地基于数值目的来选择支割线的相关信息,一个良好的参考如下:

参见

Kahan, W: Branch cuts for complex elementary functions; or, Much ado about nothing's sign bit. In Iserles, A., and Powell, M. (eds.), The state of the art in numerical analysis. Clarendon Press (1987) pp165--211.