File kernel/src/utils/SiconosAlgebra/SiconosMatrix.hpp

Go to the source code of this file

Interface for matrices handling.

Typedefs

typedef std::vector<int> VInt

A STL vector of int.

union MATRIX_UBLAS_TYPE
#include <SiconosMatrix.hpp>

Union of DenseMat pointer, TriangMat pointer BandedMat, SparseMat, SymMat, Zero and Identity mat pointers.

Public Members

BandedMat *Banded
DenseMat *Dense
IdentityMat *Identity
SparseMat *Sparse
SparseCoordinateMat *SparseCoordinate
SymMat *Sym
TriangMat *Triang
ZeroMat *Zero
class SiconosMatrix
#include <SiconosMatrix.hpp>

Abstract class to provide interface for matrices handling.

Matrices can be either block or Simple. See Derived classes for details.

In Siconos, a “matrix” can be either a SimpleMatrix or a BlockMatrix, ie a container of several pointers to SiconosMatrix

You can find an overview on how to build and use vectors and matrices in siconos users guide .

Subclassed by BlockMatrix, SimpleMatrix

Public Functions

virtual ~SiconosMatrix()

Destructor.

virtual BandedMat *banded(unsigned int row = 0, unsigned int col = 0) const = 0

get a pointer on BandedMat matrix

Return

a BandedMat*

Parameters
  • row: an unsigned int, position of the block (row) - Useless for SimpleMatrix

  • col: an unsigned int, position of the block (column) - Useless for SimpleMatrix

virtual SP::SiconosMatrix block(unsigned int row = 0, unsigned int col = 0)

get block at position row-col if BlockMatrix, else if SimpleMatrix return this

Return

SP::SiconosMatrix

Parameters
  • row: unsigned int row

  • col: unsigned int col

virtual SPC::SiconosMatrix block(unsigned int row = 0, unsigned int col = 0) const

get block at position row-col if BlockMatrix, else if SimpleMatrix return this

Return

SPC::SiconosMatrix

Parameters
  • row: unsigned int row

  • col: unsigned int col

virtual DenseMat *dense(unsigned int row = 0, unsigned int col = 0) const = 0

get a pointer on DenseMat matrix

Return

a DenseMat*

Parameters
  • row: an unsigned int, position of the block (row) - Useless for SimpleMatrix

  • col: an unsigned int, position of the block (column) - Useless for SimpleMatrix

virtual void display() const = 0

display data on standard output

virtual void displayExpert(bool brief = true) const = 0

display data on standard output

virtual void eye() = 0

set an identity matrix

bool fillCSC(CSparseMatrix *csc, size_t row_off, size_t col_off, double tol = 1e-14)

Fill sparse matrix.

Return

true if function worked.

Parameters
  • csc: the compressed column sparse matrix

  • row_off:

  • col_off:

  • tol: the tolerance under which a number is considered as equal to zero

bool fillTriplet(CSparseMatrix *csc, size_t row_off, size_t col_off, double tol = 1e-14)

return the number of non-zero in the matrix

Return

the number of non-zeros

Parameters
  • csc: the compressed column sparse matrix

  • row_off:

  • col_off:

  • tol: the tolerance to consider a number zero (not used if the matrix is sparse)

virtual double *getArray(unsigned int row = 0, unsigned int col = 0) const = 0

return the address of the array of double values of the matrix ( for block(i,j) if this is a block matrix)

Return

double* : the pointer on the double array

Parameters
  • row: position for the required block

  • col: position for the required block

virtual const BandedMat getBanded(unsigned int row = 0, unsigned int col = 0) const = 0

get BandedMat matrix

Return

a BandedMat

Parameters
  • row: an unsigned int, position of the block (row) - Useless for SimpleMatrix

  • col: an unsigned int, position of the block (column) - Useless for SimpleMatrix

virtual void getCol(unsigned int index, SiconosVector &vOut) const = 0

get column index of current matrix and save it into vOut

Parameters
  • index: column we want to get

  • [out] vOut: SiconosVector that will contain the desired column

virtual const DenseMat getDense(unsigned int row = 0, unsigned int col = 0) const = 0

get DenseMat matrix

Return

