I have a problem with some notes. I need a numpy array to behave unusually, returning a slice in the form of data that I sliced, not copies. So here is an example of what I want to do:
Say we have a simple array:
a = array([1, 0, 0, 0])
I would like to update sequential entries in an array (moving from left to right) with the previous entry from the array using a syntax similar to this:
a[1:] = a[0:3]
This will result in the following result:
a = array([1, 1, 1, 1])
Or something like this:
a[1:] = 2*a[:3]
To illustrate further, I want the following behavior:
for i in range(len(a)): if i == 0 or i+1 == len(a): continue a[i+1] = a[i]
Also, I want numpy speed.
The default behavior of numpy is to take a copy of the slice, so I really get the following:
a = array([1, 1, 0, 0])
I already have this array as a subclass of ndarray, so I can make additional changes to it, if necessary, I just need the slice on the right side to be constantly updated, since it updates the fragment on the left side.
Do I dream or is this magic possible?
Update: all this because I'm trying to use the Gauss-Seidel iteration to solve linear algebra problems, more or less. This is a special case related to harmonic functions, I tried to avoid it, because it really is not needed and, probably, things get confused further, but it goes here.
The algorithm is as follows:
while not converged: for i in range(len(u[:,0])): for j in range(len(u[0,:])):
Right? But you can do this in two ways, Jacobi includes updating each element with its neighbors without taking into account the updates that you have already done before the while loop, to do this in cycles, you must copy the array and then update one array from the copied array. However, Gauss-Seidel uses the information that you have already updated for each of the i-1 and j-1 entries, so there is no need for copying, the loop should essentially “know”, since the array was reevaluated after updating each individual element, that is, each the time we call a record like u [i-1, j] or u [i, j-1], the information calculated in the previous loop will be there.
I want to replace this slow and ugly situation of a nested loop with one beautiful clean line of code using numpy slicing:
u[1:-1,1:-1] = 0.25(u[:-2,1:-1] + u[2:,1:-1] + u[1:-1,:-2] + u[1:-1,2:])
But the result is an iteration of Jacobi, because when you take the fragment: u [:, - 2,1: -1], you copy the data, so the slice does not know about the updates made. Now is numpy still looping? Its not parallel it is just a faster loop path that looks like a parallel operation in python. I want to use this numpy hacking behavior to return a pointer instead of a copy when I take a piece. Right? Then each time after numpy cycles this slice will be “updated” or really just copy everything that happened in the update. To do this, I need slices on both sides of the array to be pointers.
In any case, if there is a really really smart person who is amazing there, but I very much reconciled myself with the belief that the only answer is a loop in C.