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

Contents

File kernel/src/utils/SiconosAlgebra/SimpleMatrix.hpp#

Go to the source code of this file

class SimpleMatrix : public SiconosMatrix
#include <SimpleMatrix.hpp>

Matrix (embedded various types of Boost matrices of double)

SimpleMatrix is used in the platform to store matrices (mathematical object) of double.

Possible types: Siconos::DENSE (default), TRIANGULAR, SYMMETRIC, SPARSE, BANDED, ZERO, Siconos::IDENTITY, Siconos::SPARSE_COORDINATE.

Todo:

: review resize function for Banded, Symetric and Triangular. Error in tests.

Public Functions

SimpleMatrix()

Default constructor.

SimpleMatrix(unsigned int row, unsigned int col, Siconos::UBLAS_TYPE typ = Siconos::DENSE, unsigned int upper = 1, unsigned int lower = 1)

constructor with the type and the dimension of the Boost matrix

Parameters:
  • row – number of rows.

  • col – number of columns.

  • typ – the type of matrix

  • upper – if Siconos::UBLAS_TYPE==SPARSE, number of non-zero terms, if Siconos::UBLAS_TYPE == BANDED, number of diags. under the main diagonal

  • lower – if Siconos::UBLAS_TYPE == BANDED, number of diags. over the main diagonal

SimpleMatrix(unsigned int row, unsigned int col, double inputValue, Siconos::UBLAS_TYPE typ = Siconos::DENSE, unsigned int upper = 1, unsigned int lower = 1)

constructor with the the dimensions of the Boost matrix, a default value and the type.

Parameters:
  • row – number of rows.

  • col – number of columns.

  • inputValue – double a, so that *this = [a a a …]

  • typ – the type of matrix

  • upper – if Siconos::UBLAS_TYPE==SPARSE, number of non-zero terms, if Siconos::UBLAS_TYPE == BANDED, number of diags. under the main diagonal

  • lower – if Siconos::UBLAS_TYPE == BANDED, number of diags. over the main diagonal

SimpleMatrix(const SimpleMatrix &smat)

copy constructor

Parameters:

smat – the matrix to copy

SimpleMatrix(const SimpleMatrix &A, const Index &coord)

copy constructor of a block given by the coord = [r0A r1A c0A c1A]

Parameters:
  • A – the matrix which contains the block to extract

  • coord – positions of the block to be extracted (row:start, row:end, col:start, col:end)

SimpleMatrix(const SiconosMatrix &m)

copy constructor

Parameters:

m – the matrix to copy

SimpleMatrix(const DenseMat &m)

constructor with a DenseMat matrix (see SiconosMatrix.h for details)

Parameters:

m – a DenseMat

SimpleMatrix(const TriangMat &m)

constructor with a TriangMat matrix (see SiconosMatrix.h for details)

Parameters:

m – a TriangMat

SimpleMatrix(const SymMat &m)

constructor with a SymMat matrix (see SiconosMatrix.h for details)

Parameters:

m – a SymMat

SimpleMatrix(const BandedMat &m)

constructor with a BandedMat matrix (see SiconosMatrix.h for details)

Parameters:

m – a BandedMat

SimpleMatrix(const SparseMat &m)

constructor with a SparseMat matrix (see SiconosMatrix.h for details)

Parameters:

m – a SparseMat

SimpleMatrix(const SparseCoordinateMat &m)

constructor with a SparseCoordinateMat matrix (see SiconosMatrix.h for details)

Parameters:

m – a SparseMat

SimpleMatrix(const ZeroMat &m)

constructor with a ZeroMat matrix (see SiconosMatrix.h for details)

Parameters:

m – a ZeroMat

SimpleMatrix(const IdentityMat &m)

constructor with a IdentityMat matrix (see SiconosMatrix.h for details)

Parameters:

m – a IdentityMat

SimpleMatrix(const std::string &file, bool ascii = true)

constructor with an input file

Parameters:
  • file – the input file path

  • ascii – a boolean to indicate if the file is in ascii

~SimpleMatrix()

destructor

virtual void updateNumericsMatrix() override#
inline virtual NumericsMatrix *numericsMatrix() const override#
inline virtual bool isPLUInversed() const override

determines if the matrix has been inversed

Returns:

true if the matrix is inversed

inline virtual bool isPLUFactorized() const override

determines if the matrix has been factorized

Returns:

true if the matrix is factorized

inline virtual bool isPLUFactorizedInPlace() const override

determines if the matrix has been factorized

Returns:

true if the matrix is factorized

inline virtual bool isCholeskyFactorized() const override

