# Mutable complex numbers [was Re: output formatting for classes]

Schüle Daniel uval at rz.uni-karlsruhe.de
Sun Mar 12 00:06:09 CET 2006

```Steven D'Aprano wrote:
> On Fri, 10 Mar 2006 02:19:10 +0100, Schüle Daniel wrote:
>
>
>>yeah, i miss some things in complex implementation
>>for example c=complex()
>>c.abs = 2**0.5
>>c.angle = pi/2
>>
>>should result in 1+1j :)
>
>
> Smiley noted, but consider:
>
> c = complex()
> => what is the value of c here?

default value is 0, for complex number that means
real = 0, imag = 0
is the same as
c.abs=0, c.angle=0

ok mathematically c.angle can be of arbitrary value
but defaulting it to zero is very handy
c = complex()
c.abs = 10
yields 10+0j

c=complex()
c.real = 2
c.imag = 2
c.abs = 50**0.5	# angle remains, length changed
yields 5+5j
c.angle = 0
yields 50**0.5 + 0j

> c.abs = 2**0.5
> => what is c's value now?

c.abs = 2**0.5
c.angle = 0

>
> c.angle = pi/2
> => now c has the value 1+1j
>
> Objects with indeterminate values are rarely a good idea.

IMHO it's perfectly consistent with
>>> int()
0
>>> long()
0L
>>> float()
0.0
>>>
complex()
>>> complex()
0j
>>>

but extending complex with default angle=0

> A better way would be for complex numbers to take a constructor that can
> take arguments in either Cartesian or polar form. So, hypothetically, the
> following would all be equivalent:
>
> 1+1j
> complex(1,1)
> complex(real=1, img=1)
> complex(len=2**0.5, theta=pi/2)

ack
but after the creation of complex number one will have to
do all the transformations in another coord. system manually

> Another alternative would be a function to construct polar form complex
> numbers. It could be a plain function or a static method:
>
> cmath.polar(2**0.5, pi/2) => 1+1j
> complex.polar(2**0.5, pi/2) => 1+1j

maybe adding

c=complex.from_polar((length,angle))
d=complex.to_polar(c)
d == (length, angle)
True

would be sufficient, but I would prefer the other version

Regards

```

More information about the Python-list mailing list