[Numpy-discussion] trouble subclassing ndarray

Sébastien Barthélemy barthelemy@crans....
Wed Dec 3 08:19:29 CST 2008


I'm trying to write a small library of differential geometry, and I
have some trouble subclassing ndarray.

I'd like an HomogeneousMatrix class that subclasse ndarray and
overloads some methods, such as inv().

Here is my first try, the inv() function and the inv_v1() method work
as expected, but the inv_v2() and inv_v3() methods do not change the
object at all. Can somebody explain me what is happening here ?

import numpy as np
def inv(H):
    inverse of an homogeneous matrix
    R = H[0:3,0:3]
    p = H[0:3,3:4]
    return np.vstack( (np.hstack((R.T,-np.dot(R.T,p))), [0,0,0,1]))

class HomogeneousMatrix(np.ndarray):
    def __new__(subtype, data=np.eye(4)):
        subarr = np.array(data)
        if htr.ishomogeneousmatrix(subarr):
            return subarr.view(subtype)
            raise ValueError
    def inv_v1(self):
        self[0:4,0:4] = htr.inv(self)
    def inv_v2(self):
        data = htr.inv(self)
        self = HomogeneousMatrix(data)
    def inv_v3(self):
        self = htr.inv(self)

Thank you !


More information about the Numpy-discussion mailing list