determines if the matrix has been factorized

Returns:

true if the matrix is factorized

inline bool isCholeskyFactorizedInPlace() const

determines if the matrix has been factorized

Returns:

true if the matrix is factorized

inline bool isQRFactorized() const

determines if the matrix has been factorized

Returns:

true if the matrix is factorized

inline virtual SP::VInt ipiv() const override#
virtual bool checkSymmetry(double tol) const override

determines if the matrix is symmetric up to a given tolerance

Returns:

true if the matrix is inversed

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

get DenseMat matrix

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

  • col – an unsigned int, position of the block - Useless for SimpleMatrix

Returns:

a DenseMat

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

get TriangMat matrix

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

  • col – an unsigned int, position of the block - Useless for SimpleMatrix

Returns:

a TriangMat

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

get SymMat matrix

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

  • col – an unsigned int, position of the block - Useless for SimpleMatrix

Returns:

a SymMat

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

get BandedMat matrix

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

  • col – an unsigned int, position of the block - Useless for SimpleMatrix

Returns:

a BandedMat

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

get SparseMat matrix

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

  • col – an unsigned int, position of the block - Useless for SimpleMatrix

Returns:

a SparseMat

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

get SparseCoordinateMat matrix

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

  • col – an unsigned int, position of the block - Useless for SimpleMatrix

Returns:

a SparseCoordinateMat

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

get ZeroMat matrix

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

  • col – an unsigned int, position of the block - Useless for SimpleMatrix

Returns:

a ZeroMat

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

get getIdentity matrix

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

  • col – an unsigned int, position of the block - Useless for SimpleMatrix

Returns:

an IdentityMat

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

get a pointer on DenseMat matrix

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

  • col – an unsigned int, position of the block - Useless for SimpleMatrix

Returns:

a DenseMat*

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

get a pointer on TriangMat matrix

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

  • col – an unsigned int, position of the block - Useless for SimpleMatrix

Returns:

a TriangMat*

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

get a pointer on SymMat matrix

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

  • col – an unsigned int, position of the block - Useless for SimpleMatrix

Returns:

a SymMat*

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

get a pointer on BandedMat matrix

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

  • col – an unsigned int, position of the block - Useless for SimpleMatrix

Returns:

a BandedMat*

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

get a pointer on SparseMat matrix

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

  • col – an unsigned int, position of the block - Useless for SimpleMatrix

Returns:

a SparseMat*

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

get a pointer on SparseCoordinateMat matrix

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

  • col – an unsigned int, position of the block - Useless for SimpleMatrix

Returns:

a SparseCoordinateMat*

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

get a pointer on ZeroMat matrix

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

  • col – an unsigned int, position of the block - Useless for SimpleMatrix

Returns:

a ZeroMat*

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

get a pointer on Identity matrix

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

  • col – an unsigned int, position of the block - Useless for SimpleMatrix

Returns:

an IdentityMat*

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

return the address of the array of double values of the matrix

Parameters:
  • row – position for the required block ->useless for SimpleMatrix

  • col – position for the required block ->useless for SimpleMatrix

Returns:

double* : the pointer on the double array

virtual void zero() override

sets all the values of the matrix to 0.0

virtual void randomize() override

Initialize the matrix with random values.

virtual void randomize_sym() override

Initialize a symmetric matrix with random values.

virtual void eye() override

set an identity matrix

unsigned copyData(double *data) const

copy the matrix data to the array given in parameter’ Works only for dense matrices !

Parameters:

data – array where the matrix is copied

Returns:

the size of the matrix

void assign(const SimpleMatrix &smat)#
virtual unsigned int size(unsigned int index) const override

get the number of rows or columns of the matrix

Parameters:

index – 0 for rows, 1 for columns

Returns:

the size

virtual void resize(unsigned int row, unsigned int col, unsigned int lower = 0, unsigned int upper = 0, bool preserve = true) override

resize the matrix with nbrow rows and nbcol columns The existing elements of the matrix are preseved when specified.

Parameters:
  • row – the new number of rows

  • col – the mew number of columns

  • lower – (only for Banded)

  • upper – (only for Banded)

  • preserve – preserve existing elements

virtual double normInf() const override

compute the infinite norm of the matrix

Returns:

a double

void normInfByColumn(SP::SiconosVector vIn) const

Compute the normInf for each column.

Parameters:

vIn – column

double det() const

compute the determinant of the matrix (use LU factorization)

Returns:

a double

virtual void display() const override

display data on standard output

virtual void displayExpert(bool brief = true) const override

display data on standard output

virtual std::string toString() const override

put data of the matrix into a std::string

