1.1.0
CCC
 
matrix.cpp File Reference
#include "geoslib_old_f.h"
#include "geoslib_enum.h"
#include "Enum/ECst.hpp"
#include "Basic/Utilities.hpp"
#include "Basic/VectorHelper.hpp"
#include "Basic/OptCst.hpp"
#include <math.h>
#include <cmath>

Functions

int matrix_eigen (const double *a_in, int neq, double *value, double *vector)
 
void matrix_product (int n1, int n2, int n3, const double *v1, const double *v2, double *v3)
 
void matrix_product_safe (int n1, int n2, int n3, const double *v1, const double *v2, double *v3)
 
int matrix_prod_norme (int transpose, int n1, int n2, const double *v1, const double *a, double *w)
 
void matrix_transpose (int n1, int n2, double *v1, double *w1)
 
void matrix_int_transpose (int n1, int n2, int *v1, int *w1)
 
void matrix_transpose_in_place (int n1, int n2, double *v1)
 
void matrix_int_transpose_in_place (int n1, int n2, int *v1)
 
int matrix_invert (double *a, int neq, int rank)
 
int matrix_invert_copy (const double *a, int neq, double *b)
 
int matrix_solve (int mode, const double *a, const double *b, double *x, int neq, int nrhs, int *pivot)
 
int is_matrix_symmetric (int neq, const double *a, int verbose)
 
int is_matrix_definite_positive (int neq, const double *a, double *valpro, double *vecpro, int verbose)
 
int is_matrix_non_negative (int nrow, int ncol, double *a, int verbose)
 
double matrix_determinant (int neq, const double *b)
 
int is_matrix_rotation (int neq, const double *a, int verbose)
 
int matrix_cholesky_decompose (const double *a, double *tl, int neq)
 
void matrix_cholesky_product (int mode, int neq, int nrhs, const double *tl, const double *a, double *x)
 
void matrix_cholesky_invert (int neq, const double *tl, double *xl)
 
void matrix_cholesky_norme (int mode, int neq, const double *tl, const double *a, double *b)
 
int matrix_invgen (double *a, int neq, double *tabout, double *cond)
 
double matrix_normA (double *b, double *a, int neq, int subneq)
 
void matrix_triangle_to_square (int mode, int neq, const double *tl, double *a)
 
void matrix_produit_cholesky (int neq, const double *tl, double *a)
 
VectorDouble matrix_produit_cholesky_VD (int neq, const double *tl)
 
void matrix_svd_inverse (int neq, double *s, double *u, double *v, double *tabout)
 
int matrix_invsvdsym (double *mat, int neq, int rank)
 
void matrix_manage (int nrows, int ncols, int nr, int nc, int *rowsel, int *colsel, double *v1, double *v2)
 
void matrix_combine (int nval, double coeffa, double *a, double coeffb, double *b, double *c)
 
double matrix_norminf (int nval, double *tab)
 
void matrix_product_by_diag (int mode, int neq, double *a, double *c, double *b)
 
int matrix_eigen_tridiagonal (const double *vecdiag, const double *vecinf, const double *vecsup, int neq, double *eigvec, double *eigval)
 
int matrix_qo (int neq, double *hmat, double *gmat, double *xmat)
 
int matrix_qoc (int flag_invert, int neq, double *hmat, double *gmat, int na, double *amat, double *bmat, double *xmat, double *lambda)
 
int matrix_qoci (int neq, double *hmat, double *gmat, int nae, double *aemat, double *bemat, int nai, double *aimat, double *bimat, double *xmat)
 
double * matrix_bind (int mode, int n11, int n12, double *a1, int n21, int n22, double *a2, int *n31, int *n32)
 
int matrix_get_extreme (int mode, int ntab, double *tab)
 
int matrix_invreal (double *mat, int neq)
 
int matrix_invert_triangle (int neq, double *tl, int rank)
 
void matrix_tl2tu (int neq, const double *tl, double *tu)
 
int matrix_LU_decompose (int neq, const double *a, double *tls, double *tus)
 
int matrix_LU_solve (int neq, const double *tus, const double *tls, const double *b, double *x)
 
