Class DynamicMatrix

Class Documentation

class DynamicMatrix

The DynamicMatrix class represents a matrix that is optimized for sparse matrices and internally uses a graph data structure. DynamicMatrix should be used when changes to the structure of the matrix are frequent.

Public Functions

DynamicMatrix()

Default constructor

DynamicMatrix(count dimension, double zero = 0.0)

Constructs the Matrix with size dimension x dimension.

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

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

DynamicMatrix(count nRows, count nCols, double zero = 0.0)

Constructs the Matrix with size nRows x nCols.

Parameters:
  • nRows – Number of rows.

  • nCols – Number of columns.

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

DynamicMatrix(count dimension, const std::vector<Triplet> &triplets, double zero = 0.0)

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.0).

DynamicMatrix(count nRows, count nCols, const std::vector<Triplet> &triplets, double zero = 0.0)

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.0).

inline bool operator==(const DynamicMatrix &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 operator!=(const DynamicMatrix &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
Returns:

Number of rows.

inline count numberOfColumns() const
Returns:

Number of columns.

inline double getZero() const

Returns the zero element of the matrix.

count nnzInRow(index i) const
Parameters:

i – The row index.

Returns:

Number of non-zeros in row i.

count nnz() const
Returns:

Number of non-zeros in this matrix.

double operator()(index i, index j) const
Returns:

Value at matrix position (i,j).

void setValue(index i, index j, double value)

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

Vector row(index i) const
Returns:

Row i of this matrix as vector.

Vector column(index j) const
Returns:

Column j of this matrix as vector.

Vector diagonal() const
Returns:

The main diagonal of this matrix.

DynamicMatrix operator+(const DynamicMatrix &other) const

Adds this matrix to other and returns the result.

Returns:

The sum of this matrix and other.

DynamicMatrix &operator+=(const DynamicMatrix &other)

Adds other to this matrix.

Returns:

Reference to this matrix.

DynamicMatrix operator-(const DynamicMatrix &other) const

Subtracts other from this matrix and returns the result.

Returns:

The difference of this matrix and other.

DynamicMatrix &operator-=(const DynamicMatrix &other)

Subtracts other from this matrix.

Returns:

Reference to this matrix.

DynamicMatrix operator*(double scalar) const

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

Returns:

The result of multiplying this matrix with scalar.

DynamicMatrix &operator*=(double scalar)

Multiplies this matrix with a scalar specified in scalar.

Returns:

Reference to this matrix.

Vector operator*(const Vector &vector) const

Multiplies this matrix with vector and returns the result.

Returns:

The result of multiplying this matrix with vector.

DynamicMatrix operator*(const DynamicMatrix &other) const

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

Returns:

The result of multiplying this matrix with other.

DynamicMatrix operator/(double 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.

DynamicMatrix &operator/=(double divisor)

Divides this matrix by a divisor specified in divisor.

Returns:

Reference to this matrix.

DynamicMatrix transpose() const

Transposes this matrix and returns it.

DynamicMatrix 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

void assign(const std::vector<index> &rowIndices, const std::vector<index> &columnIndices, const DynamicMatrix &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 handle(index row, index column, double value)

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

Iterate in parallel over all non-zero elements of row row in the matrix and call handler(index column, double 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, double 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 (including zeros) 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 elements (including zeros) in row order and call handle (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 handle(index row, index column, double value).

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

Iterate in parallel over all rows and call handle(index row, index column, double value) on non-zero elements of the matrix.

Public Static Functions

static DynamicMatrix mTmMultiply(const DynamicMatrix &A, const DynamicMatrix &B)

Computes A^T * B.

Parameters:
  • A

  • B

static DynamicMatrix mmTMultiply(const DynamicMatrix &A, const DynamicMatrix &B)

Computes A * B^T.

Parameters:
  • A

  • B

static Vector mTvMultiply(const DynamicMatrix &matrix, const Vector &vector)

Computes matrix^T * vector

Parameters:
  • matrix

  • vector

static DynamicMatrix adjacencyMatrix(const Graph &graph, double zero = 0.0)

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

Parameters:

graph

static DynamicMatrix diagonalMatrix(const Vector &diagonalElements, double zero = 0.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 DynamicMatrix incidenceMatrix(const Graph &graph, double zero = 0.0)

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

Parameters:

graph

static DynamicMatrix laplacianMatrix(const Graph &graph, double zero = 0.0)

Returns the (weighted) Laplacian matrix of the (weighteD) Graph graph.

Parameters:

graph

static DynamicMatrix normalizedLaplacianMatrix(const Graph &graph, double zero = 0.0)

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

Parameters:

graph

Protected Attributes

Graph graph
count nRows
count nCols
double zero