Class SimpleMatrix

Defined in Program listing for file kernel/src/utils/SiconosAlgebra/SimpleMatrix.hpp

class SimpleMatrix : public SiconosMatrix

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.

See Linear Algebra in Siconos in Users guide.

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

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

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

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

Return
a sub-matrix
Parameters
  • row: row index
  • col: column index

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

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

Return
a sub-matrix
Parameters
  • row: row index
  • col: column index

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 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
  • 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 row index of current matrix and save it into vOut

Parameters
  • row: index row we want to get
  • 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

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

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

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

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

SimpleMatrix &operator+=(const SiconosMatrix &m)

operator +=

Return
SimpleMatrix&
Parameters
  • m: a matrix to add

SimpleMatrix &operator-=(const SiconosMatrix &m)

operator -=

Return
SimpleMatrix&
Parameters
  • m: a matrix to subtract

SimpleMatrix &operator=(const SiconosMatrix &m)

assignment

Return
SimpleMatrix&
Parameters
  • m: the matrix to be copied

SimpleMatrix &operator=(const SimpleMatrix &m)

assignment

Return
SimpleMatrix&
Parameters
  • m: the matrix to be copied

SimpleMatrix &operator=(const DenseMat &m)

assignment to a DenseMat

Return
SimpleMatrix&
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
  • 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
  • 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)

set block starting at row “posRow” (first argument) and column “posCol” (second argument) with m (last arg)

Parameters
  • posRow: an int, row-position of the first element of the required block
  • posCol: an int, col-position of the first element of the required block
  • m: a ref to a SiconosMatrix m

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

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

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
  • C: a SiconosMatrix

void axpy_prod(const SiconosMatrix&, const SiconosMatrix&, SiconosMatrix&, bool)

prod(A, B, C) computes C = A*B in an optimal way (if init = true, else +=).

Parameters
  • A: a SiconosMatrix
  • B: a SiconosMatrix
  • C: a SiconosMatrix
  • init: a bool (default = true)

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

computes y = A*x (init = true) or y += A*x (init = false)

Parameters

SP::SiconosVector compareMatrices(const SimpleMatrix &data, const SimpleMatrix &ref)

returns a vector of maximum relative error for each column

Return
a pointer filled with the maximum relative error for each value in data
Parameters
  • data: the matrix filled with simulation results
  • ref: the matrix filled with the reference values

void gemm(double a, const SiconosMatrix &A, const SiconosMatrix &B, double b, SiconosMatrix &C)

gemm(a, A, B, b, C) computes C = a*A*B+ b*C This function wrapped blas gemm through boost bindings.

Reserved to dense matrices and vectors.

Parameters
  • a: a double
  • A: a SiconosMatrix
  • B: a SiconosMatrix
  • b: a double
  • C: a SiconosMatrix

void gemmtranspose(double a, const SiconosMatrix &A, const SiconosMatrix &B, double b, SiconosMatrix &C)

gemmtranspose(a, A, B, b, C) computes C = a*transpose(A)*transpose(B) + b*C This function wrapped blas gemm through boost bindings.

Reserved to dense matrices and vectors.

Parameters
  • a: a double
  • A: a SiconosMatrix
  • B: a SiconosMatrix
  • b: a double
  • C: a SiconosMatrix

void gemv(double a, const SiconosMatrix &A, const SiconosVector &x, double b, SiconosVector &y)

gemv(a, A, x, b, y) computes y = a*A*x+ b*y This function wrapped blas gemv through boost bindings.

Reserved to dense matrices and vectors.

Parameters

void gemvtranspose(double a, const SiconosMatrix &A, const SiconosVector &x, double b, SiconosVector &y)

gemvtranspose(a, A, x, b, y) computes y = a*transpose(A)*x + b*y This function wrapped blas gemv through boost bindings.

Reserved to dense matrices and vectors.

Parameters

const SimpleMatrix matrix_pow(const SimpleMatrix &A, unsigned int)

compute the power of the matrix (!)

