cmathMathematical functions for complex numbers复数的数学函数


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.它们还将接受任何具有__complex__()__float__()方法的Python对象:这些方法分别用于将对象转换为复数或浮点数,然后将函数应用于转换结果。

Note

On platforms with hardware and system-level support for signed zeros, functions involving branch cuts are continuous on both sides of the branch cut: the sign of the zero distinguishes one side of the branch cut from the other. 在硬件和系统级支持符号零的平台上,涉及分支切割的功能在分支切割的两侧是连续的:零的符号将分支切割的一侧与另一侧区分开来。On platforms that do not support signed zeros the continuity is as specified below.在不支持符号零的平台上,连续性如下所述。

Conversions to and from polar coordinates极坐标之间的转换

A Python complex number z is stored internally using rectangular or Cartesian coordinates. Python复数z使用直角坐标或笛卡尔坐标在内部存储。It is completely determined by its real part z.real and its imaginary part z.imag. 它完全由其实部z.real虚部z.imag决定。In other words:换句话说:

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

Polar coordinates give an alternative way to represent a complex number. 极坐标提供了一种表示复数的替代方法。In polar coordinates, a complex number z is defined by the modulus r and the phase angle phi. 在极坐标系中,复数z由模数r和相位角phi定义。The modulus r is the distance from z to the origin, while the phase phi is the counterclockwise angle, measured in radians, from the positive x-axis to the line segment that joins the origin to z.模数r是从z到原点的距离,而相位phi是从正x轴到将原点连接到z的线段的逆时针角度,以弧度测量。

The following functions can be used to convert from the native rectangular coordinates to polar coordinates and back.以下函数可用于将本机直角坐标转换为极坐标,然后再转换为极坐标。

cmath.phase(x)

Return the phase of x (also known as the argument of x), as a float. 以浮点形式返回x的相位(也称为x参数)。phase(x) is equivalent to math.atan2(x.imag, x.real). phase(x)相当于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:在支持符号零的系统上(包括当前使用的大多数系统),这意味着结果的符号与x.imag的符号相同,即使x.imag为零:

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

Note

The modulus (absolute value) of a complex number x can be computed using the built-in abs() function. 复数x的模数(绝对值)可以使用内置的abs()函数计算。There is no separate cmath module function for this operation.此操作没有单独的abs()模块功能。

cmath.polar(x)

Return the representation of x in polar coordinates. 返回x在极坐标中的表示形式。Returns a pair (r, phi) where r is the modulus of x and phi is the phase of x. 返回一对(r, phi),其中rx的模,phix的相位。polar(x) is equivalent to (abs(x), phase(x)).polar(x)相当于(abs(x), phase(x))

cmath.rect(r, phi)

Return the complex number x with polar coordinates r and phi. 返回带有极坐标rphi的复数xEquivalent to r * (math.cos(phi) + math.sin(phi)*1j).等效于r * (math.cos(phi) + math.sin(phi)*1j)

Power and logarithmic functions幂函数和对数函数

cmath.exp(x)

Return e raised to the power x, where e is the base of natural logarithms.返回e的幂x,其中e是自然对数的底。

cmath.log(x[, base])

Returns the logarithm of x to the given base. x的对数返回到给定的baseIf the base is not specified, returns the natural logarithm of x. 如果未指定base,则返回x的自然对数。There is one branch cut, from 0 along the negative real axis to -∞, continuous from above.有一个分支切割,从0沿负实轴到-∞,从上面连续。

cmath.log10(x)

Return the base-10 logarithm of x. 返回x的以10为底的对数。This has the same branch cut as log().这与log()具有相同的分支切割。

cmath.sqrt(x)

Return the square root of x. 返回x的平方根。This has the same branch cut as log().这与log()具有相同的分支切割。

Trigonometric functions三角函数

cmath.acos(x)

Return the arc cosine of x. 返回x的弧余弦。There are two branch cuts: One extends right from 1 along the real axis to ∞, continuous from below. 有两个分支切割:一个沿着实轴从1向右延伸到∞,从下方连续。The other extends left from -1 along the real axis to -∞, continuous from above.另一个沿实轴从-1向左延伸到-∞,从上方连续。

cmath.asin(x)

Return the arc sine of x. 返回x的正弦弧。This has the same branch cuts as acos().这与acos()具有相同的分支切割。

cmath.atan(x)

Return the arc tangent of x. 返回x的反正切。There are two branch cuts: One extends from 1j along the imaginary axis to ∞j, continuous from the right. 有两个分支切割:一个沿着假想轴从1j延伸到∞j,从右侧连续。The other extends from -1j along the imaginary axis to -∞j, continuous from the left.另一个沿虚轴从-1j延伸到-∞j,从左侧连续。

cmath.cos(x)

Return the cosine of x.返回x的余弦。

cmath.sin(x)

Return the sine of x.返回x的正弦。

cmath.tan(x)

Return the tangent of x.返回x的切线。

Hyperbolic functions双曲函数

cmath.acosh(x)

Return the inverse hyperbolic cosine of x. 返回x的反双曲余弦。There is one branch cut, extending left from 1 along the real axis to -∞, continuous from above.有一个分支切割,沿实轴从1向左延伸到-∞,从上方连续。

cmath.asinh(x)

Return the inverse hyperbolic sine of x. 返回x的反双曲正弦。There are two branch cuts: One extends from 1j along the imaginary axis to ∞j, continuous from the right. 有两个分支切割:一个沿着假想轴从1j延伸到∞j,从右侧连续。The other extends from -1j along the imaginary axis to -∞j, continuous from the left.另一个沿虚轴从-1j延伸到-∞j,从左侧连续。

cmath.atanh(x)

