annotate Lib/dos-8x3/complex.py @ 5817:48b94de85bf3 legacy-trunk

Extensive changes by AMK.
author Guido van Rossum <guido@python.org>
date Thu, 17 Jul 1997 16:29:42 +0000
parents b41019557b6e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3784
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # Complex numbers
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 # ---------------
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 # This module represents complex numbers as instances of the class Complex.
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 # A Complex instance z has two data attribues, z.re (the real part) and z.im
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 # (the imaginary part). In fact, z.re and z.im can have any value -- all
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 # arithmetic operators work regardless of the type of z.re and z.im (as long
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 # as they support numerical operations).
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 #
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 # The following functions exist (Complex is actually a class):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 # Complex([re [,im]) -> creates a complex number from a real and an imaginary part
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 # IsComplex(z) -> true iff z is a complex number (== has .re and .im attributes)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 # Polar([r [,phi [,fullcircle]]]) ->
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 # the complex number z for which r == z.radius() and phi == z.angle(fullcircle)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 # (r and phi default to 0)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 #
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 # Complex numbers have the following methods:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 # z.abs() -> absolute value of z
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 # z.radius() == z.abs()
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 # z.angle([fullcircle]) -> angle from positive X axis; fullcircle gives units
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 # z.phi([fullcircle]) == z.angle(fullcircle)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 #
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 # These standard functions and unary operators accept complex arguments:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 # abs(z)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 # -z
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 # +z
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 # not z
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 # repr(z) == `z`
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 # str(z)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 # hash(z) -> a combination of hash(z.re) and hash(z.im) such that if z.im is zero
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 # the result equals hash(z.re)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 # Note that hex(z) and oct(z) are not defined.
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 #
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 # These conversions accept complex arguments only if their imaginary part is zero:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 # int(z)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 # long(z)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 # float(z)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 #
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 # The following operators accept two complex numbers, or one complex number
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 # and one real number (int, long or float):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 # z1 + z2
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 # z1 - z2
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 # z1 * z2
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 # z1 / z2
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 # pow(z1, z2)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 # cmp(z1, z2)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 # Note that z1 % z2 and divmod(z1, z2) are not defined,
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 # nor are shift and mask operations.
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 #
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 # The standard module math does not support complex numbers.
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 # (I suppose it would be easy to implement a cmath module.)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 #
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 # Idea:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 # add a class Polar(r, phi) and mixed-mode arithmetic which
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 # chooses the most appropriate type for the result:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 # Complex for +,-,cmp
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 # Polar for *,/,pow
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
58
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
59
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 import types, math
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
61
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 if not hasattr(math, 'hypot'):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 def hypot(x, y):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 # XXX I know there's a way to compute this without possibly causing
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 # overflow, but I can't remember what it is right now...
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 return math.sqrt(x*x + y*y)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 math.hypot = hypot
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
68
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 twopi = math.pi*2.0
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 halfpi = math.pi/2.0
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
71
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 def IsComplex(obj):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 return hasattr(obj, 're') and hasattr(obj, 'im')
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
74
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 def Polar(r = 0, phi = 0, fullcircle = twopi):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 phi = phi * (twopi / fullcircle)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 return Complex(math.cos(phi)*r, math.sin(phi)*r)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
78
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 class Complex:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
80
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 def __init__(self, re=0, im=0):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 if IsComplex(re):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 im = im + re.im
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 re = re.re
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 if IsComplex(im):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 re = re - im.im
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 im = im.re
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 self.re = re
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 self.im = im
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
90
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 def __setattr__(self, name, value):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 if hasattr(self, name):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 raise TypeError, "Complex numbers have set-once attributes"
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 self.__dict__[name] = value
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
95
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 def __repr__(self):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 if not self.im:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 return 'Complex(%s)' % `self.re`
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 else:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 return 'Complex(%s, %s)' % (`self.re`, `self.im`)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
101
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 def __str__(self):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 if not self.im:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 return `self.re`
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 else:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 return 'Complex(%s, %s)' % (`self.re`, `self.im`)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
107
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 def __coerce__(self, other):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 if IsComplex(other):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 return self, other
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 return self, Complex(other) # May fail
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
112
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 def __cmp__(self, other):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 return cmp(self.re, other.re) or cmp(self.im, other.im)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
115
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 def __hash__(self):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 if not self.im: return hash(self.re)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 mod = sys.maxint + 1L
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 return int((hash(self.re) + 2L*hash(self.im) + mod) % (2L*mod) - mod)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
120
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 def __neg__(self):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 return Complex(-self.re, -self.im)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
123
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 def __pos__(self):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 return self
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
126
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 def __abs__(self):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 return math.hypot(self.re, self.im)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 ##return math.sqrt(self.re*self.re + self.im*self.im)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
130
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
131
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 def __int__(self):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 if self.im:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 raise ValueError, "can't convert Complex with nonzero im to int"
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 return int(self.re)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
136
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 def __long__(self):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 if self.im:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 raise ValueError, "can't convert Complex with nonzero im to long"
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 return long(self.re)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
141
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 def __float__(self):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 if self.im:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 raise ValueError, "can't convert Complex with nonzero im to float"
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 return float(self.re)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
146
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 def __nonzero__(self):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 return not (self.re == self.im == 0)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
149
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 abs = radius = __abs__
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
151
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 def angle(self, fullcircle = twopi):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 return (fullcircle/twopi) * ((halfpi - math.atan2(self.re, self.im)) % twopi)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
154
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 phi = angle
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
156
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 def __add__(self, other):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 return Complex(self.re + other.re, self.im + other.im)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
159
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 __radd__ = __add__
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
161
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 def __sub__(self, other):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 return Complex(self.re - other.re, self.im - other.im)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
164
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 def __rsub__(self, other):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 return Complex(other.re - self.re, other.im - self.im)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
167
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 def __mul__(self, other):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 return Complex(self.re*other.re - self.im*other.im,
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 self.re*other.im + self.im*other.re)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
171
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 __rmul__ = __mul__
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
173
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 def __div__(self, other):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 # Deviating from the general principle of not forcing re or im
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 # to be floats, we cast to float here, otherwise division
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 # of Complex numbers with integer re and im parts would use
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 # the (truncating) integer division
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 d = float(other.re*other.re + other.im*other.im)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 if not d: raise ZeroDivisionError, 'Complex division'
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 return Complex((self.re*other.re + self.im*other.im) / d,
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 (self.im*other.re - self.re*other.im) / d)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
183
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 def __rdiv__(self, other):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 return other / self
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
186
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 def __pow__(self, n, z=None):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 if z is not None:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 raise TypeError, 'Complex does not support ternary pow()'
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 if IsComplex(n):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 if n.im: raise TypeError, 'Complex to the Complex power'
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 n = n.re
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 r = pow(self.abs(), n)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 phi = n*self.angle()
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 return Complex(math.cos(phi)*r, math.sin(phi)*r)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
196
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 def __rpow__(self, base):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 return pow(base, self)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
199
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
200
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 # Everything below this point is part of the test suite
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
202
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 def checkop(expr, a, b, value, fuzz = 1e-6):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 import sys
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 print ' ', a, 'and', b,
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 try:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 result = eval(expr)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 except:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 result = sys.exc_type
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 print '->', result
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 if (type(result) == type('') or type(value) == type('')):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 ok = result == value
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 else:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 ok = abs(result - value) <= fuzz
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 if not ok:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 print '!!\t!!\t!! should be', value, 'diff', abs(result - value)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
217
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
218
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 def test():
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 testsuite = {
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 'a+b': [
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 (1, 10, 11),
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
223 (1, Complex(0,10), Complex(1,10)),
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
224 (Complex(0,10), 1, Complex(1,10)),
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 (Complex(0,10), Complex(1), Complex(1,10)),
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 (Complex(1), Complex(0,10), Complex(1,10)),
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
227 ],
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
228 'a-b': [
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 (1, 10, -9),
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
230 (1, Complex(0,10), Complex(1,-10)),
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
231 (Complex(0,10), 1, Complex(-1,10)),
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
232 (Complex(0,10), Complex(1), Complex(-1,10)),
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
233 (Complex(1), Complex(0,10), Complex(1,-10)),
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
234 ],
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
235 'a*b': [
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
236 (1, 10, 10),
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 (1, Complex(0,10), Complex(0, 10)),
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 (Complex(0,10), 1, Complex(0,10)),
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
239 (Complex(0,10), Complex(1), Complex(0,10)),
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
240 (Complex(1), Complex(0,10), Complex(0,10)),
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 ],
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
242 'a/b': [
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
243 (1., 10, 0.1),
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
244 (1, Complex(0,10), Complex(0, -0.1)),
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 (Complex(0, 10), 1, Complex(0, 10)),
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 (Complex(0, 10), Complex(1), Complex(0, 10)),
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
247 (Complex(1), Complex(0,10), Complex(0, -0.1)),
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 ],
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
249 'pow(a,b)': [
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
250 (1, 10, 1),
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
251 (1, Complex(0,10), 'TypeError'),
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
252 (Complex(0,10), 1, Complex(0,10)),
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
253 (Complex(0,10), Complex(1), Complex(0,10)),
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
254 (Complex(1), Complex(0,10), 'TypeError'),
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
255 (2, Complex(4,0), 16),
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
256 ],
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
257 'cmp(a,b)': [
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
258 (1, 10, -1),
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
259 (1, Complex(0,10), 1),
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
260 (Complex(0,10), 1, -1),
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
261 (Complex(0,10), Complex(1), -1),
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
262 (Complex(1), Complex(0,10), 1),
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
263 ],
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
264 }
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
265 exprs = testsuite.keys()
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 exprs.sort()
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
267 for expr in exprs:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
268 print expr + ':'
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
269 t = (expr,)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
270 for item in testsuite[expr]:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
271 apply(checkop, t+item)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
272
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
273
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
274 if __name__ == '__main__':
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
275 test()