Numpy Gauss-Seidel算法,修改数组到位

Numpy Gauss-Seidel算法,修改数组到位,numpy,in-place,Numpy,In Place,我想实现Gauss-Seidel算法。 下面的代码不正确,因为U[i,j]的计算必须使用在当前迭代中已经计算的U[h,k] for k in xrange(s): U[1:nx, 1:ny] = (1 - 4*gamma)*U[1:nx, 1:ny] + gamma*( U[:nx-1, 1:ny] + U[2:, 1

我想实现Gauss-Seidel算法。 下面的代码不正确,因为U[i,j]的计算必须使用在当前迭代中已经计算的U[h,k]

for k in xrange(s):
    U[1:nx, 1:ny] = (1 - 4*gamma)*U[1:nx, 1:ny] + gamma*(
                                            U[:nx-1, 1:ny]
                                          + U[2:, 1:ny]
                                          + U[1:nx, :ny-1]
                                          + U[1:nx, 2:])
是否可以就地修改U[1:nx,1:ny]? 例如,假设我们有ufunc:
f=lambda u,a,b,c,d:(1-4*伽马)*u+伽马*(a+b+c+d)
那么,也许可以写这样的东西:

for k in xrange(1):
    f(U[1:nx, 1:ny], U[:nx-1, 1:ny], U[2:, 1:ny], U[1:nx, :ny-1], U[1:nx, 2:],
        out=U[1:nx, 1:ny])