Return the inverse hyperbolic tangent of x. 返回x的反双曲正切。There are two branch cuts: One extends from 1 along the real axis to , continuous from below. 有两个分支切割:一个沿着实轴从1延伸到,从下面连续。The other extends from -1 along the real axis to -∞, continuous from above.另一个沿实轴从-1延伸到-∞,从上方连续。

cmath.cosh(x)

Return the hyperbolic cosine of x.返回x的双曲余弦。

cmath.sinh(x)

Return the hyperbolic sine of x.返回x的双曲正弦。

cmath.tanh(x)

Return the hyperbolic tangent of x.返回x的双曲正切。

Classification functions分类函数

cmath.isfinite(x)

Return True if both the real and imaginary parts of x are finite, and False otherwise.如果x的实部和虚部都是有限的,则返回True,否则返回False

New in version 3.2.版本3.2中新增。

cmath.isinf(x)

Return True if either the real or the imaginary part of x is an infinity, and False otherwise.如果x的实部或虚部为无穷大,则返回True,否则返回False

cmath.isnan(x)

Return True if either the real or the imaginary part of x is a NaN, and False otherwise.如果x的实部或虚部为NaN,则返回True,否则返回False

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

Return True if the values a and b are close to each other and False otherwise.如果值ab彼此接近,则返回True,否则返回False

Whether or not two values are considered close is determined according to given absolute and relative tolerances.根据给定的绝对和相对公差确定两个值是否接近。

rel_tol is the relative tolerance – it is the maximum allowed difference between a and b, relative to the larger absolute value of a or b. rel_tol是相对公差-它是相对于a或b的较大绝对值,ab之间允许的最大差值。For example, to set a tolerance of 5%, pass rel_tol=0.05. 例如,要将公差设置为5%,通过rel_tol=0.05The default tolerance is 1e-09, which assures that the two values are the same within about 9 decimal digits. 默认公差为1e-09,确保两个值在大约9位小数内相同。rel_tol must be greater than zero.rel_tol必须大于零。

abs_tol is the minimum absolute tolerance – useful for comparisons near zero. abs_tol是最小绝对公差-用于接近零的比较。abs_tol must be at least zero.abs_tol必须至少为零。

If no errors occur, the result will be: abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol).

The IEEE 754 special values of NaN, inf, and -inf will be handled according to IEEE rules. 将根据IEEE规则处理NaNinf-inf的IEEE 754特殊值。Specifically, NaN is not considered close to any other value, including NaN. 具体来说,NaN不被视为接近任何其他值,包括NaNinf and -inf are only considered close to themselves.inf-inf只被认为与它们自己很接近。

New in version 3.5.版本3.5中新增。

See also

PEP 485A function for testing approximate equality检验近似等式的函数

Constants常数

cmath.pi

The mathematical constant π, as a float.数学常数π,形式为浮点数。

cmath.e

The mathematical constant e, as a float.数学常数e,作为浮点数。

cmath.tau

The mathematical constant τ, as a float.数学常数τ,作为浮点数。

New in version 3.6.版本3.6中新增。

cmath.inf

Floating-point positive infinity. 浮点正无穷大。Equivalent to float('inf').相当于float('inf')

New in version 3.6.版本3.6中新增。

cmath.infj

Complex number with zero real part and positive infinity imaginary part. 实数部分为零,虚部为无穷正的复数。Equivalent to complex(0.0, float('inf')).相当于complex(0.0, float('inf'))

New in version 3.6.版本3.6中新增。

cmath.nan

A floating-point “not a number” (NaN) value. 浮点“非数字”(NaN)值。Equivalent to float('nan').相当于float('nan')

New in version 3.6.版本3.6中新增。

cmath.nanj

Complex number with zero real part and NaN imaginary part. 实数部分为零,虚部为零的复数。Equivalent to complex(0.0, float('nan')).相当于complex(0.0, float('nan'))

New in version 3.6.版本3.6中新增。

Note that the selection of functions is similar, but not identical, to that in module math. 请注意,函数的选择与math数学中的选择类似,但不完全相同。The reason for having two modules is that some users aren’t interested in complex numbers, and perhaps don’t even know what they are. 之所以有两个模块,是因为一些用户对复数不感兴趣,甚至可能不知道它们是什么。They would rather have math.sqrt(-1) raise an exception than return a complex number. 他们宁愿让math.sqrt(-1)引发异常,也不愿返回复数。Also note that the functions defined in cmath always return a complex number, even if the answer can be expressed as a real number (in which case the complex number has an imaginary part of zero).还要注意,即使答案可以表示为实数(在这种情况下,复数的虚部为零),cmath中定义的函数始终返回复数。

A note on branch cuts: They are curves along which the given function fails to be continuous. 关于分支切割的注意事项:它们是给定函数不连续的曲线。They are a necessary feature of many complex functions. 它们是许多复杂函数的必要特征。It is assumed that if you need to compute with complex functions, you will understand about branch cuts. 假设您需要使用复杂函数进行计算,您将了解分支切割。Consult almost any (not too elementary) book on complex variables for enlightenment. 参考几乎所有关于复杂变量的书(不是太初级),以获得启发。For information of the proper choice of branch cuts for numerical purposes, a good reference should be the following:为便于数值计算,正确选择分支切割的信息,应参考以下内容:

See also

Kahan, W: Branch cuts for complex elementary functions; or, Much ado about nothing’s sign bit. Kahan,W:复初等函数的分支割;或者,无事生非的符号位。In Iserles, A., and Powell, M. (eds.), The state of the art in numerical analysis. 在Iserles,A.和Powell,M.(编辑)中,介绍了数值分析的最新技术。Clarendon Press (1987) pp165–211.克拉伦登出版社(1987)第165-211页。