Python Packages for Complex Numbers and Matrix Math

Ignacio Vazquez-Abrams ignacio at openservices.net
Mon Aug 27 00:51:40 CEST 2001


On Sun, 26 Aug 2001, Maan Hamze wrote:

> Ignacio
> I am quite confused by the implementation of complex numbers:
> 1.  Try
> >>>  r = 4
> >>> r.imag
> Error:  int object has no attrib imag
> >>> r.real
> error:  int object has no attrib real
>
> But every real/int number is a complex number with a 0 imaginary part (r =
> 4+0j)

In math, yes. But not in Python. In Python there are four independent types of
numbers: integer, long, double, and complex. If you want r to be considered a
complex number then you'd have to write either 'r=4+0j' or 'r=complex(4,0)'.

> 2.  Also,
> >>> x = 4 + 7j
> >>> x.imag = 6
> error:  object has a read-only attrib
> >>> x.real = 7
> same error of read-olny attrib
>
> why?

Because. I could look into the source (or point you to the source), but the
answer would still simply be "Because".

> 3.  Finally,
> >>> t.imag = 6
> error:  name 't' is not defined    (you expect this to set t=6j without
> further ado)
> >>> t.real = 7
> error: name 't' is not defined    (you expect this to set t=7 automatically)
> Is not this lame :O
> In this case, one expects:
> >>> t.imag = 7
> >>> t
> 7j
> >>> t.real = 6
> >>> t
> 6+7j

See the two answers above and put them together ;)

> Maan

-- 
Ignacio Vazquez-Abrams  <ignacio at openservices.net>





More information about the Python-list mailing list