In a project, I created a class, and I needed an operation between this new class and a real matrix, so I overloaded the __rmul__ function like this
<
You can define __numpy_ufunc__ function in your class. It works even without subclassing the np.ndarray. You can find the documentation here.
Here is an example based on your case:
class foo(object):
aarg = 0
def __init__(self):
self.aarg = 1
def __numpy_ufunc__(self, *args):
pass
def __rmul__(self,A):
print(A)
return 0
def __mul__(self,A):
print(A)
return 0
And if we try it,
A = [[i*j for i in np.arange(2) ] for j in np.arange(3)]
A = np.array(A)
R = foo()
C = A * R
Output:
[[0 0]
[0 1]
[0 2]]
It works!