[Numpy-discussion] numpy subclass

Jacopo jacopo.pecci@gmail....
Thu Jul 23 15:03:52 CDT 2009


Pierre GM <pgmdevlist <at> gmail.com> writes:

> 
> 
> On Jul 23, 2009, at 1:13 PM, Jacopo wrote:
> >
> > In short, I want to create a class Dummy, inherited from np.ndarray,  
> > which
> > returns a plain array whenever an instance is sliced or viewed. I  
> > cannot figure
> > out how.
> 
> Easy enough for slices:
>      class Dummy(np.ndarray):
>          def __new__(cls, array):
>            obj=array.view(cls)
>            return obj
>          def __getslice__(self, i, j):
>              return np.ndarray.__getslice__(self, i, j).view(np.ndarray)
> 
> For views, the problem needs to be better described: why would you  
> want to get just a basic ndarray ?
> 


Pierre,

Dummy(np.ndarray) is designed to contain a 2d-array and 2 vectors, one for the
x-Axis and the other for the y-Axis. I use this coordinates to select and order
row and columns of the 2d-array. i.e if I sum two Dummys I may have to reorder
the column of one term to be consistent with the other. After the rearrangement
I want to perform some liner algebra with the matrices. So let's I want to
compute the transpose, I dont want to keep track also of the changes in the x
axis and y axis, at this stage I am happy to lose this information and not to
have the overhead. (e.g, if i compute the inverse the axis start to have no
meaning anymore.)

Thanks, Jacopo



More information about the NumPy-Discussion mailing list