I suspect that the problem here is that NumPy cannot store Python long
values in its arrays. As soon as you try to do this, it switches the data type of the array to object
. Arithmetic operations on the array then become trickier because NumPy can no longer do the arithmetic itself.
>>> np.array(27**40)
array(1797010299914431210413179829509605039731475627537851106401L, dtype=object)
>>> np.array(27**40) * np.tan(1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for *: 'numpy.ndarray' and 'numpy.float64'
Oddly enough, swapping the order of the arguments can sometimes work:
>>> np.tan(1) * np.array(27**40)
2.7986777223711575e+57
In this second case, the type of the result is a Python float
, not a NumPy array.
The fix is to avoid creating long
values in NumPy arrays, and use float
s instead:
>>> np.array(27.0**40)
array(1.797010299914431e+57)
>>> np.array(27.0**40) * np.tan(1)
2.7986777223711575e+57
>>> np.multiply(np.square(np.add(np.divide(np.zeros(10),42),(-27.0)**40)),np.tan(1))
array([ 5.02925269e+114, 5.02925269e+114, 5.02925269e+114,
5.02925269e+114, 5.02925269e+114, 5.02925269e+114,
5.02925269e+114, 5.02925269e+114, 5.02925269e+114,
5.02925269e+114])
If you do get an error like this is the future, the first thing to do is to check the dtype
of the array being multiplied. Does it contain NumPy values or Python objects?