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

Anne Archibald peridot.faceted at gmail.com
Mon Jul 28 13:41:37 EDT 2008


2008/7/28 Alan G Isaac <aisaac at american.edu>:
> 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?


Maybe. The result is dependent on the memory layout of a.

Anne



More information about the SciPy-User mailing list