int matrix_LU_invert (int neq, double *a)
 
void matrix_fill_symmetry (int neq, double *a)
 

Function Documentation

int is_matrix_definite_positive ( int  neq,
const double *  a,
double *  valpro,
double *  vecpro,
int  verbose 
)

Check if a matrix is definite positive

Returns
1 if the matrix is definite positive; 0 otherwise
Parameters
[in]neqSize of the matrix
[in]aSymmetric square matrix to be checked
[in]verbose1 for the verbose option
[out]valproArray of eigen values (Dimension: neq)
[out]vecproArray of eigen vectors (Dimension: neq * neq)
int is_matrix_non_negative ( int  nrow,
int  ncol,
double *  a,
int  verbose 
)

Check if all the elements of a matrix are non-negative

Returns
1 if the matrix is non-negative; 0 otherwise
Parameters
[in]nrowNumber of rows
[in]ncolNumber of columns
[in]aArray to be checked
[in]verbose1 for the verbose option
int is_matrix_rotation ( int  neq,
const double *  a,
int  verbose 
)

Check if a matrix is a rotation matrix

Returns
1 if the matrix is a rotation matrix; 0 otherwise
Parameters
[in]neqSize of the matrix
[in]aSquare matrix to be checked
[in]verbose1 for the verbose option
Remarks
A rotation matrix must be orthogonal with determinant equal to 1
int is_matrix_symmetric ( int  neq,
const double *  a,
int  verbose 
)

Check if a matrix is symmetric

Returns
1 if the matrix is symmetric; 0 otherwise
Parameters
[in]neqSize of the matrix
[in]aSymmetric square matrix to be checked
[in]verbose1 for the verbose option
double* matrix_bind ( int  mode,
int  n11,
int  n12,
double *  a1,
int  n21,
int  n22,
double *  a2,
int *  n31,
int *  n32 
)

Concatenate two matrices

Returns
Pointer on the newly created concatenated matrix (or NULL)
Parameters
[in]modeConcatenation type: 1 : by row (n31=n11+n21; n32=n12=n22) 2 : by column (n31=n11=n21; n32=n12+n22)
[in]n11First dimension of the first matrix
[in]n12Second dimension of the first matrix
[in]a1Pointer to the first matrix
[in]n21First dimension of the second matrix
[in]n22Second dimension of the second matrix
[in]a2Pointer to the second matrix
[out]n31First dimension of the output matrix
[out]n32Second dimension of the output matrix
int matrix_cholesky_decompose ( const double *  a,
double *  tl,
int  neq 
)

Performs the Cholesky triangular decomposition of a definite positive symmetric matrix A = t(TL) * TL

Returns
Return code: >0 rank of zero pivot or 0 if no error
Parameters
[in]asymmetric matrix
[in]neqnumber of equations in the system
[out]tlLower triangular matrix defined by column
Remarks
the matrix a[] is destroyed during the calculations
void matrix_cholesky_invert ( int  neq,
const double *  tl,
double *  xl 
)

Invert the Cholesky matrix

Parameters
[in]neqnumber of equations in the system
[in]tllower triangular matrix defined by column
[out]xllower triangular inverted matrix defined by column
void matrix_cholesky_norme ( int  mode,
int  neq,
const double *  tl,
const double *  a,
double *  b 
)

Performs the product B = TL * A * TU or TU * A * TL where TL,TU is a triangular matrix and A a square symmetric matrix

Parameters
[in]mode0: TL * A * TU; 1: TU * A * TL
[in]neqnumber of equations in the system
[in]tlTriangular matrix defined by column
[in]aSquare symmetric matrix (optional)
[out]bSquare symmetric matrix
void matrix_cholesky_product ( int  mode,
int  neq,
int  nrhs,
const double *  tl,
const double *  a,
double *  x 
)

Performs the product between a triangular and a square matrix TL is the lower triangular matrix and X is a square matrix

