a = np.random.random((512,512,3)).astype(np.float32)
b = np.ones((512,512,1), dtype=np.int32)
c = a / b
c.dtype
>> dtype('float64')
Dividing a float32 matrix by a int32 matrix gives a float64 matrix. Currently I have to do
return c.astype(np.float32)
This is extra work for the CPU. Is there a way for me to avoid the final conversion and telling numpy to do the work in float32?
You will have to use the
outargument ofnp.divide().In numpy 1.6, you will be able to do
c = np.divide(a, b, dtype=np.float32)