blob: b50ca5ebfc4ce3027b62ba104dfd4a9aa34d7a83 [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
Mark Dickinson1de22552009-07-28 16:32:56 +000025Conversions to and from polar coordinates
26-----------------------------------------
Christian Heimes53876d92008-04-19 00:31:39 +000027
Mark Dickinson1de22552009-07-28 16:32:56 +000028A Python complex number ``z`` is stored internally using *rectangular*
29or *Cartesian* coordinates. It is completely determined by its *real
30part* ``z.real`` and its *imaginary part* ``z.imag``. In other
31words::
Christian Heimes53876d92008-04-19 00:31:39 +000032
Mark Dickinson1de22552009-07-28 16:32:56 +000033 z == z.real + z.imag*1j
Christian Heimes53876d92008-04-19 00:31:39 +000034
Mark Dickinson1de22552009-07-28 16:32:56 +000035*Polar coordinates* give an alternative way to represent a complex
36number. In polar coordinates, a complex number *z* is defined by the
37modulus *r* and the phase angle *phi*. The modulus *r* is the distance
38from *z* to the origin, while the phase *phi* is the counterclockwise
39angle from the positive x-axis to the line segment that joins the
40origin to *z*.
Christian Heimes53876d92008-04-19 00:31:39 +000041
Mark Dickinson1de22552009-07-28 16:32:56 +000042The following functions can be used to convert from the native
43rectangular coordinates to polar coordinates and back.
Christian Heimes53876d92008-04-19 00:31:39 +000044
45.. function:: phase(x)
46
Mark Dickinson1de22552009-07-28 16:32:56 +000047 Return the phase of *x* (also known as the *argument* of *x*), as a
48 float. ``phase(x)`` is equivalent to ``math.atan2(x.imag,
49 x.real)``. The result lies in the range [-π, π], and the branch
50 cut for this operation lies along the negative real axis,
51 continuous from above. On systems with support for signed zeros
52 (which includes most systems in current use), this means that the
53 sign of the result is the same as the sign of ``x.imag``, even when
54 ``x.imag`` is zero::
55
56 >>> phase(complex(-1.0, 0.0))
57 3.141592653589793
58 >>> phase(complex(-1.0, -0.0))
59 -3.141592653589793
60
61
62.. note::
63
64 The modulus (absolute value) of a complex number *x* can be
65 computed using the built-in :func:`abs` function. There is no
66 separate :mod:`cmath` module function for this operation.
Christian Heimes53876d92008-04-19 00:31:39 +000067
Christian Heimes53876d92008-04-19 00:31:39 +000068
69.. function:: polar(x)
70
Mark Dickinson1de22552009-07-28 16:32:56 +000071 Return the representation of *x* in polar coordinates. Returns a
72 pair ``(r, phi)`` where *r* is the modulus of *x* and phi is the
73 phase of *x*. ``polar(x)`` is equivalent to ``(abs(x),
74 phase(x))``.
Christian Heimes53876d92008-04-19 00:31:39 +000075
Christian Heimes53876d92008-04-19 00:31:39 +000076
77.. function:: rect(r, phi)
78
Mark Dickinson1de22552009-07-28 16:32:56 +000079 Return the complex number *x* with polar coordinates *r* and *phi*.
80 Equivalent to ``r * (math.cos(phi) + math.sin(phi)*1j)``.
Christian Heimes53876d92008-04-19 00:31:39 +000081
Christian Heimes53876d92008-04-19 00:31:39 +000082
Mark Dickinson1de22552009-07-28 16:32:56 +000083Power and logarithmic functions
84-------------------------------
Christian Heimes53876d92008-04-19 00:31:39 +000085
Mark Dickinson1de22552009-07-28 16:32:56 +000086.. function:: exp(x)
87
88 Return the exponential value ``e**x``.
89
90
91.. function:: log(x[, base])
92
93 Returns the logarithm of *x* to the given *base*. If the *base* is not
94 specified, returns the natural logarithm of *x*. There is one branch cut, from 0
95 along the negative real axis to -∞, continuous from above.
96
97 .. versionchanged:: 2.4
98 *base* argument added.
99
100
101.. function:: log10(x)
102
103 Return the base-10 logarithm of *x*. This has the same branch cut as
104 :func:`log`.
105
106
107.. function:: sqrt(x)
108
109 Return the square root of *x*. This has the same branch cut as :func:`log`.
110
111
112Trigonometric functions
113-----------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000114
115.. function:: acos(x)
116
117 Return the arc cosine of *x*. There are two branch cuts: One extends right from
118 1 along the real axis to ∞, continuous from below. The other extends left from
119 -1 along the real axis to -∞, continuous from above.
120
121
Georg Brandl116aa622007-08-15 14:28:22 +0000122.. function:: asin(x)
123
124 Return the arc sine of *x*. This has the same branch cuts as :func:`acos`.
125
126
Georg Brandl116aa622007-08-15 14:28:22 +0000127.. function:: atan(x)
128
129 Return the arc tangent of *x*. There are two branch cuts: One extends from
Christian Heimes53876d92008-04-19 00:31:39 +0000130 ``1j`` along the imaginary axis to ``j``, continuous from the right. The
Georg Brandl116aa622007-08-15 14:28:22 +0000131 other extends from ``-1j`` along the imaginary axis to ``-∞j``, continuous
Christian Heimes53876d92008-04-19 00:31:39 +0000132 from the left.
133
Georg Brandl116aa622007-08-15 14:28:22 +0000134
Mark Dickinson1de22552009-07-28 16:32:56 +0000135.. function:: cos(x)
136
137 Return the cosine of *x*.
138
139
140.. function:: sin(x)
141
142 Return the sine of *x*.
143
144
145.. function:: tan(x)
146
147 Return the tangent of *x*.
148
149
150Hyperbolic functions
151--------------------
152
153.. function:: acosh(x)
154
155 Return the hyperbolic arc cosine of *x*. There is one branch cut, extending left
156 from 1 along the real axis to -∞, continuous from above.
157
158
159.. function:: asinh(x)
160
161 Return the hyperbolic arc sine of *x*. There are two branch cuts:
162 One extends from ``1j`` along the imaginary axis to ``j``,
163 continuous from the right. The other extends from ``-1j`` along
164 the imaginary axis to ``-∞j``, continuous from the left.
165
166
Georg Brandl116aa622007-08-15 14:28:22 +0000167.. function:: atanh(x)
168
169 Return the hyperbolic arc tangent of *x*. There are two branch cuts: One
Christian Heimes53876d92008-04-19 00:31:39 +0000170 extends from ``1`` along the real axis to ````, continuous from below. The
Georg Brandl116aa622007-08-15 14:28:22 +0000171 other extends from ``-1`` along the real axis to ``-∞``, continuous from
Christian Heimes53876d92008-04-19 00:31:39 +0000172 above.
173
Georg Brandl116aa622007-08-15 14:28:22 +0000174
Georg Brandl116aa622007-08-15 14:28:22 +0000175.. function:: cosh(x)
176
177 Return the hyperbolic cosine of *x*.
178
179
Mark Dickinson1de22552009-07-28 16:32:56 +0000180.. function:: sinh(x)
Georg Brandl116aa622007-08-15 14:28:22 +0000181
Mark Dickinson1de22552009-07-28 16:32:56 +0000182 Return the hyperbolic sine of *x*.
Georg Brandl116aa622007-08-15 14:28:22 +0000183
184
Mark Dickinson1de22552009-07-28 16:32:56 +0000185.. function:: tanh(x)
186
187 Return the hyperbolic tangent of *x*.
188
189
190Classification functions
191------------------------
192
Christian Heimes53876d92008-04-19 00:31:39 +0000193.. function:: isinf(x)
194
195 Return *True* if the real or the imaginary part of x is positive
196 or negative infinity.
197
Christian Heimes53876d92008-04-19 00:31:39 +0000198
199.. function:: isnan(x)
200
201 Return *True* if the real or imaginary part of x is not a number (NaN).
202
Christian Heimes53876d92008-04-19 00:31:39 +0000203
Mark Dickinson1de22552009-07-28 16:32:56 +0000204Constants
205---------
Georg Brandl116aa622007-08-15 14:28:22 +0000206
207
208.. data:: pi
209
Mark Dickinson1de22552009-07-28 16:32:56 +0000210 The mathematical constant *π*, as a float.
Georg Brandl116aa622007-08-15 14:28:22 +0000211
212
213.. data:: e
214
215 The mathematical constant *e*, as a float.
216
217.. index:: module: math
218
219Note that the selection of functions is similar, but not identical, to that in
220module :mod:`math`. The reason for having two modules is that some users aren't
221interested in complex numbers, and perhaps don't even know what they are. They
222would rather have ``math.sqrt(-1)`` raise an exception than return a complex
223number. Also note that the functions defined in :mod:`cmath` always return a
224complex number, even if the answer can be expressed as a real number (in which
225case the complex number has an imaginary part of zero).
226
227A note on branch cuts: They are curves along which the given function fails to
228be continuous. They are a necessary feature of many complex functions. It is
229assumed that if you need to compute with complex functions, you will understand
230about branch cuts. Consult almost any (not too elementary) book on complex
231variables for enlightenment. For information of the proper choice of branch
232cuts for numerical purposes, a good reference should be the following:
233
234
235.. seealso::
236
237 Kahan, W: Branch cuts for complex elementary functions; or, Much ado about
238 nothing's sign bit. In Iserles, A., and Powell, M. (eds.), The state of the art
239 in numerical analysis. Clarendon Press (1987) pp165-211.
240
Christian Heimes53876d92008-04-19 00:31:39 +0000241