Parameters
[in]modeType of calculations: 0 : X=TU%*A 1 : X=TL%*A 2 : X=A%*TU 3 : X=A%*TL 4 : X=t(A)%*TU 5 : X=t(A)%*TL
[in]neqnumber of equations in the system
[in]nrhsnumber of columns in x
[in]tlTriangular matrix defined by column (dimension: neq * neq)
[in]amatrix (dimension neq * nrhs)
[out]xresulting matrix (dimension neq * nrhs)
void matrix_combine ( int  nval,
double  coeffa,
double *  a,
double  coeffb,
double *  b,
double *  c 
)

Perform a linear combinaison of matrices or vectors [C] = coeffa * [A] + coeffb * [B]

Parameters
[in]nvalNumber of elements of the matrices or vectors
[in]coeffaCoefficient applied to the first matrix or vector
[in]aFirst matrix or vector (not used if NULL)
[in]coeffbCoefficient applied to the second matrix or vector
[in]bSecond matrix or vector (not used if NULL)
[out]cResulting matrix or vector
Remarks
No test is performed to check that the three matrices or vectors
have the same dimensions
Matrix c[] can coincide with matrices a[] or b[]
double matrix_determinant ( int  neq,
const double *  b 
)

Calculate the determinant of the square matrix (full storage)

Returns
Value of the determinant
Parameters
[in]neqSize of the matrix
[in]bSquare matrix to be checked
int matrix_eigen ( const double *  a_in,
int  neq,
double *  value,
double *  vector 
)

Calculates the eigen values and eigen vectors of a symmetric square matrix A X = X l

Returns
Return code:
0 no error
1 convergence problem
Parameters
[in]a_insquare symmetric matrix (dimension = neq*neq)
[in]neqmatrix dimension
[out]valuematrix of the eigen values (dimension: neq)
[out]vectormatrix of the eigen vectors (dimension: neq*neq)
Remarks
a_in is protected
int matrix_eigen_tridiagonal ( const double *  vecdiag,
const double *  vecinf,
const double *  vecsup,
int  neq,
double *  eigvec,
double *  eigval 
)

Transform a tridiagonal non-symmetric matrix into a symmetric one

Returns
Error return code (see remarks)
Parameters
[in]vecdiagVector for the main diagonal
[in]vecinfVector for the subdiagonal (in the last neq-1 positions)
[in]vecsupVector for the superdiagonal (in the first neq positions)
[in]neqmatrix dimension
[out]eigvecsquare symmetric matrix (dimension: neq * neq)
[out]eigvalvector (dimensionL neq)
Remarks
Given the nonsymmetric tridiagonal matrix, we must have the products
of corresponding pairs of off-diagonal elements are all non-negative,
and zero only when both factors are zero
void matrix_fill_symmetry ( int  neq,
double *  a 
)

Fill the matrix to take the symmetry into account

Parameters
[in]neqmatrix dimension
[in,out]asquare symmetric matrix (dimension = neq*neq)
Remarks
We assume that, in the input matrix, the elements A[i,j] where
i >= j have already been filled
int matrix_get_extreme ( int  mode,
int  ntab,
double *  tab 
)

Find the minimum or maximum value within an array

Returns
Rank of the minimum or maximum value
Parameters
[in]mode1 for minimum and 2 for maximum
[in]ntabNumber of samples in the array
[in]tabArray of values
void matrix_int_transpose ( int  n1,
int  n2,
int *  v1,
int *  w1 
)

Transpose a (square or rectangular) matrix

Parameters
[in]n1matrix dimension
[in]n2matrix dimension
[in]v1rectangular matrix (n1,n2)
[out]w1rectangular matrix (n2,n1)
Remarks
The matrix w1[] may NOT coincide with v1[]
void matrix_int_transpose_in_place ( int  n1,
int  n2,
int *  v1 
)

Transpose an integer matrix in place

Parameters
[in]n1matrix dimension
[in]n2matrix dimension
[in,out]v1rectangular matrix (n1,n2)
int matrix_invert ( double *  a,
int  neq,
int  rank 
)

Invert a symmetric square matrix Pivots are assumed to be located on the diagonal

