As mentioned here and here, this doesn't work anymore in numpy 1.7+ :
import numpy
A = numpy.array([1, 2, 3, 4], dtype=numpy.int16)
B = numpy.array([0.5, 2.1, 3, 4], dtype=numpy.float64)
A *= B
A workaround is to do:
def mult(a,b):
numpy.multiply(a, b, out=a, casting="unsafe")
def add(a,b):
numpy.add(a, b, out=a, casting="unsafe")
mult(A,B)
but that's way too long to write for each matrix operation!
How can override the numpy *=
operator to do this by default?
Should I subclass something?
You can create a general function and pass the intended attribute to it:
Demo:
Note that if you want to override the result you can just assign the function's return to the first matrix.
You can use
np.set_numeric_ops
to override array arithmetic methods: