Compressed row sparse matrix class. More...
#include <crowmatrix.hpp>
Public Member Functions | |
CRowMatrix () | |
Default constructor. | |
CRowMatrix (int n, int m) | |
Constructor to make empty n x m matrix. | |
CRowMatrix (int n, int m, int nz, int *ptr, int *col, double *val) | |
Constructor to make n x m matrix from compressed row matrix data. | |
CRowMatrix (const CRowMatrix &mat) | |
Copy constructor. | |
CRowMatrix (const class CColMatrix &mat) | |
Constructor for conversion from compressed column matrix. | |
CRowMatrix (const class CoordMatrix &mat) | |
Constructor for conversion from coordinate matrix. | |
CRowMatrix (const class Matrix &mat) | |
Constructor for conversion from unknown matrix type. | |
~CRowMatrix () | |
Destructor. | |
int | columns (void) const |
Returns the number of columns in the matrix. | |
int | rows (void) const |
Returns the number of rows in the matrix. | |
void | size (int &n, int &m) const |
Returns the number of columns and number of columns in nn and mm. | |
int | nz_elements (void) const |
Returns the number of non-zero elements in the matrix. | |
int | capacity (void) const |
Returns the number of elements allocated for matrix. | |
void | resize (int n, int m) |
Resizes the matrix to size n x m. | |
void | merge (CRowMatrix &mat) |
Merges matrix mat into the matrix leaving mat empty. | |
void | clear (void) |
Clear non-zero matrix elements, set all elements to zero. | |
void | clear (int i, int j) |
Clear matrix element (i,j). | |
void | reserve (int size) |
Reserve memory for size matrix elements. | |
void | order_ascending (void) |
Order (sort) matrix data in ascending column index order within each row. | |
bool | check_ascending (void) |
Check if matrix data is in ascending column index order within each row. | |
void | debug_print (void) const |
Prints the values of all internal data to std::cout. | |
double | get (int i, int j) const |
Function to get a matrix element value at (i,j). | |
double & | set (int i, int j) |
Function to get a reference to matrix element value at (i,j). | |
void | set_row (int i, int N, const int *col, const double *val) |
Function to set matrix row elements. | |
void | construct_add (int i, int j, double val) |
Adds an element to matrix while constructing the whole matrix. | |
int & | ptr (int i) |
Returns a reference to the to the internal pointer index data ptr of the matrix. | |
int & | col (int i) |
Returns a reference to the to the internal column data of the matrix. | |
double & | val (int i) |
Returns a reference to the to the internal value data of the matrix. | |
const int & | ptr (int i) const |
Returns a const reference to the to the internal pointer index data ptr of the matrix. | |
const int & | col (int i) const |
Returns a const reference to the to the internal column data of the matrix. | |
const double & | val (int i) const |
Returns a const reference to the to the internal value data of the matrix. | |
void | set_nz (int nz) |
Set number of non-zero elements in the matrix. | |
CRowMatrix & | operator= (const CRowMatrix &mat) |
Assignment operator. | |
CRowMatrix & | operator= (const class CColMatrix &mat) |
Assignment operator for conversion from compressed column matrix. | |
CRowMatrix & | operator= (const class CoordMatrix &mat) |
Assignment operator for conversion from coordinate matrix. | |
CRowMatrix & | operator= (const class Matrix &mat) |
Assignment operator for conversion from unknown matrix type. | |
void | multiply_by_vector (Vector &x, const Vector &b) const |
void | lower_unit_solve (Vector &x, const Vector &b) const |
Solves A*x = b for lower unit diagonal matrix. | |
void | upper_diag_solve (Vector &x, const Vector &b) const |
Solves A*x = b for upper diagonal matrix. | |
Friends | |
class | CColMatrix |
class | CoordMatrix |
class | Vector |
Compressed row sparse matrix class.
The matrix is stored in the standard compressed row sparse matrix storage mode. In compressed row storage method all non-zero matrix elements are stored in array val in row-by-row order. The corresponding column indices are stored in another array col in the same order. The third array ptr contains "pointer" indices indicating start and end of each row in the first two arrays. The format itself does not require a certain ordering of elements, but some implementation might need/be faster on some ordering. The elements can be ordered into ascending index order with order_ascending(). Our example matrix
| 1 2 0 0 3| | 4 5 6 0 0| A = | 0 7 8 0 9| | 0 0 0 10 0| |11 0 0 0 12|
is represented in compressed row sparse matrix class as:
ptr[] = {0, 3, 6, 9, 10, 12} val[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} col[] = {0, 1, 4, 0, 1, 2, 1, 2, 4, 3, 0, 4}
CRowMatrix::CRowMatrix | ( | ) |
Default constructor.
CRowMatrix::CRowMatrix | ( | int | n, | |
int | m | |||
) |
Constructor to make empty n x m matrix.
CRowMatrix::CRowMatrix | ( | int | n, | |
int | m, | |||
int | nz, | |||
int * | ptr, | |||
int * | col, | |||
double * | val | |||
) |
Constructor to make n x m matrix from compressed row matrix data.
The constructed matrix uses ptr, col and val as its internal data. The arrays are not copied! For memory allocation compatibility reasons, arrays ptr, col and val should be allocated using malloc and/or realloc.
CRowMatrix::CRowMatrix | ( | const CRowMatrix & | mat | ) |
Copy constructor.
CRowMatrix::CRowMatrix | ( | const class CColMatrix & | mat | ) |
Constructor for conversion from compressed column matrix.
The compressed row matrix will be built in ascending column index order.
CRowMatrix::CRowMatrix | ( | const class CoordMatrix & | mat | ) |
Constructor for conversion from coordinate matrix.
CRowMatrix::CRowMatrix | ( | const class Matrix & | mat | ) |
Constructor for conversion from unknown matrix type.
CRowMatrix::~CRowMatrix | ( | ) |
Destructor.
int CRowMatrix::capacity | ( | void | ) | const [inline] |
Returns the number of elements allocated for matrix.
bool CRowMatrix::check_ascending | ( | void | ) |
Check if matrix data is in ascending column index order within each row.
void CRowMatrix::clear | ( | int | i, | |
int | j | |||
) | [inline] |
Clear matrix element (i,j).
Removes element (i,j) from the list of non-zero matrix elements.
void CRowMatrix::clear | ( | void | ) | [virtual] |
Clear non-zero matrix elements, set all elements to zero.
Implements Matrix.
const int& CRowMatrix::col | ( | int | i | ) | const [inline] |
Returns a const reference to the to the internal column data of the matrix.
int& CRowMatrix::col | ( | int | i | ) | [inline] |
Returns a reference to the to the internal column data of the matrix.
int CRowMatrix::columns | ( | void | ) | const [inline, virtual] |
Returns the number of columns in the matrix.
Implements Matrix.
void CRowMatrix::construct_add | ( | int | i, | |
int | j, | |||
double | val | |||
) |
Adds an element to matrix while constructing the whole matrix.
This is a special function for constructing the whole matrix row-by-row in ascending row order. The elements within a row can be defined in any order. With this function, every row of the matrix has to be defined. No checking of the definitions are made.
Using this function for defining a large matrix gains drasticly in speed. The function leaves all but the next row pointers unmodified. Therefore the matrix is unvalid and should not be accessed with other functions before all rows have been defined.
void CRowMatrix::debug_print | ( | void | ) | const |
Prints the values of all internal data to std::cout.
double CRowMatrix::get | ( | int | i, | |
int | j | |||
) | const [inline] |
Function to get a matrix element value at (i,j).
Range checking is done for i and j if SPM_RANGE_CHECK
is defined. Throws ErrorRange exception on range checking errors.
Reimplemented from Matrix.
void CRowMatrix::merge | ( | CRowMatrix & | mat | ) |
Merges matrix mat into the matrix leaving mat empty.
Copies contents of matrix mat into the matrix and sets contents of matrix mat to n = 0 and m = 0.
mat | Matrix to copy from. |
Implements Matrix.
int CRowMatrix::nz_elements | ( | void | ) | const [inline] |
Returns the number of non-zero elements in the matrix.
CRowMatrix& CRowMatrix::operator= | ( | const class Matrix & | mat | ) |
Assignment operator for conversion from unknown matrix type.
CRowMatrix& CRowMatrix::operator= | ( | const class CoordMatrix & | mat | ) |
Assignment operator for conversion from coordinate matrix.
CRowMatrix& CRowMatrix::operator= | ( | const class CColMatrix & | mat | ) |
Assignment operator for conversion from compressed column matrix.
The compressed row matrix will be built in ascending column index order.
CRowMatrix& CRowMatrix::operator= | ( | const CRowMatrix & | mat | ) |
Assignment operator.
void CRowMatrix::order_ascending | ( | void | ) |
Order (sort) matrix data in ascending column index order within each row.
const int& CRowMatrix::ptr | ( | int | i | ) | const [inline] |
Returns a const reference to the to the internal pointer index data ptr of the matrix.
int& CRowMatrix::ptr | ( | int | i | ) | [inline] |
Returns a reference to the to the internal pointer index data ptr of the matrix.
void CRowMatrix::reserve | ( | int | size | ) |
Reserve memory for size matrix elements.
void CRowMatrix::resize | ( | int | n, | |
int | m | |||
) | [virtual] |
int CRowMatrix::rows | ( | void | ) | const [inline, virtual] |
Returns the number of rows in the matrix.
Implements Matrix.
double & CRowMatrix::set | ( | int | i, | |
int | j | |||
) | [inline] |
Function to get a reference to matrix element value at (i,j).
This function can be used to set or modify matrix element value. See following examples:
A.set(0,0) = 1.2 A.set(0,1) *= 2 A.set(0,1) += 0.1
Note that a reference is actually a pointer to the memory location of the element and therefore unexpected things can happen if matrix is modified while using set, for example
A.set(0,0) = A.set(0,1) = A.set(0,2) = 5.0
does not do what you would expect it to do.
Range checking is done for i and j if SPM_RANGE_CHECK
is defined. Throws ErrorRange exception on range checking errors.
Reimplemented from Matrix.
void CRowMatrix::set_nz | ( | int | nz | ) |
Set number of non-zero elements in the matrix.
This function is to be used with low level access functions. The number of non-zero elements should be set to the same value as ptr[n]. Internal arrays are resized if nz is larger than the allocated size.
void CRowMatrix::set_row | ( | int | i, | |
int | N, | |||
const int * | col, | |||
const double * | val | |||
) |
Function to set matrix row elements.
The existing elements of the row i are replaced by N elements at column indices described in array col and with values described in array val. Range checking is always done for indexes i and j. Throws ErrorRange exception on range checking errors.
void CRowMatrix::size | ( | int & | n, | |
int & | m | |||
) | const [inline, virtual] |
Returns the number of columns and number of columns in nn and mm.
Implements Matrix.
const double& CRowMatrix::val | ( | int | i | ) | const [inline] |
Returns a const reference to the to the internal value data of the matrix.
double& CRowMatrix::val | ( | int | i | ) | [inline] |
Returns a reference to the to the internal value data of the matrix.
friend class CColMatrix [friend] |
friend class CoordMatrix [friend] |