Template Class CSRGeneralMatrix

Nested Relationships

Nested Types

Class Documentation

template<class ValueType>
class CSRGeneralMatrix

The CSRGeneralMatrix class represents a sparse matrix stored in CSR-Format (i.e. compressed sparse row). If speed is important, use this CSRGeneralMatrix instead of the Matrix class.

Public Functions

inline CSRGeneralMatrix()

Default constructor

inline CSRGeneralMatrix(count dimension, ValueType zero = 0)

Constructs the CSRGeneralMatrix with size dimension x dimension.

Parameters:
  • dimension – Defines how many rows and columns this matrix has.

  • zero – The zero element (default = 0).

inline CSRGeneralMatrix(count nRows, count nCols, ValueType zero = 0)

Constructs the CSRGeneralMatrix with size nRows x nCols.

Parameters:
  • nRows – Number of rows.

  • nCols – Number of columns.

  • zero – The zero element (default = 0).

inline CSRGeneralMatrix(count dimension, const std::vector<Triplet> &triplets, ValueType zero = 0, bool isSorted = false)

Constructs the dimension x dimension Matrix from the elements at position positions with values @values.

Parameters:
  • dimension – Defines how many rows and columns this matrix has.

  • triplets – The nonzero elements.

  • zero – The zero element (default is 0).

  • isSorted – True, if the triplets are sorted per row. Default is false.

inline CSRGeneralMatrix(count nRows, count nCols, const std::vector<Triplet> &triplets, ValueType zero = 0, bool isSorted = false)

Constructs the nRows x nCols Matrix from the elements at position positions with values @values.

Parameters:
  • nRows – Defines how many rows this matrix has.

  • nCols – Defines how many columns this matrix has.

  • triplets – The nonzero elements.

  • zero – The zero element (default is 0).

  • isSorted – True, if the triplets are sorted per row. Default is false.

inline CSRGeneralMatrix(count nRows, count nCols, const std::vector<std::vector<index>> &columnIdx, const std::vector<std::vector<ValueType>> &values, ValueType zero = 0, bool isSorted = false)

Constructs the nRows x nCols Matrix from the elements stored in columnIdx and values. columnIdx and values store the colums and values by row.

Parameters:
  • nRows

  • nCols

  • columnIdx

  • values

  • zero – The zero element (default is 0).

  • isSorted – True if the column indices in columnIdx are sorted in every row.

inline CSRGeneralMatrix(count nRows, count nCols, const std::vector<index> &rowIdx, const std::vector<index> &columnIdx, const std::vector<ValueType> &nonZeros, ValueType zero = 0, bool isSorted = false)

Constructs the nRows x nCols Matrix from the elements at position positions with values @values.

Parameters:
  • nRows – Defines how many rows this matrix has.

  • nCols – Defines how many columns this matrix has.

  • rowIdx – The rowIdx vector of the CSR format.

  • columnIdx – The columnIdx vector of the CSR format.

  • nonZeros – The nonZero vector of the CSR format. Should be as long as the columnIdx vector.

  • zero – The zero element (default is 0).

  • isSorted – True, if the triplets are sorted per row. Default is false.

CSRGeneralMatrix(const CSRGeneralMatrix &other) = default

Default copy constructor

CSRGeneralMatrix(CSRGeneralMatrix &&other) noexcept = default

Default move constructor

~CSRGeneralMatrix() = default

Default destructor

CSRGeneralMatrix &operator=(CSRGeneralMatrix &&other) noexcept = default

Default move assignment operator

CSRGeneralMatrix &operator=(const CSRGeneralMatrix &other) = default

Default copy assignment operator

inline IndexProxy operator()(index i, index j)
inline bool operator==(const CSRGeneralMatrix &other) const

Compares this matrix to other and returns true if the shape and zero element are the same as well as all entries, otherwise returns false.

Parameters:

other

inline bool isApprox(const CSRGeneralMatrix &other, const double eps = 0.01) const

Compares this matrix to other and returns true if the shape and zero element are the same as well as all entries are the same (within the absolute error range of eps), otherwise returns false.

