The cmath module provides related mathematical operation functions for complex numbers, so that complex numbers can be calculated like integer or floating-point numbers.
Complex Polar Coordinate System Conversion Function
In the Python data type (1) number type , we talked about the storage essence of complex numbers and the definition in the polar coordinate system. The relevant polar coordinate conversion functions are as follows:
Function name |
Paraphrase |
cmath.phase(x) |
Returns the radians of x as a floating point number, in the range of [-π, π] |
cmath.polar(x) |
Returns the coordinate of x in the polar coordinate system (r, phi) |
cmath.rect(r, phi) |
Returns the complex number corresponding to the coordinates (r, phi) in the polar coordinate system |
>>> import cmath
>>> cmath.phase(complex(-1.0, 0.0))
3.141592653589793
>>> cmath.phase(complex(-1.0,0.0))
3.141592653589793
>>> cmath.phase(complex(-0.5, 0.0))
3.141592653589793
>>> cmath.phase(complex(-0.5, 0.3))
2.601173153319209
>>> cmath.polar(complex(-0.5, 0.3))
(0.58309518948453, 2.601173153319209)
>>> cmath.rect(0.58309518948453, 2.601173153319209)
(-0.49999999999999994+0.3j)
Power function and logarithmic function
function |
Paraphrase |
cmath.exp(x) |
returne**x |
cmath.log(x,base) |
Returns the logarithm of x with base as the base |
cmath.log10(x) |
Returns the base 10 logarithm of x |
cmath.sqrt(x) |
square root of x |
>>> cmath.exp(complex(2.0, 0.0))
(7.38905609893065+0j)
>>> cmath.exp(complex(10.0, 2.0))
(-9166.244060822655+20028.608669281643j)
>>> cmath.log10(complex(100.0,2.0))
(2.0000868415292326+0.008684731797315706j)
>>> cmath.sqrt(complex(2.0, 1.0))
(1.455346690225355+0.34356074972251244j)
Trigonometric function
function |
Paraphrase |
cmath.acos(x) |
Returns the arc cosine of x |
cmath.asin(x) |
Returns the arc sine of x |
cmath.atan(x) |
Returns the arctangent of x |
cmath.cos(x) |
Returns the cosine of x |
cmath. sin (x) |
Returns the sine of x |
cmath.tan(x) |
Returns the tangent of x |
Hyperbolic function
function |
Paraphrase |
cmath.acosh(x) |
Returns the arc cosine of x in the hyperbola |
cmath.asinh(x) |
Returns the arc sine of x in the hyperbola |
cmath.atanh(x) |
Returns the arctangent of x in the hyperbola |
cmath.cosh(x) |
Returns the cosine of x in the hyperbola |
cmath. birth (x) |
Returns the sine of x in the hyperbola |
cmath. fishy (x) |
Returns the tangent of x in the hyperbola |
Classification function
function |
Paraphrase |
cmath.isfinite(x) |
If the modulus r and the radian phi are both rational numbers, it returns True, otherwise it returns False |
cmath.isinf(x) |
If the modulus r and the radian phi are both close to positive infinity, it returns True, otherwise it returns False |
cmath.isnan(x) |
If the modulus r and the radian phi are both Nan, it returns True, otherwise it returns False |
cmath.isclose(a,b,*,rel_tol,abs_tol) |
If the values of a and b are relatively close, it returns True, otherwise it returns False. The criterion is based on the given absolute and relative tolerances. rel_tol is the relative tolerance, must be greater than 0, is the maximum allowable difference between a and b. abs_tol is the minimum absolute tolerance, at least 0 |
constant
Constant name |
Paraphrase |
cmath.pi |
The constant π=3.141926... |
cmath.e |
The constant e= 2.718281... |
cmath. your |
The constant τ = 6.283185..., which is twice π |
cmath.inf |
Positive infinity floating point number |
cmath.infj |
The real part is 0 and the imaginary part is a complex number with positive infinity floating point numbers |
cmath. in |
Floating point number "not a number" |
cmath. nanj |
The real part is 0, and the imaginary part is the "non-numeric value" of Nan |
Many functions of cmath module and math module look similar, but they are actually different functions. The value returned by cmath is a complex number, even if the imaginary part is 0. Many people do not understand why a cmath module is defined separately instead of a math module to solve complex number arithmetic problems together. Suppose one day you need to use a particularly complicated way to calculate, you will understand the reason for such a definition.