1:mod:`cmath` --- Mathematical functions for complex numbers
2===========================================================
3
4.. module:: cmath
5   :synopsis: Mathematical functions for complex numbers.
6
7--------------
8
9This module provides access to mathematical functions for complex numbers.  The
10functions in this module accept integers, floating-point numbers or complex
11numbers as arguments. They will also accept any Python object that has either a
12:meth:`__complex__` or a :meth:`__float__` method: these methods are used to
13convert the object to a complex or floating-point number, respectively, and
14the function is then applied to the result of the conversion.
15
16.. note::
17
18   For functions involving branch cuts, we have the problem of deciding how to
19   define those functions on the cut itself. Following Kahan's "Branch cuts for
20   complex elementary functions" paper, as well as Annex G of C99 and later C
21   standards, we use the sign of zero to distinguish one side of the branch cut
22   from the other: for a branch cut along (a portion of) the real axis we look
23   at the sign of the imaginary part, while for a branch cut along the
24   imaginary axis we look at the sign of the real part.
25
26   For example, the :func:`cmath.sqrt` function has a branch cut along the
27   negative real axis. An argument of ``complex(-2.0, -0.0)`` is treated as
28   though it lies *below* the branch cut, and so gives a result on the negative
29   imaginary axis::
30
31      >>> cmath.sqrt(complex(-2.0, -0.0))
32      -1.4142135623730951j
33
34   But an argument of ``complex(-2.0, 0.0)`` is treated as though it lies above
35   the branch cut::
36
37      >>> cmath.sqrt(complex(-2.0, 0.0))
38      1.4142135623730951j
39
40
41Conversions to and from polar coordinates
42-----------------------------------------
43
44A Python complex number ``z`` is stored internally using *rectangular*
45or *Cartesian* coordinates.  It is completely determined by its *real
46part* ``z.real`` and its *imaginary part* ``z.imag``.  In other
47words::
48
49   z == z.real + z.imag*1j
50
51*Polar coordinates* give an alternative way to represent a complex
52number.  In polar coordinates, a complex number *z* is defined by the
53modulus *r* and the phase angle *phi*. The modulus *r* is the distance
54from *z* to the origin, while the phase *phi* is the counterclockwise
55angle, measured in radians, from the positive x-axis to the line
56segment that joins the origin to *z*.
57
58The following functions can be used to convert from the native
59rectangular coordinates to polar coordinates and back.
60
61.. function:: phase(x)
62
63   Return the phase of *x* (also known as the *argument* of *x*), as a float.
64   ``phase(x)`` is equivalent to ``math.atan2(x.imag, x.real)``.  The result
65   lies in the range [-\ *π*, *π*], and the branch cut for this operation lies
66   along the negative real axis.  The sign of the result is the same as the
67   sign of ``x.imag``, even when ``x.imag`` is zero::
68
69      >>> phase(complex(-1.0, 0.0))
70      3.141592653589793
71      >>> phase(complex(-1.0, -0.0))
72      -3.141592653589793
73
74
75.. note::
76
77   The modulus (absolute value) of a complex number *x* can be
78   computed using the built-in :func:`abs` function.  There is no
79   separate :mod:`cmath` module function for this operation.
80
81
82.. function:: polar(x)
83
84   Return the representation of *x* in polar coordinates.  Returns a
85   pair ``(r, phi)`` where *r* is the modulus of *x* and phi is the
86   phase of *x*.  ``polar(x)`` is equivalent to ``(abs(x),
87   phase(x))``.
88
89
90.. function:: rect(r, phi)
91
92   Return the complex number *x* with polar coordinates *r* and *phi*.
93   Equivalent to ``r * (math.cos(phi) + math.sin(phi)*1j)``.
94
95
96Power and logarithmic functions
97-------------------------------
98
99.. function:: exp(x)
100
101   Return *e* raised to the power *x*, where *e* is the base of natural
102   logarithms.
103
104
105.. function:: log(x[, base])
106
107   Returns the logarithm of *x* to the given *base*. If the *base* is not
108   specified, returns the natural logarithm of *x*. There is one branch cut,
109   from 0 along the negative real axis to -∞.
110
111
112.. function:: log10(x)
113
114   Return the base-10 logarithm of *x*. This has the same branch cut as
115   :func:`log`.
116
117
118.. function:: sqrt(x)
119
120   Return the square root of *x*. This has the same branch cut as :func:`log`.
121
122
123Trigonometric functions
124-----------------------
125
126.. function:: acos(x)
127
128   Return the arc cosine of *x*. There are two branch cuts: One extends right
129   from 1 along the real axis to ∞. The other extends left from -1 along the
130   real axis to -∞.
131
132
133.. function:: asin(x)
134
135   Return the arc sine of *x*. This has the same branch cuts as :func:`acos`.
136
137
138.. function:: atan(x)
139
140   Return the arc tangent of *x*. There are two branch cuts: One extends from
141   ``1j`` along the imaginary axis to ``∞j``. The other extends from ``-1j``
142   along the imaginary axis to ``-∞j``.
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
155.. function:: tan(x)
156
157   Return the tangent of *x*.
158
159
160Hyperbolic functions
161--------------------
162
163.. function:: acosh(x)
164
165   Return the inverse hyperbolic cosine of *x*. There is one branch cut,
166   extending left from 1 along the real axis to -∞.
167
168
169.. function:: asinh(x)
170
171   Return the inverse hyperbolic sine of *x*. There are two branch cuts:
172   One extends from ``1j`` along the imaginary axis to ``∞j``.  The other
173   extends from ``-1j`` along the imaginary axis to ``-∞j``.
174
175
176.. function:: atanh(x)
177
178   Return the inverse hyperbolic tangent of *x*. There are two branch cuts: One
179   extends from ``1`` along the real axis to ``∞``. The other extends from
180   ``-1`` along the real axis to ``-∞``.
181
182
183.. function:: cosh(x)
184
185   Return the hyperbolic cosine of *x*.
186
187
188.. function:: sinh(x)
189
190   Return the hyperbolic sine of *x*.
191
192
193.. function:: tanh(x)
194
195   Return the hyperbolic tangent of *x*.
196
197
198Classification functions
199------------------------
200
201.. function:: isfinite(x)
202
203   Return ``True`` if both the real and imaginary parts of *x* are finite, and
204   ``False`` otherwise.
205
206   .. versionadded:: 3.2
207
208
209.. function:: isinf(x)
210
211   Return ``True`` if either the real or the imaginary part of *x* is an
212   infinity, and ``False`` otherwise.
213
214
215.. function:: isnan(x)
216
217   Return ``True`` if either the real or the imaginary part of *x* is a NaN,
218   and ``False`` otherwise.
219
220
221.. function:: isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0)
222
223   Return ``True`` if the values *a* and *b* are close to each other and
224   ``False`` otherwise.
225
226   Whether or not two values are considered close is determined according to
227   given absolute and relative tolerances.
228
229   *rel_tol* is the relative tolerance -- it is the maximum allowed difference
230   between *a* and *b*, relative to the larger absolute value of *a* or *b*.
231   For example, to set a tolerance of 5%, pass ``rel_tol=0.05``.  The default
232   tolerance is ``1e-09``, which assures that the two values are the same
233   within about 9 decimal digits.  *rel_tol* must be greater than zero.
234
235   *abs_tol* is the minimum absolute tolerance -- useful for comparisons near
236   zero. *abs_tol* must be at least zero.
237
238   If no errors occur, the result will be:
239   ``abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)``.
240
241   The IEEE 754 special values of ``NaN``, ``inf``, and ``-inf`` will be
242   handled according to IEEE rules.  Specifically, ``NaN`` is not considered
243   close to any other value, including ``NaN``.  ``inf`` and ``-inf`` are only
244   considered close to themselves.
245
246   .. versionadded:: 3.5
247
248   .. seealso::
249
250      :pep:`485` -- A function for testing approximate equality
251
252
253Constants
254---------
255
256.. data:: pi
257
258   The mathematical constant *π*, as a float.
259
260
261.. data:: e
262
263   The mathematical constant *e*, as a float.
264
265
266.. data:: tau
267
268   The mathematical constant *τ*, as a float.
269
270   .. versionadded:: 3.6
271
272
273.. data:: inf
274
275   Floating-point positive infinity. Equivalent to ``float('inf')``.
276
277   .. versionadded:: 3.6
278
279
280.. data:: infj
281
282   Complex number with zero real part and positive infinity imaginary
283   part. Equivalent to ``complex(0.0, float('inf'))``.
284
285   .. versionadded:: 3.6
286
287
288.. data:: nan
289
290   A floating-point "not a number" (NaN) value.  Equivalent to
291   ``float('nan')``.
292
293   .. versionadded:: 3.6
294
295
296.. data:: nanj
297
298   Complex number with zero real part and NaN imaginary part. Equivalent to
299   ``complex(0.0, float('nan'))``.
300
301   .. versionadded:: 3.6
302
303
304.. index:: pair: module; math
305
306Note that the selection of functions is similar, but not identical, to that in
307module :mod:`math`.  The reason for having two modules is that some users aren't
308interested in complex numbers, and perhaps don't even know what they are.  They
309would rather have ``math.sqrt(-1)`` raise an exception than return a complex
310number. Also note that the functions defined in :mod:`cmath` always return a
311complex number, even if the answer can be expressed as a real number (in which
312case the complex number has an imaginary part of zero).
313
314A note on branch cuts: They are curves along which the given function fails to
315be continuous.  They are a necessary feature of many complex functions.  It is
316assumed that if you need to compute with complex functions, you will understand
317about branch cuts.  Consult almost any (not too elementary) book on complex
318variables for enlightenment.  For information of the proper choice of branch
319cuts for numerical purposes, a good reference should be the following:
320
321
322.. seealso::
323
324   Kahan, W:  Branch cuts for complex elementary functions; or, Much ado about
325   nothing's sign bit.  In Iserles, A., and Powell, M. (eds.), The state of the art
326   in numerical analysis. Clarendon Press (1987) pp165--211.
327