values
containing all the nonzero values.rows
of integers containing their corresponding row indices.cols
of integers containing their corresponding column indices.For the example at hand, reading the matrix row-by-row, we have:
values = [3.1, 4, 5, 7.4, 2, 6]
rows = [ 0, 0, 1, 1, 3, 3]
columns = [ 2, 4, 2, 4, 1, 3]
This expresses that entry
This format employs again three arrays to represent
values
containing all the nonzero values (length nnz),columns
of integers containing their column indices (length nnz),row_idx
of integers with the cumulative number of nonzero entries up to the row_idx[0] = 0
.In such a way, the quantity row_idx[i+1] - row_idx[i]
represents the number of nonzero elements in the
For the example at hand, we get:
values = [3.1, 4, 5, 7.4, 2, 6]
columns = [2, 4, 2, 4, 1, 3]
row_idx = [0, 2, 4, 4, 6]
main.cpp
file to test and demonstrate the correctness of the proposed implementation.Compile your code using the following compilation flags:
g++ -std=c++17 -Wall -Wpendantic main.cpp [other_files.cpp] -o sparse_matrix
Provide clear instructions on how to compile and run your code or, preferably, a working compilation script.
You are required to implement an abstract base class SparseMatrix
that provides a public interface to perform the following operations:
const double x = A(2, 3);
). If indices are out of bound, then throw an error.A(2, 3) = 5.7;
). If indices are out of bound, then throw an error. If indices are compatible with the matrix size but the entry has not been allocated yet, either print an error or (bonus) allocate it.std::vector<type>
or raw arrays to store data.operator()
.operator*
.const
and non-const
overloads in member functions for const-correctness.SparseMatrix
to implement classes for both COO and CSR storage schemes (e.g., SparseMatrixCOO
and SparseMatrixCSR
).int
or double
).The main.cpp
file should include tests to validate the correctness of your program. Here are some test ideas: