[Numpy-discussion] ndarray of complex-like data

Robert Kern robert.kern at gmail.com
Wed Feb 17 11:08:46 EST 2010


On Wed, Feb 17, 2010 at 02:43, Brecht Machiels
<brecht.machiels at esat.kuleuven.be> wrote:
> Robert Kern wrote:
>>>> 2) Subclass the ndarray to do what you want.
>>> I have subclassed ndarray, but I'm not sure how to continue from there.
>>> I was thinking of overriding __getitem__ and casting the complex to my
>>> complex subclass. Would that be the way to go? How would that work with
>>> slices?
>>
>> I strongly recommend simply implementing an arg() function that works
>> on both arrays and and complex objects. Then just use abs() and arg()
>> instead of trying to get instances of your class and using their
>> attributes.
>
> Hmm. I hadn't thought of that option. I guess this makes more sense as
> this can than also operate on arrays like you say.
>
> Still, it would be interesting to know whether it's possible to have an
> ndarray subclass return instances of my complex subclass, preferably
> without having to copy the complex data (which would occur if I were to
> implement this behaviour by overriding __getitem__ as described above).
> But I assume that would require a user-defined type written in C?

Well, when numpy indexes to get a scalar, the data is always copied
(apart from object arrays, of course). It's not something you can or
should try to avoid.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco



More information about the NumPy-Discussion mailing list