| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`cmath` --- Mathematical functions for complex numbers | 
 | 2 | =========================================================== | 
 | 3 |  | 
 | 4 | .. module:: cmath | 
 | 5 |    :synopsis: Mathematical functions for complex numbers. | 
 | 6 |  | 
 | 7 |  | 
 | 8 | This module is always available.  It provides access to mathematical functions | 
 | 9 | for complex numbers.  The functions in this module accept integers, | 
 | 10 | floating-point numbers or complex numbers as arguments. They will also accept | 
 | 11 | any Python object that has either a :meth:`__complex__` or a :meth:`__float__` | 
 | 12 | method: these methods are used to convert the object to a complex or | 
 | 13 | floating-point number, respectively, and the function is then applied to the | 
 | 14 | result of the conversion. | 
 | 15 |  | 
| Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 16 | .. note:: | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 17 |  | 
| Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 18 |    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 Dickinson | c2eab89 | 2009-07-28 16:31:03 +0000 | [diff] [blame] | 25 | Conversions to and from polar coordinates | 
 | 26 | ----------------------------------------- | 
| Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 27 |  | 
| Mark Dickinson | c2eab89 | 2009-07-28 16:31:03 +0000 | [diff] [blame] | 28 | A Python complex number ``z`` is stored internally using *rectangular* | 
 | 29 | or *Cartesian* coordinates.  It is completely determined by its *real | 
 | 30 | part* ``z.real`` and its *imaginary part* ``z.imag``.  In other | 
 | 31 | words:: | 
| Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 32 |  | 
| Mark Dickinson | c2eab89 | 2009-07-28 16:31:03 +0000 | [diff] [blame] | 33 |    z == z.real + z.imag*1j | 
| Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 34 |  | 
| Mark Dickinson | c2eab89 | 2009-07-28 16:31:03 +0000 | [diff] [blame] | 35 | *Polar coordinates* give an alternative way to represent a complex | 
 | 36 | number.  In polar coordinates, a complex number *z* is defined by the | 
 | 37 | modulus *r* and the phase angle *phi*. The modulus *r* is the distance | 
 | 38 | from *z* to the origin, while the phase *phi* is the counterclockwise | 
| Mark Dickinson | 5251cce | 2010-01-02 14:33:10 +0000 | [diff] [blame] | 39 | angle, measured in radians, from the positive x-axis to the line | 
 | 40 | segment that joins the origin to *z*. | 
| Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 41 |  | 
| Mark Dickinson | c2eab89 | 2009-07-28 16:31:03 +0000 | [diff] [blame] | 42 | The following functions can be used to convert from the native | 
 | 43 | rectangular coordinates to polar coordinates and back. | 
| Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 44 |  | 
 | 45 | .. function:: phase(x) | 
 | 46 |  | 
| Mark Dickinson | c2eab89 | 2009-07-28 16:31:03 +0000 | [diff] [blame] | 47 |    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 Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 67 |  | 
| Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 68 |  | 
 | 69 | .. function:: polar(x) | 
 | 70 |  | 
| Mark Dickinson | c2eab89 | 2009-07-28 16:31:03 +0000 | [diff] [blame] | 71 |    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 Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 75 |  | 
| Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 76 |  | 
 | 77 | .. function:: rect(r, phi) | 
 | 78 |  | 
| Mark Dickinson | c2eab89 | 2009-07-28 16:31:03 +0000 | [diff] [blame] | 79 |    Return the complex number *x* with polar coordinates *r* and *phi*. | 
 | 80 |    Equivalent to ``r * (math.cos(phi) + math.sin(phi)*1j)``. | 
| Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 81 |  | 
| Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 82 |  | 
| Mark Dickinson | c2eab89 | 2009-07-28 16:31:03 +0000 | [diff] [blame] | 83 | Power and logarithmic functions | 
 | 84 | ------------------------------- | 
| Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 85 |  | 
| Mark Dickinson | c2eab89 | 2009-07-28 16:31:03 +0000 | [diff] [blame] | 86 | .. 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 |  | 
| Mark Dickinson | c2eab89 | 2009-07-28 16:31:03 +0000 | [diff] [blame] | 97 |  | 
 | 98 | .. function:: log10(x) | 
 | 99 |  | 
 | 100 |    Return the base-10 logarithm of *x*. This has the same branch cut as | 
 | 101 |    :func:`log`. | 
 | 102 |  | 
 | 103 |  | 
 | 104 | .. function:: sqrt(x) | 
 | 105 |  | 
 | 106 |    Return the square root of *x*. This has the same branch cut as :func:`log`. | 
 | 107 |  | 
 | 108 |  | 
 | 109 | Trigonometric functions | 
 | 110 | ----------------------- | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 111 |  | 
 | 112 | .. function:: acos(x) | 
 | 113 |  | 
 | 114 |    Return the arc cosine of *x*. There are two branch cuts: One extends right from | 
 | 115 |    1 along the real axis to ∞, continuous from below. The other extends left from | 
 | 116 |    -1 along the real axis to -∞, continuous from above. | 
 | 117 |  | 
 | 118 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 119 | .. function:: asin(x) | 
 | 120 |  | 
 | 121 |    Return the arc sine of *x*. This has the same branch cuts as :func:`acos`. | 
 | 122 |  | 
 | 123 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 124 | .. function:: atan(x) | 
 | 125 |  | 
 | 126 |    Return the arc tangent of *x*. There are two branch cuts: One extends from | 
| Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 127 |    ``1j`` along the imaginary axis to ``∞j``, continuous from the right. The | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 128 |    other extends from ``-1j`` along the imaginary axis to ``-∞j``, continuous | 
| Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 129 |    from the left. | 
 | 130 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 131 |  | 
| Mark Dickinson | c2eab89 | 2009-07-28 16:31:03 +0000 | [diff] [blame] | 132 | .. function:: cos(x) | 
 | 133 |  | 
 | 134 |    Return the cosine of *x*. | 
 | 135 |  | 
 | 136 |  | 
 | 137 | .. function:: sin(x) | 
 | 138 |  | 
 | 139 |    Return the sine of *x*. | 
 | 140 |  | 
 | 141 |  | 
 | 142 | .. function:: tan(x) | 
 | 143 |  | 
 | 144 |    Return the tangent of *x*. | 
 | 145 |  | 
 | 146 |  | 
 | 147 | Hyperbolic functions | 
 | 148 | -------------------- | 
 | 149 |  | 
 | 150 | .. function:: acosh(x) | 
 | 151 |  | 
 | 152 |    Return the hyperbolic arc cosine of *x*. There is one branch cut, extending left | 
 | 153 |    from 1 along the real axis to -∞, continuous from above. | 
 | 154 |  | 
 | 155 |  | 
 | 156 | .. function:: asinh(x) | 
 | 157 |  | 
 | 158 |    Return the hyperbolic arc sine of *x*. There are two branch cuts: | 
 | 159 |    One extends from ``1j`` along the imaginary axis to ``∞j``, | 
 | 160 |    continuous from the right.  The other extends from ``-1j`` along | 
 | 161 |    the imaginary axis to ``-∞j``, continuous from the left. | 
 | 162 |  | 
 | 163 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 164 | .. function:: atanh(x) | 
 | 165 |  | 
 | 166 |    Return the hyperbolic arc tangent of *x*. There are two branch cuts: One | 
| Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 167 |    extends from ``1`` along the real axis to ``∞``, continuous from below. The | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 168 |    other extends from ``-1`` along the real axis to ``-∞``, continuous from | 
| Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 169 |    above. | 
 | 170 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 171 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 172 | .. function:: cosh(x) | 
 | 173 |  | 
 | 174 |    Return the hyperbolic cosine of *x*. | 
 | 175 |  | 
 | 176 |  | 
| Mark Dickinson | c2eab89 | 2009-07-28 16:31:03 +0000 | [diff] [blame] | 177 | .. function:: sinh(x) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 178 |  | 
| Mark Dickinson | c2eab89 | 2009-07-28 16:31:03 +0000 | [diff] [blame] | 179 |    Return the hyperbolic sine of *x*. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 180 |  | 
 | 181 |  | 
