Movatterモバイル変換


[0]ホーム

URL:


Navigation

9.3.cmath — Mathematical functions for complex numbers

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 onbothsides 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 numberz is stored internally usingrectangularorCartesian coordinates. It is completely determined by itsrealpartz.real and itsimaginary partz.imag. In otherwords:

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

Polar coordinates give an alternative way to represent a complexnumber. In polar coordinates, a complex numberz is defined by themodulusr and the phase anglephi. The modulusr is the distancefromz to the origin, while the phasephi is the counterclockwiseangle, measured in radians, from the positive x-axis to the linesegment that joins the origin toz.

The following functions can be used to convert from the nativerectangular coordinates to polar coordinates and back.

cmath.phase(x)

Return the phase ofx (also known as theargument ofx), as afloat.phase(x) is equivalent tomath.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 ofx.imag, even whenx.imag is zero:

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

Note

The modulus (absolute value) of a complex numberx can becomputed using the built-inabs() function. There is noseparatecmath module function for this operation.

cmath.polar(x)

Return the representation ofx in polar coordinates. Returns apair(r,phi) wherer is the modulus ofx and phi is thephase ofx.polar(x) is equivalent to(abs(x),phase(x)).

cmath.rect(r,phi)

Return the complex numberx with polar coordinatesr andphi.Equivalent tor*(math.cos(phi)+math.sin(phi)*1j).

9.3.2. Power and logarithmic functions

cmath.exp(x)

Return the exponential valuee**x.

cmath.log(x[,base])

Returns the logarithm ofx to the givenbase. If thebase is notspecified, returns the natural logarithm ofx. There is one branch cut, from 0along the negative real axis to -∞, continuous from above.

cmath.log10(x)

Return the base-10 logarithm ofx. This has the same branch cut aslog().

cmath.sqrt(x)

Return the square root ofx. This has the same branch cut aslog().

9.3.3. Trigonometric functions

cmath.acos(x)

Return the arc cosine ofx. 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 ofx. This has the same branch cuts asacos().

cmath.atan(x)

Return the arc tangent ofx. There are two branch cuts: One extends from1j 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 ofx.

cmath.sin(x)

Return the sine ofx.

cmath.tan(x)

Return the tangent ofx.

9.3.4. Hyperbolic functions

cmath.acosh(x)

Return the hyperbolic arc cosine ofx. There is one branch cut, extending leftfrom 1 along the real axis to -∞, continuous from above.

cmath.asinh(x)

Return the hyperbolic arc sine ofx. There are two branch cuts:One extends from1j 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 hyperbolic arc tangent ofx. There are two branch cuts: Oneextends from1 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 ofx.

cmath.sinh(x)

Return the hyperbolic sine ofx.

cmath.tanh(x)

Return the hyperbolic tangent ofx.

9.3.5. Classification functions

cmath.isfinite(x)

ReturnTrue if both the real and imaginary parts ofx are finite, andFalse otherwise.

New in version 3.2.

cmath.isinf(x)

ReturnTrue if either the real or the imaginary part ofx is aninfinity, andFalse otherwise.

cmath.isnan(x)

ReturnTrue if either the real or the imaginary part ofx is a NaN,andFalse otherwise.

9.3.6. Constants

cmath.pi

The mathematical constantπ, as a float.

cmath.e

The mathematical constante, as a float.

Note that the selection of functions is similar, but not identical, to that inmodulemath. 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 havemath.sqrt(-1) raise an exception than return a complexnumber. Also note that the functions defined incmath 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.

Table Of Contents

Previous topic

9.2.math — Mathematical functions

Next topic

9.4.decimal — Decimal fixed point and floating point arithmetic

This Page

Quick search

Enter search terms or a module, class or function name.

Navigation

©Copyright 1990-2017, Python Software Foundation.
The Python Software Foundation is a non-profit corporation.Please donate.
Last updated on Sep 19, 2017.Found a bug?
Created usingSphinx 1.2.

[8]ページ先頭

©2009-2025 Movatter.jp