Return
a SimpleMatrix
Parameters

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 &A, double a)

multiplication of a matrix by a double

Return
a SimpleMatrix
Parameters
  • A: a SiconosMatrix
  • a: a double

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

multiplication of a SimpleMatrix by a SimpleMatrix

Return
a const SimpleMatrixmultiplication of a SP::SimpleMatrix by a SP::SimpleMatrix
Return
a SP::SimpleMatrix
Parameters
Parameters
  • A: a SP::SiconosMatrix
  • B: a SP::SimpleMatrix

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

multiplication of a matrix by a double

Return
a SimpleMatrix
Parameters
  • a: a double
  • A: a SiconosMatrix

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

Addition of two matrices, C = A+B.

Return
a SimpleMatrix C
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 A, SP::SimpleMatrix B)

operator += add B to A

Parameters
  • A: a SP::SiconosMatrix
  • B: a SP::SiconosMatrix

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

Subtraction of two matrices, C = A-B.

Return
a SimpleMatrix
Parameters
  • A: a SiconosMatrix
  • B: a SiconosMatrix

const SimpleMatrix operator/(const SiconosMatrix &A, double a)

division of the matrix by a double

Return
a SimpleMatrix
Parameters
  • A: a SiconosMatrix
  • a: a double

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

send data of the matrix to an ostream

Return
The same output stream
Parameters

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 prod(const SiconosMatrix &A, const SiconosMatrix &B)

product of two matrices, C = A*B

Return
C a SimpleMatrix
Parameters
  • A: a SiconosMatrix
  • B: a SiconosMatrix

void prod(const SiconosMatrix &A, const SiconosMatrix &B, SiconosMatrix &C, bool init)

prod(A, B, C) computes C = A*B in an optimal way, or C += AB if init = false.

Parameters
  • A: a SiconosMatrix
  • B: a SiconosMatrix
  • C: a SiconosMatrix
  • init: a bool (default = true)

const SiconosVector prod(const SiconosMatrix &A, const SiconosVector &x)

prod(A, x) returns the product Ax

Return
a SiconosVector
Parameters

void prod(const SiconosMatrix &A, const SiconosVector &x, SiconosVector &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)

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

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(double a, const SiconosMatrix &A, const SiconosVector &x, SiconosVector &y, bool init)

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

Parameters

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

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

Parameters

void scal(double a, const SiconosMatrix &A, SiconosMatrix &B, bool)

multiplication of a matrix by a scalar, B = a*A (init = true) or B += a*A (init = false)

Parameters
  • a: a double
  • A: a SiconosMatrix
  • B: a SiconosMatrix
  • init: a bool

void setBlock(SPC::SiconosMatrix MIn, SP::SiconosMatrix MOut, const Index &dim, const Index &start)

Copy a subBlock of MIn into a sub-block of MOut - Dim and positions of the sub-block are given in dim and start.

Parameters
  • MIn: a SPC::SiconosMatrix
  • MOut: a SP::SiconosMatrix
  • dim: an Index, dim[0], dim[1]: number of rows and columns of the sub-block
  • start: an Index, start[0], start[1]: position (row, column) of the first element of the sub-block in MIn start[2], start[3]: position (row, column) of the first element of the sub-block in MOut.

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

Subtraction of two matrices C = A-B.

Parameters
  • A: a SiconosMatrix
  • B: a SiconosMatrix
  • C: a SiconosMatrix

void subprod(const SiconosMatrix &A, const SiconosVector &x, SiconosVector &y, const Index &coord, bool init)

subprod(A, x, y) computes sub_y = sub_A*sub_x or sub_y += sub_A*sub_x if init = false

Parameters
  • A: a SiconosMatrix
  • x: a SiconosVector
  • y: a SiconosVector
  • coord: an Index = [r0A r1A c0A c1A r0x r1x r0y r1y]; subA is the sub-matrix of A, for row numbers between r0A and r1A-1 and columns between c0A and c1A-1; The same for x and y with rix and riy.
  • init: a bool (default = true)