why no arg, abs methods for comlex type?

Daniel Schüle uval at rz.uni-karlsruhe.de
Fri Aug 5 12:16:38 EDT 2005


Hi Terry,

 >>In my opinion it would also be nice to have the
 >>possibility to write it as
 >>c.abs()
 >>it looks more OO
 >
 >
 > Python is object based but not rigidly OO in syntax or looks.  This 
is an
 > intentional design decision.  Not being gratuitiously redundant is 
another.

I agree, redundancy confuses people

 >>unfortunately there is no arg method to get the angle
 >>of the complex number
 >
 >
 > I agree that this is a deficiency.  I would think .angle() should be a
 > no-param method like .conjugate(), though its internal implementation 
would
 > need access to the appropriate cmath functions.  I think returning 
radians
 > might be enough though.

I don't know what nomenclature is used in english speaking
mathematical world for angle of a complex number
I learned it in german as Arg(z) .. Arg standing for argument
you see, we would have named it differently, hence making
it difficult for the reader, eventually creating redundancy

 > You could submit to the SourceForge tracker a RFE
 > (Request For Enhancement) if not a patch.

I never did, but I will see

 >>> c = 1 + 1j
 >>> def arg(c, angle_mode = "RAD"):
...     if angle_mode not in ("RAD", "GRAD"):
...             raise ValueError
...     import math
...     ret = math.atan2(c.imag, c.real)
...     if angle_mode == "GRAD":
...             return ret / math.pi * 180
...     return ret

it's pretty easy and straightforward implementation
while writing this, I was struck by an idea and
reimplemented it as following

 >>> def arg(self, angle_mode = "RAD"):
...     if angle_mode not in ("RAD", "GRAD"):
...             raise ValueError
...     import math
...     ret = math.atan2(self.imag, self.real)
...     if angle_mode == "GRAD":
...             return ret / math.pi * 180
...     return ret
...
 >>> class Complex(complex): pass
 >>> Complex.arg = arg
 >>> c = Complex(1+1j)
 >>> c.arg(angle_mode = "GRAD")
45.0

later I realized that this represents "yet another implementation"
which could be done by someone, thus again leading to possible
redundancy and confuse people

all this would not have happened when we would have
arg or angle builtin in complex type


 >>I would also like to see some more functions to make
 >>calculations with complex number more convenient
 >>e.g.
 >>c = 27
 >>c.pow(numerator = 1, denominator = 3,
 >>      mode = cmath.EULER, angle_mode = cmath.GRAD)
 >>-> ((3,0), (3,120), (3,240))
 >
 >
 > Wanting all roots is fairly specialized.  sqrt(4) is 2, not (2, -2).

it could be named .allroots(n) -> ((), (), ())
with n whole number

indeed .pow() is not a good name for it, since z**x
always yields one complex number

if x is 2.5 or alike, it could be aproximated with n/m and use
res = []
for i in z.allroots(m):
	res.append(i**n)

I am not sure, this is correct iterpretation of z**2.5
I will need to ask in math newsgroup :)
and approximation might not be good enough

 > Have you looked for complex math functions in numpy, numarray, scipy or
 > similar packages?  It is possible that a cmath2 module, written in 
Python,
 > could be useful.

I hope so
I will google for cmath2, I never heard about it
I am new to Numeric and numarray, I was playing with them and
ufunc-tionsand matplotlab, as for SciPy I couldnt find
any binary for 2.4 Python unfortunately :-/

Regards

--
Daniel



More information about the Python-list mailing list