Le mercredi 03 décembre 2008, Sébastien Barthélemy a écrit :
Hello, Hi Sebastien!
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) else: 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)
There is something I missed: what is htr? I guess htr.inv is the inv
function defined before the class.
Another point: it seems weird to me that, in the class' methods inv_v2
and inv_v3, you 'unref' the previous instance of HomogeneousMatrix and
link the 'self' label to a new instance... In inv_v1, you just modify
the coefficient of the Homogeneous Matrix with the coefficient of
htr.inv(self)
--
Fabrice Silva