根据评论中的建议,我将发布我为自己的问题找到的答案。 SciPy 的稀疏包中有几个矩阵类,列出了它们here http://docs.scipy.org/doc/scipy/reference/sparse.html。人们可以将稀疏矩阵从一类转换为另一类。因此,对于我需要做的事情,我选择将稀疏矩阵转换为 csr_matrix 类,只需通过
H = sp.csr_matrix(H)
然后我可以使用常规 NumPy 表示法将第一行中的元素设置为 0:
H[0,0] = 0
H[0,1] = 0
H[0,2] = 0
为了完整起见,我在下面发布了完整修改的代码片段。
#SciPy Sparse linear algebra takes care of sparse matrix computations
#http://docs.scipy.org/doc/scipy/reference/sparse.linalg.html
import scipy.sparse as sp
import scipy.sparse.linalg as la
import numpy as np
import matplotlib.pyplot as plt
#discretize x-axis
N = 1100
x = np.linspace(-100,100,N)
V = x * x / 2.
h = len(x)/(N)
hi2 = 1./(h**2)
#discretize Schroedinger Equation, i.e. build
#banded matrix from difference equation
C0 = np.ones(N)*30. + V
C1 = np.ones(N) * -16.
C2 = np.ones(N) * 1.
H = sp.spdiags([C2, C1, C0, C1, C2],[-2,-1,0,1,2], N, N)
H *= hi2 * (- 1./12.) * (- 1. / 2.)
H = sp.csr_matrix(H)
H[0,0] = 0
H[0,1] = 0
H[0,2] = 0
#check structure of H
plt.figure()
plt.spy(H)
plt.show()
EV = la.eigsh(H,return_eigenvectors = False)