long time reader, first time writer.
I searched around on google and stack overflow, but wasn't really able to find a general answer to this question.
I am getting an "unsupported operand type(s) for *: 'numpy.ndarray' and 'numpy.float64'" error in python 2.7.3 using numpy 1.6.2.
The error comes from multiplying a numpy array and a numpy float, but it doesn't happen every time.
For example:
x = np.tan(1) # numpy.float64
y = np.array([0,1,2,3]) # numpy.ndarray
np.multiply(x,y) # works no problem
Or
x = np.tan(np.abs(np.multiply(-31,41))) # numpy.float64
y = np.square(np.add(np.divide(np.zeros(100),42),(-27)**40)) # numpy.ndarray
np.multiply(x,y) # works no problem
Both work
Now for the problem children:
np.multiply(np.square(np.add(np.divide(np.zeros(100),42),-27)**40)),
np.tan(np.abs(np.multiply(-31,41))))
or, with x defined as above:
np.multiply(np.square(np.add(np.divide(np.zeros(100),42),(-27)**40)),x)
both produce the error: NotImplemented
I know the random functions and numbers seem odd, but conceptually this still should work, as it worked when both were set to variables individually.
Why does this happen? How can I fix it in a general sense?
Thanks so much! Jason
a
isnp.ndarray
andx
isnp.float64
, then bothx * a
anda * x[...]
work, but none ofa * x
,a + x
,a / x
ora - x
do. It is hard to understand why, if the__mul__
method ofa
cannot handle it,__rmul__
ofx
is not called, as it seems to know how to handle the case... – Geminationx
withx[...]
it works, but it is a nasty hack... – Gemination(-27.)**47
should fix your problem... You could go for arbitrary precision arithmetic, but there is no support in numpy, see this answer – Rill