9.3. cmath ——关于复数的数学函数

This module is always available. It 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.

注解

在具有对于有符号零的硬件和系统级支持的平台上,涉及分歧点的函数在分歧点的 两侧 都是连续的:零的符号可用来区别分歧点的一侧和另一侧。 在不支持有符号零的平台上,连续性的规则见下文。

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

使用 矩形坐标笛卡尔坐标 在内部存储 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, continuous from above. On systems with support for signed zeros (which includes most systems in current use), this means that 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.1415926535897931
>>> phase(complex(-1.0, -0.0))
-3.1415926535897931

2.6 新版功能.

注解

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

cmath.polar(x)

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

2.6 新版功能.

cmath.rect(r, phi)

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

2.6 新版功能.

9.3.2. 幂函数与对数函数

cmath.exp(x)

Return the exponential value e**x.

cmath.log(x[, base])

返回给定 basex 的对数。如果没有给定 base,返回 x 的自然对数。 从 0 到 -∞ 存在一个分歧点,沿负实轴之上连续。

在 2.4 版更改: base argument added.

cmath.log10(x)

返回底数为 10 的 x 的对数。它具有与 log() 相同的分歧点。

cmath.sqrt(x)

返回 x 的平方根。 它具有与 log() 相同的分歧点。

9.3.3. 三角函数

cmath.acos(x)

返回 x 的反余弦。这里有两个分歧点:一个沿着实轴从 1 向右延伸到 ∞,从下面连续延伸。另外一个沿着实轴从 -1 向左延伸到 -∞,从上面连续延伸。

cmath.asin(x)

返回 x 的反正弦。它与 acos() 有相同的分歧点。

cmath.atan(x)

返回 x 的反正切。它具有两个分歧点:一个沿着虚轴从 1j 延伸到 ∞j,向右持续延伸。另一个是沿着虚轴从 -1j 延伸到 -∞j ,向左持续延伸。

在 2.6 版更改: direction of continuity of upper cut reversed

cmath.cos(x)

返回 x 的余弦。

cmath.sin(x)

返回 x 的正弦。

cmath.tan(x)

返回 x 的正切。

9.3.4. 双曲函数

cmath.acosh(x)

返回 x 的反双曲余弦。它有一个分歧点沿着实轴从 1 到 -∞ 向左延伸,从上方持续延伸。

cmath.asinh(x)

返回 x 的反双曲正弦。它有两个分歧点:一个沿着虚轴从 1j 向右持续延伸到 ∞j。另一个是沿着虚轴从 -1j 向左持续延伸到 -∞j

在 2.6 版更改: branch cuts moved to match those recommended by the C99 standard

cmath.atanh(x)

返回 x 的反双曲正切。它有两个分歧点:一个是沿着实轴从 1 延展到 ,从下面持续延展。另一个是沿着实轴从 -1 延展到 -∞,从上面持续延展。

在 2.6 版更改: direction of continuity of right cut reversed

cmath.cosh(x)

返回 x 的双曲余弦值。

cmath.sinh(x)

返回 x 的双曲正弦值。

cmath.tanh(x)

返回 x 的双曲正切值。

9.3.5. 分类函数

cmath.isinf(x)

Return True if the real or the imaginary part of x is positive or negative infinity.

2.6 新版功能.

cmath.isnan(x)

Return True if the real or imaginary part of x is not a number (NaN).

2.6 新版功能.

9.3.6. 常量

cmath.pi

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

cmath.e

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

请注意,函数的选择与模块 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.