All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Member Functions | Static Public Member Functions | List of all members
NetworKit::CSRMatrix Class Reference

The CSRMatrix class represents a sparse matrix stored in CSR-Format (i.e. More...

#include <CSRMatrix.h>

Public Member Functions

 CSRMatrix ()
 Default constructor. More...
 
 CSRMatrix (const count dimension, const double zero=0.0)
 Constructs the CSRMatrix with size dimension x dimension. More...
 
 CSRMatrix (const count nRows, const count nCols, const double zero=0.0)
 Constructs the CSRMatrix with size nRows x nCols. More...
 
 CSRMatrix (const count dimension, const std::vector< Triplet > &triplets, const double zero=0.0, bool isSorted=false)
 Constructs the dimension x dimension Matrix from the elements at position positions with values . More...
 
 CSRMatrix (const count nRows, const count nCols, const std::vector< Triplet > &triplets, const double zero=0.0, bool isSorted=false)
 Constructs the nRows x nCols Matrix from the elements at position positions with values . More...
 
 CSRMatrix (const count nRows, const count nCols, const std::vector< std::vector< index >> &columnIdx, const std::vector< std::vector< double >> &values, const double zero=0.0, bool isSorted=false)
 Constructs the nRows x nCols Matrix from the elements stored in columnIdx and values. More...
 
 CSRMatrix (const count nRows, const count nCols, const std::vector< index > &rowIdx, const std::vector< index > &columnIdx, const std::vector< double > &nonZeros, const double zero=0.0, bool isSorted=false)
 Constructs the nRows x nCols Matrix from the elements at position positions with values . More...
 
 CSRMatrix (const CSRMatrix &other)=default
 Default copy constructor. More...
 
 CSRMatrix (CSRMatrix &&other)=default
 Default move constructor. More...
 
virtual ~CSRMatrix ()=default
 Default destructor. More...
 
CSRMatrixoperator= (CSRMatrix &&other)=default
 Default move assignment operator. More...
 
CSRMatrixoperator= (const CSRMatrix &other)=default
 Default copy assignment operator. More...
 
bool operator== (const CSRMatrix &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. More...
 
bool operator!= (const CSRMatrix &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. More...
 
count numberOfRows () const
 
count numberOfColumns () const
 
double getZero () const
 Returns the zero element of the matrix. More...
 
count nnzInRow (const index i) const
 
count nnz () const
 
double operator() (const index i, const index j) const
 
void setValue (const index i, const index j, const double value)
 Set the matrix at position (i, j) to value. More...
 
void sort ()
 Sorts the column indices in each row for faster access. More...
 
bool sorted () const
 
Vector row (const index i) const
 
Vector column (const index j) const
 
Vector diagonal () const
 
CSRMatrix operator+ (const CSRMatrix &other) const
 Adds this matrix to other and returns the result. More...
 
CSRMatrixoperator+= (const CSRMatrix &other)
 Adds other to this matrix. More...
 
CSRMatrix operator- (const CSRMatrix &other) const
 Subtracts other from this matrix and returns the result. More...
 
CSRMatrixoperator-= (const CSRMatrix &other)
 Subtracts other from this matrix. More...
 
CSRMatrix operator* (const double &scalar) const
 Multiplies this matrix with a scalar specified in scalar and returns the result. More...
 
CSRMatrixoperator*= (const double &scalar)
 Multiplies this matrix with a scalar specified in scalar. More...
 
Vector operator* (const Vector &vector) const
 Multiplies this matrix with vector and returns the result. More...
 
CSRMatrix operator* (const CSRMatrix &other) const
 Multiplies this matrix with other and returns the result in a new matrix. More...
 
CSRMatrix operator/ (const double &divisor) const
 Divides this matrix by a divisor specified in divisor and returns the result in a new matrix. More...
 
CSRMatrixoperator/= (const double &divisor)
 Divides this matrix by a divisor specified in divisor. More...
 
CSRMatrix transpose () const
 Transposes this matrix and returns it. More...
 
CSRMatrix 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. More...
 
void assign (const std::vector< index > &rowIndices, const std::vector< index > &columnIndices, const CSRMatrix &source)
 Assign the contents of the matrix source to this matrix at rows and columns specified by rowIndices and columnIndices. More...
 
template<typename F >
void apply (const F unaryElementFunction)
 Applies the unary function unaryElementFunction to each value in the matrix. More...
 
template<typename L >
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) More...
 
template<typename L >
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) More...
 
template<typename L >
void forElementsInRow (index i, L handle) const
 Iterate over all elements in row i in the matrix and call handle(index column, double value) More...
 
template<typename L >
void forNonZeroElementsInRowOrder (L handle) const
 Iterate over all non-zero elements of the matrix in row order and call handler (lambda closure). More...
 
template<typename L >
void parallelForNonZeroElementsInRowOrder (L handle) const
 Iterate in parallel over all rows and call handler (lambda closure) on non-zero elements of the matrix. More...
 

Static Public Member Functions

template<typename L >
static CSRMatrix binaryOperator (const CSRMatrix &A, const CSRMatrix &B, L binaryOp)
 Computes A binaryOp B on the elements of matrix A and matrix B. More...
 
static CSRMatrix mTmMultiply (const CSRMatrix &A, const CSRMatrix &B)
 Computes A^T * B. More...
 
static CSRMatrix mmTMultiply (const CSRMatrix &A, const CSRMatrix &B)
 Computes A * B^T. More...
 
static Vector mTvMultiply (const CSRMatrix &matrix, const Vector &vector)
 Computes matrix^T * vector. More...
 
static CSRMatrix adjacencyMatrix (const Graph &graph, double zero=0.0)
 Compute the (weighted) adjacency matrix of the (weighted) Graph graph. More...
 
static CSRMatrix diagonalMatrix (const Vector &diagonalElements, double zero=0.0)
 Creates a diagonal matrix with dimension equal to the dimension of the Vector diagonalElements. More...
 
static CSRMatrix incidenceMatrix (const Graph &graph, double zero=0.0)
 Returns the (weighted) incidence matrix of the (weighted) Graph graph. More...
 
static CSRMatrix laplacianMatrix (const Graph &graph, double zero=0.0)
 Compute the (weighted) Laplacian of the (weighted) Graph graph. More...
 
static CSRMatrix normalizedLaplacianMatrix (const Graph &graph, double zero=0.0)
 Returns the (weighted) normalized Laplacian matrix of the (weighted) Graph graph. More...
 

Detailed Description

The CSRMatrix class represents a sparse matrix stored in CSR-Format (i.e.

compressed sparse row). If speed is important, use this CSRMatrix instead of the Matrix class.

Constructor & Destructor Documentation

NetworKit::CSRMatrix::CSRMatrix ( )

Default constructor.

NetworKit::CSRMatrix::CSRMatrix ( const count  dimension,
const double  zero = 0.0 
)

Constructs the CSRMatrix with size dimension x dimension.

Parameters
dimensionDefines how many rows and columns this matrix has.
zeroThe zero element (default = 0.0).
NetworKit::CSRMatrix::CSRMatrix ( const count  nRows,
const count  nCols,
const double  zero = 0.0 
)

Constructs the CSRMatrix with size nRows x nCols.

Parameters
nRowsNumber of rows.
nColsNumber of columns.
zeroThe zero element (default = 0.0).
NetworKit::CSRMatrix::CSRMatrix ( const count  dimension,
const std::vector< Triplet > &  triplets,
const double  zero = 0.0,
bool  isSorted = false 
)

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

Parameters
dimensionDefines how many rows and columns this matrix has.
tripletsThe nonzero elements.
zeroThe zero element (default is 0.0).
isSortedTrue, if the triplets are sorted per row. Default is false.
NetworKit::CSRMatrix::CSRMatrix ( const count  nRows,
const count  nCols,
const std::vector< Triplet > &  triplets,
const double  zero = 0.0,
bool  isSorted = false 
)

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

Parameters
nRowsDefines how many rows this matrix has.
nColsDefines how many columns this matrix has.
tripletsThe nonzero elements.
zeroThe zero element (default is 0.0).
isSortedTrue, if the triplets are sorted per row. Default is false.
NetworKit::CSRMatrix::CSRMatrix ( const count  nRows,
const count  nCols,
const std::vector< std::vector< index >> &  columnIdx,
const std::vector< std::vector< double >> &  values,
const double  zero = 0.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
zeroThe zero element (default is 0.0).
isSortedTrue if the column indices in columnIdx are sorted in every row.
NetworKit::CSRMatrix::CSRMatrix ( const count  nRows,
const count  nCols,
const std::vector< index > &  rowIdx,
const std::vector< index > &  columnIdx,
const std::vector< double > &  nonZeros,
const double  zero = 0.0,
bool  isSorted = false 
)

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

Parameters
nRowsDefines how many rows this matrix has.
nColsDefines how many columns this matrix has.
rowIdxThe rowIdx vector of the CSR format.
columnIdxThe columnIdx vector of the CSR format.
nonZerosThe nonZero vector of the CSR format. Should be as long as the columnIdx vector.
zeroThe zero element (default is 0.0).
isSortedTrue, if the triplets are sorted per row. Default is false.
NetworKit::CSRMatrix::CSRMatrix ( const CSRMatrix other)
default

Default copy constructor.

NetworKit::CSRMatrix::CSRMatrix ( CSRMatrix &&  other)
default

Default move constructor.

virtual NetworKit::CSRMatrix::~CSRMatrix ( )
virtualdefault

Default destructor.

Member Function Documentation

CSRMatrix NetworKit::CSRMatrix::adjacencyMatrix ( const Graph graph,
double  zero = 0.0 
)
static

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

Parameters
graph
template<typename F >
void NetworKit::CSRMatrix::apply ( const 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
void NetworKit::CSRMatrix::assign ( const std::vector< index > &  rowIndices,
const std::vector< index > &  columnIndices,
const CSRMatrix 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 and columnIndices must coincide with the number of rows and columns of source.

Parameters
rowIndices
columnIndices
source
template<typename L >
CSRMatrix NetworKit::CSRMatrix::binaryOperator ( const CSRMatrix A,
const CSRMatrix B,
binaryOp 
)
inlinestatic

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

Parameters
ASorted CSRMatrix.
BSorted CSRMatrix.
binaryOpFunction handling (double, double) -> double
Returns
A binaryOp B.
Note
A and B must have the same dimensions and must be sorted.
Vector NetworKit::CSRMatrix::column ( const index  j) const
Returns
Column j of this matrix as vector.
Vector NetworKit::CSRMatrix::diagonal ( ) const
Returns
The main diagonal of this matrix.
CSRMatrix NetworKit::CSRMatrix::diagonalMatrix ( const Vector diagonalElements,
double  zero = 0.0 
)
static

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
CSRMatrix NetworKit::CSRMatrix::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
template<typename L >
void NetworKit::CSRMatrix::forElementsInRow ( index  i,
handle 
) const
inline

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

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

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

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

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

double NetworKit::CSRMatrix::getZero ( ) const
inline

Returns the zero element of the matrix.

CSRMatrix NetworKit::CSRMatrix::incidenceMatrix ( const Graph graph,
double  zero = 0.0 
)
static

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

Parameters
graph
CSRMatrix NetworKit::CSRMatrix::laplacianMatrix ( const Graph graph,
double  zero = 0.0 
)
static

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

Parameters
graph
CSRMatrix NetworKit::CSRMatrix::mmTMultiply ( const CSRMatrix A,
const CSRMatrix B 
)
static

Computes A * B^T.

Parameters
A
B
Returns
A * B^T.
Note
The number of columns of A must be equal to the number of columns of B.
CSRMatrix NetworKit::CSRMatrix::mTmMultiply ( const CSRMatrix A,
const CSRMatrix B 
)
static

Computes A^T * B.

Parameters
A
B
Returns
A^T * B.
Note
The number of rows of A must be equal to the number of rows of B.
Vector NetworKit::CSRMatrix::mTvMultiply ( const CSRMatrix matrix,
const Vector vector 
)
static

Computes matrix^T * vector.

Parameters
matrix
vector
Returns
matrix^T * vector.
Note
The number of rows of matrix must be equal to the dimension of vector.
count NetworKit::CSRMatrix::nnz ( ) const
Returns
Number of non-zeros in this matrix.
count NetworKit::CSRMatrix::nnzInRow ( const index  i) const
Parameters
iThe row index.
Returns
Number of non-zeros in row i.
CSRMatrix NetworKit::CSRMatrix::normalizedLaplacianMatrix ( const Graph graph,
double  zero = 0.0 
)
static

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

Parameters
graph
count NetworKit::CSRMatrix::numberOfColumns ( ) const
inline
Returns
Number of columns.
count NetworKit::CSRMatrix::numberOfRows ( ) const
inline
Returns
Number of rows.
bool NetworKit::CSRMatrix::operator!= ( const CSRMatrix other) const
inline

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
double NetworKit::CSRMatrix::operator() ( const index  i,
const index  j 
) const
Returns
Value at matrix position (i,j).
CSRMatrix NetworKit::CSRMatrix::operator* ( const 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.
Vector NetworKit::CSRMatrix::operator* ( const Vector vector) const

Multiplies this matrix with vector and returns the result.

Returns
The result of multiplying this matrix with vector.
CSRMatrix NetworKit::CSRMatrix::operator* ( const CSRMatrix other) const

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

Returns
The result of multiplying this matrix with other.
CSRMatrix & NetworKit::CSRMatrix::operator*= ( const double &  scalar)

Multiplies this matrix with a scalar specified in scalar.

Returns
Reference to this matrix.
CSRMatrix NetworKit::CSRMatrix::operator+ ( const CSRMatrix other) const

Adds this matrix to other and returns the result.

Returns
The sum of this matrix and other.
CSRMatrix & NetworKit::CSRMatrix::operator+= ( const CSRMatrix other)

Adds other to this matrix.

Returns
Reference to this matrix.
CSRMatrix NetworKit::CSRMatrix::operator- ( const CSRMatrix other) const

Subtracts other from this matrix and returns the result.

Returns
The difference of this matrix and other.
CSRMatrix & NetworKit::CSRMatrix::operator-= ( const CSRMatrix other)

Subtracts other from this matrix.

Returns
Reference to this matrix.
CSRMatrix NetworKit::CSRMatrix::operator/ ( const 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.
CSRMatrix & NetworKit::CSRMatrix::operator/= ( const double &  divisor)

Divides this matrix by a divisor specified in divisor.

Returns
Reference to this matrix.
CSRMatrix& NetworKit::CSRMatrix::operator= ( CSRMatrix &&  other)
default

Default move assignment operator.

CSRMatrix& NetworKit::CSRMatrix::operator= ( const CSRMatrix other)
default

Default copy assignment operator.

bool NetworKit::CSRMatrix::operator== ( const CSRMatrix other) const
inline

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
template<typename L >
void NetworKit::CSRMatrix::parallelForNonZeroElementsInRow ( index  row,
handle 
) const
inline

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

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

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

Vector NetworKit::CSRMatrix::row ( const index  i) const
Returns
Row i of this matrix as vector.
void NetworKit::CSRMatrix::setValue ( const index  i,
const index  j,
const double  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
void NetworKit::CSRMatrix::sort ( )

Sorts the column indices in each row for faster access.

bool NetworKit::CSRMatrix::sorted ( ) const
Returns
True if the matrix is sorted, otherwise false.
CSRMatrix NetworKit::CSRMatrix::transpose ( ) const

Transposes this matrix and returns it.


The documentation for this class was generated from the following files: