trouble subclassing ndarray
Hello, 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) Thank you !  Sébastien
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 <silva@lma.cnrsmrs.fr> LMA UPR CNRS 7051  équipe S2M
2008/12/3 Fabrice Silva <silva@lma.cnrsmrs.fr>:
Le mercredi 03 décembre 2008, Sébastien Barthélemy a écrit :
Hello, Hi Sebastien!
Hello Fabrice
There is something I missed: what is htr? I guess htr.inv is the inv function defined before the class.
yes, I cutnpasted the function definition from the htr module and forgot to tell it, sorry Thank you
On Wed, Dec 3, 2008 at 9:19 AM, Sébastien Barthélemy <barthelemy@crans.org>wrote:
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)
self is a reference, so you're just overwriting it with references to new values in v2 and v3. The original object is unchanged. Only v1 changes self. You may want to use "self[:] = ....". Kevin
2008/12/3 Kevin Jacobs <jacobs@bioinformed.com> <bioinformed@gmail.com>:
On Wed, Dec 3, 2008 at 9:19 AM, Sébastien Barthélemy <barthelemy@crans.org> wrote:
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)
self is a reference, so you're just overwriting it with references to new values in v2 and v3. The original object is unchanged. Only v1 changes self. You may want to use "self[:] = ....".
okay, it seems obvious now. I definitely spent to much time with matlab. Thanks
participants (3)

Fabrice Silva

Kevin Jacobs <jacobs＠bioinformed.com>

Sébastien Barthélemy