Thursday 15 July 2010

Is there a Python slice notation equivalent for (i+1)%N? -


I have a Python nonpoint array that I am using for a simulation with toroidal boundary conditions

For example, when i = N-1 , i + 1 becomes 0 on the border.

I'm using a [(i + 1)% n, (j + 1)% n] to reach out to the nearest neighbors, so the index wraps around itself .

"post-text" itemprop = "text">

Python Take advantage of negative indexing of

  a [(i + 1) -n, (j + 1) -n]   

Using your modules is equivalent to your version:

  & gt; & Gt; & Gt; A = [1, 2, 3, 4, 5, 6, 7, 8, 9] & gt; & Gt; & Gt; In range (lane (a)): print (one [(i + 1)% lane (a)], a [i-1-len (a)]) 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 1 1 If the boundaries are smaller than the length of the axis of the array, then you can take one piece of the array with the right borders (which should not be too much in Numpy Use memory because it will have only one view in the original array) and then use the negative indexing method.   

No comments:

Post a Comment