The Python math module provides many mathematical operation functions for floating-point numbers.

The Python cmath module contains some functions for complex number operations.

The functions of the cmath module are basically the same as those of the math module. The difference is that the cmath module deals with complex numbers, while the math module deals with real numbers.

To use the math or cmath functions, you must import them first:

import math

View the contents of the math package:

>>> import math
>>> dir(math)
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']
>>>

Explanation of each function:

Python complex number support and cmath module application

Python provides support for complex number operations. The expression of a complex number in Python is C==c.real+c.imag*j, a complex number C consists of its real part and imaginary part.

For complex numbers, Python supports addition, subtraction, multiplication, and division operations, and also provides the cmath module to support other complex operations. The cmath module corresponds to the math module in Python; math provides support for real numbers. Here we mainly discuss the usage of several functions in the cmath module.

View the contents of the cmath package:

>>> import cmath
>>> dir(cmath)
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atanh', 'cos', 'cosh', 'e', 'exp', 'inf', 'infj', 'isclose', 'isfinite', 'isinf', 'isnan', 'log', 'log10', 'nan', 'nanj', 'phase', 'pi', 'polar', 'rect', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau']
>>>

1. Conversion between polar and Cartesian coordinate representations.

C==c.real+c.imag*jThe representation method for complex numbers is the Cartesian representation. The polar() method and rect() method in the cmath module can convert between the polar representation and the Cartesian representation of complex numbers. Example:

>>> import cmath
>>> Z=1+2j
>>> print cmath.polar(Z)
(2.23606797749979, 1.1071487177940904)
>>> a,b=cmath.polar(Z)
>>> print cmath.rect(a,b)
(1+2j)
>>>

The polar function computes an input complex number in Cartesian form and outputs a two-element tuple. The first value is the modulus of Z, and the second is the phase value. The rect() function computes the input modulus and phase value and outputs the Cartesian representation.

If you need to solve for the phase value of a complex number separately, you can call the cmath.phrase(x) function, which returns the phase value.

2. Exponential and logarithmic functions of complex numbers

The exponential function for complex numbers is cmath.exp(x), used to evaluate the expression e^x.

cmath.log(x[,base])Used to find the logarithm of x to the base Base.

cmath.log10(x)Used to find the logarithm of x to the base 10.

cmath.sqrt(x)Used to find the square root of x.

3. Trigonometric function equations of complex numbers

Includes all trigonometric function calculations.acos(x) asin(x) atan(x) sin(x) cos(x) tan(x)。

4. Parameter type judgment

cmath.isinf(x)Returns true if the real part or imaginary part of x is infinite.

cmath.isnan(x)Returns true if the real part or imaginary part of x is not a number.

5. Constant support

cmat.piFloating-point value, representing the value of pi.

cmat.eFloating-point value, representing the base of the natural logarithm.