Although I'm sure somewhere this issue is discussed in this (great) group, I didn't know the proper search words for it (although I tried).
I'm using python (2.6) scientifically mostly, and created a simple class to store time series (my 'Signal' class). I need this class to have a possibility to get multiplied by an array, but pre and post multiplication have different mathematical outcomes ( basically A* B != B*A ) . Post multiplication by an array works fine defining __mul__ in the Signal class, but pre multiplication does not. It keeps trying to multiply all elements separately instead to send this array to my __rmul__ function. How can I fix this without the need for a separate 'multiplysignal(A,B)' function? To make things easy I've made a small example: [code] import numpy as np class Signal(object): def __init__(self,data,dt): self.data=data self.dt=dt def Nch(self): return self.data.shape[0] def __mul__(self,other): print 'mul called! ',other if isinstance(other,type(np.array([1,2]))): #it's an array: use dot product: return Signal(np.dot(self.data,other),self.dt) if other.__class__.__name__=='Signal': # do something pass def __rmul__(self,other): print 'rmul called! ',other if isinstance(other,type(np.array([1,2]))): #it's an array: use dot product: return Signal(np.dot(other,self.data),self.dt) if other.__class__.__name__=='Signal': # do something pass mySignal=Signal(np.array([[1.,2],[4,5]]),1.) myArray=np.array([[1.,2.],[4.,3.]]) result_mul = mySignal*myArray result_rmul = myArray*mySignal #called 4 times for all members once! #result: #mul called! [[ 1. 2.] # [ 4. 3.]] #rmul called! 1.0 #rmul called! 2.0 #rmul called! 4.0 #rmul called! 3.0 [/code] -- http://mail.python.org/mailman/listinfo/python-list