This module provides access to mathematical functions for complex numbers. Thefunctions in this module accept integers, floating-point numbers or complexnumbers as arguments. They will also accept any Python object that has either a
__complex__()
or a __float__()
method: these methods are used toconvert the object to a complex or floating-point number, respectively, andthe function is then applied to the result of the conversion.Note
Convert between Polar and Cartesian Coordinates. Cartesian and Polar coordinate converting. Sponsored Links. In the Cartesian system the coordinates are. NumPy Random Object Exercises, Practice and Solution: Write a NumPy program to convert cartesian coordinates to polar coordinates of a random 10x3 matrix representing cartesian coordinates.
On platforms with hardware and system-level support for signedzeros, functions involving branch cuts are continuous on bothsides of the branch cut: the sign of the zero distinguishes oneside of the branch cut from the other. On platforms that do notsupport signed zeros the continuity is as specified below.
Conversions to and from polar coordinates¶
A Python complex number
z
is stored internally using rectangularor Cartesian coordinates. It is completely determined by its realpartz.real
and its imaginary partz.imag
. In otherwords:Polar coordinates give an alternative way to represent a complexnumber. In polar coordinates, a complex number z is defined by themodulus r and the phase angle phi. The modulus r is the distancefrom z to the origin, while the phase phi is the counterclockwiseangle, measured in radians, from the positive x-axis to the linesegment that joins the origin to z.
The following functions can be used to convert from the nativerectangular coordinates to polar coordinates and back.
cmath.
phase
(x)¶Return the phase of x (also known as the argument of x), as afloat.
phase(x)
is equivalent to math.atan2(x.imag,x.real)
. The result lies in the range [-π, π], and the branchcut 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 thesign of the result is the same as the sign of x.imag
, even whenx.imag
is zero:Note
The modulus (absolute value) of a complex number x can becomputed using the built-in
abs()
function. There is noseparate cmath
module function for this operation.cmath.
polar
(x)¶Return the representation of x in polar coordinates. Returns apair
(r,phi)
where r is the modulus of x and phi is thephase of x. polar(x)
is equivalent to (abs(x),phase(x))
.cmath.
rect
(r, phi)¶Return the complex number x with polar coordinates r and phi.Equivalent to
r*(math.cos(phi)+math.sin(phi)*1j)
.Power and logarithmic functions¶
![Polar to cartesian in python function Polar to cartesian in python function](/uploads/1/2/5/8/125832278/295024954.png)
cmath.
exp
(x)¶Return e raised to the power x, where e is the base of naturallogarithms.
cmath.
log
(x[, base])¶Returns the logarithm of x to the given base. If the base is notspecified, returns the natural logarithm of x. There is one branch cut, from 0along the negative real axis to -∞, continuous from above.
cmath.
log10
(x)¶Return the base-10 logarithm of x. This has the same branch cut as
log()
.cmath.
sqrt
(x)¶Return the square root of x. This has the same branch cut as
log()
.Trigonometric functions¶
cmath.
acos
(x)¶Return the arc cosine of x. There are two branch cuts: One extends right from1 along the real axis to ∞, continuous from below. The other extends left from-1 along the real axis to -∞, continuous from above.
cmath.
asin
(x)¶Return the arc sine of x. This has the same branch cuts as
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
, continuous from the right. Theother extends from -1j
along the imaginary axis to -∞j
, continuousfrom the left.cmath.
cos
(x)¶Return the cosine of x.
cmath.
sin
(x)¶Return the sine of x.
cmath.
tan
(x)¶Return the tangent of x.
Hyperbolic functions¶
cmath.
acosh
(x)¶Return the inverse hyperbolic cosine of x. There is one branch cut,extending left from 1 along the real axis to -∞, continuous from above.
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
,continuous from the right. The other extends from -1j
alongthe imaginary axis to -∞j
, continuous from the left.cmath.
atanh
(x)¶Return the inverse hyperbolic tangent of x. There are two branch cuts: Oneextends from
1
along the real axis to ∞
, continuous from below. Theother extends from -1
along the real axis to -∞
, continuous fromabove.cmath.
cosh
(x)¶Return the hyperbolic cosine of x.
cmath.
sinh
(x)¶Return the hyperbolic sine of x.
cmath.
tanh
(x)¶Return the hyperbolic tangent of x.
Classification functions¶
cmath.
isfinite
(x)¶Return
True
if both the real and imaginary parts of x are finite, andFalse
otherwise.cmath.
isinf
(x)¶Return
True
if either the real or the imaginary part of x is aninfinity, and False
otherwise.cmath.
isnan
(x)¶Return
True
if either the real or the imaginary part of x is a NaN,and False
otherwise.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 andFalse
otherwise.Whether or not two values are considered close is determined according togiven absolute and relative tolerances.
rel_tol is the relative tolerance – it is the maximum allowed differencebetween a and b, relative to the larger absolute value of a or b.For example, to set a tolerance of 5%, pass
rel_tol=0.05
. The defaulttolerance is 1e-09
, which assures that the two values are the samewithin about 9 decimal digits. rel_tol must be greater than zero.abs_tol is the minimum absolute tolerance – useful for comparisons nearzero. abs_tol must be at least zero.
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 behandled according to IEEE rules. Specifically, NaN
is not consideredclose to any other value, including NaN
. inf
and -inf
are onlyconsidered close to themselves.New in version 3.5.
See also
PEP 485 – A function for testing approximate equality
Constants¶
cmath.
pi
¶The mathematical constant π, as a float.
cmath.
e
¶The mathematical constant e, as a float.
cmath.
tau
¶The mathematical constant τ, as a float.
cmath.
inf
¶Floating-point positive infinity. Equivalent to
float('inf')
.New in version 3.6.
cmath.
infj
¶Complex number with zero real part and positive infinity imaginarypart. Equivalent to
complex(0.0,float('inf'))
.cmath.
nan
¶A floating-point “not a number” (NaN) value. Equivalent to
float('nan')
.New in version 3.6.
cmath.
nanj
¶Complex number with zero real part and NaN imaginary part. Equivalent to
complex(0.0,float('nan'))
.Note that the selection of functions is similar, but not identical, to that inmodule
math
. The reason for having two modules is that some users aren’tinterested in complex numbers, and perhaps don’t even know what they are. Theywould rather have math.sqrt(-1)
raise an exception than return a complexnumber. Also note that the functions defined in cmath
always return acomplex number, even if the answer can be expressed as a real number (in whichcase the complex number has an imaginary part of zero).A note on branch cuts: They are curves along which the given function fails tobe continuous. They are a necessary feature of many complex functions. It isassumed that if you need to compute with complex functions, you will understandabout branch cuts. Consult almost any (not too elementary) book on complexvariables for enlightenment. For information of the proper choice of branchcuts for numerical purposes, a good reference should be the following:
See also
Kahan, W: Branch cuts for complex elementary functions; or, Much ado aboutnothing’s sign bit. In Iserles, A., and Powell, M. (eds.), The state of the artin numerical analysis. Clarendon Press (1987) pp165–211.
This module is always available. It provides access to mathematical functionsfor complex numbers. The functions in this module accept integers,floating-point numbers or complex numbers as arguments. They will also acceptany Python object that has either a
__complex__()
or a __float__()
method: these methods are used to convert the object to a complex orfloating-point number, respectively, and the function is then applied to theresult of the conversion.Note
On platforms with hardware and system-level support for signedzeros, functions involving branch cuts are continuous on bothsides of the branch cut: the sign of the zero distinguishes oneside of the branch cut from the other. On platforms that do notsupport signed zeros the continuity is as specified below.
9.3.1. Conversions to and from polar coordinates¶
A Python complex number
z
is stored internally using rectangularor Cartesian coordinates. It is completely determined by its realpartz.real
and its imaginary partz.imag
. In otherwords:Polar coordinates give an alternative way to represent a complexnumber. In polar coordinates, a complex number z is defined by themodulus r and the phase angle phi. The modulus r is the distancefrom z to the origin, while the phase phi is the counterclockwiseangle, measured in radians, from the positive x-axis to the linesegment that joins the origin to z.
The following functions can be used to convert from the nativerectangular coordinates to polar coordinates and back.
cmath.
phase
(x)¶Return the phase of x (also known as the argument of x), as afloat.
phase(x)
is equivalent to math.atan2(x.imag,x.real)
. The result lies in the range [-π, π], and the branchcut 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 thesign of the result is the same as the sign of x.imag
, even whenx.imag
is zero:New in version 2.6.
Note
The modulus (absolute value) of a complex number x can becomputed using the built-in
abs()
function. There is noseparate cmath
module function for this operation.cmath.
polar
(x)¶Return the representation of x in polar coordinates. Returns apair
(r,phi)
where r is the modulus of x and phi is thephase of x. polar(x)
is equivalent to (abs(x),phase(x))
.cmath.
rect
(r, phi)¶Return the complex number x with polar coordinates r and phi.Equivalent to
r*(math.cos(phi)+math.sin(phi)*1j)
.New in version 2.6.
9.3.2. Power and logarithmic functions¶
cmath.
exp
(x)¶Return the exponential value
e**x
.cmath.
log
(x[, base])¶Returns the logarithm of x to the given base. If the base is notspecified, returns the natural logarithm of x. There is one branch cut, from 0along the negative real axis to -∞, continuous from above.
cmath.
log10
(x)¶Return the base-10 logarithm of x. This has the same branch cut as
log()
.cmath.
sqrt
(x)¶Return the square root of x. This has the same branch cut as
log()
.9.3.3. Trigonometric functions¶
cmath.
acos
(x)¶Return the arc cosine of x. There are two branch cuts: One extends right from1 along the real axis to ∞, continuous from below. The other extends left from-1 along the real axis to -∞, continuous from above.
cmath.
asin
(x)¶Return the arc sine of x. This has the same branch cuts as
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
, continuous from the right. Theother extends from -1j
along the imaginary axis to -∞j
, continuousfrom the left.Changed in version 2.6: direction of continuity of upper cut reversed
cmath.
cos
(x)¶Return the cosine of x.
cmath.
sin
(x)¶Return the sine of x.
cmath.
tan
(x)¶Return the tangent of x.
9.3.4. Hyperbolic functions¶
cmath.
acosh
(x)¶Return the inverse hyperbolic cosine of x. There is one branch cut,extending left from 1 along the real axis to -∞, continuous from above.
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
,continuous from the right. The other extends from -1j
alongthe imaginary axis to -∞j
, continuous from the left.Changed in version 2.6: branch cuts moved to match those recommended by the C99 standard
cmath.
atanh
(x)¶Return the inverse hyperbolic tangent of x. There are two branch cuts: Oneextends from
1
along the real axis to ∞
, continuous from below. Theother extends from -1
along the real axis to -∞
, continuous fromabove.Changed in version 2.6: direction of continuity of right cut reversed
cmath.
cosh
(x)¶Return the hyperbolic cosine of x.
cmath.
sinh
(x)¶Return the hyperbolic sine of x.
cmath.
tanh
(x)¶Return the hyperbolic tangent of x.
9.3.5. Classification functions¶
cmath.
isinf
(x)¶Return
True
if the real or the imaginary part of x is positiveor negative infinity.cmath.
isnan
(x)¶Return
True
if the real or imaginary part of x is not a number (NaN).New in version 2.6.
9.3.6. Constants¶
cmath.
pi
¶The mathematical constant π, as a float.
cmath.
e
¶The mathematical constant e, as a float.
Note that the selection of functions is similar, but not identical, to that inmodule
math
. The reason for having two modules is that some users aren’tinterested in complex numbers, and perhaps don’t even know what they are. Theywould rather have math.sqrt(-1)
raise an exception than return a complexnumber. Also note that the functions defined in cmath
always return acomplex number, even if the answer can be expressed as a real number (in whichcase the complex number has an imaginary part of zero).A note on branch cuts: They are curves along which the given function fails tobe continuous. They are a necessary feature of many complex functions. It isassumed that if you need to compute with complex functions, you will understandabout branch cuts. Consult almost any (not too elementary) book on complexvariables for enlightenment. For information of the proper choice of branchcuts for numerical purposes, a good reference should be the following:
![Polar To Cartesian In Python Polar To Cartesian In Python](/uploads/1/2/5/8/125832278/110150790.png)
See also
Kahan, W: Branch cuts for complex elementary functions; or, Much ado aboutnothing’s sign bit. In Iserles, A., and Powell, M. (eds.), The state of the artin numerical analysis. Clarendon Press (1987) pp165–211.