Linear Algebra in Siconos¶
Matrices and vectors handling in kernel component¶
In Siconos kernel, all matrices and vectors are just interfaces to Eigen vectors and matrices
and defined in the files SiconosMatrix.hpp and SiconosVector.hpp.
Please refer to Eigen documentation for details.
Siconos wrappers are:
SiconosVector: dense vector of real numbers (type : double precision)BlockVector: vector ofSiconosVectorSiconosDenseMatrix: dense matrix of doubleSiconosSparseMatrix: sparse matrix of double, column-major storage.
and some fixed-sized objects, like SiconosMatrix33, SiconosMatrix66 ….
Notice that BlockVector are no more that a collection of pointers to SiconosVector. Then in most cases, to build such an object, you just need to insert some existing objects. The usual ways of construction are described below.
Matrix Storage in numerics component¶
Numerics component proposes different ways to store ‘matrix-like’ objects, all handled through a C structure, NumericsMatrix .
Numerics component proposes different ways to store ‘matrix-like’ objects, all handled through a C structure, NumericsMatrix .
A number (NumericsMatrix::storageType ) identify the type of storage while only one pointer among NumericsMatrix.matrixX, X = storageType = 0, 1 or 2, is not NULL and hold the values of the matrix.
At the time, the following storages are available:
“classical” (i.e. dense) column-major storage in a double*,
NumericsMatrix::matrix0sparse block storage, in a structure of type
SparseBlockStructuredMatrix(warning: only for square matrices!!),NumericsMatrix::matrix1sparse storage (csc, csr or triplet), based on CSparse (from T.Davis),
NumericsMatrix::matrix2
As an example, consider the following matrix A of size 8X8:
How can we store this matrix ?
The first classical storage results in:
M.storageType = 0
M.size0 = 8, M.size1 = 8
M.matrix0 = [1 2 0 5 0 0 0 0 2 1 0 0 …]
matrix0 being a double* of size 64.
For the second way of storage, SparseBlockStructuredMatrix we have:
M.storageType = 1
M.size0 = 8, M.size1 = 8
M.matrix1 a
SparseBlockStructuredMatrixin which we save:the number of non null blocks, 6 (matrix1->nbblocks) and the number of diagonal blocks, 3 (matrix1->size).
the vector matrix1->blocksize which collects the sum of diagonal blocks sizes until the present one, is equal to [4,6,8],
blocksize[i] = blocksize[i-1] + ni, ni being the size of the diagonal block at row(block) i.
Note that the last element of blocksize corresponds to the real size of the matrix.
the list of positions of non null blocks in vectors matrix1->ColumnIndex and matrix1->RowIndex, equal to [0,1,1,2,0,2] and [0,0,1,1,2,2]
the list of non null blocks, in matrix1->block, stored in Fortran order (column-major) as
matrix1->block[0] = [1,2,0,5,2,1,0,0,0,0,1,-1,4,0,-1,6]
matrix1->block[1] = [3,4,0,0,-1,1,0,6]
…
matrix1->block[5] = [2,-1,2,2]
Todo write proper doc for CSparse storage and complete the example above.
Functions on NumericsMatrix:
Create, fill and delete NumericsMatrix functions:
NM_create(): allocation without initial valuesNM_create_from_data(): allocation and set default values from external dataNM_fill(): needs a pre-definedNumericsMatrix, set default values from external dataNM_free(): free a NumericsMatrix
These last two functions accept a data parameter, which if non-NULL contains the matrix data.
Linear Algebra:
The following linear algebra operation are supported:
BLAS-like functions:
product matrix - vector:
NM_gemv()andNM_tgemv()(transpose)product matrix - matrix:
NM_gemm()partial product matrix - vector:
NM_row_prod()
-LAPACK-like functions -NM_gesv(): solve a linear system Ax = b
Input / Output:
NM_display(): display aNumericsMatrixNM_display_row_by_row(): display aNumericsMatrixrow by rowNM_write_in_filename(),NM_write_in_file(): save to filesystemNM_read_in_filename(),NM_read_in_file(): fill aNumericsMatrixfrom a fileNM_new_from_file(): create newNumericsMatrixfrom a file