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

Public Functions

SimpleMatrix()

computes res = a*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 double

• 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 DenseVectcomputes y = a*subA*x (init =true) or += a*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.

• a: a double

• A: a pointer to SiconosMatrix

• start: an int, sub-block position

• x: a pointer to a SiconosVector

• y: a pointer to a SiconosVector

• inita: boolcomputes y = subA*x (init =true) or += subA * x (init = false), subA being a submatrix of trans(A) (all columns, and rows between start and start+sizeY). If x is a block vector, it call the present function for all blocks.

• x: a pointer to a SiconosVector

• A: a pointer to SiconosMatrix

• start: an int, sub-block position

• y: a pointer to a SiconosVector

• init: a boolDefault constructor

SimpleMatrix(int i)
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

ACCEPT_STD_VISITORS()

Visitors hook.

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 assign(const SimpleMatrix &smat)
BandedMat *banded(unsigned int row = 0, unsigned int col = 0) const

get a pointer on BandedMat matrix

Return

a BandedMat*

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

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

unsigned copyData(double *data) const

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

Return

the size of the matrix

Parameters
• data: array where the matrix is copied

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

get a pointer on DenseMat matrix

Return

a DenseMat*

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

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

double det() const

compute the determinant of the matrix (use LU factorization)

Return

a double

void display() const

display data on standard output

void displayExpert(bool brief = true) const
void eye()

set an identity matrix

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

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

Return

double* : the pointer on the double array

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

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

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

get BandedMat matrix

Return

a BandedMat

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

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

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

get column index of current matrix and save it into vOut

Parameters
• col: index column we want to get

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

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

get DenseMat matrix

Return

a DenseMat

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

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

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

get getIdentity matrix

Return

an IdentityMat

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

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

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

get block at position row-col, (current matrix in SimpleMatrix case)

Return

a sub-matrix get block at position row-col, (current matrix in SimpleMatrix case)

Return

a sub-matrixget row index of current matrix and save it into vOut

Parameters
• row: row index

• col: column index

Parameters
• row: row index

• col: column index

Parameters
• row: index row we want to get

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

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

get SparseMat matrix

Return

a SparseMat

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

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

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

get SparseCoordinateMat matrix

Return

a SparseCoordinateMat

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

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

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

• [out] vOut: 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

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

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

get SymMat matrix

Return

a SymMat

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

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

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

get TriangMat matrix

Return

a TriangMat

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

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

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

return the element matrix[i,j]

Return

a double

Parameters
• i: an unsigned int

• j: an unsigned int

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

get ZeroMat matrix

Return

a ZeroMat

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

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

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

get a pointer on Identity matrix

Return

an IdentityMat*

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

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

SP::VInt ipiv() const
bool isPLUFactorized() const

determines if the matrix has been factorized

Return

true if the matrix is factorized

bool isPLUInversed() const

determines if the matrix has been inversed

Return

true if the matrix is inversed

bool isQRFactorized() const

determines if the matrix has been factorized

Return

true if the matrix is factorized

bool isSymmetric(double tol) const
double normInf() const

compute the infinite norm of the matrix

Return

a double

void normInfByColumn(SP::SiconosVector vIn) const

Compute the normInf for each column.

Parameters
• vIn: column

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

get or set the element matrix[i,j]

Return

the element matrix[i,j]

Parameters
• i: an unsigned int

• j: an unsigned int

Exceptions
• SiconosMatrixException:

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

get or set the element matrix[i,j]

Return

the element matrix[i,j]

Parameters
• i: an unsigned int

• j: an unsigned int

Exceptions
• SiconosMatrixException:

SimpleMatrix &operator+=(const SiconosMatrix &m)

operator +=

Return
Parameters
• m: a matrix to add

SimpleMatrix &operator-=(const SiconosMatrix &m)

operator -=

Return
Parameters
• m: a matrix to subtract

SimpleMatrix &operator=(const SiconosMatrix &m)

assignment

Return
Parameters
• m: the matrix to be copied

SimpleMatrix &operator=(const SimpleMatrix &m)

assignment

Return
Parameters
• m: the matrix to be copied

SimpleMatrix &operator=(const DenseMat &m)

assignment to a DenseMat

Return
Parameters
• m: the matrix to be copied

void PLUFactorizationInPlace()

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.

void PLUForwardBackwardInPlace(SiconosMatrix &B)

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

void PLUForwardBackwardInPlace(SiconosVector &B)

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

void PLUInverseInPlace()

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.

void randomize()

Initialize the matrix with random values.

void randomize_sym()

Initialize a symmetric matrix with random values.

void resetLU()

set to false all LU indicators.

Useful in case of assignment for example.

void resetQR()

set to false all QR indicators.

Useful in case of assignment for example.

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

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

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.

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

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 setRow(unsigned int row, const SiconosVector &vIn)

set line row of the current matrix with vector v

Parameters
• row: index row we want to set