Parameters:
  • other

  • eps

inline bool operator!=(const CSRGeneralMatrix &other) const

Compares this matrix to other and returns false if the shape and zero element are the same as well as all entries, otherwise returns true.

Parameters:

other

inline count numberOfRows() const noexcept
Returns:

Number of rows.

inline count numberOfColumns() const noexcept
Returns:

Number of columns.

inline ValueType getZero() const noexcept

Returns the zero element of the matrix.

inline count nnzInRow(const index i) const
Parameters:

i – The row index.

Returns:

Number of non-zeros in row i.

inline count nnz() const noexcept
Returns:

Number of non-zeros in this matrix.

inline const ValueType &operator()(index i, index j) const
Returns:

Value at matrix position (i,j).

inline void setValue(index i, index j, ValueType value)

Set the matrix at position (i, j) to value.

Note

This operation can be linear in the number of non-zeros due to vector element movements

inline void sort()

Sorts the column indices in each row for faster access.

inline bool sorted() const noexcept
Returns:

True if the matrix is sorted, otherwise false.

inline Vector row(index i) const
Returns:

Row i of this matrix as vector.

inline Vector column(index j) const
Returns:

Column j of this matrix as vector.

inline Vector diagonal() const
Returns:

The main diagonal of this matrix.

inline CSRGeneralMatrix operator+(const CSRGeneralMatrix &other) const

Adds this matrix to other and returns the result.

Returns:

The sum of this matrix and other.

inline CSRGeneralMatrix &operator+=(const CSRGeneralMatrix &other)

Adds other to this matrix.

Returns:

Reference to this matrix.

inline CSRGeneralMatrix operator-(const CSRGeneralMatrix &other) const

Subtracts other from this matrix and returns the result.

Returns:

The difference of this matrix and other.

inline CSRGeneralMatrix &operator-=(const CSRGeneralMatrix &other)

Subtracts other from this matrix.

Returns:

Reference to this matrix.

inline CSRGeneralMatrix operator*(const ValueType &scalar) const

Multiplies this matrix with a scalar specified in scalar and returns the result.

Returns:

The result of multiplying this matrix with scalar.

inline CSRGeneralMatrix &operator*=(const ValueType &scalar)

Multiplies this matrix with a scalar specified in scalar.

Returns:

Reference to this matrix.

inline Vector operator*(const Vector &vector) const

Multiplies this matrix with vector and returns the result.

Returns:

The result of multiplying this matrix with vector.

inline CSRGeneralMatrix operator*(const CSRGeneralMatrix &other) const

Multiplies this matrix with other and returns the result in a new matrix.

Returns:

The result of multiplying this matrix with other.

inline CSRGeneralMatrix operator/(const ValueType &divisor) const

Divides this matrix by a divisor specified in divisor and returns the result in a new matrix.

Returns:

The result of dividing this matrix by divisor.

inline CSRGeneralMatrix &operator/=(const ValueType &divisor)

Divides this matrix by a divisor specified in divisor.

Returns:

Reference to this matrix.

inline CSRGeneralMatrix transpose() const

Transposes this matrix and returns it.

inline CSRGeneralMatrix extract(const std::vector<index> &rowIndices, const std::vector<index> &columnIndices) const

Extracts a matrix with rows and columns specified by rowIndices and columnIndices from this matrix. The order of rows and columns is equal to the order in rowIndices and columnIndices. It is also possible to specify a row or column more than once to get duplicates.

Parameters:
  • rowIndices

  • columnIndices

inline void assign(const std::vector<index> &rowIndices, const std::vector<index> &columnIndices, const CSRGeneralMatrix &source)

Assign the contents of the matrix source to this matrix at rows and columns specified by rowIndices and columnIndices. That is, entry (i,j) of source is assigned to entry (rowIndices[i], columnIndices[j]) of this matrix. Note that the dimensions of @rowIndices and columnIndices must coincide with the number of rows and columns of source.

Parameters:
  • rowIndices

  • columnIndices

  • source

template<typename F>
void apply(F unaryElementFunction)

Applies the unary function unaryElementFunction to each value in the matrix. Note that it must hold that the function applied to the zero element of this matrix returns the zero element.

Parameters:

unaryElementFunction

template<typename L>
inline void forNonZeroElementsInRow(index row, L handle) const

Iterate over all non-zero elements of row row in the matrix and call handler(index column, ValueType value)

template<typename L>
inline void parallelForNonZeroElementsInRow(index row, L handle) const

Iterate in parallel over all non-zero elements of row row in the matrix and call handler(index column, ValueType value)

template<typename L>
inline void forElementsInRow(index i, L handle) const

Iterate over all elements in row i in the matrix and call handle(index column, ValueType value)

template<typename L>
inline void parallelForElementsInRow(index row, L handle) const

Iterate in parallel over all elements (including zeros) of row row in the matrix and call handler(index column, double value)

template<typename L>
inline void forElementsInRowOrder(L handle) const

Iterate over all elements of the matrix in row order and call handler (lambda closure).

template<typename L>
inline void parallelForElementsInRowOrder(L handle) const

Iterate in parallel over all rows and call handler (lambda closure) on elements of the matrix.

template<typename L>
inline void forNonZeroElementsInRowOrder(L handle) const

Iterate over all non-zero elements of the matrix in row order and call handler (lambda closure).

template<typename L>
inline void parallelForNonZeroElementsInRowOrder(L handle) const

Iterate in parallel over all rows and call handler (lambda closure) on non-zero elements of the matrix.

template<typename L>
inline CSRGeneralMatrix<ValueType> binaryOperator(const CSRGeneralMatrix<ValueType> &A, const CSRGeneralMatrix<ValueType> &B, L binaryOp)

Public Static Functions

template<typename L>
static CSRGeneralMatrix binaryOperator(const CSRGeneralMatrix &A, const CSRGeneralMatrix &B, L binaryOp)

Computes A binaryOp B on the elements of matrix A and matrix B.

Note

A and B must have the same dimensions and must be sorted.

Parameters:
Returns:

A binaryOp B.

static inline CSRGeneralMatrix mTmMultiply(const CSRGeneralMatrix &A, const CSRGeneralMatrix &B)

Computes A^T * B.

Note

The number of rows of A must be equal to the number of rows of B.

Parameters:
  • A

  • B

Returns:

A^T * B.

static inline CSRGeneralMatrix mmTMultiply(const CSRGeneralMatrix &A, const CSRGeneralMatrix &B)

Computes A * B^T.

Note

The number of columns of A must be equal to the number of columns of B.

Parameters:
  • A

  • B

Returns:

A * B^T.

static inline Vector mTvMultiply(const CSRGeneralMatrix &matrix, const Vector &vector)

Computes matrix^T * vector.

Note

The number of rows of matrix must be equal to the dimension of vector.

Parameters:
  • matrix

  • vector

Returns:

matrix^T * vector.

static inline CSRGeneralMatrix adjacencyMatrix(const Graph &graph, ValueType zero = 0)

Compute the (weighted) adjacency matrix of the (weighted) Graph graph.

Parameters:

graph

static inline CSRGeneralMatrix diagonalMatrix(const Vector &diagonalElements, ValueType zero = 0)

Creates a diagonal matrix with dimension equal to the dimension of the Vector diagonalElements. The values on the diagonal are the ones stored in diagonalElements (i.e. D(i,i) = diagonalElements[i]).

Parameters:

diagonalElements

static inline CSRGeneralMatrix incidenceMatrix(const Graph &graph, ValueType zero = 0)

Returns the (weighted) incidence matrix of the (weighted) Graph graph.

Parameters:

graph

static inline CSRGeneralMatrix laplacianMatrix(const Graph &graph, ValueType zero = 0)

Compute the (weighted) Laplacian of the (weighted) Graph graph.

Parameters:

graph

static inline CSRGeneralMatrix normalizedLaplacianMatrix(const Graph &graph, ValueType zero = 0)

Returns the (weighted) normalized Laplacian matrix of the (weighted) Graph graph

Parameters:

graph