Search code examples
python-3.xnumpymultidimensional-arrayexpnumpy-ndarray

Perform numpy exp function in-place


As in title, I need to perform numpy.exp on a very large ndarray, let's say ar, and store the result in ar itself. Can this operation be performed in-place?


Solution

  • You can use the optional outargument of exp:

    a = np.array([3.4, 5])
    res = np.exp(a, a)
    print(res is a)
    print(a)
    

    Output:

    True
    [  29.96410005  148.4131591 ]
    

    exp(x[, out])

    Calculate the exponential of all elements in the input array.

    Returns

    out : ndarray Output array, element-wise exponential of x.

    Here all elements of a will be replaced by the result of exp. The return value res is the same as a. No new array is created