blob: 4469573ccf4229bdc56558df269d37eb911bba76 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001
2:mod:`cmath` --- Mathematical functions for complex numbers
3===========================================================
4
5.. module:: cmath
6 :synopsis: Mathematical functions for complex numbers.
7
8
9This module is always available. It provides access to mathematical functions
10for complex numbers. The functions in this module accept integers,
11floating-point numbers or complex numbers as arguments. They will also accept
12any Python object that has either a :meth:`__complex__` or a :meth:`__float__`
13method: these methods are used to convert the object to a complex or
14floating-point number, respectively, and the function is then applied to the
15result of the conversion.
16
Christian Heimes6f341092008-04-18 23:13:07 +000017.. note::
Georg Brandl8ec7f652007-08-15 14:28:01 +000018
Christian Heimes6f341092008-04-18 23:13:07 +000019 On platforms with hardware and system-level support for signed
20 zeros, functions involving branch cuts are continuous on *both*
21 sides of the branch cut: the sign of the zero distinguishes one
22 side of the branch cut from the other. On platforms that do not
23 support signed zeros the continuity is as specified below.
24
25
26Complex coordinates
27-------------------
28
29Complex numbers can be expressed by two important coordinate systems.
30Python's :class:`complex` type uses rectangular coordinates where a number
31on the complex plain is defined by two floats, the real part and the imaginary
32part.
33
34Definition::
35
36 z = x + 1j * y
37
38 x := real(z)
39 y := imag(z)
40
41In engineering the polar coordinate system is popular for complex numbers. In
42polar coordinates a complex number is defined by the radius *r* and the phase
Georg Brandl3e8bb4e2008-06-22 19:07:59 +000043angle *phi*. The radius *r* is the absolute value of the complex, which can be
Christian Heimes6f341092008-04-18 23:13:07 +000044viewed as distance from (0, 0). The radius *r* is always 0 or a positive float.
Georg Brandl3e8bb4e2008-06-22 19:07:59 +000045The phase angle *phi* is the counter clockwise angle from the positive x axis,
Christian Heimes6f341092008-04-18 23:13:07 +000046e.g. *1* has the angle *0*, *1j* has the angle *π/2* and *-1* the angle *-π*.
47
48.. note::
49 While :func:`phase` and func:`polar` return *+π* for a negative real they
50 may return *-π* for a complex with a very small negative imaginary
51 part, e.g. *-1-1E-300j*.
52
53
54Definition::
55
Georg Brandl3e8bb4e2008-06-22 19:07:59 +000056 z = r * exp(1j * phi)
57 z = r * cis(phi)
Christian Heimes6f341092008-04-18 23:13:07 +000058
59 r := abs(z) := sqrt(real(z)**2 + imag(z)**2)
60 phi := phase(z) := atan2(imag(z), real(z))
Georg Brandl3e8bb4e2008-06-22 19:07:59 +000061 cis(phi) := cos(phi) + 1j * sin(phi)
Christian Heimes6f341092008-04-18 23:13:07 +000062
63
64.. function:: phase(x)
65
66 Return phase, also known as the argument, of a complex.
67
68 .. versionadded:: 2.6
69
70
71.. function:: polar(x)
72
Georg Brandlc62ef8b2009-01-03 20:55:06 +000073 Convert a :class:`complex` from rectangular coordinates to polar
Christian Heimes6f341092008-04-18 23:13:07 +000074 coordinates. The function returns a tuple with the two elements
Georg Brandlc62ef8b2009-01-03 20:55:06 +000075 *r* and *phi*. *r* is the distance from 0 and *phi* the phase
Christian Heimes6f341092008-04-18 23:13:07 +000076 angle.
77
78 .. versionadded:: 2.6
79
80
81.. function:: rect(r, phi)
82
83 Convert from polar coordinates to rectangular coordinates and return
84 a :class:`complex`.
85
86 .. versionadded:: 2.6
87
88
89
90cmath functions
91---------------
Georg Brandl8ec7f652007-08-15 14:28:01 +000092
93.. function:: acos(x)
94
95 Return the arc cosine of *x*. There are two branch cuts: One extends right from
96 1 along the real axis to ∞, continuous from below. The other extends left from
97 -1 along the real axis to -∞, continuous from above.
98
99
100.. function:: acosh(x)
101
102 Return the hyperbolic arc cosine of *x*. There is one branch cut, extending left
103 from 1 along the real axis to -∞, continuous from above.
104
105
106.. function:: asin(x)
107
108 Return the arc sine of *x*. This has the same branch cuts as :func:`acos`.
109
110
111.. function:: asinh(x)
112
Christian Heimes6f341092008-04-18 23:13:07 +0000113 Return the hyperbolic arc sine of *x*. There are two branch cuts:
114 One extends from ``1j`` along the imaginary axis to ``∞j``,
115 continuous from the right. The other extends from ``-1j`` along
116 the imaginary axis to ``-∞j``, continuous from the left.
117
118 .. versionchanged:: 2.6
119 branch cuts moved to match those recommended by the C99 standard
Georg Brandl8ec7f652007-08-15 14:28:01 +0000120
121
122.. function:: atan(x)
123
124 Return the arc tangent of *x*. There are two branch cuts: One extends from
Christian Heimes6f341092008-04-18 23:13:07 +0000125 ``1j`` along the imaginary axis to ``∞j``, continuous from the right. The
Georg Brandl8ec7f652007-08-15 14:28:01 +0000126 other extends from ``-1j`` along the imaginary axis to ``-∞j``, continuous
Christian Heimes6f341092008-04-18 23:13:07 +0000127 from the left.
128
129 .. versionchanged:: 2.6
130 direction of continuity of upper cut reversed
Georg Brandl8ec7f652007-08-15 14:28:01 +0000131
132
133.. function:: atanh(x)
134
135 Return the hyperbolic arc tangent of *x*. There are two branch cuts: One
Christian Heimes6f341092008-04-18 23:13:07 +0000136 extends from ``1`` along the real axis to ``∞``, continuous from below. The
Georg Brandl8ec7f652007-08-15 14:28:01 +0000137 other extends from ``-1`` along the real axis to ``-∞``, continuous from
Christian Heimes6f341092008-04-18 23:13:07 +0000138 above.
139
140 .. versionchanged:: 2.6
141 direction of continuity of right cut reversed
Georg Brandl8ec7f652007-08-15 14:28:01 +0000142
143
144.. function:: cos(x)
145
146 Return the cosine of *x*.
147
148
149.. function:: cosh(x)
150
151 Return the hyperbolic cosine of *x*.
152
153
154.. function:: exp(x)
155
156 Return the exponential value ``e**x``.
157
158
Christian Heimes6f341092008-04-18 23:13:07 +0000159.. function:: isinf(x)
160
161 Return *True* if the real or the imaginary part of x is positive
162 or negative infinity.
163
164 .. versionadded:: 2.6
165
166
167.. function:: isnan(x)
168
169 Return *True* if the real or imaginary part of x is not a number (NaN).
170
171 .. versionadded:: 2.6
172
173
Georg Brandl8ec7f652007-08-15 14:28:01 +0000174.. function:: log(x[, base])
175
176 Returns the logarithm of *x* to the given *base*. If the *base* is not
177 specified, returns the natural logarithm of *x*. There is one branch cut, from 0
178 along the negative real axis to -∞, continuous from above.
179
180 .. versionchanged:: 2.4
181 *base* argument added.
182
183
184.. function:: log10(x)
185
186 Return the base-10 logarithm of *x*. This has the same branch cut as
187 :func:`log`.
188
189
190.. function:: sin(x)
191
192 Return the sine of *x*.
193
194
195.. function:: sinh(x)
196
197 Return the hyperbolic sine of *x*.
198
199
200.. function:: sqrt(x)
201
202 Return the square root of *x*. This has the same branch cut as :func:`log`.
203
204
205.. function:: tan(x)
206
207 Return the tangent of *x*.
208
209
210.. function:: tanh(x)
211
212 Return the hyperbolic tangent of *x*.
213
214The module also defines two mathematical constants:
215
216
217.. data:: pi
218
219 The mathematical constant *pi*, as a float.
220
221
222.. data:: e
223
224 The mathematical constant *e*, as a float.
225
226.. index:: module: math
227
228Note that the selection of functions is similar, but not identical, to that in
229module :mod:`math`. The reason for having two modules is that some users aren't
230interested in complex numbers, and perhaps don't even know what they are. They
231would rather have ``math.sqrt(-1)`` raise an exception than return a complex
232number. Also note that the functions defined in :mod:`cmath` always return a
233complex number, even if the answer can be expressed as a real number (in which
234case the complex number has an imaginary part of zero).
235
236A note on branch cuts: They are curves along which the given function fails to
237be continuous. They are a necessary feature of many complex functions. It is
238assumed that if you need to compute with complex functions, you will understand
239about branch cuts. Consult almost any (not too elementary) book on complex
240variables for enlightenment. For information of the proper choice of branch
241cuts for numerical purposes, a good reference should be the following:
242
243
244.. seealso::
245
246 Kahan, W: Branch cuts for complex elementary functions; or, Much ado about
247 nothing's sign bit. In Iserles, A., and Powell, M. (eds.), The state of the art
248 in numerical analysis. Clarendon Press (1987) pp165-211.
249
Christian Heimes6f341092008-04-18 23:13:07 +0000250