blob: 1a75d87b6b56934e07512fa7b70132b659ba07c2 [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
Mark Dickinsonf8edb672009-07-28 16:15:25 +000026Conversions to and from polar coordinates
27-----------------------------------------
Christian Heimes6f341092008-04-18 23:13:07 +000028
Mark Dickinsonf8edb672009-07-28 16:15:25 +000029A Python complex number ``z`` is stored internally using *rectangular*
30or *Cartesian* coordinates. It is completely determined by its *real
31part* ``z.real`` and its *imaginary part* ``z.imag``. In other
32words::
Christian Heimes6f341092008-04-18 23:13:07 +000033
Mark Dickinsonf8edb672009-07-28 16:15:25 +000034 z == z.real + z.imag*1j
Christian Heimes6f341092008-04-18 23:13:07 +000035
Mark Dickinsonf8edb672009-07-28 16:15:25 +000036*Polar coordinates* give an alternative way to represent a complex
37number. In polar coordinates, a complex number *z* is defined by the
38modulus *r* and the phase angle *phi*. The modulus *r* is the distance
39from *z* to the origin, while the phase *phi* is the counterclockwise
40angle from the positive x-axis to the line segment that joins the
41origin to *z*.
Christian Heimes6f341092008-04-18 23:13:07 +000042
Mark Dickinsonf8edb672009-07-28 16:15:25 +000043The following functions can be used to convert from the native
44rectangular coordinates to polar coordinates and back.
Christian Heimes6f341092008-04-18 23:13:07 +000045
46.. function:: phase(x)
47
Mark Dickinsonf8edb672009-07-28 16:15:25 +000048 Return the phase of *x* (also known as the *argument* of *x*), as a
49 float. ``phase(x)`` is equivalent to ``math.atan2(x.imag,
50 x.real)``. The result lies in the range [-π, π], and the branch
51 cut for this operation lies along the negative real axis,
52 continuous from above. On systems with support for signed zeros
53 (which includes most systems in current use), this means that the
54 sign of the result is the same as the sign of ``x.imag``, even when
55 ``x.imag`` is zero::
56
57 >>> phase(complex(-1.0, 0.0))
58 3.1415926535897931
59 >>> phase(complex(-1.0, -0.0))
60 -3.1415926535897931
Christian Heimes6f341092008-04-18 23:13:07 +000061
62 .. versionadded:: 2.6
63
64
Mark Dickinsonf8edb672009-07-28 16:15:25 +000065.. note::
66
67 The modulus (absolute value) of a complex number *x* can be
68 computed using the built-in :func:`abs` function. There is no
69 separate :mod:`cmath` module function for this operation.
70
71
Christian Heimes6f341092008-04-18 23:13:07 +000072.. function:: polar(x)
73
Mark Dickinsonf8edb672009-07-28 16:15:25 +000074 Return the representation of *x* in polar coordinates. Returns a
75 pair ``(r, phi)`` where *r* is the modulus of *x* and phi is the
76 phase of *x*. ``polar(x)`` is equivalent to ``(abs(x),
77 phase(x))``.
Christian Heimes6f341092008-04-18 23:13:07 +000078
79 .. versionadded:: 2.6
80
81
82.. function:: rect(r, phi)
83
Mark Dickinsonf8edb672009-07-28 16:15:25 +000084 Return the complex number *x* with polar coordinates *r* and *phi*.
85 Equivalent to ``r * (math.cos(phi) + math.sin(phi)*1j)``.
Christian Heimes6f341092008-04-18 23:13:07 +000086
87 .. versionadded:: 2.6
88
89
Mark Dickinsonf8edb672009-07-28 16:15:25 +000090Power and logarithmic functions
91-------------------------------
Georg Brandl8ec7f652007-08-15 14:28:01 +000092
93.. function:: exp(x)
94
95 Return the exponential value ``e**x``.
96
97
98.. function:: log(x[, base])
99
100 Returns the logarithm of *x* to the given *base*. If the *base* is not
101 specified, returns the natural logarithm of *x*. There is one branch cut, from 0
102 along the negative real axis to -∞, continuous from above.
103
104 .. versionchanged:: 2.4
105 *base* argument added.
106
107
108.. function:: log10(x)
109
110 Return the base-10 logarithm of *x*. This has the same branch cut as
111 :func:`log`.
112
113
Georg Brandl8ec7f652007-08-15 14:28:01 +0000114.. function:: sqrt(x)
115
116 Return the square root of *x*. This has the same branch cut as :func:`log`.
117
118
Mark Dickinsonf8edb672009-07-28 16:15:25 +0000119Trigonometric functions
120-----------------------
121
122.. function:: acos(x)
123
124 Return the arc cosine of *x*. There are two branch cuts: One extends right from
125 1 along the real axis to ∞, continuous from below. The other extends left from
126 -1 along the real axis to -∞, continuous from above.
127
128
129.. function:: asin(x)
130
131 Return the arc sine of *x*. This has the same branch cuts as :func:`acos`.
132
133
134.. function:: atan(x)
135
136 Return the arc tangent of *x*. There are two branch cuts: One extends from
137 ``1j`` along the imaginary axis to ``j``, continuous from the right. The
138 other extends from ``-1j`` along the imaginary axis to ``-∞j``, continuous
139 from the left.
140
141 .. versionchanged:: 2.6
142 direction of continuity of upper cut reversed
143
144
145.. function:: cos(x)
146
147 Return the cosine of *x*.
148
149
150.. function:: sin(x)
151
152 Return the sine of *x*.
153
154
Georg Brandl8ec7f652007-08-15 14:28:01 +0000155.. function:: tan(x)
156
157 Return the tangent of *x*.
158
159
Mark Dickinsonf8edb672009-07-28 16:15:25 +0000160Hyperbolic functions
161--------------------
162
163.. function:: acosh(x)
164
165 Return the hyperbolic arc cosine of *x*. There is one branch cut, extending left
166 from 1 along the real axis to -∞, continuous from above.
167
168
169.. function:: asinh(x)
170
171 Return the hyperbolic arc sine of *x*. There are two branch cuts:
172 One extends from ``1j`` along the imaginary axis to ``j``,
173 continuous from the right. The other extends from ``-1j`` along
174 the imaginary axis to ``-∞j``, continuous from the left.
175
176 .. versionchanged:: 2.6
177 branch cuts moved to match those recommended by the C99 standard
178
179
180.. function:: atanh(x)
181
182 Return the hyperbolic arc tangent of *x*. There are two branch cuts: One
183 extends from ``1`` along the real axis to ````, continuous from below. The
184 other extends from ``-1`` along the real axis to ``-∞``, continuous from
185 above.
186
187 .. versionchanged:: 2.6
188 direction of continuity of right cut reversed
189
190
191.. function:: cosh(x)
192
193 Return the hyperbolic cosine of *x*.
194
195
196.. function:: sinh(x)
197
198 Return the hyperbolic sine of *x*.
199
200
Georg Brandl8ec7f652007-08-15 14:28:01 +0000201.. function:: tanh(x)
202
203 Return the hyperbolic tangent of *x*.
204
Mark Dickinsonf8edb672009-07-28 16:15:25 +0000205
206Classification functions
207------------------------
208
209.. function:: isinf(x)
210
211 Return *True* if the real or the imaginary part of x is positive
212 or negative infinity.
213
214 .. versionadded:: 2.6
215
216
217.. function:: isnan(x)
218
219 Return *True* if the real or imaginary part of x is not a number (NaN).
220
221 .. versionadded:: 2.6
222
223
224Constants
225---------
Georg Brandl8ec7f652007-08-15 14:28:01 +0000226
227
228.. data:: pi
229
Mark Dickinsonf8edb672009-07-28 16:15:25 +0000230 The mathematical constant *π*, as a float.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000231
232
233.. data:: e
234
235 The mathematical constant *e*, as a float.
236
237.. index:: module: math
238
239Note that the selection of functions is similar, but not identical, to that in
240module :mod:`math`. The reason for having two modules is that some users aren't
241interested in complex numbers, and perhaps don't even know what they are. They
242would rather have ``math.sqrt(-1)`` raise an exception than return a complex
243number. Also note that the functions defined in :mod:`cmath` always return a
244complex number, even if the answer can be expressed as a real number (in which
245case the complex number has an imaginary part of zero).
246
247A note on branch cuts: They are curves along which the given function fails to
248be continuous. They are a necessary feature of many complex functions. It is
249assumed that if you need to compute with complex functions, you will understand
250about branch cuts. Consult almost any (not too elementary) book on complex
251variables for enlightenment. For information of the proper choice of branch
252cuts for numerical purposes, a good reference should be the following:
253
254
255.. seealso::
256
257 Kahan, W: Branch cuts for complex elementary functions; or, Much ado about
258 nothing's sign bit. In Iserles, A., and Powell, M. (eds.), The state of the art
259 in numerical analysis. Clarendon Press (1987) pp165-211.
260
Christian Heimes6f341092008-04-18 23:13:07 +0000261