mirror of
https://github.com/python/cpython.git
synced 2025-12-04 08:34:25 +00:00
Added note about Python's support of complex numbers.
Added exp(z).
This commit is contained in:
parent
89cb67bb64
commit
72ba616066
1 changed files with 11 additions and 1 deletions
|
|
@ -1,6 +1,9 @@
|
||||||
# Complex numbers
|
# Complex numbers
|
||||||
# ---------------
|
# ---------------
|
||||||
|
|
||||||
|
# [Now that Python has a complex data type built-in, this is not very
|
||||||
|
# useful, but it's still a nice example class]
|
||||||
|
|
||||||
# This module represents complex numbers as instances of the class Complex.
|
# This module represents complex numbers as instances of the class Complex.
|
||||||
# A Complex instance z has two data attribues, z.re (the real part) and z.im
|
# A Complex instance z has two data attribues, z.re (the real part) and z.im
|
||||||
# (the imaginary part). In fact, z.re and z.im can have any value -- all
|
# (the imaginary part). In fact, z.re and z.im can have any value -- all
|
||||||
|
|
@ -15,6 +18,7 @@
|
||||||
# PolarToComplex([r [,phi [,fullcircle]]]) ->
|
# PolarToComplex([r [,phi [,fullcircle]]]) ->
|
||||||
# the complex number z for which r == z.radius() and phi == z.angle(fullcircle)
|
# the complex number z for which r == z.radius() and phi == z.angle(fullcircle)
|
||||||
# (r and phi default to 0)
|
# (r and phi default to 0)
|
||||||
|
# exp(z) -> returns the complex exponential of z. Equivalent to pow(math.e,z).
|
||||||
#
|
#
|
||||||
# Complex numbers have the following methods:
|
# Complex numbers have the following methods:
|
||||||
# z.abs() -> absolute value of z
|
# z.abs() -> absolute value of z
|
||||||
|
|
@ -202,7 +206,9 @@ class Complex:
|
||||||
if z is not None:
|
if z is not None:
|
||||||
raise TypeError, 'Complex does not support ternary pow()'
|
raise TypeError, 'Complex does not support ternary pow()'
|
||||||
if IsComplex(n):
|
if IsComplex(n):
|
||||||
if n.im: raise TypeError, 'Complex to the Complex power'
|
if n.im:
|
||||||
|
if self.im: raise TypeError, 'Complex to the Complex power'
|
||||||
|
else: return exp(math.log(self.re)*n)
|
||||||
n = n.re
|
n = n.re
|
||||||
r = pow(self.abs(), n)
|
r = pow(self.abs(), n)
|
||||||
phi = n*self.angle()
|
phi = n*self.angle()
|
||||||
|
|
@ -212,6 +218,10 @@ class Complex:
|
||||||
base = ToComplex(base)
|
base = ToComplex(base)
|
||||||
return pow(base, self)
|
return pow(base, self)
|
||||||
|
|
||||||
|
def exp(z):
|
||||||
|
r = math.exp(z.re)
|
||||||
|
return Complex(math.cos(z.im)*r,math.sin(z.im)*r)
|
||||||
|
|
||||||
|
|
||||||
def checkop(expr, a, b, value, fuzz = 1e-6):
|
def checkop(expr, a, b, value, fuzz = 1e-6):
|
||||||
import sys
|
import sys
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue