11# Complex numbers
22# ---------------
33
4+ # [Now that Python has a complex data type built-in, this is not very
5+ # useful, but it's still a nice example class]
6+
47# This module represents complex numbers as instances of the class Complex.
58# A Complex instance z has two data attribues, z.re (the real part) and z.im
69# (the imaginary part). In fact, z.re and z.im can have any value -- all
1518# PolarToComplex([r [,phi [,fullcircle]]]) ->
1619# the complex number z for which r == z.radius() and phi == z.angle(fullcircle)
1720# (r and phi default to 0)
21+ # exp(z) -> returns the complex exponential of z. Equivalent to pow(math.e,z).
1822#
1923# Complex numbers have the following methods:
2024# z.abs() -> absolute value of z
@@ -202,7 +206,9 @@ def __pow__(self, n, z=None):
202206 if z is not None :
203207 raise TypeError , 'Complex does not support ternary pow()'
204208 if IsComplex (n ):
205- if n .im : raise TypeError , 'Complex to the Complex power'
209+ if n .im :
210+ if self .im : raise TypeError , 'Complex to the Complex power'
211+ else : return exp (math .log (self .re )* n )
206212 n = n .re
207213 r = pow (self .abs (), n )
208214 phi = n * self .angle ()
@@ -211,6 +217,10 @@ def __pow__(self, n, z=None):
211217 def __rpow__ (self , base ):
212218 base = ToComplex (base )
213219 return pow (base , self )
220+
221+ def exp (z ):
222+ r = math .exp (z .re )
223+ return Complex (math .cos (z .im )* r ,math .sin (z .im )* r )
214224
215225
216226def checkop (expr , a , b , value , fuzz = 1e-6 ):
0 commit comments