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 | |
| 17 | The functions are: |
| 18 | |
| 19 | |
| 20 | .. function:: acos(x) |
| 21 | |
| 22 | Return the arc cosine of *x*. There are two branch cuts: One extends right from |
| 23 | 1 along the real axis to ∞, continuous from below. The other extends left from |
| 24 | -1 along the real axis to -∞, continuous from above. |
| 25 | |
| 26 | |
| 27 | .. function:: acosh(x) |
| 28 | |
| 29 | Return the hyperbolic arc cosine of *x*. There is one branch cut, extending left |
| 30 | from 1 along the real axis to -∞, continuous from above. |
| 31 | |
| 32 | |
| 33 | .. function:: asin(x) |
| 34 | |
| 35 | Return the arc sine of *x*. This has the same branch cuts as :func:`acos`. |
| 36 | |
| 37 | |
| 38 | .. function:: asinh(x) |
| 39 | |
| 40 | Return the hyperbolic arc sine of *x*. There are two branch cuts, extending |
| 41 | left from ``±1j`` to ``±∞j``, both continuous from above. These branch cuts |
| 42 | should be considered a bug to be corrected in a future release. The correct |
| 43 | branch cuts should extend along the imaginary axis, one from ``1j`` up to |
| 44 | ``∞j`` and continuous from the right, and one from ``-1j`` down to ``-∞j`` |
| 45 | and continuous from the left. |
| 46 | |
| 47 | |
| 48 | .. function:: atan(x) |
| 49 | |
| 50 | Return the arc tangent of *x*. There are two branch cuts: One extends from |
| 51 | ``1j`` along the imaginary axis to ``∞j``, continuous from the left. The |
| 52 | other extends from ``-1j`` along the imaginary axis to ``-∞j``, continuous |
| 53 | from the left. (This should probably be changed so the upper cut becomes |
| 54 | continuous from the other side.) |
| 55 | |
| 56 | |
| 57 | .. function:: atanh(x) |
| 58 | |
| 59 | Return the hyperbolic arc tangent of *x*. There are two branch cuts: One |
| 60 | extends from ``1`` along the real axis to ``∞``, continuous from above. The |
| 61 | other extends from ``-1`` along the real axis to ``-∞``, continuous from |
| 62 | above. (This should probably be changed so the right cut becomes continuous |
| 63 | from the other side.) |
| 64 | |
| 65 | |
| 66 | .. function:: cos(x) |
| 67 | |
| 68 | Return the cosine of *x*. |
| 69 | |
| 70 | |
| 71 | .. function:: cosh(x) |
| 72 | |
| 73 | Return the hyperbolic cosine of *x*. |
| 74 | |
| 75 | |
| 76 | .. function:: exp(x) |
| 77 | |
| 78 | Return the exponential value ``e**x``. |
| 79 | |
| 80 | |
| 81 | .. function:: log(x[, base]) |
| 82 | |
| 83 | Returns the logarithm of *x* to the given *base*. If the *base* is not |
| 84 | specified, returns the natural logarithm of *x*. There is one branch cut, from 0 |
| 85 | along the negative real axis to -∞, continuous from above. |
| 86 | |
| 87 | .. versionchanged:: 2.4 |
| 88 | *base* argument added. |
| 89 | |
| 90 | |
| 91 | .. function:: log10(x) |
| 92 | |
| 93 | Return the base-10 logarithm of *x*. This has the same branch cut as |
| 94 | :func:`log`. |
| 95 | |
| 96 | |
| 97 | .. function:: sin(x) |
| 98 | |
| 99 | Return the sine of *x*. |
| 100 | |
| 101 | |
| 102 | .. function:: sinh(x) |
| 103 | |
| 104 | Return the hyperbolic sine of *x*. |
| 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 | |
| 112 | .. function:: tan(x) |
| 113 | |
| 114 | Return the tangent of *x*. |
| 115 | |
| 116 | |
| 117 | .. function:: tanh(x) |
| 118 | |
| 119 | Return the hyperbolic tangent of *x*. |
| 120 | |
| 121 | The module also defines two mathematical constants: |
| 122 | |
| 123 | |
| 124 | .. data:: pi |
| 125 | |
| 126 | The mathematical constant *pi*, as a float. |
| 127 | |
| 128 | |
| 129 | .. data:: e |
| 130 | |
| 131 | The mathematical constant *e*, as a float. |
| 132 | |
| 133 | .. index:: module: math |
| 134 | |
| 135 | Note that the selection of functions is similar, but not identical, to that in |
| 136 | module :mod:`math`. The reason for having two modules is that some users aren't |
| 137 | interested in complex numbers, and perhaps don't even know what they are. They |
| 138 | would rather have ``math.sqrt(-1)`` raise an exception than return a complex |
| 139 | number. Also note that the functions defined in :mod:`cmath` always return a |
| 140 | complex number, even if the answer can be expressed as a real number (in which |
| 141 | case the complex number has an imaginary part of zero). |
| 142 | |
| 143 | A note on branch cuts: They are curves along which the given function fails to |
| 144 | be continuous. They are a necessary feature of many complex functions. It is |
| 145 | assumed that if you need to compute with complex functions, you will understand |
| 146 | about branch cuts. Consult almost any (not too elementary) book on complex |
| 147 | variables for enlightenment. For information of the proper choice of branch |
| 148 | cuts for numerical purposes, a good reference should be the following: |
| 149 | |
| 150 | |
| 151 | .. seealso:: |
| 152 | |
| 153 | Kahan, W: Branch cuts for complex elementary functions; or, Much ado about |
| 154 | nothing's sign bit. In Iserles, A., and Powell, M. (eds.), The state of the art |
| 155 | in numerical analysis. Clarendon Press (1987) pp165-211. |
| 156 | |