Returns
Return code: 0 no error; k if the k-th pivot is zero
Parameters
[in,out]ainput matrix, destroyed in computation and replaced by resultant inverse
[in]neqnumber of equations in the matrix 'a'
[in]rankType of message when inversion problem is encountered >=0: message involves 'rank+1' -1: neutral message -2: no message
Remarks
It is unnecessary to edit a message if inversion problem occurs
int matrix_invert_copy ( const double *  a,
int  neq,
double *  b 
)

Invert a symmetric square matrix Pivots are assumed to located on the diagonal

Returns
Return code: 0 no error; k if the k-th pivot is zero
Parameters
[in]ainput matrix
[in]neqnumber of equations in the matrix 'a'
[out]boutput matrix
Remarks
The difference with matrix_invert() is that the output
matrix is different from input matrix
int matrix_invert_triangle ( int  neq,
double *  tl,
int  rank 
)

Invert a symmetric square matrix (stored as triangular)

Returns
Error returned code
Parameters
[in,out]tlinput matrix, destroyed in computation and replaced by resultant inverse
[in]neqnumber of equations in the matrix 'a'
[in]rankType of message when inversion problem is encountered >=0: message involves 'rank+1' -1: neutral message -2: no message
Remarks
It is unnecessary to edit a message if inversion problem occurs
int matrix_invgen ( double *  a,
int  neq,
double *  tabout,
double *  cond 
)

Calculate the generalized inverse of a square symmetric matrix

Returns
Error returned code
Parameters
[in]aSymmetric matrix to be inverted
[in]neqNumber of equations
[out]taboutInverted matrix
[out]condCondition number (MAX(ABS(eigval))/MIN(ABS(eigval)))
Remarks
The input and output matrices can matchprint
int matrix_invreal ( double *  mat,
int  neq 
)

Invert a square real full matrix

Returns
Error return code
Parameters
[in]matinput matrix, destroyed in computation and replaced by resultant inverse
[in]neqnumber of equations in the matrix 'a'
int matrix_invsvdsym ( double *  mat,
int  neq,
int  rank 
)

Invert a symmetric square matrix (SVD method)

Returns
Error return code
Parameters
[in]matinput matrix, destroyed in computation and replaced by resultant inverse
[in]neqnumber of equations in the matrix 'a'
[out]rankinstability rank (if > 0)
Remarks
This routine has been adapted from a Pascal implementation
(c) 1988 J. C. Nash, "Compact numerical methods for computers",
Hilger 1990.
int matrix_LU_decompose ( int  neq,
const double *  a,
double *  tls,
double *  tus 
)

LU Decomposition of a square matrix (not necessarily symmetric)

Parameters
neqSize of the matrix (number of rows or columns)
aInput square matrix (stored column-wise)
tlsOutput square matrix containing lower triangle (stored columnwise)
tusOutput square matrix containing upper triangle (stored linewise)
Remarks
The output matrices 'tus' and 'tls' must be allocated beforehand
int matrix_LU_invert ( int  neq,
double *  a 
)
int matrix_LU_solve ( int  neq,
const double *  tus,
const double *  tls,
const double *  b,
double *  x 
)
void matrix_manage ( int  nrows,
int  ncols,
int  nr,
int  nc,
int *  rowsel,
int *  colsel,
double *  v1,
double *  v2 
)

Manage a matrix and derive a submatrix

Parameters
[in]nrowsNumber of rows of the input matrix
[in]ncolsNumber of columns of the input matrix
[in]nrNumber of rows of interest
  • >0 : for extraction
  • 0 : no action
  • <0 : for suppression
[in]ncNumber of columns of interest
  • >0 : for extraction
  • 0 : no action
  • <0 : for suppression
