Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bdaad8c

Browse files
committed
* Fix error in definition of Im() which returned self instead of 0
for non-complex arguments. * Replace type() comparisons with isinstance() checks. * Replace apply() calls with equivalent syntactic form. * Simplify __hash__ definition to hash the underlying tuple. * Use math.hypot() for more robust computation of __abs__(). * Use sorted() instead of the multi-step keys/sort/iter. * Update comment on the cmath module.
1 parent ab9ec16 commit bdaad8c

1 file changed

Lines changed: 14 additions & 19 deletions

File tree

Demo/classes/Complex.py

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@
5454
# nor are shift and mask operations.
5555
#
5656
# The standard module math does not support complex numbers.
57-
# (I suppose it would be easy to implement a cmath module.)
57+
# The cmath modules should be used instead.
5858
#
5959
# Idea:
6060
# add a class Polar(r, phi) and mixed-mode arithmetic which
6161
# chooses the most appropriate type for the result:
6262
# Complex for +,-,cmp
6363
# Polar for *,/,pow
6464

65-
import types, math
65+
import math
6666
import sys
6767

6868
twopi = math.pi*2.0
@@ -74,8 +74,8 @@ def IsComplex(obj):
7474
def ToComplex(obj):
7575
if IsComplex(obj):
7676
return obj
77-
elif type(obj) == types.TupleType:
78-
return apply(Complex, obj)
77+
elif isinstance(obj, tuple):
78+
return Complex(*obj)
7979
else:
8080
return Complex(obj)
8181

@@ -86,14 +86,12 @@ def PolarToComplex(r = 0, phi = 0, fullcircle = twopi):
8686
def Re(obj):
8787
if IsComplex(obj):
8888
return obj.re
89-
else:
90-
return obj
89+
return obj
9190

9291
def Im(obj):
9392
if IsComplex(obj):
9493
return obj.im
95-
else:
96-
return obj
94+
return 0
9795

9896
class Complex:
9997

@@ -119,9 +117,9 @@ def __setattr__(self, name, value):
119117
raise TypeError, 'Complex numbers are immutable'
120118

121119
def __hash__(self):
122-
if not self.im: return hash(self.re)
123-
mod = sys.maxint + 1L
124-
return int((hash(self.re) + 2L*hash(self.im) + mod) % (2L*mod) - mod)
120+
if not self.im:
121+
return hash(self.re)
122+
return hash((self.re, self.im))
125123

126124
def __repr__(self):
127125
if not self.im:
@@ -142,8 +140,7 @@ def __pos__(self):
142140
return self
143141

144142
def __abs__(self):
145-
# XXX could be done differently to avoid overflow!
146-
return math.sqrt(self.re*self.re + self.im*self.im)
143+
return math.hypot(self.re, self.im)
147144

148145
def __int__(self):
149146
if self.im:
@@ -238,8 +235,8 @@ def checkop(expr, a, b, value, fuzz = 1e-6):
238235
except:
239236
result = sys.exc_type
240237
print '->', result
241-
if (type(result) == type('') or type(value) == type('')):
242-
ok = result == value
238+
if isinstance(result, str) or isinstance(value, str):
239+
ok = (result == value)
243240
else:
244241
ok = abs(result - value) <= fuzz
245242
if not ok:
@@ -312,13 +309,11 @@ def test():
312309
(Complex(1), Complex(0,10), 1),
313310
],
314311
}
315-
exprs = testsuite.keys()
316-
exprs.sort()
317-
for expr in exprs:
312+
for expr in sorted(testsuite):
318313
print expr + ':'
319314
t = (expr,)
320315
for item in testsuite[expr]:
321-
apply(checkop, t+item)
316+
checkop(*(t+item))
322317

323318

324319
if __name__ == '__main__':

0 commit comments

Comments
 (0)