melmacianalf
Technical User
Hello,
I am trying to figure out an algorithm to add two sparse matrices stored according to CRS (compressed row storage) format.
For an introduction to CRS, please refer to
The 3 arrays needed to store the non-zero elements (according to the link above) are val(1:nnz), col_ind(1:nnz), and row_ptr(1:N+1). These can be stored as follows
Note that the above code is only to give an idea of the storage procedure. In the actual implementation, one dumps val and col_ind together in a file and row_ptr in a seperate file and reads them again.
I attach a f90 file where two matrices A and B are initialized for a predefined number of non-zero elements. I would appreciate if any one gives an algorithm to get a matrix C = A+B where the addition is done sparsely. Thank you.
I am trying to figure out an algorithm to add two sparse matrices stored according to CRS (compressed row storage) format.
For an introduction to CRS, please refer to
The 3 arrays needed to store the non-zero elements (according to the link above) are val(1:nnz), col_ind(1:nnz), and row_ptr(1:N+1). These can be stored as follows
Code:
!=== Matrix A ===
! A = [
! 1 0 0 2
! 0 3 1 3
! 0 1 5 0
! 2 3 0 7
!]
N = size(A,1)
nnz = 0
ipre = 0
do i = 1, N
do j = 1, N
if ( abs(A(i,j)) .gt. 5e-16 ) then
nnz = nnz + 1
col_ind_A(nnz) = j
val_A(nnz) = A(i,j)
if ( nnz .eq. 1) then
row_ptr_A(i) = nnz
ipre = i
else if ( i .ne. ipre) then
row_ptr_A(i) = nnz
ipre = i
end if
end if
end do
end do
row_ptr_A(N+1) = nnz + 1
Note that the above code is only to give an idea of the storage procedure. In the actual implementation, one dumps val and col_ind together in a file and row_ptr in a seperate file and reads them again.
I attach a f90 file where two matrices A and B are initialized for a predefined number of non-zero elements. I would appreciate if any one gives an algorithm to get a matrix C = A+B where the addition is done sparsely. Thank you.