[SciPy-user] numarray complex comparisons

eric jones eric at enthought.com
Wed Dec 11 18:37:16 EST 2002


> Tim Hochberg writes:
>   Pearu Peterson writes:
> > > +1 for .real and .imag attributes be available for non-complex
> > arrays. It
> > > would be a very useful addition also to current Numeric.
> >
> > I like this idea in abstract, and it would be easy to do for the
real
> > attrib, but I have a hard time seeing how it would work for the
> imaginary
> > attribute? For instance:
> >
> > >>> a = array([1, 2, 3+1j])
> > >>> a.imag[0] = 5
> > >>> a
> > array([ 1.+5.j,  2.+0.j,  3.+1.j])
> > >>> b = array([1,2,3])
> > >>> b.imag # OK, this could work -- it could dynamically create
> > an array of
> > zeros and return them
> > array([0., 0., 0.])  # Float or int? I dunno
> > >>> b.imag[0] = 5 # But what the heck is this supposed to? Change
> > b from int
> > to complex? Do nothing? Spooky!
> > ????
> >
> > So, I'm +0.5 on always providing '.real', but not '.imag' (-1
> > until shown it
> > can work reasonably).
> >
> I'd agree with Tim that having an .imag attribute for all types
> doesn't appear to make sense since there is no data that
> really corresponds to it. This could lead to unexpected behavior.
> By the way, there is a misleading aspect of .real in that if applied
> to an Int array, I would return a reference to the Int array. There
> may be some that think that .real would return a Float type.

+1 for .real and .imag and real() and imag() working on all arrays
(based on the same generic programming arguments I've used before).

Can't we make .imag readonly somehow?  The following is caught:

>>> from Numeric import *
>>> a = array((1,2,3),Int32)
>>> a[1] = 1+3j
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
TypeError: can't convert complex to int; use e.g. int(abs(z))

Numarray could definitely catch an assignment on non-complex arrays
through ___setattr__ or the new(2.2) properties mechanism or whatever
(right?):

>>> a = array((1,2,3),Int32)
>>> a.imag = 1

This following one requires a new array type that is readonly... right?

>>> a.imag[0] = 1

Is defining this sort of thing easy in numarray?  I'm guessing you just
have to derive from numarray.ndarray.NDArray and override the
__setitem__ and __setslice__ method to always throw an exception?

eric

p.s:

It bugs me that the above example (assigning complex into an int array
element) is caught in Numeric but this is not:

>>> a = array((1,2,3))
>>> a = array((1,2,3),Int32)
>>> a[1] = 3.2
>>> a
array([1, 3, 3])

This is a downcast also.  Either both should truncate or both should
throw a type exception.





More information about the SciPy-User mailing list