[in]rowselArray of rows indices of interest (starting from 0) (Dimension: ABS(nr)
[in]colselArray of columns indices of interest (starting from 0) (Dimension: ABS(nr)
[in]v1Input rectangular matrix (Dimension: nrows * ncols)
[out]v2Output rectangular matrix
double matrix_normA ( double *  b,
double *  a,
int  neq,
int  subneq 
)

Calculate the square norm of a vector issued from the inner product relative to a matrix A

Returns
Value of the norm
Parameters
[in]bVector (Dimension: neq)
[in]aMatrix (Symmetric, Dimension neq x neq)
[in]neqSpace dimension
[in]subneqDimension of the subspace to be considered
double matrix_norminf ( int  nval,
double *  tab 
)

Calculate the maximum of the absolute values of a vector

Parameters
[in]nvalvector dimension
[in]tabvector
int matrix_prod_norme ( int  transpose,
int  n1,
int  n2,
const double *  v1,
const double *  a,
double *  w 
)

Performs the product t(G) %*% A %*% G or G %*% A %*% t(G)

Returns
Error return code
Parameters
[in]transposetransposition mode -1 : transpose the first term +1 : transpose the last term
[in]n1matrix dimension
[in]n2matrix dimension
[in]v1rectangular matrix (n1,n2)
[in]asquare matrix (optional)
[out]wsquare matrix
Remarks
According to the value of 'transpose':
-1: the output array has dimension (n2,n2)
+1: the output array has dimension (n1,n1)
According to the value of 'transpose':
-1: the optional array A has dimension (n1,n1)
+1: the optional array A has dimension (n2,n2)
void matrix_product ( int  n1,
int  n2,
int  n3,
const double *  v1,
const double *  v2,
double *  v3 
)

Performs the product of two matrices

Parameters
[in]n1matrix dimension
[in]n2matrix dimension
[in]n3matrix dimension
[in]v1rectangular matrix (n1,n2)
[in]v2rectangular matrix (n2,n3)
[out]v3rectangular matrix (n1,n3)
Remarks
The matrix v3[] may coincide with one of the two initial ones
void matrix_product_by_diag ( int  mode,
int  neq,
double *  a,
double *  c,
double *  b 
)

Performs the A %*% diag(c) where A is a square matrix and c a vector

Parameters
[in]mode0: c as is; 1: sqrt(c); 2: 1/c; 3: 1/sqrt(c)
[in]neqmatrix dimension for A
[in]asquare matrix
[in]cvector
[out]bsquare matrix
Remarks
Matrices a() and b() may coincide
void matrix_product_safe ( int  n1,
int  n2,
int  n3,
const double *  v1,
const double *  v2,
double *  v3 
)

Performs the product of two matrices

Parameters
[in]n1matrix dimension
[in]n2matrix dimension
[in]n3matrix dimension
[in]v1rectangular matrix (n1,n2)
[in]v2rectangular matrix (n2,n3)
[out]v3rectangular matrix (n1,n3)
Remarks
The matrix v3[] may NOT coincide with one of the two initial ones
void matrix_produit_cholesky ( int  neq,
const double *  tl,
double *  a 
)

Calculate the product of 'tl' (lower triangle) by its transpose

Parameters
[in]neqnumber of equations in the system
[in]tlLower triangular matrix defined by column
[out]aResulting square matrix
VectorDouble matrix_produit_cholesky_VD ( int  neq,
const double *  tl 
)

Calculate the product of 'tl' (lower triangle) by its transpose and store the result in a VectorDouble

Returns
The resulting VectorDouble
Parameters
[in]neqnumber of equations in the system
[in]tlLower triangular matrix defined by column
int matrix_qo ( int  neq,
double *  hmat,
double *  gmat,
double *  xmat 
)

Solve a linear system: H %*% g = x

Returns
Error return code
Parameters
[in]neqnumber of equations in the system
[in]hmatsymmetric square matrix (Dimension: neq * neq)
[in]gmatright-hand side vector (Dimension: neq)
[out]xmatsolution vector (Dimension: neq)
Remarks
In output, hmat contains the inverse matrix
int matrix_qoc ( int  flag_invert,
int  neq,
double *  hmat,
double *  gmat,
int  na,
double *  amat,
double *  bmat,
double *  xmat,
double *  lambda 
)

Minimize 1/2 t(x) %*% H %*% x + t(g) %*% x under the constraints t(A) %*% x = b

Returns
Error return code
Parameters
[in]flag_invertTells if the inverse has already been calculated
[in]neqnumber of equations in the system
[in]hmatsymmetric square matrix (Dimension: neq * neq)
[in]gmatright-hand side vector (Dimension: neq)
[in]naNumber of equalities
[in]amatmatrix for inequalities (Dimension: neq * na)
[in]bmatinequality vector (Dimension: na)
[in]xmatsolution of the linear system with no constraint. On return, solution with constraints (Dimension: neq)
[out]lambdaworking vector (Dimension: na)
Remarks
In input:
If flag_invert== 1, H is provided as the generalized inverse
and x contains the solution of the linear system with no constraint
If flag_invert==0, H is the primal matrix
In output, H contains the inverse matrix
int matrix_qoci ( int  neq,
double *  hmat,
double *  gmat,
int  nae,
double *  aemat,
double *  bemat,
int  nai,
double *  aimat,
double *  bimat,
double *  xmat 
)

Minimize 1/2 t(x) %*% H %*% x + t(g) %*% x under the constraints t(Ae) %*% x = be and t(Ai) %*% x = bi

Returns
Error return code
Parameters
[in]neqnumber of equations in the system
[in]hmatsymmetric square matrix (Dimension: neq * neq)
[in]gmatright-hand side vector (Dimension: neq)
[in]naeNumber of equalities
[in]aematmatrix for equalities (Dimension: neq * nae)
[in]bematright-hand side for equalities (Dimension: nae)
[in]naiNumber of inequalities
[in]aimatmatrix for inequalities (Dimension: neq * nai)
[in]bimatright-hand side for inequalities (Dimension: nai)
[in,out]xmatsolution of the linear system with constraints (neq)

REMARKS: The initial xmat has to satisfied all the constraints.

int matrix_solve ( int  mode,
const double *  a,
const double *  b,
double *  x,
int  neq,
int  nrhs,
int *  pivot 
)

Solve a system of linear equations with symmetric coefficient matrix upper triangular part of which is stored columnwise. Use is restricted to matrices whose leading principal minors have non-zero determinants.

Returns
Error return code : 1 for core problem; 0 otherwise
Parameters
[in]modeStorage: 0 for upper triangular, 1 for lower triangular
[in]atriangular matrix (dimension = neq*(neq+1)/2)
[in]bright-hand side matrix (dimension = neq*nrhs)
[in]neqnumber of equations in the system
[in]nrhsnumber of right-hand side vectors
[out]xmatrix of solutions (dimension = neq*nrhs)
[out]pivotrank of the pivoting error (0 none)
void matrix_svd_inverse ( int  neq,
double *  s,
double *  u,
double *  v,
double *  tabout 
)

Invert a symmetric square matrix (SVD method)

Parameters
[in]neqnumber of equations in the matrix 'a'
[in]sinput matrix (square)
[out]ufirst decomposition matrix
[out]vsecond decomposition matrix
[out]taboutinverse matrix
Remarks
This routine has been adapted from a Pascal implementation
(c) 1988 J. C. Nash, "Compact numerical methods for computers",
Hilger 1990.
void matrix_tl2tu ( int  neq,
const double *  tl,
double *  tu 
)

Change the storage of triangular matrices (stored columnwise)

Parameters
[in]neqMatrix dimension
[in]tlLower Triangular Matrix
[in]tuUpper Triangular Matrix
void matrix_transpose ( int  n1,
int  n2,
double *  v1,
double *  w1 
)

Transpose a (square or rectangular) matrix

Parameters
[in]n1matrix dimension
[in]n2matrix dimension
[in]v1rectangular matrix (n1,n2)
[out]w1rectangular matrix (n2,n1)
Remarks
The matrix w1[] may NOT coincide with v1[]
void matrix_transpose_in_place ( int  n1,
int  n2,
double *  v1 
)

Transpose a matrix in place

Parameters
[in]n1matrix dimension
[in]n2matrix dimension
[in,out]v1rectangular matrix (n1,n2)
void matrix_triangle_to_square ( int  mode,
int  neq,
const double *  tl,
double *  a 
)

Fill a square matrix with a triangular matrix

Parameters
[in]mode0: TL (upper); 1: TL (lower)
[in]neqnumber of equations in the system
[in]tlTriangular matrix (lower part)
[out]aResulting square matrix