Subclassification of numpy scalar types

I'm trying to subclass numpy.complex64 to use the way numpy stores data (the adjacent, alternating real and imaginary parts), but uses my own routines __add__ , __sub__ , ....

My problem is that when I create numpy.ndarray by setting dtype=mysubclass , I get a numpy.ndarray with dtype='numpy.complex64' instead, which leads to the fact that numpy does not use my own functions for additions, subtractions, etc.

Example:

 import numpy as np class mysubclass(np.complex64): pass a = mysubclass(1+1j) A = np.empty(2, dtype=mysubclass) print type(a) print repr(A) 

Conclusion:

 <class '__main__.mysubclass'> array([ -2.07782988e-20 +4.58546896e-41j, -2.07782988e-20 +4.58546896e-41j], dtype=complex64)' 

Does anyone know how to do this?

Thanks in advance - Soren

+6
source share
2 answers

A system like NumPy is only intended to be extended from C via the PyArray_RegisterDataType function. It may be possible to access this function from Python using ctypes, but I would not recommend it; it’s better to write an extension in C or Cython, or a subclass of ndarray , as @seberg describes.

Here is a dtype example in the NumPy source tree: newdtype_example / floatint.c . If you are in Pyrex, reference.pyx in the pytables source might be worth a look.

+3
source

Note that scalars and arrays are very different from numpy. np.complex64 (this is a 32-bit float, just to be noted, not double precision). You cannot change such an array, you will need to subclass the array and then override it __add__ and __sub__ .

If that’s all you want to do, it should just work in reverse order http://docs.scipy.org/doc/numpy/user/basics.subclassing.html , since subclassing the array is not so simple.

However, if you want to use this type also as a scalar. For example, you want to index scalars, this is getting harder, at least for the time being. You can get a little more by specifying __array_wrap__ to convert scalars to your own scalar type for some reduction functions, so that indexing works in all cases, it seems to me that you can define your own __getitem__ at the moment.

In all cases, with this approach, you are still using a complex data type, and all functions that are not explicitly overridden will still behave the same. @ecatmur mentioned that you can create new data types from the C side if this is really what you want.

+3
source

All Articles