Returns:

std::string

virtual double &operator()(unsigned int i, unsigned int j) override

get or set the element matrix[i,j]

Parameters:
  • i – an unsigned int

  • j – an unsigned int

Returns:

the element matrix[i,j]

virtual double operator()(unsigned int i, unsigned int j) const override

get or set the element matrix[i,j]

Parameters:
  • i – an unsigned int

  • j – an unsigned int

Returns:

the element matrix[i,j]

virtual double getValue(unsigned int i, unsigned int j) const override

return the element matrix[i,j]

Parameters:
  • i – an unsigned int

  • j – an unsigned int

Returns:

a double

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

set the element matrix[i,j]

Parameters:
  • i – an unsigned int

  • j – an unsigned int

  • value

void setBlock(unsigned int posRow, unsigned int posCol, const SiconosMatrix &m)

Copy of the content of a given matrix into the current object, at position (posRow, posCol).

Defined in SimpleMatrixSetGet.cpp.

Parameters:
  • posRow – row-index of the targeted block

  • posCol – col-index of the targeted block

  • m – source matrix to be copied. Can be a SimpleMatrix or a BlockMatrix.

virtual void getRow(unsigned int row, SiconosVector &vOut) const override

get row index of current matrix and save it into vOut

Parameters:
  • row – index row we want to get

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

virtual void getCol(unsigned int col, SiconosVector &vOut) const override

get column index of current matrix and save it into vOut

Parameters:
  • col – index column we want to get

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

virtual void setRow(unsigned int row, const SiconosVector &vIn) override

set line row of the current matrix with vector v

Parameters:
  • row – index row we want to set

  • vInSiconosVector containing the new row

virtual void setCol(unsigned int col, const SiconosVector &vIn) override

set column col of the current matrix with vector v

Parameters:
  • col – index column we want to set

  • vIn – a SiconosVector containing the new column

void getSubCol(unsigned int index, unsigned int pos, SP::SiconosVector vOut) const

get column number index of current matrix, starting from element at position pos and save it into vOut

Parameters:
  • index – index of required column

  • pos – index of the first required element in the column

  • vOut[out] a SP::SiconosVector

void getSubRow(unsigned int index, unsigned int pos, SP::SiconosVector vOut) const

get row number index of current matrix, starting from element at position pos and save it into vOut

Parameters:
  • index – index of the required row

  • pos – index of the first required element in the row

  • vOut[out] a SP::SiconosVector that will contain the sub row

void setSubCol(unsigned int index, unsigned int pos, SP::SiconosVector vIn)

set column number index of current matrix, starting from element at position pos, with vIn

Parameters:
  • index – index of required column

  • pos – index of the first required element in the column

  • vIn – a vector

void setSubRow(unsigned int index, unsigned int pos, SP::SiconosVector vIn)

set row number index of current matrix, starting from element at position pos, with vIn

Parameters:
  • index – index of required row

  • pos – index of the first required element in the row

  • vIn – a vector

void addBlock(unsigned int i, unsigned int j, const SiconosMatrix &m)

add the input matrix to the elements starting from position i (row) and j (col).

Parameters:
  • i – an unsigned int

  • j – an unsigned int

  • m – a SiconosMatrix

void subBlock(unsigned int i, unsigned int j, const SiconosMatrix &m)

subtract the input matrix to the elements starting from position i (row) and j (col).

Parameters:
  • i – an unsigned int

  • j – an unsigned int

  • m – a SiconosMatrix

virtual void trans() override

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

virtual void trans(const SiconosMatrix &mat) override

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

Parameters:

mat – the matrix to be transposed.

virtual SimpleMatrix &operator=(const SiconosMatrix &m) override

assignment

Parameters:

m – the matrix to be copied

Returns:

SimpleMatrix&

SimpleMatrix &operator=(const SimpleMatrix &m)

assignment

Parameters:

m – the matrix to be copied

Returns:

SimpleMatrix&

virtual SimpleMatrix &operator=(const DenseMat &m) override

assignment to a DenseMat

Parameters:

m – the matrix to be copied

Returns:

SimpleMatrix&

virtual SimpleMatrix &operator+=(const SiconosMatrix &m) override

operator +=

Parameters:

m – a matrix to add

Returns:

SimpleMatrix&

virtual SimpleMatrix &operator-=(const SiconosMatrix &m) override

operator -=

Parameters:

m – a matrix to subtract

Returns:

SimpleMatrix&

virtual void PLUFactorizationInPlace() override

computes an 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 Factorize() override

computes a factorization of a general M-by-N matrix

virtual void PLUInverseInPlace() override

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 PLUForwardBackwardInPlace(SiconosMatrix &B) override

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:

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

virtual void Solve(SiconosMatrix &B) override

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

Parameters:

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

virtual void PLUForwardBackwardInPlace(SiconosVector &B) override

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:

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

virtual void Solve(SiconosVector &B) override

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

Parameters:

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

void SolveByLeastSquares(SiconosMatrix &B)

solves a system of linear equations A * X = B (A=this) with a general N-by-N matrix A using the Least squares method

Parameters:

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

void SolveByLeastSquares(SiconosVector &B)

solves a system of linear equations A * X = B (A=this) with a general N-by-N matrix A using the Least squares method

Parameters:

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

virtual void resetLU() override

set to false all LU indicators.

Useful in case of assignment for example.

void resetCholesky()

set to false all Cholesky indicators.

Useful in case of assignment for example.

void resetQR()

set to false all QR indicators.

Useful in case of assignment for example.

virtual void resetFactorizationFlags() override

set to false all factorization indicators.

Useful in case of assignment for example.

ACCEPT_STD_VISITORS()

Visitors hook.

Protected Functions

ACCEPT_SERIALIZATION(SimpleMatrix)#

Protected Attributes

MATRIX_UBLAS_TYPE mat#

Union of The Boost Matrices : DenseMat, TriangMat, SymMat …

(See SiconosMatrix.h for more details on MATRIX_UBLAS_TYPE);

Private Functions

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

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:
void private_addprod(unsigned int startRow, unsigned int startCol, const SiconosVector &x, SiconosVector &res)#

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

Private Members

SP::VInt _ipiv#

VInt _ipiv; The pivot indices obtained from DGETRF (PLUFactorizationInPlace)

bool _isPLUFactorized = false#

True if the Matrix is PLU Factorized.

bool _isPLUFactorizedInPlace = false#

True if the Matrix is PLU Factorized in place.

bool _isCholeskyFactorized = false#

True if the Matrix is Cholesky Factorized.

bool _isCholeskyFactorizedInPlace = false#

True if the Matrix is Cholesky Factorized in place.

bool _isQRFactorized = false#

True if the Matrix has been QR Factorized in place.

bool _isPLUInversed = false#

True if the Matrix has been Inversed in Place.

SP::NumericsMatrix _numericsMatrix#

Numerics Matrix structure This matrix is used to perform computation using Numerics, for instance, the LU factorization of a sparse matrix.

It may contains copy or pointer on the SimpleMatrix.

Friends

friend std::ostream &operator<<(std::ostream &os, const SimpleMatrix &sm)#
friend const SimpleMatrix operator*(const SiconosMatrix&, double)#
friend SP::SimpleMatrix operator*(const SP::SimpleMatrix, const SP::SimpleMatrix)#
friend void operator+=(SP::SiconosMatrix, SP::SimpleMatrix)#
friend SimpleMatrix operator*(double, const SiconosMatrix&)

multiplication of a matrix by a double

Parameters:
Returns:

a SimpleMatrix

friend const SimpleMatrix operator/(const SiconosMatrix&, double)#
friend const SimpleMatrix operator+(const SiconosMatrix&, const SiconosMatrix&)

operator += add B to A

Parameters:
  • A[inout] a SP::SiconosMatrix

  • B – a SP::SiconosMatrix Addition of two matrices, C = A+B

  • A – a SiconosMatrix

  • B – a SiconosMatrix

Returns:

a SimpleMatrix C

friend SP::SimpleMatrix operator+(const SP::SimpleMatrix, const SP::SimpleMatrix)

Addition of two matrices, C = A+B.

Parameters:
  • A – a SP::SiconosMatrix

  • B – a SP::SiconosMatrix

Returns:

a SP::SimpleMatrix

friend void add(const SiconosMatrix&, const SiconosMatrix&, SiconosMatrix&)

Addition of two matrices C = A+B.

Parameters:
friend const SimpleMatrix operator-(const SiconosMatrix&, const SiconosMatrix&)

Subtraction of two matrices, C = A-B.

Parameters:
Returns:

a SimpleMatrix

friend void sub(const SiconosMatrix&, const SiconosMatrix&, SiconosMatrix&)

Subtraction of two matrices C = A-B.

Parameters:
friend bool operator==(const SiconosMatrix&, const SiconosMatrix&)

: A==B when (A-B).normInf()<tolerance

Parameters:
Returns:

a boolean

friend bool operator!=(const SiconosMatrix&, const SiconosMatrix&)

: A!=B when (A-B).normInf()>tolerance

Parameters:
Returns:

a boolean