[SciPy-user] temporary copies for in-place array modification?

Alan G Isaac aisaac@american....
Mon Jul 28 11:48:39 CDT 2008


On Mon, 28 Jul 2008, Robert Kern apparently wrote:
> Unfortunately, a temporary is unavoidable. If you modify `a` in-place, 
> you will mess up the computation. For example, we could try using the 
> third argument to the subtract() ufunc to place the results back into 
> a[1:]:
> In [1]: import numpy 
> In [2]: a = numpy.arange(1,7) 
> In [3]: numpy.subtract(a[1:], a[:-1], a[1:])
> Out[3]: array([1, 2, 2, 3, 3])


But he could work from the other end::

    >>> np.subtract(a[1:],a[:-1],a[:-1])
    array([1, 1, 1, 1, 1])

As long as he can then use a[:-1] instead of a[1:].
Right?

Alan





More information about the SciPy-user mailing list