| Mark Dickinson | c2eab89 | 2009-07-28 16:31:03 +0000 | [diff] [blame] | 182 | .. function:: tanh(x) | 
 | 183 |  | 
 | 184 |    Return the hyperbolic tangent of *x*. | 
 | 185 |  | 
 | 186 |  | 
 | 187 | Classification functions | 
 | 188 | ------------------------ | 
 | 189 |  | 
| Mark Dickinson | 8e0c996 | 2010-07-11 17:38:24 +0000 | [diff] [blame] | 190 | .. function:: isfinite(x) | 
 | 191 |  | 
| Mark Dickinson | c762242 | 2010-07-11 19:47:37 +0000 | [diff] [blame] | 192 |    Return ``True`` if both the real and imaginary parts of *x* are finite, and | 
 | 193 |    ``False`` otherwise. | 
 | 194 |  | 
 | 195 |    .. versionadded:: 3.2 | 
| Mark Dickinson | 8e0c996 | 2010-07-11 17:38:24 +0000 | [diff] [blame] | 196 |  | 
 | 197 |  | 
| Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 198 | .. function:: isinf(x) | 
 | 199 |  | 
| Mark Dickinson | c762242 | 2010-07-11 19:47:37 +0000 | [diff] [blame] | 200 |    Return ``True`` if either the real or the imaginary part of *x* is an | 
 | 201 |    infinity, and ``False`` otherwise. | 
| Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 202 |  | 
| Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 203 |  | 
 | 204 | .. function:: isnan(x) | 
 | 205 |  | 
| Mark Dickinson | c762242 | 2010-07-11 19:47:37 +0000 | [diff] [blame] | 206 |    Return ``True`` if either the real or the imaginary part of *x* is a NaN, | 
 | 207 |    and ``False`` otherwise. | 
| Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 208 |  | 
| Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 209 |  | 
| Mark Dickinson | c2eab89 | 2009-07-28 16:31:03 +0000 | [diff] [blame] | 210 | Constants | 
 | 211 | --------- | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 212 |  | 
 | 213 |  | 
 | 214 | .. data:: pi | 
 | 215 |  | 
| Mark Dickinson | c2eab89 | 2009-07-28 16:31:03 +0000 | [diff] [blame] | 216 |    The mathematical constant *π*, as a float. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 217 |  | 
 | 218 |  | 
 | 219 | .. data:: e | 
 | 220 |  | 
 | 221 |    The mathematical constant *e*, as a float. | 
 | 222 |  | 
 | 223 | .. index:: module: math | 
 | 224 |  | 
 | 225 | Note that the selection of functions is similar, but not identical, to that in | 
 | 226 | module :mod:`math`.  The reason for having two modules is that some users aren't | 
 | 227 | interested in complex numbers, and perhaps don't even know what they are.  They | 
 | 228 | would rather have ``math.sqrt(-1)`` raise an exception than return a complex | 
 | 229 | number. Also note that the functions defined in :mod:`cmath` always return a | 
 | 230 | complex number, even if the answer can be expressed as a real number (in which | 
 | 231 | case the complex number has an imaginary part of zero). | 
 | 232 |  | 
 | 233 | A note on branch cuts: They are curves along which the given function fails to | 
 | 234 | be continuous.  They are a necessary feature of many complex functions.  It is | 
 | 235 | assumed that if you need to compute with complex functions, you will understand | 
 | 236 | about branch cuts.  Consult almost any (not too elementary) book on complex | 
 | 237 | variables for enlightenment.  For information of the proper choice of branch | 
 | 238 | cuts for numerical purposes, a good reference should be the following: | 
 | 239 |  | 
 | 240 |  | 
 | 241 | .. seealso:: | 
 | 242 |  | 
 | 243 |    Kahan, W:  Branch cuts for complex elementary functions; or, Much ado about | 
 | 244 |    nothing's sign bit.  In Iserles, A., and Powell, M. (eds.), The state of the art | 
 | 245 |    in numerical analysis. Clarendon Press (1987) pp165-211. | 
 | 246 |  | 
| Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 247 |  |