[Numpy-discussion] ndarray of complex-like data
Robert Kern
robert.kern@gmail....
Tue Feb 16 10:02:17 CST 2010
On Tue, Feb 16, 2010 at 09:58, Brecht Machiels
<brecht.machiels@esat.kuleuven.be> wrote:
> Travis Oliphant wrote:
>> On Feb 16, 2010, at 5:00 AM, Brecht Machiels wrote:
>>> I have written a subclass of Python's complex type, which only adds a
>>> couple of properties that return values calculated from the real and
>>> imaginary parts (magnitude and angle, for example).
>>>
>>> Now I would like to store objects of this new type in an ndarray. As the
>>> new type doesn't store more information than a complex number, the
>>> ndarray's dtype can be 'complex'. I also assume that is is better for
>>> performance to use dtype=complex instead of dtype=object? Using
>>> dtype=complex also ensures that anything put into the array can be cast
>>> to a complex.
>>>
>>> However, I would like the array to return objects of my new type when I
>>> retrieve an item from the ndarray. I'm not sure how to do that. I would
>>> rather avoid having to define the new type in C.
>>
>> 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.
--
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