[SciPy-user] temporary copies for in-place array modification?
Tue Jul 29 03:39:53 CDT 2008
On Jul 28 13:41, Steve Schmerler wrote:
> Hi all
> Say I do this:
> >>> a
> array([1, 2, 3, 4, 5, 6])
> >>> a[1:] = a[1:] - a[:-1]
> >>> a
> array([1, 1, 1, 1, 1, 1])
> >>> b = a[1:]
> >>> ...
> I.e. I want to compute the difference of a's elements and store them in place
> in b = a[1:] to work with that (b is only a view so that's ok, no copy).
> Are there any temp copies of `a` involved? I ask b/c `a` will be large.
Thanks for all answers.
Actually, `a` will be a 3D array which I'm filling with 2D arrays in
a = empty(x,y,z)
first_2d_array(a[:,0,:]) # in-place modification
for j in xrange(1, N+1):
and I get 2D arrays of differences.
a[:,1:,:] = a[:,1:,:] a[:,:-1,:]
b = [:,1:,:]
ATM a's size is in the order of several MB, so the cost of the temporary is
But for the future, since the temporary is in general unavoidable (or it's
safe to consider one), I'll use smaller temp 2D arrays in the loop and compute
the diffs right there (and b.shape = a.shape-1).
old = first_2d_array(...)
for j in xrange(0, N)
# view only, operate on `w`
w = b[:,j,:]
tmp = w.copy()
w -= old
old = tmp
More information about the SciPy-user