Class DenseMatrix

Class Documentation

class DenseMatrix

Represents a dense matrix. Use this matrix to run LU decompositions and LU solves. Note that most matrices are rather sparse s.t. CSRMatrix might be a better representation.

Public Functions

DenseMatrix()

Default constructor

DenseMatrix(count dimension, double zero = 0.0)

Constructs the DenseMatrix with size dimension x dimension.

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

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

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

Constructs the DenseMatrix with size nRows x nCols.

Parameters:
  • nRows – Number of rows.

  • nCols – Number of columns.

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

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

Constructs the dimension x dimension DenseMatrix 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).

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

Constructs the nRows x nCols DenseMatrix 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).

DenseMatrix(count nRows, count nCols, const std::vector<double> &entries, double zero = 0.0)

Constructs an instance of DenseMatrix given the number of rows (nRows) and the number of columns (nCols) and its values (entries).

Note

The size of the entries vector should be equal to nRows * nCols.

Parameters:
  • nRows – Number of rows.

  • nCols – Number of columns.

  • entries – Entries of the matrix.

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

~DenseMatrix() = default

Default destructor

DenseMatrix(const DenseMatrix &other) = default

Default copy constructor

DenseMatrix(DenseMatrix &&other) = default

Default move constructor

DenseMatrix &operator=(DenseMatrix &&other) = default

Default copy assignment operator

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

Default move assignment operator

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

Note

This function is linear in the number of columns of the matrix.

Parameters:

i – The row index.

Returns:

Number of non-zeros in row i.

count nnz() const

Note

This function takes nRows * nCols operations.

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.

DenseMatrix operator+(const DenseMatrix &other) const

Adds this matrix to other and returns the result.

Returns:

The sum of this matrix and other.

DenseMatrix &operator+=(const DenseMatrix &other)

Adds other to this matrix.

Returns:

Reference to this matrix.

DenseMatrix operator-(const DenseMatrix &other) const

Subtracts other from this matrix and returns the result.

Returns:

The difference of this matrix and other.

DenseMatrix &operator-=(const DenseMatrix &other)

Subtracts other from this matrix.

Returns:

Reference to this matrix.

DenseMatrix 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.

DenseMatrix &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.

DenseMatrix operator*(const DenseMatrix &other) const

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

Returns:

The result of multiplying this matrix with other.

DenseMatrix 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.

DenseMatrix &operator/=(double divisor)

Divides this matrix by a divisor specified in divisor.

Returns:

Reference to this matrix.

DenseMatrix transpose() const

Transposes this matrix and returns it.

DenseMatrix 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 DenseMatrix &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 forElementsInRow(index row, L handle) const

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

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

Iterate in parallel over all elements 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 forNonZeroElementsInRow(index row, L handle) const

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

Note

This is a DenseMatrix! Therefore this operation needs O(numberOfRows()) time regardless of the number of non-zeros actually present.

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, double value)

Note

This is a DenseMatrix! Therefore this operation needs O(numberOfRows()) sequential time regardless of the number of non-zeros actually present.

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

Note

This is a DenseMatrix! Therefore this operation needs O(numberOfRows() * numberOfColumns()) time regardless of the number of non-zeros actually present.

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.

Note

This is a DenseMatrix! Therefore this operation needs O(numberOfRows() * numberOfColumns()) sequential time regardless of the number of non-zeros actually present.

Public Static Functions

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

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

Parameters:

graph

static DenseMatrix 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 DenseMatrix incidenceMatrix(const Graph &graph, double zero = 0.0)

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

Parameters:

graph

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

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

Parameters:

graph

static void LUDecomposition(DenseMatrix &matrix)

Decomposes the given matrix into lower L and upper U matrix (in-place).

Parameters:

matrix – The matrix to decompose into LU.

static Vector LUSolve(const DenseMatrix &LU, const Vector &b)

Computes the solution vector x to the system LU * x = b where LU is a matrix decomposed into L and U.

Parameters:
  • LU – Matrix decomposed into lower L and upper U matrix.

  • b – Right-hand side.

Returns:

Solution vector x to the linear equation system LU * x = b.

template<typename L>
static inline DenseMatrix binaryOperator(const DenseMatrix &A, const DenseMatrix &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.

Parameters:
  • A

  • B

  • binaryOp – Function handling (double, double) -> double

Returns:

A binaryOp B.