Python np.diag() 内存错误

Python Memory Error with np.diag()

我想实现以下方程(Horn-Schunck 方法的精确解):

我的代码:

def HS_exact(Ix, Iy, It, u, v, lamda):
  M = u.shape[0]
  N = u.shape[1]
  nabla = np.zeros((M,N))
  nabla = spnabla(M,N)
  transpose = np.transpose(nabla)
  u_0 = u
  v_0 = v
  Ix = Ix.flatten()
  Iy = Iy.flatten()
  diag_Ix_squared = np.diag(Ix*Ix)
  diag_Ix_Iy = np.diag(Ix*Iy)
  diag_Iy_squared = np.diag(Iy*Iy)
  tmp = lamda*diag_Ix_squared
  A11 = transpose*nabla + tmp 
  A12 = lamda*diag_Ix_Iy
  A21 = lamda*diag_Ix_Iy
  A22 = transpose*nabla + lamda*diag_Iy_squared

创建diag_Ix_squared时出现错误。我是 python 的新手,我不知道如何处理大数组。

现在我知道问题出在哪里了。我使用密集矩阵而不是稀疏矩阵。

 Ix_flat = Ix.flatten()
 Iy_flat = Iy.flatten()
 diag_Ix_squared = sp.spdiags(Ix_flat*Ix_flat, 0, M*N, M*N)

这解决了我的问题。谢谢你们的帮助。