summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1996-07-30 19:02:01 (GMT)
committerGuido van Rossum <guido@python.org>1996-07-30 19:02:01 (GMT)
commit72ba616066e6b90d32f2d0b49be394561bb1e518 (patch)
treec99455ed949bf2ad20a77119a6890866db856d73
parent89cb67bb642ee958d9f095728c99e943e994ca54 (diff)
downloadcpython-72ba616066e6b90d32f2d0b49be394561bb1e518.zip
cpython-72ba616066e6b90d32f2d0b49be394561bb1e518.tar.gz
cpython-72ba616066e6b90d32f2d0b49be394561bb1e518.tar.bz2
Added note about Python's support of complex numbers.
Added exp(z).
-rwxr-xr-xDemo/classes/Complex.py12
1 files changed, 11 insertions, 1 deletions
diff --git a/Demo/classes/Complex.py b/Demo/classes/Complex.py
index 5ac6b18..d2f6f23 100755
--- a/Demo/classes/Complex.py
+++ b/Demo/classes/Complex.py
@@ -1,6 +1,9 @@
# 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.
# 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
@@ -15,6 +18,7 @@
# PolarToComplex([r [,phi [,fullcircle]]]) ->
# the complex number z for which r == z.radius() and phi == z.angle(fullcircle)
# (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:
# z.abs() -> absolute value of z
@@ -202,7 +206,9 @@ class Complex:
if z is not None:
raise TypeError, 'Complex does not support ternary pow()'
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
r = pow(self.abs(), n)
phi = n*self.angle()
@@ -211,6 +217,10 @@ class Complex:
def __rpow__(self, base):
base = ToComplex(base)
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):