a DenseMat

Parameters
  • row: an unsigned int position of the block (row) - Useless for SimpleMatrix

  • col: an unsigned int position of the block (column) - Useless for SimpleMatrix

virtual const IdentityMat getIdentity(unsigned int row = 0, unsigned int col = 0) const = 0

get getIdentity matrix

Return

an IdentityMat

Parameters
  • row: an unsigned int, position of the block (row) - Useless for SimpleMatrix

  • col: an unsigned int, position of the block (column) - Useless for SimpleMatrix

virtual void getRow(unsigned int index, SiconosVector &vOut) const = 0

get row index of current matrix and save it into vOut

Parameters
  • index: row we want to get

  • [out] vOut: SiconosVector that will contain the desired row

virtual const SparseMat getSparse(unsigned int row = 0, unsigned int col = 0) const = 0

get SparseMat matrix

Return

a SparseMat

Parameters
  • row: an unsigned int, position of the block (row) - Useless for SimpleMatrix

  • col: an unsigned int, position of the block (column) - Useless for SimpleMatrix

virtual const SparseCoordinateMat getSparseCoordinate(unsigned int row = 0, unsigned int col = 0) const = 0

get SparseCoordinateMat matrix

Return

a SparseCoordinateMat

Parameters
  • row: an unsigned int, position of the block (row) - Useless for SimpleMatrix

  • col: an unsigned int, position of the block (column) - Useless for SimpleMatrix

virtual const SymMat getSym(unsigned int row = 0, unsigned int col = 0) const = 0

get SymMat matrix

Return

a SymMat

Parameters
  • row: an unsigned int, position of the block (row) - Useless for SimpleMatrix

  • col: an unsigned int, position of the block (column) - Useless for SimpleMatrix

virtual const TriangMat getTriang(unsigned int row = 0, unsigned int col = 0) const = 0

get TriangMat matrix

Return

a TriangMat

Parameters
  • row: an unsigned int, position of the block (row) - Useless for SimpleMatrix

  • col: an unsigned int, position of the block (column) - Useless for SimpleMatrix

virtual double getValue(unsigned int i, unsigned int j) const = 0

return the element matrix[i,j]

Return

a double

Parameters
  • i: an unsigned int i

  • j: an unsigned int j

virtual const ZeroMat getZero(unsigned int row = 0, unsigned int col = 0) const = 0

get ZeroMat matrix

Return

a ZeroMat

Parameters
  • row: an unsigned int, position of the block (row) - Useless for SimpleMatrix

  • col: an unsigned int, position of the block (column) - Useless for SimpleMatrix

virtual IdentityMat *identity(unsigned int row = 0, unsigned int col = 0) const = 0

get a pointer on Identity matrix

Return

an IdentityMat*

Parameters
  • row: an unsigned int, position of the block (row) - Useless for SimpleMatrix

  • col: an unsigned int, position of the block (column) - Useless for SimpleMatrix

virtual SP::VInt ipiv() const
bool isBlock(void) const

true if the matrix is block else false.

Return

a bool.

virtual bool isPLUFactorized() const

determines if the matrix has been PLU factorized in place

Return

true if the matrix is factorized

virtual bool isPLUInversed() const

determines if the matrix has been inversed in place

Return

true if the matrix is inversed

bool isQRFactorized() const

determines if the matrix has been QR factorized

Return

true if the matrix is factorized

virtual bool isSymmetric(double tol) const = 0

determines if the matrix is symmetric up to a given tolerance

Return

true if the matrix is inversed

virtual size_t nnz(double tol = 1e-14)

return the number of non-zero in the matrix

Return

the number of non-zeros

Parameters
  • tol: the tolerance to consider a number zero (not used if the matrix is sparse)

virtual double normInf() const = 0

compute the infinite norm of the matrix

Return

a double

unsigned int num() const

get the attribute num of current matrix

Return

an unsigned int.

virtual unsigned int numberOfBlocks(unsigned int i) const

get the number of block (i=0, row, i=1 col)

Return

an unsigned int. 1 as default for SimpleMatrix.

Parameters
  • i: unsigned int(i=0, row, i=1 col)

virtual double &operator()(unsigned int i, unsigned int j) = 0

