I have been assigned to use the scipy sparse matrix solver, scipy.sparse.linalg.spsolve, to solve a sparse matrix-vector problem.
The problem is I do not understand how to set up the question properly. Once the matrix can be set up the data analysis and GPU acceleration of the code should be easy. I'm getting stuck at the first hurdle because of the notation.
Here is what I have attempted so far:
def matrix_A(N):
nelements = 1 / N
row_ind = []
col_ind = []
data = []
f = np.zeros(N, dtype=np.float64)
count = 0
for i in range(N):
for j in range(N):
if i==j:
row_ind[count] = col_ind[count] = 1
if i==0 or i==N:
if i==N:
row_ind[count] = col_ind[count] = 1
if i==j:
row_ind[count] = col_ind[count] = 2-(h**2 * k**2)
if j==i+1 or j==i-1:
row_ind[count] = col_ind[count] = -1
else:
row_ind[count] = col_ind[count] = 0
return coo_matrix((data, (row_ind, col_ind)), shape=(N**2, N**2)).tocsr(), f
Well, the first part says that
A[i,j] = 1wherei = j, which means your diagonal elements are (from what we know so far) all 1. In other words, you can start by creating the identity matrix.The next part pertains to
i = 0, N-1(the image saysN, but Python is 0-indexed and they probably mean the last value, which is indexN-1). With that, it saysA[i,j] = 2 - h^2*k^2ifi = j. Since we are referring to specificivalues, this can be interpreted as wheni = j = 0, N-1.It also says that
A[i,j] = -1ifj = i + 1orj = i - 1. Again, because we are dealing withi = 0, N-1, we only considerA[0,1]andA[N-1,N-2]. We ignorej = i - 1wheni = 0andj = i + 1wheni = N - 1because they would give values forjthat are out of range.After that, you can convert your matrix to a sparse matrix.