blob: 8761c886b800e3a57d7df7c2bc8c3eb98fe1a5c7 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`cmath` --- Mathematical functions for complex numbers
2===========================================================
3
4.. module:: cmath
5 :synopsis: Mathematical functions for complex numbers.
6
7
8This module is always available. It provides access to mathematical functions
9for complex numbers. The functions in this module accept integers,
10floating-point numbers or complex numbers as arguments. They will also accept
11any Python object that has either a :meth:`__complex__` or a :meth:`__float__`
12method: these methods are used to convert the object to a complex or
13floating-point number, respectively, and the function is then applied to the
14result of the conversion.
15
Christian Heimes53876d92008-04-19 00:31:39 +000016.. note::
Georg Brandl116aa622007-08-15 14:28:22 +000017
Christian Heimes53876d92008-04-19 00:31:39 +000018 On platforms with hardware and system-level support for signed
19 zeros, functions involving branch cuts are continuous on *both*
20 sides of the branch cut: the sign of the zero distinguishes one
21 side of the branch cut from the other. On platforms that do not
22 support signed zeros the continuity is as specified below.
23
24
25Complex coordinates
26-------------------
27
28Complex numbers can be expressed by two important coordinate systems.
29Python's :class:`complex` type uses rectangular coordinates where a number
30on the complex plain is defined by two floats, the real part and the imaginary
31part.
32
33Definition::
34
35 z = x + 1j * y
36
37 x := real(z)
38 y := imag(z)
39
40In engineering the polar coordinate system is popular for complex numbers. In
41polar coordinates a complex number is defined by the radius *r* and the phase
Benjamin Petersondcf97b92008-07-02 17:30:14 +000042angle *phi*. The radius *r* is the absolute value of the complex, which can be
Christian Heimes53876d92008-04-19 00:31:39 +000043viewed as distance from (0, 0). The radius *r* is always 0 or a positive float.
Benjamin Petersondcf97b92008-07-02 17:30:14 +000044The phase angle *phi* is the counter clockwise angle from the positive x axis,
Christian Heimes53876d92008-04-19 00:31:39 +000045e.g. *1* has the angle *0*, *1j* has the angle *π/2* and *-1* the angle *-π*.
46
47.. note::
48 While :func:`phase` and func:`polar` return *+π* for a negative real they
49 may return *-π* for a complex with a very small negative imaginary
50 part, e.g. *-1-1E-300j*.
51
52
53Definition::
54
Benjamin Petersondcf97b92008-07-02 17:30:14 +000055 z = r * exp(1j * phi)
56 z = r * cis(phi)
Christian Heimes53876d92008-04-19 00:31:39 +000057
58 r := abs(z) := sqrt(real(z)**2 + imag(z)**2)
59 phi := phase(z) := atan2(imag(z), real(z))
Benjamin Petersondcf97b92008-07-02 17:30:14 +000060 cis(phi) := cos(phi) + 1j * sin(phi)
Christian Heimes53876d92008-04-19 00:31:39 +000061
62
63.. function:: phase(x)
64
65 Return phase, also known as the argument, of a complex.
66
Christian Heimes53876d92008-04-19 00:31:39 +000067
68.. function:: polar(x)
69
Georg Brandl48310cd2009-01-03 21:18:54 +000070 Convert a :class:`complex` from rectangular coordinates to polar
Christian Heimes53876d92008-04-19 00:31:39 +000071 coordinates. The function returns a tuple with the two elements
Georg Brandl48310cd2009-01-03 21:18:54 +000072 *r* and *phi*. *r* is the distance from 0 and *phi* the phase
Christian Heimes53876d92008-04-19 00:31:39 +000073 angle.
74
Christian Heimes53876d92008-04-19 00:31:39 +000075
76.. function:: rect(r, phi)
77
78 Convert from polar coordinates to rectangular coordinates and return
79 a :class:`complex`.
80
Christian Heimes53876d92008-04-19 00:31:39 +000081
82
83cmath functions
84---------------
Georg Brandl116aa622007-08-15 14:28:22 +000085
86.. function:: acos(x)
87
88 Return the arc cosine of *x*. There are two branch cuts: One extends right from
89 1 along the real axis to ∞, continuous from below. The other extends left from
90 -1 along the real axis to -∞, continuous from above.
91
92
93.. function:: acosh(x)
94
95 Return the hyperbolic arc cosine of *x*. There is one branch cut, extending left
96 from 1 along the real axis to -∞, continuous from above.
97
98
99.. function:: asin(x)
100
101 Return the arc sine of *x*. This has the same branch cuts as :func:`acos`.
102
103
104.. function:: asinh(x)
105
Christian Heimes53876d92008-04-19 00:31:39 +0000106 Return the hyperbolic arc sine of *x*. There are two branch cuts:
107 One extends from ``1j`` along the imaginary axis to ``∞j``,
108 continuous from the right. The other extends from ``-1j`` along
109 the imaginary axis to ``-∞j``, continuous from the left.
110
Georg Brandl116aa622007-08-15 14:28:22 +0000111
112.. function:: atan(x)
113
114 Return the arc tangent of *x*. There are two branch cuts: One extends from
Christian Heimes53876d92008-04-19 00:31:39 +0000115 ``1j`` along the imaginary axis to ``∞j``, continuous from the right. The
Georg Brandl116aa622007-08-15 14:28:22 +0000116 other extends from ``-1j`` along the imaginary axis to ``-∞j``, continuous
Christian Heimes53876d92008-04-19 00:31:39 +0000117 from the left.
118
Georg Brandl116aa622007-08-15 14:28:22 +0000119
120.. function:: atanh(x)
121
122 Return the hyperbolic arc tangent of *x*. There are two branch cuts: One
Christian Heimes53876d92008-04-19 00:31:39 +0000123 extends from ``1`` along the real axis to ``∞``, continuous from below. The
Georg Brandl116aa622007-08-15 14:28:22 +0000124 other extends from ``-1`` along the real axis to ``-∞``, continuous from
Christian Heimes53876d92008-04-19 00:31:39 +0000125 above.
126
Georg Brandl116aa622007-08-15 14:28:22 +0000127
128.. function:: cos(x)
129
130 Return the cosine of *x*.
131
132
133.. function:: cosh(x)
134
135 Return the hyperbolic cosine of *x*.
136
137
138.. function:: exp(x)
139
140 Return the exponential value ``e**x``.
141
142
Christian Heimes53876d92008-04-19 00:31:39 +0000143.. function:: isinf(x)
144
145 Return *True* if the real or the imaginary part of x is positive
146 or negative infinity.
147
Christian Heimes53876d92008-04-19 00:31:39 +0000148
149.. function:: isnan(x)
150
151 Return *True* if the real or imaginary part of x is not a number (NaN).
152
Christian Heimes53876d92008-04-19 00:31:39 +0000153
Georg Brandl116aa622007-08-15 14:28:22 +0000154.. function:: log(x[, base])
155
156 Returns the logarithm of *x* to the given *base*. If the *base* is not
157 specified, returns the natural logarithm of *x*. There is one branch cut, from 0
158 along the negative real axis to -∞, continuous from above.
159
Georg Brandl116aa622007-08-15 14:28:22 +0000160
161.. function:: log10(x)
162
163 Return the base-10 logarithm of *x*. This has the same branch cut as
164 :func:`log`.
165
166
167.. function:: sin(x)
168
169 Return the sine of *x*.
170
171
172.. function:: sinh(x)
173
174 Return the hyperbolic sine of *x*.
175
176
177.. function:: sqrt(x)
178
179 Return the square root of *x*. This has the same branch cut as :func:`log`.
180
181
182.. function:: tan(x)
183
184 Return the tangent of *x*.
185
186
187.. function:: tanh(x)
188
189 Return the hyperbolic tangent of *x*.
190
191The module also defines two mathematical constants:
192
193
194.. data:: pi
195
196 The mathematical constant *pi*, as a float.
197
198
199.. data:: e
200
201 The mathematical constant *e*, as a float.
202
203.. index:: module: math
204
205Note that the selection of functions is similar, but not identical, to that in
206module :mod:`math`. The reason for having two modules is that some users aren't
207interested in complex numbers, and perhaps don't even know what they are. They
208would rather have ``math.sqrt(-1)`` raise an exception than return a complex
209number. Also note that the functions defined in :mod:`cmath` always return a
210complex number, even if the answer can be expressed as a real number (in which
211case the complex number has an imaginary part of zero).
212
213A note on branch cuts: They are curves along which the given function fails to
214be continuous. They are a necessary feature of many complex functions. It is
215assumed that if you need to compute with complex functions, you will understand
216about branch cuts. Consult almost any (not too elementary) book on complex
217variables for enlightenment. For information of the proper choice of branch
218cuts for numerical purposes, a good reference should be the following:
219
220
221.. seealso::
222
223 Kahan, W: Branch cuts for complex elementary functions; or, Much ado about
224 nothing's sign bit. In Iserles, A., and Powell, M. (eds.), The state of the art
225 in numerical analysis. Clarendon Press (1987) pp165-211.
226
Christian Heimes53876d92008-04-19 00:31:39 +0000227