get or set the element matrix[i,j]

Return

the element matrix[i,j]

Parameters
  • i: an unsigned int i

  • j: an unsigned int j

virtual double operator()(unsigned int i, unsigned int j) const = 0

get or set the element matrix[i,j]

Return

the element matrix[i,j]

Parameters
  • i: an unsigned int i

  • j: an unsigned int j

virtual SiconosMatrix &operator+=(const SiconosMatrix &m) = 0

operator +=

Return

SiconosMatrix&

Parameters
  • m: a matrix to add

virtual SiconosMatrix &operator-=(const SiconosMatrix &m) = 0

operator -=

Return

SiconosMatrix&

Parameters
  • m: a matrix to subtract

virtual SiconosMatrix &operator=(const SiconosMatrix &m) = 0

operator =

Return

SiconosMatrix&

Parameters
  • m: the matrix to be copied

virtual SiconosMatrix &operator=(const DenseMat &m) = 0

operator = to a DenseMat

Return

SiconosMatrix&

Parameters
  • m: the DenseMat to be copied

virtual void PLUFactorizationInPlace() = 0

computes a LU factorization of a general M-by-N matrix using partial pivoting with row interchanges.

The result is returned in this (InPlace). Based on Blas dgetrf function.

virtual void PLUForwardBackwardInPlace(SiconosMatrix &B) = 0

solves a system of linear equations A * X = B (A=this) with a general N-by-N matrix A using the LU factorization computed by PLUFactorizationInPlace.

Based on Blas dgetrs function.

Parameters
  • [inout] B: on input the RHS matrix b; on output the result x

virtual void PLUForwardBackwardInPlace(SiconosVector &B) = 0

solves a system of linear equations A * X = B (A=this) with a general N-by-N matrix A using the LU factorization computed by PLUFactorizationInPlace.

Based on Blas dgetrs function.

Parameters
  • [inout] B: on input the RHS matrix b; on output the result x

virtual void PLUInverseInPlace() = 0

compute inverse of this thanks to LU factorization with Partial pivoting.

This method inverts U and then computes inv(A) by solving the system inv(A)*L = inv(U) for inv(A). The result is returned in this (InPlace). Based on Blas dgetri function.

virtual void randomize() = 0

Initialize the matrix with random values.

virtual void randomize_sym() = 0

Initialize a symmetric matrix with random values.

virtual void resetLU()

set to false all LU indicators.

Useful in case of assignment for example.

virtual void resize(unsigned int nbrow, unsigned int nbcol, unsigned int lower = 0, unsigned int upper = 0, bool preserve = true) = 0

resize the matrix with nbrow rows and nbcol columns, upper and lower are only useful for BandedMatrix .

The existing elements of the matrix are preseved when specified.

Parameters
  • nbrow:

  • nbcol:

  • lowerupper: for banded matrices

  • preserve:

virtual void setCol(unsigned int index, const SiconosVector &vIn) = 0

set column col of the current matrix with vector v

Parameters
  • index: column we want to set

  • vIn: a SiconosVector containing the new column

virtual void setRow(unsigned int index, const SiconosVector &vIn) = 0

set line row of the current matrix with vector v

Parameters
  • index: row we want to set

  • vIn: SiconosVector containing the new row

virtual void setValue(unsigned int i, unsigned int j, double value) = 0

set the element matrix[i,j]

Parameters
  • i: an unsigned int i

  • j: an unsigned int j

  • value:

virtual unsigned int size(unsigned int index) const = 0

get the number of rows or columns of the matrix

Return

an int

Parameters
  • index: 0 for rows, 1 for columns

virtual SparseMat *sparse(unsigned int row = 0, unsigned int col = 0) const = 0

get a pointer on SparseMat matrix

Return

a SparseMat*

Parameters
  • row: an unsigned int, position of the block (row) - Useless for SimpleMatrix

  • col: an unsigned int, position of the block (column) - Useless for SimpleMatrix

virtual SparseCoordinateMat *sparseCoordinate(unsigned int row = 0, unsigned int col = 0) const = 0

get a pointer on SparseCoordinateMat matrix

Return

a SparseCoordinateMat*

Parameters
  • row: an unsigned int, position of the block (row) - Useless for SimpleMatrix

  • col: an unsigned int, position of the block (column) - Useless for SimpleMatrix

virtual SymMat *sym(unsigned int row = 0, unsigned int col = 0) const = 0

get a pointer on SymMat matrix

Return

a SymMat*

Parameters
  • row: an unsigned int, position of the block (row) - Useless for SimpleMatrix

  • col: an unsigned int, position of the block (column) - Useless for SimpleMatrix

virtual const SP::Index tabCol() const

reserved to BlockMatrix - get the index tab of columns

Return

a pointer to a standard vector of int

virtual const SP::Index tabRow() const

reserved to BlockMatrix - get the index tab for rows

Return

a pointer to a standard vector of int

virtual std::string toString() const = 0

put data of the matrix into a std::string

Return

std::string

virtual void trans() = 0

transpose in place: x->trans() is x = transpose of x.

virtual void trans(const SiconosMatrix &m) = 0

transpose a matrix: x->trans(m) is x = transpose of m.

Parameters
  • m: the matrix to be transposed.

virtual TriangMat *triang(unsigned int row = 0, unsigned int col = 0) const = 0

get a pointer on TriangMat matrix

Return

a TriangMat*

Parameters
  • row: an unsigned int, position of the block (row) - Useless for SimpleMatrix

  • col: an unsigned int, position of the block (column) - Useless for SimpleMatrix

VIRTUAL_ACCEPT_VISITORS(SiconosMatrix)

Visitors hook.

virtual void zero() = 0

sets all the values of the matrix to 0.0

virtual ZeroMat *zero_mat(unsigned int row = 0, unsigned int col = 0) const = 0

get a pointer on ZeroMat matrix

Return

a ZeroMat*

Parameters
  • row: an unsigned int, position of the block (row) - Useless for SimpleMatrix

  • col: an unsigned int, position of the block (column) - Useless for SimpleMatrix

Protected Functions

SiconosMatrix()

default constructor

SiconosMatrix(unsigned int type)

basic constructor

Parameters
  • type: unsigned int type-number of the vector

ACCEPT_SERIALIZATION(SiconosMatrix)

serialization hooks

void private_addprod(unsigned int startRow, unsigned int startCol, const SiconosVector &x, SiconosVector &res) const

computes res = subA*x +res, subA being a submatrix of A (rows from startRow to startRow+sizeY and columns between startCol and startCol+sizeX).

If x is a block vector, it call the present function for all blocks.

Parameters
  • A: a pointer to SiconosMatrix

  • startRow: an int, sub-block position

  • startCol: an int, sub-block position

  • x: a pointer to a SiconosVector

  • res: a DenseVect

void private_prod(unsigned int startRow, const SiconosVector &x, SiconosVector &y, bool init) const

computes y = subA*x (init =true) or += subA * x (init = false), subA being a submatrix of A (all columns, and rows between start and start+sizeY).

If x is a block vector, it call the present function for all blocks.

Parameters

Protected Attributes

unsigned int _num

A number to specify the type of the matrix: (block or ublas-type) 0-> BlockMatrix, 1 -> DenseMat, 2 -> TriangMat, 3 -> SymMat, 4->SparseMat, 5->BandedMat, 6->zeroMat, 7->IdentityMat.

Friends

SiconosMatrix &operator*=(SiconosMatrix &m, const double &s)

multiply the current matrix with a scalar

Return

SiconosMatrix&

Parameters
  • m: the matrix to operate on

  • s: the scalar

SiconosMatrix &operator/=(SiconosMatrix &m, const double &s)

divide the current matrix with a scalar

Return

SiconosMatrix&

Parameters
  • m: the matrix to operate on

  • s: the scalar

std::ostream &operator<<(std::ostream &os, const SiconosMatrix &sm)

send data of the matrix to an ostream

Return

The same output stream

Parameters

void prod(const SiconosMatrix &A, const SiconosVector &x, BlockVector &y, bool init)

prod(A, x, y, init) computes y = A*x or y += A*x if init = false

Parameters

void prod(const SiconosMatrix &A, const BlockVector &x, SiconosVector &y, bool init)