Mercurial > cpython-lru_cache-weakref
annotate Demo/classes/Complex.py @ 25838:8540887bcec6 2.2
Backport of 2.183:
Fix memory leak in add_subclass() found by NealN with valgrind.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 18 Oct 2002 13:42:21 +0000 |
| parents | 341a6e47a8ff |
| children | 558689b6d7f0 |
| 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 |
|
3868
2ac2a601205b
Added note about Python's support of complex numbers.
Guido van Rossum <guido@python.org>
parents:
1958
diff
changeset
|
4 # [Now that Python has a complex data type built-in, this is not very |
|
2ac2a601205b
Added note about Python's support of complex numbers.
Guido van Rossum <guido@python.org>
parents:
1958
diff
changeset
|
5 # useful, but it's still a nice example class] |
|
2ac2a601205b
Added note about Python's support of complex numbers.
Guido van Rossum <guido@python.org>
parents:
1958
diff
changeset
|
6 |
|
1958
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
7 # 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
|
8 # 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
|
9 # (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
|
10 # 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
|
11 # as they support numerical operations). |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
12 # |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
13 # 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
|
14 # 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
|
15 # 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
|
16 # 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
|
17 # 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
|
18 # PolarToComplex([r [,phi [,fullcircle]]]) -> |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
19 # 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
|
20 # (r and phi default to 0) |
|
3868
2ac2a601205b
Added note about Python's support of complex numbers.
Guido van Rossum <guido@python.org>
parents:
1958
diff
changeset
|
21 # exp(z) -> returns the complex exponential of z. Equivalent to pow(math.e,z). |
|
1958
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
22 # |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
23 # Complex numbers have the following methods: |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
24 # z.abs() -> absolute value of z |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
25 # z.radius() == z.abs() |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
26 # 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
|
27 # z.phi([fullcircle]) == z.angle(fullcircle) |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
28 # |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
29 # 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
|
30 # abs(z) |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
31 # -z |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
32 # +z |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
33 # not z |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
34 # repr(z) == `z` |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
35 # str(z) |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
36 # 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
|
37 # the result equals hash(z.re) |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
38 # 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
|
39 # |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
40 # 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
|
41 # int(z) |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
42 # long(z) |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
43 # float(z) |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
44 # |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
45 # 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
|
46 # 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
|
47 # z1 + z2 |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
48 # z1 - z2 |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
49 # z1 * z2 |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
50 # z1 / z2 |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
51 # pow(z1, z2) |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
52 # cmp(z1, z2) |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
53 # 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
|
54 # nor are shift and mask operations. |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
55 # |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
56 # 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
|
57 # (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
|
58 # |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
59 # Idea: |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
60 # 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
|
61 # 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
|
62 # Complex for +,-,cmp |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
63 # Polar for *,/,pow |
| 945 | 64 |
| 65 | |
|
1958
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
66 import types, math |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
67 |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
68 twopi = math.pi*2.0 |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
69 halfpi = math.pi/2.0 |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
70 |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
71 def IsComplex(obj): |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
72 return hasattr(obj, 're') and hasattr(obj, 'im') |
| 945 | 73 |
|
1958
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
74 def ToComplex(obj): |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
75 if IsComplex(obj): |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
76 return obj |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
77 elif type(obj) == types.TupleType: |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
78 return apply(Complex, obj) |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
79 else: |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
80 return Complex(obj) |
|
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 PolarToComplex(r = 0, phi = 0, fullcircle = twopi): |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
83 phi = phi * (twopi / fullcircle) |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
84 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
|
85 |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
86 def Re(obj): |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
87 if IsComplex(obj): |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
88 return obj.re |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
89 else: |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
90 return obj |
|
1427
8701b93383ab
*** empty log message ***
Guido van Rossum <guido@python.org>
parents:
945
diff
changeset
|
91 |
|
1958
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
92 def Im(obj): |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
93 if IsComplex(obj): |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
94 return obj.im |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
95 else: |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
96 return obj |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
97 |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
98 class Complex: |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
99 |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
100 def __init__(self, re=0, im=0): |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
101 if IsComplex(re): |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
102 im = i + Complex(0, re.im) |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
103 re = re.re |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
104 if IsComplex(im): |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
105 re = re - im.im |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
106 im = im.re |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
107 self.__dict__['re'] = re |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
108 self.__dict__['im'] = im |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
109 |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
110 def __setattr__(self, name, value): |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
111 raise TypeError, 'Complex numbers are immutable' |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
112 |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
113 def __hash__(self): |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
114 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
|
115 mod = sys.maxint + 1L |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
116 return int((hash(self.re) + 2L*hash(self.im) + mod) % (2L*mod) - mod) |
| 945 | 117 |
| 118 def __repr__(self): | |
|
1958
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
119 if not self.im: |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
120 return 'Complex(%s)' % `self.re` |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
121 else: |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
122 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
|
123 |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
124 def __str__(self): |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
125 if not self.im: |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
126 return `self.re` |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
127 else: |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
128 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
|
129 |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
130 def __neg__(self): |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
131 return Complex(-self.re, -self.im) |
| 945 | 132 |
|
1958
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
133 def __pos__(self): |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
134 return self |
|
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 __abs__(self): |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
137 # 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
|
138 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
|
139 |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
140 def __int__(self): |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
141 if self.im: |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
142 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
|
143 return int(self.re) |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
144 |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
145 def __long__(self): |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
146 if self.im: |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
147 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
|
148 return long(self.re) |
| 945 | 149 |
| 150 def __float__(self): | |
| 151 if self.im: | |
|
1958
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
152 raise ValueError, "can't convert Complex with nonzero im to float" |
| 945 | 153 return float(self.re) |
| 154 | |
|
1958
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
155 def __cmp__(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((self.re, self.im), (other.re, other.im)) |
| 945 | 158 |
|
1958
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
159 def __rcmp__(self, other): |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
160 other = ToComplex(other) |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
161 return cmp(other, self) |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
162 |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
163 def __nonzero__(self): |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
164 return not (self.re == self.im == 0) |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
165 |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
166 abs = radius = __abs__ |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
167 |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
168 def angle(self, fullcircle = twopi): |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
169 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
|
170 |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
171 phi = angle |
| 945 | 172 |
|
1958
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
173 def __add__(self, other): |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
174 other = ToComplex(other) |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
175 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
|
176 |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
177 __radd__ = __add__ |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
178 |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
179 def __sub__(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 Complex(self.re - other.re, self.im - other.im) |
| 945 | 182 |
|
1958
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
183 def __rsub__(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 other - self |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
186 |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
187 def __mul__(self, other): |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
188 other = ToComplex(other) |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
189 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
|
190 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
|
191 |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
192 __rmul__ = __mul__ |
| 945 | 193 |
|
1958
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
194 def __div__(self, other): |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
195 other = ToComplex(other) |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
196 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
|
197 if not d: raise ZeroDivisionError, 'Complex division' |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
198 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
|
199 (self.im*other.re - self.re*other.im) / d) |
| 945 | 200 |
|
1958
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
201 def __rdiv__(self, other): |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
202 other = ToComplex(other) |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
203 return other / self |
| 945 | 204 |
|
1958
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
205 def __pow__(self, n, z=None): |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
206 if z is not None: |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
207 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
|
208 if IsComplex(n): |
|
3868
2ac2a601205b
Added note about Python's support of complex numbers.
Guido van Rossum <guido@python.org>
parents:
1958
diff
changeset
|
209 if n.im: |
|
2ac2a601205b
Added note about Python's support of complex numbers.
Guido van Rossum <guido@python.org>
parents:
1958
diff
changeset
|
210 if self.im: raise TypeError, 'Complex to the Complex power' |
|
2ac2a601205b
Added note about Python's support of complex numbers.
Guido van Rossum <guido@python.org>
parents:
1958
diff
changeset
|
211 else: return exp(math.log(self.re)*n) |
|
1958
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
212 n = n.re |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
213 r = pow(self.abs(), n) |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
214 phi = n*self.angle() |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
215 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
|
216 |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
217 def __rpow__(self, base): |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
218 base = ToComplex(base) |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
219 return pow(base, self) |
|
3868
2ac2a601205b
Added note about Python's support of complex numbers.
Guido van Rossum <guido@python.org>
parents:
1958
diff
changeset
|
220 |
|
2ac2a601205b
Added note about Python's support of complex numbers.
Guido van Rossum <guido@python.org>
parents:
1958
diff
changeset
|
221 def exp(z): |
|
2ac2a601205b
Added note about Python's support of complex numbers.
Guido van Rossum <guido@python.org>
parents:
1958
diff
changeset
|
222 r = math.exp(z.re) |
|
2ac2a601205b
Added note about Python's support of complex numbers.
Guido van Rossum <guido@python.org>
parents:
1958
diff
changeset
|
223 return Complex(math.cos(z.im)*r,math.sin(z.im)*r) |
|
1958
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
224 |
| 945 | 225 |
|
1958
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
226 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
|
227 import sys |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
228 print ' ', a, 'and', b, |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
229 try: |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
230 result = eval(expr) |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
231 except: |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
232 result = sys.exc_type |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
233 print '->', result |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
234 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
|
235 ok = result == value |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
236 else: |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
237 ok = abs(result - value) <= fuzz |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
238 if not ok: |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
239 print '!!\t!!\t!! should be', value, 'diff', abs(result - value) |
| 945 | 240 |
| 241 | |
| 242 def test(): | |
|
1958
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
243 testsuite = { |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
244 'a+b': [ |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
245 (1, 10, 11), |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
246 (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 (Complex(0,10), 1, Complex(1,10)), |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
248 (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
|
249 (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
|
250 ], |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
251 'a-b': [ |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
252 (1, 10, -9), |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
253 (1, Complex(0,10), Complex(1,-10)), |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
254 (Complex(0,10), 1, Complex(-1,10)), |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
255 (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
|
256 (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
|
257 ], |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
258 'a*b': [ |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
259 (1, 10, 10), |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
260 (1, Complex(0,10), Complex(0, 10)), |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
261 (Complex(0,10), 1, Complex(0,10)), |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
262 (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
|
263 (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
|
264 ], |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
265 'a/b': [ |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
266 (1., 10, 0.1), |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
267 (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
|
268 (Complex(0, 10), 1, Complex(0, 10)), |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
269 (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
|
270 (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
|
271 ], |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
272 'pow(a,b)': [ |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
273 (1, 10, 1), |
|
6799
341a6e47a8ff
Fix the test for pow() -- no more TypeErrors are raised!
Guido van Rossum <guido@python.org>
parents:
3868
diff
changeset
|
274 (1, Complex(0,10), 1), |
|
1958
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
275 (Complex(0,10), 1, Complex(0,10)), |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
276 (Complex(0,10), Complex(1), Complex(0,10)), |
|
6799
341a6e47a8ff
Fix the test for pow() -- no more TypeErrors are raised!
Guido van Rossum <guido@python.org>
parents:
3868
diff
changeset
|
277 (Complex(1), Complex(0,10), 1), |
|
1958
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
278 (2, Complex(4,0), 16), |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
279 ], |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
280 'cmp(a,b)': [ |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
281 (1, 10, -1), |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
282 (1, Complex(0,10), 1), |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
283 (Complex(0,10), 1, -1), |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
284 (Complex(0,10), Complex(1), -1), |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
285 (Complex(1), Complex(0,10), 1), |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
286 ], |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
287 } |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
288 exprs = testsuite.keys() |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
289 exprs.sort() |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
290 for expr in exprs: |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
291 print expr + ':' |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
292 t = (expr,) |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
293 for item in testsuite[expr]: |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
294 apply(checkop, t+item) |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
295 |
| 945 | 296 |
|
1958
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
297 if __name__ == '__main__': |
|
545e235831b7
totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents:
1427
diff
changeset
|
298 test() |
