blob: 465c8fd89f91c45b7d745044cb9537d46adb65fc [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
8functions for complex numbers. The functions are:
Fred Drake1b914b31997-09-30 20:14:50 +00009
Guido van Rossum14a80241997-07-17 16:13:45 +000010\begin{funcdesc}{acos}{x}
Fred Drake1b914b31997-09-30 20:14:50 +000011Return the arc cosine of \var{x}.
Fred Drake8058bfa2001-06-23 03:16:29 +000012There are two branch cuts:
13One extends right from 1 along the real axis to \infinity, continuous
14from below.
15The other extends left from -1 along the real axis to -\infinity,
16continuous from above.
Guido van Rossum14a80241997-07-17 16:13:45 +000017\end{funcdesc}
Fred Drake1b914b31997-09-30 20:14:50 +000018
19\begin{funcdesc}{acosh}{x}
20Return the hyperbolic arc cosine of \var{x}.
Fred Drake8058bfa2001-06-23 03:16:29 +000021There is one branch cut, extending left from 1 along the real axis
22to -\infinity, continuous from above.
Fred Drake1b914b31997-09-30 20:14:50 +000023\end{funcdesc}
24
25\begin{funcdesc}{asin}{x}
26Return the arc sine of \var{x}.
Fred Drake8058bfa2001-06-23 03:16:29 +000027This has the same branch cuts as \function{acos()}.
Fred Drake1b914b31997-09-30 20:14:50 +000028\end{funcdesc}
29
30\begin{funcdesc}{asinh}{x}
31Return the hyperbolic arc sine of \var{x}.
Fred Drake8058bfa2001-06-23 03:16:29 +000032There are two branch cuts, extending left from \plusminus\code{1j} to
33\plusminus-\infinity\code{j}, both continuous from above.
34These branch cuts should be considered a bug to be corrected in a
35future release.
36The correct branch cuts should extend along the imaginary axis,
37one from \code{1j} up to \infinity\code{j} and continuous from the
38right, and one from -\code{1j} down to -\infinity\code{j} and
39continuous from the left.
Fred Drake1b914b31997-09-30 20:14:50 +000040\end{funcdesc}
41
42\begin{funcdesc}{atan}{x}
43Return the arc tangent of \var{x}.
Fred Drake8058bfa2001-06-23 03:16:29 +000044There are two branch cuts:
45One extends from \code{1j} along the imaginary axis to
46\infinity\code{j}, continuous from the left.
47The other extends from -\code{1j} along the imaginary axis to
48-\infinity\code{j}, continuous from the left.
49(This should probably be changed so the upper cut becomes continuous
50from the other side.)
Fred Drake1b914b31997-09-30 20:14:50 +000051\end{funcdesc}
52
53\begin{funcdesc}{atanh}{x}
54Return the hyperbolic arc tangent of \var{x}.
Fred Drake8058bfa2001-06-23 03:16:29 +000055There are two branch cuts:
56One extends from 1 along the real axis to \infinity, continuous
57from above.
58The other extends from -1 along the real axis to -\infinity,
59continuous from above.
60(This should probably be changed so the right cut becomes continuous from
61the other side.)
Fred Drake1b914b31997-09-30 20:14:50 +000062\end{funcdesc}
63
64\begin{funcdesc}{cos}{x}
65Return the cosine of \var{x}.
66\end{funcdesc}
67
68\begin{funcdesc}{cosh}{x}
69Return the hyperbolic cosine of \var{x}.
70\end{funcdesc}
71
72\begin{funcdesc}{exp}{x}
Guido van Rossumc57aff21998-02-11 22:33:28 +000073Return the exponential value \code{e**\var{x}}.
Fred Drake1b914b31997-09-30 20:14:50 +000074\end{funcdesc}
75
Raymond Hettingerb67ad7e2004-06-14 07:40:10 +000076\begin{funcdesc}{log}{x\optional{, base}}
77Returns the logarithm of \var{x} to the given \var{base}.
78If the \var{base} is not specified, returns the natural logarithm of \var{x}.
Fred Drake8058bfa2001-06-23 03:16:29 +000079There is one branch cut, from 0 along the negative real axis to
80-\infinity, continuous from above.
Raymond Hettingerb67ad7e2004-06-14 07:40:10 +000081\versionchanged[\var{base} argument added]{2.4}
Fred Drake1b914b31997-09-30 20:14:50 +000082\end{funcdesc}
83
84\begin{funcdesc}{log10}{x}
85Return the base-10 logarithm of \var{x}.
Fred Drake8058bfa2001-06-23 03:16:29 +000086This has the same branch cut as \function{log()}.
Fred Drake1b914b31997-09-30 20:14:50 +000087\end{funcdesc}
88
89\begin{funcdesc}{sin}{x}
90Return the sine of \var{x}.
91\end{funcdesc}
92
93\begin{funcdesc}{sinh}{x}
94Return the hyperbolic sine of \var{x}.
95\end{funcdesc}
96
97\begin{funcdesc}{sqrt}{x}
98Return the square root of \var{x}.
Fred Drake8058bfa2001-06-23 03:16:29 +000099This has the same branch cut as \function{log()}.
Fred Drake1b914b31997-09-30 20:14:50 +0000100\end{funcdesc}
101
102\begin{funcdesc}{tan}{x}
103Return the tangent of \var{x}.
104\end{funcdesc}
105
106\begin{funcdesc}{tanh}{x}
107Return the hyperbolic tangent of \var{x}.
108\end{funcdesc}
Guido van Rossum14a80241997-07-17 16:13:45 +0000109
110The module also defines two mathematical constants:
Fred Drake1b914b31997-09-30 20:14:50 +0000111
Guido van Rossum14a80241997-07-17 16:13:45 +0000112\begin{datadesc}{pi}
Fred Drake1b914b31997-09-30 20:14:50 +0000113The mathematical constant \emph{pi}, as a real.
Guido van Rossum14a80241997-07-17 16:13:45 +0000114\end{datadesc}
Fred Drake1b914b31997-09-30 20:14:50 +0000115
116\begin{datadesc}{e}
117The mathematical constant \emph{e}, as a real.
118\end{datadesc}
Guido van Rossum14a80241997-07-17 16:13:45 +0000119
120Note that the selection of functions is similar, but not identical, to
Fred Drake8307e211999-04-21 16:29:18 +0000121that in module \refmodule{math}\refbimodindex{math}. The reason for having
Fred Drake9118f7c1999-06-29 15:53:52 +0000122two modules is that some users aren't interested in complex numbers,
Fred Drakeaa5dba01998-01-09 21:30:03 +0000123and perhaps don't even know what they are. They would rather have
124\code{math.sqrt(-1)} raise an exception than return a complex number.
Fred Drake8307e211999-04-21 16:29:18 +0000125Also note that the functions defined in \module{cmath} always return a
Fred Drakeaa5dba01998-01-09 21:30:03 +0000126complex number, even if the answer can be expressed as a real number
127(in which case the complex number has an imaginary part of zero).
Fred Drake8058bfa2001-06-23 03:16:29 +0000128
129A note on branch cuts: They are curves along which the given function
130fails to be continuous. They are a necessary feature of many complex
131functions. It is assumed that if you need to compute with complex
132functions, you will understand about branch cuts. Consult almost any
133(not too elementary) book on complex variables for enlightenment. For
134information of the proper choice of branch cuts for numerical
135purposes, a good reference should be the following:
136
137\begin{seealso}
138 \seetext{Kahan, W: Branch cuts for complex elementary functions;
139 or, Much ado about nothings's sign bit. In Iserles, A.,
140 and Powell, M. (eds.), \citetitle{The state of the art in
141 numerical analysis}. Clarendon Press (1987) pp165-211.}
142\end{seealso}