下面的代码实际上在做什么?..特别是索引一......
据我了解,对于“A,b = create_matrix_1(4,4)”,矩阵 A 为 16*16;b 为 16*1
我在理解这个问题时遇到了一些麻烦。任何见解将不胜感激
def create_matrix_1(n,m,force=0.0):
"""
Create a matrix associated with a tension force to a membrane.
The domain is represented by a 2D grid of size n*m
"""
hx = 1.0/(n-1)
hy = 1.0/(m-1)
hx2 = 1.0/hx**2
hy2 = 1.0/hy**2
x = linspace(0,1,n)
y = linspace(0,1,m)
A = lil_matrix((n*m, n*m))
b = zeros(n*m)
for i in range(n):
for j in range(m):
index = i + j*n
if( i==0 or i==n-1 or j==0 or j==m-1): # Boundary nodes
b[index] = saddle_function(x[i],y[j])
A[index,index] = 1.0
else: # Interior Nodes
b[index] = force
A[index,index] = -2.0*(hx2 + hy2)
A[index,index+1] = hx2
A[index,index-1] = hx2
A[index,index+n] = hy2
A[index,index-n] = hy2
return A,b