Starting from:
$35

$29

Assignment 7 Solution


    1. Coordinate list format (COO format) for sparse matrices stores a sequence of nonzero ele-ments as triples (row number, column number, value of the element). Two more compact representations of sparse matrices are known as Compressed Sparse Row (CSR) and Com-pressed Sparse Column (CSC). The first, CSR, eliminates repeated row numbers of the COO format, the second, CSC, eliminates the same column numbers in the COO format. Both use three one-dimensional arrays, R, C and V . For a matrix A:


V1
0
0
V2

0
V3
0
V4


V5
0
V6
V7








0
0
0
V8

its CSR representation is:





row index R:

[2,4,7,8]


column index C:
[1,4,2,4,1,3,4,4]
value array V :

[V1, V2, V3, V4, V5, V6, V7, V8]

Consecutive elements of R show the number of nonzero elements in the first row of A, in the first two rows of A, and so on. The column index C and the value array V store the column index (in A) and the value of consecutive nonzero elements, in the row order.

The CSC representation is similar, and for the above matrix A is:

column index C:
[2,3,4,8]
row index R:
[1,3,2,3,1,2,3,4]
value array V :
[V1, V5, V3, V6, V2, V4, V7, V8]

Write a Fortran subroutine CSRorCSC (R,C,V,n,nz) which converts the CSR representation of a sparse matrix to its CSC representation.

Write also a Fortran program which enters a sparse matrix (in the COO format) and creates its CSR representation, invokes the CSRorCSC subroutine and outputs the result.

Hint: A straightforward approach first expands the CSR format to the COO format, reorders the elements and compresses the column index to its CSC representation (stored in arrays R,
    • and V ).

Note: The CSRorCSC subroutine can also be used for the conversion of the CSC format to its CSR equivalent.

    2. Write an integer Fortran function SparseAdd (R1,C1,V1,R2,C2,V2,R3,C3,V3,n,nz1,nz2,n3) which creates sparse matrix C in its CSR representation (R3, C3 and V 3) by adding two sparse matrices A and B represented as R1, C1, V 1 with NZ1 elements and R2, C2, V 2 with NZ2 elements. The function returns the number of nonzero elements in C. The length of C, N3, cannot be exceeded; if C is too small for the result, -1 should be returned.

Write also a Fortran program which creates sparse matrices A and B, invokes SparseAdd and outputs the result.

More products