blob: 5a9ae057074bd0e302f62abba32289e673507c36 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
2:mod:`cmath` --- Mathematical functions for complex numbers
3===========================================================
4
5.. module:: cmath
6 :synopsis: Mathematical functions for complex numbers.
7
8
9This module is always available. It provides access to mathematical functions
10for complex numbers. The functions in this module accept integers,
11floating-point numbers or complex numbers as arguments. They will also accept
12any Python object that has either a :meth:`__complex__` or a :meth:`__float__`
13method: these methods are used to convert the object to a complex or
14floating-point number, respectively, and the function is then applied to the
15result of the conversion.
16
17The 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
Georg Brandl116aa622007-08-15 14:28:22 +000087
88.. function:: log10(x)
89
90 Return the base-10 logarithm of *x*. This has the same branch cut as
91 :func:`log`.
92
93
94.. function:: sin(x)
95
96 Return the sine of *x*.
97
98
99.. function:: sinh(x)
100
101 Return the hyperbolic sine of *x*.
102
103
104.. function:: sqrt(x)
105
106 Return the square root of *x*. This has the same branch cut as :func:`log`.
107
108
109.. function:: tan(x)
110
111 Return the tangent of *x*.
112
113
114.. function:: tanh(x)
115
116 Return the hyperbolic tangent of *x*.
117
118The module also defines two mathematical constants:
119
120
121.. data:: pi
122
123 The mathematical constant *pi*, as a float.
124
125
126.. data:: e
127
128 The mathematical constant *e*, as a float.
129
130.. index:: module: math
131
132Note that the selection of functions is similar, but not identical, to that in
133module :mod:`math`. The reason for having two modules is that some users aren't
134interested in complex numbers, and perhaps don't even know what they are. They
135would rather have ``math.sqrt(-1)`` raise an exception than return a complex
136number. Also note that the functions defined in :mod:`cmath` always return a
137complex number, even if the answer can be expressed as a real number (in which
138case the complex number has an imaginary part of zero).
139
140A note on branch cuts: They are curves along which the given function fails to
141be continuous. They are a necessary feature of many complex functions. It is
142assumed that if you need to compute with complex functions, you will understand
143about branch cuts. Consult almost any (not too elementary) book on complex
144variables for enlightenment. For information of the proper choice of branch
145cuts for numerical purposes, a good reference should be the following:
146
147
148.. seealso::
149
150 Kahan, W: Branch cuts for complex elementary functions; or, Much ado about
151 nothing's sign bit. In Iserles, A., and Powell, M. (eds.), The state of the art
152 in numerical analysis. Clarendon Press (1987) pp165-211.
153