Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | |
| 2 | :mod:`cmath` --- Mathematical functions for complex numbers |
| 3 | =========================================================== |
| 4 | |
| 5 | .. module:: cmath |
| 6 | :synopsis: Mathematical functions for complex numbers. |
| 7 | |
| 8 | |
| 9 | This module is always available. It provides access to mathematical functions |
| 10 | for complex numbers. The functions in this module accept integers, |
| 11 | floating-point numbers or complex numbers as arguments. They will also accept |
| 12 | any Python object that has either a :meth:`__complex__` or a :meth:`__float__` |
| 13 | method: these methods are used to convert the object to a complex or |
| 14 | floating-point number, respectively, and the function is then applied to the |
| 15 | result of the conversion. |
| 16 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 17 | .. note:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 18 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 19 | 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 | |
| 26 | Complex coordinates |
| 27 | ------------------- |
| 28 | |
| 29 | Complex numbers can be expressed by two important coordinate systems. |
| 30 | Python's :class:`complex` type uses rectangular coordinates where a number |
| 31 | on the complex plain is defined by two floats, the real part and the imaginary |
| 32 | part. |
| 33 | |
| 34 | Definition:: |
| 35 | |
| 36 | z = x + 1j * y |
| 37 | |
| 38 | x := real(z) |
| 39 | y := imag(z) |
| 40 | |
| 41 | In engineering the polar coordinate system is popular for complex numbers. In |
| 42 | polar coordinates a complex number is defined by the radius *r* and the phase |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 43 | angle *phi*. The radius *r* is the absolute value of the complex, which can be |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 44 | viewed as distance from (0, 0). The radius *r* is always 0 or a positive float. |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 45 | The phase angle *phi* is the counter clockwise angle from the positive x axis, |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 46 | e.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 | |
| 54 | Definition:: |
| 55 | |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 56 | z = r * exp(1j * phi) |
| 57 | z = r * cis(phi) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 58 | |
| 59 | r := abs(z) := sqrt(real(z)**2 + imag(z)**2) |
| 60 | phi := phase(z) := atan2(imag(z), real(z)) |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 61 | cis(phi) := cos(phi) + 1j * sin(phi) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 62 | |
| 63 | |
| 64 | .. function:: phase(x) |
| 65 | |
| 66 | Return phase, also known as the argument, of a complex. |
| 67 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 68 | |
| 69 | .. function:: polar(x) |
| 70 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 71 | Convert a :class:`complex` from rectangular coordinates to polar |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 72 | coordinates. The function returns a tuple with the two elements |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 73 | *r* and *phi*. *r* is the distance from 0 and *phi* the phase |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 74 | angle. |
| 75 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 76 | |
| 77 | .. function:: rect(r, phi) |
| 78 | |
| 79 | Convert from polar coordinates to rectangular coordinates and return |
| 80 | a :class:`complex`. |
| 81 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 82 | |
| 83 | |
| 84 | cmath functions |
| 85 | --------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 86 | |
| 87 | .. function:: acos(x) |
| 88 | |
| 89 | Return the arc cosine of *x*. There are two branch cuts: One extends right from |
| 90 | 1 along the real axis to ∞, continuous from below. The other extends left from |
| 91 | -1 along the real axis to -∞, continuous from above. |
| 92 | |
| 93 | |
| 94 | .. function:: acosh(x) |
| 95 | |
| 96 | Return the hyperbolic arc cosine of *x*. There is one branch cut, extending left |
| 97 | from 1 along the real axis to -∞, continuous from above. |
| 98 | |
| 99 | |
| 100 | .. function:: asin(x) |
| 101 | |
| 102 | Return the arc sine of *x*. This has the same branch cuts as :func:`acos`. |
| 103 | |
| 104 | |
| 105 | .. function:: asinh(x) |
| 106 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 107 | Return the hyperbolic arc sine of *x*. There are two branch cuts: |
| 108 | One extends from ``1j`` along the imaginary axis to ``∞j``, |
| 109 | continuous from the right. The other extends from ``-1j`` along |
| 110 | the imaginary axis to ``-∞j``, continuous from the left. |
| 111 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 112 | |
| 113 | .. function:: atan(x) |
| 114 | |
| 115 | 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] | 116 | ``1j`` along the imaginary axis to ``∞j``, continuous from the right. The |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 117 | other extends from ``-1j`` along the imaginary axis to ``-∞j``, continuous |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 118 | from the left. |
| 119 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 120 | |
| 121 | .. function:: atanh(x) |
| 122 | |
| 123 | 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] | 124 | extends from ``1`` along the real axis to ``∞``, continuous from below. The |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 125 | other extends from ``-1`` along the real axis to ``-∞``, continuous from |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 126 | above. |
| 127 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 128 | |
| 129 | .. function:: cos(x) |
| 130 | |
| 131 | Return the cosine of *x*. |
| 132 | |
| 133 | |
| 134 | .. function:: cosh(x) |
| 135 | |
| 136 | Return the hyperbolic cosine of *x*. |
| 137 | |
| 138 | |
| 139 | .. function:: exp(x) |
| 140 | |
| 141 | Return the exponential value ``e**x``. |
| 142 | |
| 143 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 144 | .. function:: isinf(x) |
| 145 | |
| 146 | Return *True* if the real or the imaginary part of x is positive |
| 147 | or negative infinity. |
| 148 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 149 | |
| 150 | .. function:: isnan(x) |
| 151 | |
| 152 | Return *True* if the real or imaginary part of x is not a number (NaN). |
| 153 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 154 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 155 | .. function:: log(x[, base]) |
| 156 | |
| 157 | Returns the logarithm of *x* to the given *base*. If the *base* is not |
| 158 | specified, returns the natural logarithm of *x*. There is one branch cut, from 0 |
| 159 | along the negative real axis to -∞, continuous from above. |
| 160 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 161 | |
| 162 | .. function:: log10(x) |
| 163 | |
| 164 | Return the base-10 logarithm of *x*. This has the same branch cut as |
| 165 | :func:`log`. |
| 166 | |
| 167 | |
| 168 | .. function:: sin(x) |
| 169 | |
| 170 | Return the sine of *x*. |
| 171 | |
| 172 | |
| 173 | .. function:: sinh(x) |
| 174 | |
| 175 | Return the hyperbolic sine of *x*. |
| 176 | |
| 177 | |
| 178 | .. function:: sqrt(x) |
| 179 | |
| 180 | Return the square root of *x*. This has the same branch cut as :func:`log`. |
| 181 | |
| 182 | |
| 183 | .. function:: tan(x) |
| 184 | |
| 185 | Return the tangent of *x*. |
| 186 | |
| 187 | |
| 188 | .. function:: tanh(x) |
| 189 | |
| 190 | Return the hyperbolic tangent of *x*. |
| 191 | |
| 192 | The module also defines two mathematical constants: |
| 193 | |
| 194 | |
| 195 | .. data:: pi |
| 196 | |
| 197 | The mathematical constant *pi*, as a float. |
| 198 | |
| 199 | |
| 200 | .. data:: e |
| 201 | |
| 202 | The mathematical constant *e*, as a float. |
| 203 | |
| 204 | .. index:: module: math |
| 205 | |
| 206 | Note that the selection of functions is similar, but not identical, to that in |
| 207 | module :mod:`math`. The reason for having two modules is that some users aren't |
| 208 | interested in complex numbers, and perhaps don't even know what they are. They |
| 209 | would rather have ``math.sqrt(-1)`` raise an exception than return a complex |
| 210 | number. Also note that the functions defined in :mod:`cmath` always return a |
| 211 | complex number, even if the answer can be expressed as a real number (in which |
| 212 | case the complex number has an imaginary part of zero). |
| 213 | |
| 214 | A note on branch cuts: They are curves along which the given function fails to |
| 215 | be continuous. They are a necessary feature of many complex functions. It is |
| 216 | assumed that if you need to compute with complex functions, you will understand |
| 217 | about branch cuts. Consult almost any (not too elementary) book on complex |
| 218 | variables for enlightenment. For information of the proper choice of branch |
| 219 | cuts for numerical purposes, a good reference should be the following: |
| 220 | |
| 221 | |
| 222 | .. seealso:: |
| 223 | |
| 224 | Kahan, W: Branch cuts for complex elementary functions; or, Much ado about |
| 225 | nothing's sign bit. In Iserles, A., and Powell, M. (eds.), The state of the art |
| 226 | in numerical analysis. Clarendon Press (1987) pp165-211. |
| 227 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 228 | |