annotate Demo/classes/Complex.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents 48c71ff38578
children 7ada16a14968
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
945
465b9841770e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # Complex numbers
1958
545e235831b7 totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents: 1427
diff changeset
2 # ---------------
945
465b9841770e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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]]]) ->
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
19 # the complex number z for which r == z.radius() and phi == z.angle(fullcircle)
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
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.
34891
48c71ff38578 * Fix error in definition of Im() which returned self instead of 0
Raymond Hettinger <python@rcn.com>
parents: 34889
diff changeset
57 # The cmath modules should be used instead.
1958
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
465b9841770e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
64
34891
48c71ff38578 * Fix error in definition of Im() which returned self instead of 0
Raymond Hettinger <python@rcn.com>
parents: 34889
diff changeset
65 import math
34889
9e659cb727ea Patch #1177597: Correct various bugs, add comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
66 import sys
1958
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):
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
72 return hasattr(obj, 're') and hasattr(obj, 'im')
945
465b9841770e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
73
1958
545e235831b7 totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents: 1427
diff changeset
74 def ToComplex(obj):
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
75 if IsComplex(obj):
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
76 return obj
34891
48c71ff38578 * Fix error in definition of Im() which returned self instead of 0
Raymond Hettinger <python@rcn.com>
parents: 34889
diff changeset
77 elif isinstance(obj, tuple):
48c71ff38578 * Fix error in definition of Im() which returned self instead of 0
Raymond Hettinger <python@rcn.com>
parents: 34889
diff changeset
78 return Complex(*obj)
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
79 else:
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
80 return Complex(obj)
1958
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):
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
83 phi = phi * (twopi / fullcircle)
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
84 return Complex(math.cos(phi)*r, math.sin(phi)*r)
1958
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):
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
87 if IsComplex(obj):
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
88 return obj.re
34891
48c71ff38578 * Fix error in definition of Im() which returned self instead of 0
Raymond Hettinger <python@rcn.com>
parents: 34889
diff changeset
89 return obj
1427
8701b93383ab *** empty log message ***
Guido van Rossum <guido@python.org>
parents: 945
diff changeset
90
1958
545e235831b7 totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents: 1427
diff changeset
91 def Im(obj):
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
92 if IsComplex(obj):
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
93 return obj.im
34891
48c71ff38578 * Fix error in definition of Im() which returned self instead of 0
Raymond Hettinger <python@rcn.com>
parents: 34889
diff changeset
94 return 0
1958
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 class Complex:
545e235831b7 totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents: 1427
diff changeset
97
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
98 def __init__(self, re=0, im=0):
34889
9e659cb727ea Patch #1177597: Correct various bugs, add comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
99 _re = 0
9e659cb727ea Patch #1177597: Correct various bugs, add comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
100 _im = 0
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
101 if IsComplex(re):
34889
9e659cb727ea Patch #1177597: Correct various bugs, add comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
102 _re = re.re
9e659cb727ea Patch #1177597: Correct various bugs, add comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
103 _im = re.im
9e659cb727ea Patch #1177597: Correct various bugs, add comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
104 else:
9e659cb727ea Patch #1177597: Correct various bugs, add comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
105 _re = re
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
106 if IsComplex(im):
34889
9e659cb727ea Patch #1177597: Correct various bugs, add comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
107 _re = _re - im.im
9e659cb727ea Patch #1177597: Correct various bugs, add comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
108 _im = _im + im.re
9e659cb727ea Patch #1177597: Correct various bugs, add comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
109 else:
9e659cb727ea Patch #1177597: Correct various bugs, add comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
110 _im = _im + im
9e659cb727ea Patch #1177597: Correct various bugs, add comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
111 # this class is immutable, so setting self.re directly is
9e659cb727ea Patch #1177597: Correct various bugs, add comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
112 # not possible.
9e659cb727ea Patch #1177597: Correct various bugs, add comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
113 self.__dict__['re'] = _re
9e659cb727ea Patch #1177597: Correct various bugs, add comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
114 self.__dict__['im'] = _im
1958
545e235831b7 totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents: 1427
diff changeset
115
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
116 def __setattr__(self, name, value):
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
117 raise TypeError, 'Complex numbers are immutable'
945
465b9841770e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
118
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
119 def __hash__(self):
34891
48c71ff38578 * Fix error in definition of Im() which returned self instead of 0
Raymond Hettinger <python@rcn.com>
parents: 34889
diff changeset
120 if not self.im:
48c71ff38578 * Fix error in definition of Im() which returned self instead of 0
Raymond Hettinger <python@rcn.com>
parents: 34889
diff changeset
121 return hash(self.re)
48c71ff38578 * Fix error in definition of Im() which returned self instead of 0
Raymond Hettinger <python@rcn.com>
parents: 34889
diff changeset
122 return hash((self.re, self.im))
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
123
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
124 def __repr__(self):
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
125 if not self.im:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 28788
diff changeset
126 return 'Complex(%r)' % (self.re,)
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
127 else:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 28788
diff changeset
128 return 'Complex(%r, %r)' % (self.re, self.im)
1958
545e235831b7 totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents: 1427
diff changeset
129
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
130 def __str__(self):
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
131 if not self.im:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 28788
diff changeset
132 return repr(self.re)
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
133 else:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 28788
diff changeset
134 return 'Complex(%r, %r)' % (self.re, self.im)
1958
545e235831b7 totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents: 1427
diff changeset
135
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
136 def __neg__(self):
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
137 return Complex(-self.re, -self.im)
945
465b9841770e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
138
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
139 def __pos__(self):
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
140 return self
1958
545e235831b7 totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents: 1427
diff changeset
141
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
142 def __abs__(self):
34891
48c71ff38578 * Fix error in definition of Im() which returned self instead of 0
Raymond Hettinger <python@rcn.com>
parents: 34889
diff changeset
143 return math.hypot(self.re, self.im)
1958
545e235831b7 totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents: 1427
diff changeset
144
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
145 def __int__(self):
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
146 if self.im:
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
147 raise ValueError, "can't convert Complex with nonzero im to int"
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
148 return int(self.re)
1958
545e235831b7 totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents: 1427
diff changeset
149
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
150 def __long__(self):
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
151 if self.im:
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
152 raise ValueError, "can't convert Complex with nonzero im to long"
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
153 return long(self.re)
945
465b9841770e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
154
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
155 def __float__(self):
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
156 if self.im:
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
157 raise ValueError, "can't convert Complex with nonzero im to float"
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
158 return float(self.re)
945
465b9841770e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
159
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
160 def __cmp__(self, other):
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
161 other = ToComplex(other)
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
162 return cmp((self.re, self.im), (other.re, other.im))
945
465b9841770e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
163
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
164 def __rcmp__(self, other):
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
165 other = ToComplex(other)
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
166 return cmp(other, self)
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
167
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
168 def __nonzero__(self):
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
169 return not (self.re == self.im == 0)
1958
545e235831b7 totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents: 1427
diff changeset
170
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
171 abs = radius = __abs__
1958
545e235831b7 totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents: 1427
diff changeset
172
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
173 def angle(self, fullcircle = twopi):
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
174 return (fullcircle/twopi) * ((halfpi - math.atan2(self.re, self.im)) % twopi)
1958
545e235831b7 totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents: 1427
diff changeset
175
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
176 phi = angle
945
465b9841770e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
177
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
178 def __add__(self, other):
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
179 other = ToComplex(other)
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
180 return Complex(self.re + other.re, self.im + other.im)
1958
545e235831b7 totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents: 1427
diff changeset
181
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
182 __radd__ = __add__
1958
545e235831b7 totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents: 1427
diff changeset
183
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
184 def __sub__(self, other):
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
185 other = ToComplex(other)
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
186 return Complex(self.re - other.re, self.im - other.im)
945
465b9841770e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
187
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
188 def __rsub__(self, other):
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
189 other = ToComplex(other)
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
190 return other - self
1958
545e235831b7 totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents: 1427
diff changeset
191
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
192 def __mul__(self, other):
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
193 other = ToComplex(other)
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
194 return Complex(self.re*other.re - self.im*other.im,
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
195 self.re*other.im + self.im*other.re)
1958
545e235831b7 totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents: 1427
diff changeset
196
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
197 __rmul__ = __mul__
945
465b9841770e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
198
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
199 def __div__(self, other):
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
200 other = ToComplex(other)
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
201 d = float(other.re*other.re + other.im*other.im)
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
202 if not d: raise ZeroDivisionError, 'Complex division'
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
203 return Complex((self.re*other.re + self.im*other.im) / d,
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
204 (self.im*other.re - self.re*other.im) / d)
945
465b9841770e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
205
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
206 def __rdiv__(self, other):
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
207 other = ToComplex(other)
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
208 return other / self
945
465b9841770e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
209
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
210 def __pow__(self, n, z=None):
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
211 if z is not None:
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
212 raise TypeError, 'Complex does not support ternary pow()'
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
213 if IsComplex(n):
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
214 if n.im:
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
215 if self.im: raise TypeError, 'Complex to the Complex power'
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
216 else: return exp(math.log(self.re)*n)
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
217 n = n.re
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
218 r = pow(self.abs(), n)
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
219 phi = n*self.angle()
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
220 return Complex(math.cos(phi)*r, math.sin(phi)*r)
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
221
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
222 def __rpow__(self, base):
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
223 base = ToComplex(base)
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
224 return pow(base, self)
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
225
3868
2ac2a601205b Added note about Python's support of complex numbers.
Guido van Rossum <guido@python.org>
parents: 1958
diff changeset
226 def exp(z):
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
227 r = math.exp(z.re)
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
228 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
229
945
465b9841770e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
230
1958
545e235831b7 totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents: 1427
diff changeset
231 def checkop(expr, a, b, value, fuzz = 1e-6):
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
232 print ' ', a, 'and', b,
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
233 try:
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
234 result = eval(expr)
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
235 except:
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
236 result = sys.exc_type
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
237 print '->', result
34891
48c71ff38578 * Fix error in definition of Im() which returned self instead of 0
Raymond Hettinger <python@rcn.com>
parents: 34889
diff changeset
238 if isinstance(result, str) or isinstance(value, str):
48c71ff38578 * Fix error in definition of Im() which returned self instead of 0
Raymond Hettinger <python@rcn.com>
parents: 34889
diff changeset
239 ok = (result == value)
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
240 else:
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
241 ok = abs(result - value) <= fuzz
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
242 if not ok:
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
243 print '!!\t!!\t!! should be', value, 'diff', abs(result - value)
945
465b9841770e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
244
465b9841770e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 def test():
34889
9e659cb727ea Patch #1177597: Correct various bugs, add comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
246 print 'test constructors'
9e659cb727ea Patch #1177597: Correct various bugs, add comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
247 constructor_test = (
9e659cb727ea Patch #1177597: Correct various bugs, add comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
248 # "expect" is an array [re,im] "got" the Complex.
9e659cb727ea Patch #1177597: Correct various bugs, add comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
249 ( (0,0), Complex() ),
9e659cb727ea Patch #1177597: Correct various bugs, add comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
250 ( (0,0), Complex() ),
9e659cb727ea Patch #1177597: Correct various bugs, add comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
251 ( (1,0), Complex(1) ),
9e659cb727ea Patch #1177597: Correct various bugs, add comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
252 ( (0,1), Complex(0,1) ),
9e659cb727ea Patch #1177597: Correct various bugs, add comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
253 ( (1,2), Complex(Complex(1,2)) ),
9e659cb727ea Patch #1177597: Correct various bugs, add comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
254 ( (1,3), Complex(Complex(1,2),1) ),
9e659cb727ea Patch #1177597: Correct various bugs, add comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
255 ( (0,0), Complex(0,Complex(0,0)) ),
9e659cb727ea Patch #1177597: Correct various bugs, add comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
256 ( (3,4), Complex(3,Complex(4)) ),
9e659cb727ea Patch #1177597: Correct various bugs, add comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
257 ( (-1,3), Complex(1,Complex(3,2)) ),
9e659cb727ea Patch #1177597: Correct various bugs, add comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
258 ( (-7,6), Complex(Complex(1,2),Complex(4,8)) ) )
9e659cb727ea Patch #1177597: Correct various bugs, add comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
259 cnt = [0,0]
9e659cb727ea Patch #1177597: Correct various bugs, add comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
260 for t in constructor_test:
9e659cb727ea Patch #1177597: Correct various bugs, add comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
261 cnt[0] += 1
9e659cb727ea Patch #1177597: Correct various bugs, add comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
262 if ((t[0][0]!=t[1].re)or(t[0][1]!=t[1].im)):
9e659cb727ea Patch #1177597: Correct various bugs, add comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
263 print " expected", t[0], "got", t[1]
9e659cb727ea Patch #1177597: Correct various bugs, add comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
264 cnt[1] += 1
9e659cb727ea Patch #1177597: Correct various bugs, add comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
265 print " ", cnt[1], "of", cnt[0], "tests failed"
9e659cb727ea Patch #1177597: Correct various bugs, add comments.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
266 # test operators
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
267 testsuite = {
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
268 'a+b': [
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
269 (1, 10, 11),
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
270 (1, Complex(0,10), Complex(1,10)),
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
271 (Complex(0,10), 1, Complex(1,10)),
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
272 (Complex(0,10), Complex(1), Complex(1,10)),
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
273 (Complex(1), Complex(0,10), Complex(1,10)),
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
274 ],
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
275 'a-b': [
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
276 (1, 10, -9),
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
277 (1, Complex(0,10), Complex(1,-10)),
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
278 (Complex(0,10), 1, Complex(-1,10)),
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
279 (Complex(0,10), Complex(1), Complex(-1,10)),
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
280 (Complex(1), Complex(0,10), Complex(1,-10)),
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
281 ],
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
282 'a*b': [
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
283 (1, 10, 10),
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
284 (1, Complex(0,10), Complex(0, 10)),
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
285 (Complex(0,10), 1, Complex(0,10)),
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
286 (Complex(0,10), Complex(1), Complex(0,10)),
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
287 (Complex(1), Complex(0,10), Complex(0,10)),
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
288 ],
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
289 'a/b': [
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
290 (1., 10, 0.1),
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
291 (1, Complex(0,10), Complex(0, -0.1)),
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
292 (Complex(0, 10), 1, Complex(0, 10)),
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
293 (Complex(0, 10), Complex(1), Complex(0, 10)),
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
294 (Complex(1), Complex(0,10), Complex(0, -0.1)),
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
295 ],
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
296 'pow(a,b)': [
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
297 (1, 10, 1),
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
298 (1, Complex(0,10), 1),
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
299 (Complex(0,10), 1, Complex(0,10)),
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
300 (Complex(0,10), Complex(1), Complex(0,10)),
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
301 (Complex(1), Complex(0,10), 1),
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
302 (2, Complex(4,0), 16),
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
303 ],
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
304 'cmp(a,b)': [
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
305 (1, 10, -1),
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
306 (1, Complex(0,10), 1),
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
307 (Complex(0,10), 1, -1),
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
308 (Complex(0,10), Complex(1), -1),
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
309 (Complex(1), Complex(0,10), 1),
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
310 ],
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
311 }
34891
48c71ff38578 * Fix error in definition of Im() which returned self instead of 0
Raymond Hettinger <python@rcn.com>
parents: 34889
diff changeset
312 for expr in sorted(testsuite):
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
313 print expr + ':'
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
314 t = (expr,)
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
315 for item in testsuite[expr]:
34891
48c71ff38578 * Fix error in definition of Im() which returned self instead of 0
Raymond Hettinger <python@rcn.com>
parents: 34889
diff changeset
316 checkop(*(t+item))
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
317
945
465b9841770e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
318
1958
545e235831b7 totally redone for new overloading scheme
Guido van Rossum <guido@python.org>
parents: 1427
diff changeset
319 if __name__ == '__main__':
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 6799
diff changeset
320 test()