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?
A *= Bsaves you only 3 characters overmult(A,B). Can you articulate why you need to have the shortest method possible?*=is standard among all major programming languages ; having to define amultfunction is not naturalAto end up as here?Ais an int array, and that we do inplace*=, I wantAto stay an int at the end.