• vIn: SiconosVector containing the new 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 setValue(unsigned int i, unsigned int j, double value)

set the element matrix[i,j]

Parameters
• i: an unsigned int

• j: an unsigned int

• value:

unsigned int size(unsigned int index) const

get the number of rows or columns of the matrix

Return

the size

Parameters
• index: 0 for rows, 1 for columns

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
• [inout] B: 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
• [inout] B: on input the RHS matrix b; on output the result x

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

get a pointer on SparseMat matrix

Return

a SparseMat*

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

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

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

get a pointer on SparseCoordinateMat matrix

Return

a SparseCoordinateMat*

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

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

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

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

get a pointer on SymMat matrix

Return

a SymMat*

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

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

std::string toString() const

put data of the matrix into a std::string

Return

std::string

void trans()

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

void trans(const SiconosMatrix &mat)

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

Parameters
• mat: the matrix to be transposed.

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

get a pointer on TriangMat matrix

Return

a TriangMat*

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

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

void zero()

sets all the values of the matrix to 0.0

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

get a pointer on ZeroMat matrix

Return

a ZeroMat*

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

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

Protected Functions

ACCEPT_SERIALIZATION(SimpleMatrix)

serialization hooks

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_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

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

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 computes res = subA*x +res, subA being a submatrix of trans(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.

• x: a pointer to a SiconosVector

• A: a pointer to SiconosMatrix

• startRow: an int, sub-block position

• startCol: an int, sub-block position

• res: a DenseVect, res. 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.

• A: a pointer to SiconosMatrix

• startRow: an int, sub-block position

• x: a pointer to a SiconosVector

• y: a pointer to a SiconosVector

• init: a bool

Private Members

SP::VInt _ipiv

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

bool _isPLUFactorized

bool _isPLUFactorized; Boolean = true if the Matrix has been PLU Factorized in place.

bool _isPLUInversed

bool _isPLUInversed; Boolean = true if the Matrix has been Inversed in Place

bool _isQRFactorized

bool _isQRFactorized; Boolean = true if the Matrix has been QR Factorized in place.

Friends

void add(const SiconosMatrix &A, const SiconosMatrix &B, SiconosMatrix &C)

Addition of two matrices C = A+B.

Parameters
• A: a SiconosMatrix

• B: a SiconosMatrix

• [inout] C: a SiconosMatrix

bool operator!=(const SiconosMatrix &A, const SiconosMatrix &B)

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

Return

a boolean

Parameters
• A: a SiconosMatrix

• B: a SiconosMatrix

const SimpleMatrix operator*(const SiconosMatrix&, double)
SP::SimpleMatrix operator*(const SP::SimpleMatrix, const SP::SimpleMatrix)
SimpleMatrix operator*(double a, const SiconosMatrix &A)

multiplication of a matrix by a double

Return

a SimpleMatrixmultiplication of a matrix by a double

Return
Parameters
• A: a SiconosMatrix

• a: a double

Parameters
• a: a double

• A: a SiconosMatrix

const SimpleMatrix operator+(const SiconosMatrix &A, const SiconosMatrix &B)

multiplication of a SimpleMatrix by a SimpleMatrix

Return

a const SimpleMatrixmultiplication of a SP::SimpleMatrix by a SP::SimpleMatrix

Return

a SP::SimpleMatrixoperator += add B to A

Return

a SimpleMatrix Addition of two matrices, C = A+B

Return
Parameters
• a: const SiconosMatrix&

• a: const SimpleMatrix&

Parameters
• A: a SP::SiconosMatrix

• B: a SP::SimpleMatrix

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

• B: a SP::SiconosMatrixdivision of the matrix by a double

• A: a SiconosMatrix

• a: a double

Parameters
• A: a SiconosMatrix

• B: a SiconosMatrix

SP::SimpleMatrix operator+(const SP::SimpleMatrix A, const SP::SimpleMatrix B)

Addition of two matrices, C = A+B.

Return

a SP::SimpleMatrix

Parameters
• A: a SP::SiconosMatrix

• B: a SP::SiconosMatrix

void operator+=(SP::SiconosMatrix, SP::SimpleMatrix)
const SimpleMatrix operator-(const SiconosMatrix &A, const SiconosMatrix &B)

Subtraction of two matrices, C = A-B.

Return
Parameters
• A: a SiconosMatrix

• B: a SiconosMatrix

const SimpleMatrix operator/(const SiconosMatrix&, double)
std::ostream &operator<<(std::ostream &os, const SimpleMatrix &sm)
bool operator==(const SiconosMatrix &A, const SiconosMatrix &B)

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

Return

a boolean

Parameters
• A: a SiconosMatrix

• B: a SiconosMatrix

void sub(const SiconosMatrix &A, const SiconosMatrix &B, SiconosMatrix &C)

Subtraction of two matrices C = A-B.

Parameters
• A: a SiconosMatrix

• B: a SiconosMatrix

• [inout] C: a SiconosMatrix