Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{cmath} --- |
Fred Drake | 8307e21 | 1999-04-21 16:29:18 +0000 | [diff] [blame] | 2 | Mathematical functions for complex numbers} |
| 3 | |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 4 | \declaremodule{builtin}{cmath} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 5 | \modulesynopsis{Mathematical functions for complex numbers.} |
| 6 | |
Fred Drake | 8307e21 | 1999-04-21 16:29:18 +0000 | [diff] [blame] | 7 | This module is always available. It provides access to mathematical |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame^] | 8 | functions for complex numbers. The functions in this module accept |
| 9 | integers, floating-point numbers or complex numbers as arguments. |
| 10 | They will also accept any Python object that has either a |
| 11 | \method{__complex__} or a \method{__float__} method: these methods are |
| 12 | used to convert the object to a complex or floating-point number, respectively, and |
| 13 | the function is then applied to the result of the conversion. |
| 14 | |
| 15 | The functions are: |
Fred Drake | 1b914b3 | 1997-09-30 20:14:50 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | 14a8024 | 1997-07-17 16:13:45 +0000 | [diff] [blame] | 17 | \begin{funcdesc}{acos}{x} |
Fred Drake | 1b914b3 | 1997-09-30 20:14:50 +0000 | [diff] [blame] | 18 | Return the arc cosine of \var{x}. |
Fred Drake | 8058bfa | 2001-06-23 03:16:29 +0000 | [diff] [blame] | 19 | There are two branch cuts: |
| 20 | One extends right from 1 along the real axis to \infinity, continuous |
| 21 | from below. |
| 22 | The other extends left from -1 along the real axis to -\infinity, |
| 23 | continuous from above. |
Guido van Rossum | 14a8024 | 1997-07-17 16:13:45 +0000 | [diff] [blame] | 24 | \end{funcdesc} |
Fred Drake | 1b914b3 | 1997-09-30 20:14:50 +0000 | [diff] [blame] | 25 | |
| 26 | \begin{funcdesc}{acosh}{x} |
| 27 | Return the hyperbolic arc cosine of \var{x}. |
Fred Drake | 8058bfa | 2001-06-23 03:16:29 +0000 | [diff] [blame] | 28 | There is one branch cut, extending left from 1 along the real axis |
| 29 | to -\infinity, continuous from above. |
Fred Drake | 1b914b3 | 1997-09-30 20:14:50 +0000 | [diff] [blame] | 30 | \end{funcdesc} |
| 31 | |
| 32 | \begin{funcdesc}{asin}{x} |
| 33 | Return the arc sine of \var{x}. |
Fred Drake | 8058bfa | 2001-06-23 03:16:29 +0000 | [diff] [blame] | 34 | This has the same branch cuts as \function{acos()}. |
Fred Drake | 1b914b3 | 1997-09-30 20:14:50 +0000 | [diff] [blame] | 35 | \end{funcdesc} |
| 36 | |
| 37 | \begin{funcdesc}{asinh}{x} |
| 38 | Return the hyperbolic arc sine of \var{x}. |
Fred Drake | 8058bfa | 2001-06-23 03:16:29 +0000 | [diff] [blame] | 39 | There are two branch cuts, extending left from \plusminus\code{1j} to |
| 40 | \plusminus-\infinity\code{j}, both continuous from above. |
| 41 | These branch cuts should be considered a bug to be corrected in a |
| 42 | future release. |
| 43 | The correct branch cuts should extend along the imaginary axis, |
| 44 | one from \code{1j} up to \infinity\code{j} and continuous from the |
| 45 | right, and one from -\code{1j} down to -\infinity\code{j} and |
| 46 | continuous from the left. |
Fred Drake | 1b914b3 | 1997-09-30 20:14:50 +0000 | [diff] [blame] | 47 | \end{funcdesc} |
| 48 | |
| 49 | \begin{funcdesc}{atan}{x} |
| 50 | Return the arc tangent of \var{x}. |
Fred Drake | 8058bfa | 2001-06-23 03:16:29 +0000 | [diff] [blame] | 51 | There are two branch cuts: |
| 52 | One extends from \code{1j} along the imaginary axis to |
| 53 | \infinity\code{j}, continuous from the left. |
| 54 | The other extends from -\code{1j} along the imaginary axis to |
| 55 | -\infinity\code{j}, continuous from the left. |
| 56 | (This should probably be changed so the upper cut becomes continuous |
| 57 | from the other side.) |
Fred Drake | 1b914b3 | 1997-09-30 20:14:50 +0000 | [diff] [blame] | 58 | \end{funcdesc} |
| 59 | |
| 60 | \begin{funcdesc}{atanh}{x} |
| 61 | Return the hyperbolic arc tangent of \var{x}. |
Fred Drake | 8058bfa | 2001-06-23 03:16:29 +0000 | [diff] [blame] | 62 | There are two branch cuts: |
| 63 | One extends from 1 along the real axis to \infinity, continuous |
| 64 | from above. |
| 65 | The other extends from -1 along the real axis to -\infinity, |
| 66 | continuous from above. |
| 67 | (This should probably be changed so the right cut becomes continuous from |
| 68 | the other side.) |
Fred Drake | 1b914b3 | 1997-09-30 20:14:50 +0000 | [diff] [blame] | 69 | \end{funcdesc} |
| 70 | |
| 71 | \begin{funcdesc}{cos}{x} |
| 72 | Return the cosine of \var{x}. |
| 73 | \end{funcdesc} |
| 74 | |
| 75 | \begin{funcdesc}{cosh}{x} |
| 76 | Return the hyperbolic cosine of \var{x}. |
| 77 | \end{funcdesc} |
| 78 | |
| 79 | \begin{funcdesc}{exp}{x} |
Guido van Rossum | c57aff2 | 1998-02-11 22:33:28 +0000 | [diff] [blame] | 80 | Return the exponential value \code{e**\var{x}}. |
Fred Drake | 1b914b3 | 1997-09-30 20:14:50 +0000 | [diff] [blame] | 81 | \end{funcdesc} |
| 82 | |
Raymond Hettinger | b67ad7e | 2004-06-14 07:40:10 +0000 | [diff] [blame] | 83 | \begin{funcdesc}{log}{x\optional{, base}} |
| 84 | Returns the logarithm of \var{x} to the given \var{base}. |
| 85 | If the \var{base} is not specified, returns the natural logarithm of \var{x}. |
Fred Drake | 8058bfa | 2001-06-23 03:16:29 +0000 | [diff] [blame] | 86 | There is one branch cut, from 0 along the negative real axis to |
| 87 | -\infinity, continuous from above. |
Raymond Hettinger | b67ad7e | 2004-06-14 07:40:10 +0000 | [diff] [blame] | 88 | \versionchanged[\var{base} argument added]{2.4} |
Fred Drake | 1b914b3 | 1997-09-30 20:14:50 +0000 | [diff] [blame] | 89 | \end{funcdesc} |
| 90 | |
| 91 | \begin{funcdesc}{log10}{x} |
| 92 | Return the base-10 logarithm of \var{x}. |
Fred Drake | 8058bfa | 2001-06-23 03:16:29 +0000 | [diff] [blame] | 93 | This has the same branch cut as \function{log()}. |
Fred Drake | 1b914b3 | 1997-09-30 20:14:50 +0000 | [diff] [blame] | 94 | \end{funcdesc} |
| 95 | |
| 96 | \begin{funcdesc}{sin}{x} |
| 97 | Return the sine of \var{x}. |
| 98 | \end{funcdesc} |
| 99 | |
| 100 | \begin{funcdesc}{sinh}{x} |
| 101 | Return the hyperbolic sine of \var{x}. |
| 102 | \end{funcdesc} |
| 103 | |
| 104 | \begin{funcdesc}{sqrt}{x} |
| 105 | Return the square root of \var{x}. |
Fred Drake | 8058bfa | 2001-06-23 03:16:29 +0000 | [diff] [blame] | 106 | This has the same branch cut as \function{log()}. |
Fred Drake | 1b914b3 | 1997-09-30 20:14:50 +0000 | [diff] [blame] | 107 | \end{funcdesc} |
| 108 | |
| 109 | \begin{funcdesc}{tan}{x} |
| 110 | Return the tangent of \var{x}. |
| 111 | \end{funcdesc} |
| 112 | |
| 113 | \begin{funcdesc}{tanh}{x} |
| 114 | Return the hyperbolic tangent of \var{x}. |
| 115 | \end{funcdesc} |
Guido van Rossum | 14a8024 | 1997-07-17 16:13:45 +0000 | [diff] [blame] | 116 | |
| 117 | The module also defines two mathematical constants: |
Fred Drake | 1b914b3 | 1997-09-30 20:14:50 +0000 | [diff] [blame] | 118 | |
Guido van Rossum | 14a8024 | 1997-07-17 16:13:45 +0000 | [diff] [blame] | 119 | \begin{datadesc}{pi} |
Fred Drake | 1b914b3 | 1997-09-30 20:14:50 +0000 | [diff] [blame] | 120 | The mathematical constant \emph{pi}, as a real. |
Guido van Rossum | 14a8024 | 1997-07-17 16:13:45 +0000 | [diff] [blame] | 121 | \end{datadesc} |
Fred Drake | 1b914b3 | 1997-09-30 20:14:50 +0000 | [diff] [blame] | 122 | |
| 123 | \begin{datadesc}{e} |
| 124 | The mathematical constant \emph{e}, as a real. |
| 125 | \end{datadesc} |
Guido van Rossum | 14a8024 | 1997-07-17 16:13:45 +0000 | [diff] [blame] | 126 | |
| 127 | Note that the selection of functions is similar, but not identical, to |
Fred Drake | 8307e21 | 1999-04-21 16:29:18 +0000 | [diff] [blame] | 128 | that in module \refmodule{math}\refbimodindex{math}. The reason for having |
Fred Drake | 9118f7c | 1999-06-29 15:53:52 +0000 | [diff] [blame] | 129 | two modules is that some users aren't interested in complex numbers, |
Fred Drake | aa5dba0 | 1998-01-09 21:30:03 +0000 | [diff] [blame] | 130 | and perhaps don't even know what they are. They would rather have |
| 131 | \code{math.sqrt(-1)} raise an exception than return a complex number. |
Fred Drake | 8307e21 | 1999-04-21 16:29:18 +0000 | [diff] [blame] | 132 | Also note that the functions defined in \module{cmath} always return a |
Fred Drake | aa5dba0 | 1998-01-09 21:30:03 +0000 | [diff] [blame] | 133 | complex number, even if the answer can be expressed as a real number |
| 134 | (in which case the complex number has an imaginary part of zero). |
Fred Drake | 8058bfa | 2001-06-23 03:16:29 +0000 | [diff] [blame] | 135 | |
| 136 | A note on branch cuts: They are curves along which the given function |
| 137 | fails to be continuous. They are a necessary feature of many complex |
| 138 | functions. It is assumed that if you need to compute with complex |
| 139 | functions, you will understand about branch cuts. Consult almost any |
| 140 | (not too elementary) book on complex variables for enlightenment. For |
| 141 | information of the proper choice of branch cuts for numerical |
| 142 | purposes, a good reference should be the following: |
| 143 | |
| 144 | \begin{seealso} |
| 145 | \seetext{Kahan, W: Branch cuts for complex elementary functions; |
Raymond Hettinger | 6880431 | 2005-01-01 00:28:46 +0000 | [diff] [blame] | 146 | or, Much ado about nothing's sign bit. In Iserles, A., |
Fred Drake | 8058bfa | 2001-06-23 03:16:29 +0000 | [diff] [blame] | 147 | and Powell, M. (eds.), \citetitle{The state of the art in |
| 148 | numerical analysis}. Clarendon Press (1987) pp165-211.} |
| 149 | \end{seealso} |