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