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

Robert Kern robert.kern@gmail....
Mon Jul 28 11:47:58 CDT 2008


On Mon, Jul 28, 2008 at 11:48, Alan G Isaac <aisaac@american.edu> wrote:
> 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?

Yup.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
 -- Umberto Eco


More information about the SciPy-user mailing list