blob: f8aa45bb4a156387c5bc2c856259dc4d86c58ca8 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{cmath} ---
Fred Drake8307e211999-04-21 16:29:18 +00002 Mathematical functions for complex numbers}
3
Fred Drakeb91e9341998-07-23 17:59:49 +00004\declaremodule{builtin}{cmath}
Fred Drakeb91e9341998-07-23 17:59:49 +00005\modulesynopsis{Mathematical functions for complex numbers.}
6
Fred Drake8307e211999-04-21 16:29:18 +00007This module is always available. It provides access to mathematical
Guido van Rossumd8faa362007-04-27 19:54:29 +00008functions for complex numbers. The functions in this module accept
9integers, floating-point numbers or complex numbers as arguments.
10They will also accept any Python object that has either a
11\method{__complex__} or a \method{__float__} method: these methods are
12used to convert the object to a complex or floating-point number, respectively, and
13the function is then applied to the result of the conversion.
14
15The functions are:
Fred Drake1b914b31997-09-30 20:14:50 +000016
Guido van Rossum14a80241997-07-17 16:13:45 +000017\begin{funcdesc}{acos}{x}
Fred Drake1b914b31997-09-30 20:14:50 +000018Return the arc cosine of \var{x}.
Fred Drake8058bfa2001-06-23 03:16:29 +000019There are two branch cuts:
20One extends right from 1 along the real axis to \infinity, continuous
21from below.
22The other extends left from -1 along the real axis to -\infinity,
23continuous from above.
Guido van Rossum14a80241997-07-17 16:13:45 +000024\end{funcdesc}
Fred Drake1b914b31997-09-30 20:14:50 +000025
26\begin{funcdesc}{acosh}{x}
27Return the hyperbolic arc cosine of \var{x}.
Fred Drake8058bfa2001-06-23 03:16:29 +000028There is one branch cut, extending left from 1 along the real axis
29to -\infinity, continuous from above.
Fred Drake1b914b31997-09-30 20:14:50 +000030\end{funcdesc}
31
32\begin{funcdesc}{asin}{x}
33Return the arc sine of \var{x}.
Fred Drake8058bfa2001-06-23 03:16:29 +000034This has the same branch cuts as \function{acos()}.
Fred Drake1b914b31997-09-30 20:14:50 +000035\end{funcdesc}
36
37\begin{funcdesc}{asinh}{x}
38Return the hyperbolic arc sine of \var{x}.
Fred Drake8058bfa2001-06-23 03:16:29 +000039There are two branch cuts, extending left from \plusminus\code{1j} to
40\plusminus-\infinity\code{j}, both continuous from above.
41These branch cuts should be considered a bug to be corrected in a
42future release.
43The correct branch cuts should extend along the imaginary axis,
44one from \code{1j} up to \infinity\code{j} and continuous from the
45right, and one from -\code{1j} down to -\infinity\code{j} and
46continuous from the left.
Fred Drake1b914b31997-09-30 20:14:50 +000047\end{funcdesc}
48
49\begin{funcdesc}{atan}{x}
50Return the arc tangent of \var{x}.
Fred Drake8058bfa2001-06-23 03:16:29 +000051There are two branch cuts:
52One extends from \code{1j} along the imaginary axis to
53\infinity\code{j}, continuous from the left.
54The 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
57from the other side.)
Fred Drake1b914b31997-09-30 20:14:50 +000058\end{funcdesc}
59
60\begin{funcdesc}{atanh}{x}
61Return the hyperbolic arc tangent of \var{x}.
Fred Drake8058bfa2001-06-23 03:16:29 +000062There are two branch cuts:
63One extends from 1 along the real axis to \infinity, continuous
64from above.
65The other extends from -1 along the real axis to -\infinity,
66continuous from above.
67(This should probably be changed so the right cut becomes continuous from
68the other side.)
Fred Drake1b914b31997-09-30 20:14:50 +000069\end{funcdesc}
70
71\begin{funcdesc}{cos}{x}
72Return the cosine of \var{x}.
73\end{funcdesc}
74
75\begin{funcdesc}{cosh}{x}
76Return the hyperbolic cosine of \var{x}.
77\end{funcdesc}
78
79\begin{funcdesc}{exp}{x}
Guido van Rossumc57aff21998-02-11 22:33:28 +000080Return the exponential value \code{e**\var{x}}.
Fred Drake1b914b31997-09-30 20:14:50 +000081\end{funcdesc}
82
Raymond Hettingerb67ad7e2004-06-14 07:40:10 +000083\begin{funcdesc}{log}{x\optional{, base}}
84Returns the logarithm of \var{x} to the given \var{base}.
85If the \var{base} is not specified, returns the natural logarithm of \var{x}.
Fred Drake8058bfa2001-06-23 03:16:29 +000086There is one branch cut, from 0 along the negative real axis to
87-\infinity, continuous from above.
Raymond Hettingerb67ad7e2004-06-14 07:40:10 +000088\versionchanged[\var{base} argument added]{2.4}
Fred Drake1b914b31997-09-30 20:14:50 +000089\end{funcdesc}
90
91\begin{funcdesc}{log10}{x}
92Return the base-10 logarithm of \var{x}.
Fred Drake8058bfa2001-06-23 03:16:29 +000093This has the same branch cut as \function{log()}.
Fred Drake1b914b31997-09-30 20:14:50 +000094\end{funcdesc}
95
96\begin{funcdesc}{sin}{x}
97Return the sine of \var{x}.
98\end{funcdesc}
99
100\begin{funcdesc}{sinh}{x}
101Return the hyperbolic sine of \var{x}.
102\end{funcdesc}
103
104\begin{funcdesc}{sqrt}{x}
105Return the square root of \var{x}.
Fred Drake8058bfa2001-06-23 03:16:29 +0000106This has the same branch cut as \function{log()}.
Fred Drake1b914b31997-09-30 20:14:50 +0000107\end{funcdesc}
108
109\begin{funcdesc}{tan}{x}
110Return the tangent of \var{x}.
111\end{funcdesc}
112
113\begin{funcdesc}{tanh}{x}
114Return the hyperbolic tangent of \var{x}.
115\end{funcdesc}
Guido van Rossum14a80241997-07-17 16:13:45 +0000116
117The module also defines two mathematical constants:
Fred Drake1b914b31997-09-30 20:14:50 +0000118
Guido van Rossum14a80241997-07-17 16:13:45 +0000119\begin{datadesc}{pi}
Fred Drake1b914b31997-09-30 20:14:50 +0000120The mathematical constant \emph{pi}, as a real.
Guido van Rossum14a80241997-07-17 16:13:45 +0000121\end{datadesc}
Fred Drake1b914b31997-09-30 20:14:50 +0000122
123\begin{datadesc}{e}
124The mathematical constant \emph{e}, as a real.
125\end{datadesc}
Guido van Rossum14a80241997-07-17 16:13:45 +0000126
127Note that the selection of functions is similar, but not identical, to
Fred Drake8307e211999-04-21 16:29:18 +0000128that in module \refmodule{math}\refbimodindex{math}. The reason for having
Fred Drake9118f7c1999-06-29 15:53:52 +0000129two modules is that some users aren't interested in complex numbers,
Fred Drakeaa5dba01998-01-09 21:30:03 +0000130and 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 Drake8307e211999-04-21 16:29:18 +0000132Also note that the functions defined in \module{cmath} always return a
Fred Drakeaa5dba01998-01-09 21:30:03 +0000133complex 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 Drake8058bfa2001-06-23 03:16:29 +0000135
136A note on branch cuts: They are curves along which the given function
137fails to be continuous. They are a necessary feature of many complex
138functions. It is assumed that if you need to compute with complex
139functions, you will understand about branch cuts. Consult almost any
140(not too elementary) book on complex variables for enlightenment. For
141information of the proper choice of branch cuts for numerical
142purposes, a good reference should be the following:
143
144\begin{seealso}
145 \seetext{Kahan, W: Branch cuts for complex elementary functions;
Raymond Hettinger68804312005-01-01 00:28:46 +0000146 or, Much ado about nothing's sign bit. In Iserles, A.,
Fred Drake8058bfa2001-06-23 03:16:29 +0000147 and Powell, M. (eds.), \citetitle{The state of the art in
148 numerical analysis}. Clarendon Press (1987) pp165-211.}
149\end{seealso}