这个问题与Overriding other __rmul__ with your class’s __mul__中提出的问题很接近,但我认为这是一个更普遍的问题,只有数字数据.这也没有回答,我真的不想使用矩阵乘法@来进行此操作.因此,问题.
我有一个接受与标量和数值数组相乘的对象.像往常一样,左乘法工作正常,因为它是使用myobj()方法,但在右乘法中,NumPy使用广播规则并使用dtype = object给出元素结果.
这也具有无法检查阵列大小是否兼容的副作用.
因此,问题是
Is there a way to force numpy array to look for the __rmul__() of the other object instead of broadcasting and performing elementwise __mul__()?
在我的特定情况下,对象是MIMO(多输入,多输出)传递函数矩阵(或者如果你愿意,则是滤波器系数矩阵),因此矩阵乘法在线性系统的加法和乘法方面具有特殊含义.因此,在每个条目中都有SISO系统.
import numpy as np
class myobj():
def __init__(self):
pass
def __mul__(self, other):
if isinstance(other, type(np.array([0.]))):
if other.size == 1:
print('Scalar multiplication')
else:
print('Multiplication of arrays')
def __rmul__(self, other):
if isinstance(other, type(np.array([0.]))):