[Numpy-discussion] differencing numarray arrays.

Mike Zingale zingale at ucolick.org
Tue Jul 13 12:54:02 CDT 2004


Hi, I am trying to efficiently compute a difference of two 2-d flux
arrays, as arises quite commonly in finite-difference/finite-volume
methods.  Ex:

a = arange(64)
a.shape = (8,8)

I want to do create a new array, b, of shape such that

b[i,j] = a[i,j] - a[i-1,j]

for 1 <= i < 8
    0 <= i < 8

I can obviously do this through loops, but this is quite slow.  In IDL,
which is often compared to numarray/python, this is simple to do with the
shift() function, but I cannot find an efficient way to do it with
numarray arrays.

I tried defining a list

i = range(8)
im1[1:9] = im1[1:9] - 1

and indexing with im1, but this does not work.

Any suggestions?  For large array, this simple differencing in python is
very expensive when using loops.

Thanks,

Mike

------------------------------------------------------------------------------
Michael Zingale
UCO/Lick Observatory
UCSC
Santa Cruz, CA 95064

phone:  (831) 459-5246
fax:    (831) 459-5265
e-mail: zingale at ucolick.org
web:    http://www.ucolick.org/~zingale

``Don't worry head, the computer will do our thinking now''  -- Homer





More information about the Numpy-discussion mailing list