/Doc/library/cmath.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 260 lines · 154 code · 106 blank · 0 comment · 0 complexity · 15ae969dd08e3b5a4bb3b8c2e8d3ddd4 MD5 · raw file

  1. :mod:`cmath` --- Mathematical functions for complex numbers
  2. ===========================================================
  3. .. module:: cmath
  4. :synopsis: Mathematical functions for complex numbers.
  5. This module is always available. It provides access to mathematical functions
  6. for complex numbers. The functions in this module accept integers,
  7. floating-point numbers or complex numbers as arguments. They will also accept
  8. any Python object that has either a :meth:`__complex__` or a :meth:`__float__`
  9. method: these methods are used to convert the object to a complex or
  10. floating-point number, respectively, and the function is then applied to the
  11. result of the conversion.
  12. .. note::
  13. On platforms with hardware and system-level support for signed
  14. zeros, functions involving branch cuts are continuous on *both*
  15. sides of the branch cut: the sign of the zero distinguishes one
  16. side of the branch cut from the other. On platforms that do not
  17. support signed zeros the continuity is as specified below.
  18. Conversions to and from polar coordinates
  19. -----------------------------------------
  20. A Python complex number ``z`` is stored internally using *rectangular*
  21. or *Cartesian* coordinates. It is completely determined by its *real
  22. part* ``z.real`` and its *imaginary part* ``z.imag``. In other
  23. words::
  24. z == z.real + z.imag*1j
  25. *Polar coordinates* give an alternative way to represent a complex
  26. number. In polar coordinates, a complex number *z* is defined by the
  27. modulus *r* and the phase angle *phi*. The modulus *r* is the distance
  28. from *z* to the origin, while the phase *phi* is the counterclockwise
  29. angle from the positive x-axis to the line segment that joins the
  30. origin to *z*.
  31. The following functions can be used to convert from the native
  32. rectangular coordinates to polar coordinates and back.
  33. .. function:: phase(x)
  34. Return the phase of *x* (also known as the *argument* of *x*), as a
  35. float. ``phase(x)`` is equivalent to ``math.atan2(x.imag,
  36. x.real)``. The result lies in the range [-π, π], and the branch
  37. cut for this operation lies along the negative real axis,
  38. continuous from above. On systems with support for signed zeros
  39. (which includes most systems in current use), this means that the
  40. sign of the result is the same as the sign of ``x.imag``, even when
  41. ``x.imag`` is zero::
  42. >>> phase(complex(-1.0, 0.0))
  43. 3.1415926535897931
  44. >>> phase(complex(-1.0, -0.0))
  45. -3.1415926535897931
  46. .. versionadded:: 2.6
  47. .. note::
  48. The modulus (absolute value) of a complex number *x* can be
  49. computed using the built-in :func:`abs` function. There is no
  50. separate :mod:`cmath` module function for this operation.
  51. .. function:: polar(x)
  52. Return the representation of *x* in polar coordinates. Returns a
  53. pair ``(r, phi)`` where *r* is the modulus of *x* and phi is the
  54. phase of *x*. ``polar(x)`` is equivalent to ``(abs(x),
  55. phase(x))``.
  56. .. versionadded:: 2.6
  57. .. function:: rect(r, phi)
  58. Return the complex number *x* with polar coordinates *r* and *phi*.
  59. Equivalent to ``r * (math.cos(phi) + math.sin(phi)*1j)``.
  60. .. versionadded:: 2.6
  61. Power and logarithmic functions
  62. -------------------------------
  63. .. function:: exp(x)
  64. Return the exponential value ``e**x``.
  65. .. function:: log(x[, base])
  66. Returns the logarithm of *x* to the given *base*. If the *base* is not
  67. specified, returns the natural logarithm of *x*. There is one branch cut, from 0
  68. along the negative real axis to -, continuous from above.
  69. .. versionchanged:: 2.4
  70. *base* argument added.
  71. .. function:: log10(x)
  72. Return the base-10 logarithm of *x*. This has the same branch cut as
  73. :func:`log`.
  74. .. function:: sqrt(x)
  75. Return the square root of *x*. This has the same branch cut as :func:`log`.
  76. Trigonometric functions
  77. -----------------------
  78. .. function:: acos(x)
  79. Return the arc cosine of *x*. There are two branch cuts: One extends right from
  80. 1 along the real axis to , continuous from below. The other extends left from
  81. -1 along the real axis to -, continuous from above.
  82. .. function:: asin(x)
  83. Return the arc sine of *x*. This has the same branch cuts as :func:`acos`.
  84. .. function:: atan(x)
  85. Return the arc tangent of *x*. There are two branch cuts: One extends from
  86. ``1j`` along the imaginary axis to ``j``, continuous from the right. The
  87. other extends from ``-1j`` along the imaginary axis to ``-j``, continuous
  88. from the left.
  89. .. versionchanged:: 2.6
  90. direction of continuity of upper cut reversed
  91. .. function:: cos(x)
  92. Return the cosine of *x*.
  93. .. function:: sin(x)
  94. Return the sine of *x*.
  95. .. function:: tan(x)
  96. Return the tangent of *x*.
  97. Hyperbolic functions
  98. --------------------
  99. .. function:: acosh(x)
  100. Return the hyperbolic arc cosine of *x*. There is one branch cut, extending left
  101. from 1 along the real axis to -, continuous from above.
  102. .. function:: asinh(x)
  103. Return the hyperbolic arc sine of *x*. There are two branch cuts:
  104. One extends from ``1j`` along the imaginary axis to ``j``,
  105. continuous from the right. The other extends from ``-1j`` along
  106. the imaginary axis to ``-j``, continuous from the left.
  107. .. versionchanged:: 2.6
  108. branch cuts moved to match those recommended by the C99 standard
  109. .. function:: atanh(x)
  110. Return the hyperbolic arc tangent of *x*. There are two branch cuts: One
  111. extends from ``1`` along the real axis to ````, continuous from below. The
  112. other extends from ``-1`` along the real axis to ``-``, continuous from
  113. above.
  114. .. versionchanged:: 2.6
  115. direction of continuity of right cut reversed
  116. .. function:: cosh(x)
  117. Return the hyperbolic cosine of *x*.
  118. .. function:: sinh(x)
  119. Return the hyperbolic sine of *x*.
  120. .. function:: tanh(x)
  121. Return the hyperbolic tangent of *x*.
  122. Classification functions
  123. ------------------------
  124. .. function:: isinf(x)
  125. Return *True* if the real or the imaginary part of x is positive
  126. or negative infinity.
  127. .. versionadded:: 2.6
  128. .. function:: isnan(x)
  129. Return *True* if the real or imaginary part of x is not a number (NaN).
  130. .. versionadded:: 2.6
  131. Constants
  132. ---------
  133. .. data:: pi
  134. The mathematical constant *π*, as a float.
  135. .. data:: e
  136. The mathematical constant *e*, as a float.
  137. .. index:: module: math
  138. Note that the selection of functions is similar, but not identical, to that in
  139. module :mod:`math`. The reason for having two modules is that some users aren't
  140. interested in complex numbers, and perhaps don't even know what they are. They
  141. would rather have ``math.sqrt(-1)`` raise an exception than return a complex
  142. number. Also note that the functions defined in :mod:`cmath` always return a
  143. complex number, even if the answer can be expressed as a real number (in which
  144. case the complex number has an imaginary part of zero).
  145. A note on branch cuts: They are curves along which the given function fails to
  146. be continuous. They are a necessary feature of many complex functions. It is
  147. assumed that if you need to compute with complex functions, you will understand
  148. about branch cuts. Consult almost any (not too elementary) book on complex
  149. variables for enlightenment. For information of the proper choice of branch
  150. cuts for numerical purposes, a good reference should be the following:
  151. .. seealso::
  152. Kahan, W: Branch cuts for complex elementary functions; or, Much ado about
  153. nothing's sign bit. In Iserles, A., and Powell, M. (eds.), The state of the art
  154. in numerical analysis. Clarendon Press (1987) pp165-211.