[Numpy-discussion] differencing numarray arrays.

Tim Hochberg tim.hochberg at cox.net
Tue Jul 13 12:59:00 EDT 2004


Mike Zingale wrote:

>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
>  
>
That's supposed to be a j in the second eq., right?

If I understand you right, what you want is:

b = a[1:] - a[:-1]

-tim

>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
>
>
>
>-------------------------------------------------------
>This SF.Net email sponsored by Black Hat Briefings & Training.
>Attend Black Hat Briefings & Training, Las Vegas July 24-29 - 
>digital self defense, top technical experts, no vendor pitches, 
>unmatched networking opportunities. Visit www.blackhat.com
>_______________________________________________
>Numpy-discussion mailing list
>Numpy-discussion at lists.sourceforge.net
>https://lists.sourceforge.net/lists/listinfo/numpy-discussion
>
>  
>






More information about the NumPy-Discussion mailing list