Is it generally safe to provide the input array as the optional out argument to a ufunc in numpy, provided the type is correct? For example, I have verified that the following works:
>>> import numpy as np
>>> arr = np.array([1.2, 3.4, 4.5])
>>> np.floor(arr, arr)
array([ 1., 3., 4.])
The array type must be either compatible or identical with the output (which is a float for numpy.floor()
), or this happens:
>>> arr2 = np.array([1, 3, 4], dtype = np.uint8)
>>> np.floor(arr2, arr2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: ufunc 'floor' output (typecode 'e') could not be coerced to provided output parameter (typecode 'B') according to the casting rule ''same_kind''
So given that an array of proper type, is it generally safe to apply ufuncs in-place? Or is floor()
an exceptional case? The documentation does not make it clear, and neither do the following two threads that have tangential bearing on the question:
EDIT:
As a first order guess, I would assume it is often, but not always safe, based on the tutorial at http://docs.scipy.org/doc/numpy/user/c-info.ufunc-tutorial.html. There does not appear to be any restriction on using the output array as a temporary holder for intermediate results during the computation. While something like floor()
and ciel()
may not require temporary storage, more complex functions might. That being said, the entire existing library may be written with that in mind.
out
parameter innp.dot
in this way with 2D arrays can produce incorrect results. – Pskovadd(G, C, G)
as an optimization ofG = G + C
, in the Tip under "Math operations". I'd say it's safe. (On the other hand, calling ufuncs with input and output overlapping but not identical will cause problems.) – Fragmental