Linear Algebra

Overview

These functions solve the linear systems that arise in multibody kinematics and dynamics.

LU factorization with full pivoting. The constraint equations of a mechanism with closed loops give a Jacobian matrix that is often rectangular and rank deficient, because some constraints are redundant. LU factorization with complete row and column pivoting (ksl_linalg_lu_full_rmo()) handles such matrices: it finds the rank, and the column permutation it chooses splits the generalized coordinates into a set of independent coordinates and a set of dependent coordinates that can be computed from them. This is the basis of generalized coordinate partitioning [22]. The setBMatrix functions then compute \(B = U_r^{-1} U_R\), which expresses the dependent coordinates in terms of the independent ones, and the specified variants keep chosen coordinates, such as driven joints, in the independent set. The largest entry of \(B\) (ksl_linalg_lu_infinityNormB_rmo()) grows as the partition becomes poorly conditioned, which signals that a new partition should be chosen.

Cholesky and LDLT factorization. Mass matrices are symmetric and positive definite. The Cholesky factorization \(A = L L^T\) and the closely related \(A = L D L^T\) solve such systems with about half the work of an LU factorization and without pivoting. Each comes with forward elimination, back substitution and solve functions, and symmetricMatrixInverse computes the inverse of a symmetric positive definite matrix.

Gram-Schmidt orthonormalization (ksl_linalg_gramSchmidt()) makes the columns of a matrix orthonormal.

Storage order. The matrices are plain double arrays. Functions ending in _rmo expect row major order, where element \((i, j)\) of a matrix with n columns is A[i * n + j]. Functions ending in _cmo expect column major order, where element \((i, j)\) of a matrix with m rows is A[j * m + i]. The two-dimensional arrays from ksl_array2D_alloc() store their rows in one contiguous block, so A[0] can be passed to the _rmo functions (see Arrays and Memory). [5] is the standard reference for these algorithms.

The array utilities ksl_swapArray() and ksl_maxIndex() used by these functions are described in Arrays and Memory.

Gram Schmidt Decomposition

ksl_linalg_gramSchmidt()

Function for performing modified double precision Gram-Schmidt orthonormalization.

ksl_linalg_gramSchmidtf()

Perform modified Gram-Schmidt orthonormalization on a single precision matrix.

void ksl_linalg_gramSchmidt(double *A, const int m, const int n)

Function for performing modified double precision Gram-Schmidt orthonormalization.

This function implements a compact version of the Gram-Schmidt algorithm to orthonormalize the columns of a matrix which has full column rank. The matrix, whose columns are to be orthonormalized, is passed in as A[m][n] where m and n are the respective row and column dimensions of A[][]. The orthonormalized columns are passed back in A[][].

Parameters:
  • A – [in,out] row major m by n matrix whose columns are orthonormalized in place

  • m – [in] row dimension

  • n – [in] column dimension

void ksl_linalg_gramSchmidtf(float *A, const int m, const int n)

Perform modified Gram-Schmidt orthonormalization on a single precision matrix.

This function implements a compact version of the Gram-Schmidt algorithm to orthonormalize the columns of a matrix which has full column rank. The matrix, whose columns are to be orthonormalized, is passed in as A[m][n] where m and n are the respective row and column dimensions of A[][]. The orthonormalized columns are passed back in A[][].

Parameters:
  • A – [in,out] row major m by n matrix whose columns are orthonormalized in place

  • m – [in] row dimension

  • n – [in] column dimension

LU Decomposition

row major order

ksl_linalg_lu_full_rmo()

Row Major Order LU Decomposition with complete row and column pivoting.

ksl_linalg_lu_full_specified_rmo()

Row Major Order LU Decomposition with complete row and column pivoting with one specified coordinate.

ksl_linalg_lu_rmo()

Fast Row Major Order LU Decomposition with no row or column pivoting for a rectangular matrix

ksl_linalg_lu_setBMatrix_rmo()

compute Row Major Order B matrix (inverse(Ur) * UR)

ksl_linalg_lu_setCMatrix_rmo()

compute Row Major Order C matrix

ksl_linalg_lu_full_multiSpecified_rmo()

Row Major Order LU Decomposition with complete row and column pivoting with a set of specified coordinates.

ksl_linalg_lu_infinityNormB_rmo()

Return the largest absolute value in the B matrix of a factored row major matrix.

ksl_linalg_lu_solve_rmo()

Solve \(L_r U_r \underline{x} = \underline{b}\) with the factors of ksl_linalg_lu_full_rmo().

int ksl_linalg_lu_full_rmo(const int rowDim, const int colDim, double *A, const double eps, int *pr, int *pc)

Row Major Order LU Decomposition with complete row and column pivoting.

ksl_linalg_lu_full_rmo factors a double precision matrix, A[rowDim * colDim], stored in Row Major Order using full row and column pivoting. Matrix A need not have full row or column rank. The integer variable ‘rank’ represents the matrix rank and internally is always one less than the true rank to be consistent with C’s indexing from zero. Upon return from the function, rank will be set to the correct value.

The lower triangular (rank+1) by (rank+1) Lr matrix, except its unity diagonal, is stored below the diagonal in A[0:rank][0:rank]. The upper triangular (rank+1) by (rank+1) Ur matrix is stored on and above the diagonal in A[0:rank][0:rank].

Following the first major matrix decomposition step, A[0:rank][rank+1:colDim] stores the residual matrix UR and A[rank+1:rowDim][0:rank] stores the residual matrix LR.

The following operations on this matrix are optionally performed after ksl_linalg_lu_full_rmo() has factored A.

ksl_linalg_lu_setBMatrix_rmo() computes and stores B = inverse(Ur)*UR and stores it back in A[0:rank][rank+1:colDim]. ksl_linalg_lu_setCMatrix_rmo() computes and stores C = LR*inverse(Lr) and stores it back in A[rank+1:rowDim][0:rank].

The remaining A[rank+1:rowDim][rank+1:colDim] submatrix contains numbers whose absolute values are all smaller than eps times the absolutely largest entry in the original matrix. This part of the matrix is not used.

Parameters:
  • rowDim – [in] row dimension of matrix A.

  • colDim – [in] column dimension of matrix A.

  • *A – [in/out] matrix to be factored with dimensions A[0:rowDim-1][0:colDim-1], LU factors are overwritten in original matrix

  • eps – [in] input tolerance on the order of machine roundoff.

  • pr – [out] output row permutation array with dimensions pr[0:rowDim-1]

  • pc – [out] output column permutation array with dimensions pc[0:colDim-1]

Returns:

rank of matrix A

int ksl_linalg_lu_full_specified_rmo(const int rowDim, const int colDim, double *A, double eps, int *pr, int *pc, const int specifiedIndex)

Row Major Order LU Decomposition with complete row and column pivoting with one specified coordinate.

Equivalent to ksl_linalg_lu_full_multiSpecified_rmo with a single specified column: the specified column is moved to the last column of A and is excluded from the pivot search, so the specified coordinate is always independent.

Parameters:
  • rowDim – input row dimension of matrix A.

  • colDim – input column dimension of matrix A.

  • **A – input/output matrix to be factored with dimensions A[0:rowDim-1][0:colDim-1]:

  • eps – input tolerance on the order of machine roundoff.

  • pr – output row permutation array with dimensions pr[0:rowDim-1]

  • pc – output column permutation array with dimensions pc[0:colDim-1]

  • specifiedIndex – input column index of the specified coordinate

Returns:

rank of matrix A

inline void ksl_linalg_lu_rmo(const int rank, const int colDim, double *A)

Fast Row Major Order LU Decomposition with no row or column pivoting for a rectangular matrix

This function LU factors a double precision matrix, A[rank][colDim], using no row or column pivoting. This function should be used only if the left rank by rank submatrix is known to be nonsingular.

rank <= colDim or error.

The lower triangular rank by rank Lr matrix, except the unity diagonal, is stored below the diagonal in A[0:rank-1][0:rank-1]. The upper triangular rank by rank Ur matrix is stored on and above the diagonal in A[0:rank-1][0:rank-1].

Note that this minimal algorithm variation does not compute the product B = inverse(Ur)*UR

Parameters:
  • rank – [in] row dimension of matrix A.

  • colDim – [in] column dimension of matrix A.

  • *A – [in/out] matrix to be factored with dimensions A[0:rank-1][0:colDim-1]:

inline void ksl_linalg_lu_setBMatrix_rmo(const int rank, const int colDim, double *A)

compute Row Major Order B matrix (inverse(Ur) * UR)

This block overwrites UR with inverse(Ur) * UR If rank == 0 or rank == colDim nothing to process, so exit with message. This loop computes A[0:rank-1][rank:colDim-1] = inverse(Ur)*UR where UR is stored in A[0:rank-1][rank:colDim-1] and Ur is stored in upper part of A[0:rank-1][0:rank-1]. i is the row number in Ur. j is the column number in Ur & row number in UR. k is the column number in UR.

Parameters:
  • rank – [in] rank of matrix A

  • colDim – [in] column dimension of matrix A

  • A – [in,out] row major matrix factored by ksl_linalg_lu_full_rmo()

void ksl_linalg_lu_setCMatrix_rmo(int rowDim, int colDim, int rank, double *A)

compute Row Major Order C matrix

This block overwrites LR with LR*inverse(Lr) If rank==1, the Lr matrix is a 1 by 1 identity matrix, so there is nothing to do here. This loop computes A[rank:rowDim-1][0:rank-1]=LR*inverse(Lr) where LR is stored in A[rank:rowDim-1][0:rank-1] and Lr is stored in A[0:rank-1][0:rank-1]. j is the column number in LR. It ends at 1 because the diagonal entry in row 0 of Lr is 1. i is the row number in LR. k is the column number in Lr

Parameters:
  • rowDim – [in] row dimension of matrix A.

  • colDim – [in] column dimension of matrix A.

  • rank – [in] rank of matrix A.

  • *A – [in/out] matrix with dimensions A[0:rowDim-1][0:colDim-1]:

int ksl_linalg_lu_full_multiSpecified_rmo(const int rowDim, const int colDim, double *A, double eps, int *pr, int *pc, const int specifiedCount, const int *specifiedIndices)

Row Major Order LU Decomposition with complete row and column pivoting with a set of specified coordinates.

ksl_linalg_lu_full_multiSpecified_rmo factors a double precision matrix, A[rowDim * colDim], stored in Row Major Order using full row and column pivoting. Matrix A need not have full row or column rank. The integer variable ‘rank’ represents the matrix rank and internally is always one less than the true rank to be consistent with C’s indexing from zero. Upon return from the function, rank will be set to the correct value.

The lower triangular (rank+1) by (rank+1) Lr matrix, except its unity diagonal, is stored below the diagonal in A[0:rank][0:rank]. The upper triangular (rank+1) by (rank+1) Ur matrix is stored on and above the diagonal in A[0:rank][0:rank].

Following the first major matrix decomposition step, A[0:rank][rank+1:colDim] stores the residual matrix UR and A[rank+1:rowDim][0:rank] stores the residual matrix LR.

ksl_linalg_lu_setBMatrix_rmo() computes and stores B = inverse(Ur)*UR and stores it back in A[0:rank][rank+1:colDim]. ksl_linalg_lu_setCMatrix_rmo() computes and stores C = LR*inverse(Lr) and stores it back in A[rank+1:rowDim][0:rank].

The remaining A[rank+1:rowDim][rank+1:colDim-1] submatrix contains numbers whose absolute values are all smaller than eps times the absolutely largest entry in the original matrix. This part of the matrix is not used. The specified columns are moved to the last specifiedCount columns of A and are excluded from the pivot search, so the specified coordinates are always independent (they never become pivotal, dependent coordinates). The remaining A[rank+1:rowDim][colDim-specifiedCount:colDim] submatrix could contain numbers whose absolute values are greater than eps times the absolutely largest entry in the original matrix. Adding a check here would be one way to assert that the specified variables have sufficient mobility.

Parameters:
  • rowDim – input row dimension of matrix A.

  • colDim – input column dimension of matrix A.

  • **A – input/output matrix to be factored with dimensions A[0:rowDim-1][0:colDim-1]:

  • eps – input tolerance on the order of machine roundoff.

  • pr – output row permutation array with dimensions pr[0:rowDim-1]

  • pc – output column permutation array with dimensions pc[0:colDim-1]

  • specifiedCount – input number of specified columns, 1 <= specifiedCount < colDim

  • specifiedIndices – input distinct column indices of the specified coordinates, specifiedIndices[0:specifiedCount-1]

Returns:

rank of matrix A, -1 on bad input

inline double ksl_linalg_lu_infinityNormB_rmo(int rank, int colDim, double *A)

Return the largest absolute value in the B matrix of a factored row major matrix.

B is stored in A[0:rank-1][rank:colDim-1]. A large value indicates that the partition into dependent and independent columns is poorly conditioned.

Parameters:
Returns:

maximum absolute entry of B

void ksl_linalg_lu_solve_rmo(const int rank, const int colDim, const double *A, const double *b, double *x)

Solve \(L_r U_r \underline{x} = \underline{b}\) with the factors of ksl_linalg_lu_full_rmo().

\(L_r\) (unit diagonal, not stored) and \(U_r\) are the rank by rank factors in A[0:rank-1][0:rank-1], which ksl_linalg_lu_setBMatrix_rmo() and ksl_linalg_lu_setCMatrix_rmo() leave unchanged. The solution is a forward substitution,

\[ y_i = b_i - \sum_{j < i} l_{ij}\,y_j, \]

followed by a back substitution,

\[ x_i = \frac{1}{u_{ii}}\Bigl(y_i - \sum_{j > i} u_{ij}\,x_j\Bigr). \]

The rows of the system are in pivoted order, so that b[i] is the entry of the original right side in row pr[i], and x[i] is the value of the variable of column pc[i]. With b[i] = A_orig[pr[i]][j], for instance, x is column j of \(\begin{bmatrix} I & B \end{bmatrix}\) in pivoted column order.

Parameters:
  • rank – [in] rank r returned by ksl_linalg_lu_full_rmo()

  • colDim – [in] column dimension of A

  • A – [in] factored matrix

  • b – [in] right side, r entries in pivoted row order

  • x – [out] solution, r entries; may be the same array as b

column major order

ksl_linalg_lu_full_cmo()

Column Major Order LU Decomposition with complete row and column pivoting.

ksl_linalg_lu_full_specified_cmo()

Column Major Order LU Decomposition with complete row and column pivoting with one specified coordinate.

ksl_linalg_lu_cmo()

Fast Column Major Order LU Decomposition with no row or column pivoting for a rectangular matrix

ksl_linalg_lu_setBMatrix_cmo()

compute Column Major Order B matrix (inverse(Ur) * UR)

ksl_linalg_lu_setCMatrix_cmo()

compute Column Major Order C matrix

int ksl_linalg_lu_full_cmo(const int rowDim, const int colDim, double *A, const double eps, int *pr, int *pc)

Column Major Order LU Decomposition with complete row and column pivoting.

ksl_linalg_lu_full_cmo factors a double precision matrix, A[rowDim * colDim], stored in Column Major Order using full row and column pivoting. Matrix A need not have full row or column rank. The integer variable ‘rank’ represents the matrix rank and internally is always one less than the true rank to be consistent with C’s indexing from zero. Upon return from the function, rank will be set to the correct value.

The lower triangular (rank+1) by (rank+1) Lr matrix, except its unity diagonal, is stored below the diagonal in A[0:rank][0:rank]. The upper triangular (rank+1) by (rank+1) Ur matrix is stored on and above the diagonal in A[0:rank][0:rank].

Following the first major matrix decomposition step, A[0:rank][rank+1:colDim] stores the residual matrix UR and A[rank+1:rowDim][0:rank] stores the residual matrix LR.

The following operations on this matrix are optionally performed after ksl_linalg_lu_full_cmo() has factored A.

ksl_linalg_lu_setBMatrix_cmo() computes and stores B = inverse(Ur)*UR and stores it back in A[0:rank][rank+1:colDim]. ksl_linalg_lu_setCMatrix_cmo() computes and stores C = LR*inverse(Lr) and stores it back in A[rank+1:rowDim][0:rank].

The remaining A[rank+1:rowDim][rank+1:colDim] submatrix contains numbers whose absolute values are all smaller than eps times the absolutely largest entry in the original matrix. This part of the matrix is not used.

Parameters:
  • rowDim – [in] row dimension of matrix A.

  • colDim – [in] column dimension of matrix A.

  • *A – [in/out] matrix to be factored with dimensions A[0:rowDim-1][0:colDim-1], LU factors are overwritten in original matrix

  • eps – [in] input tolerance on the order of machine roundoff.

  • pr – [out] output row permutation array with dimensions pr[0:rowDim-1]

  • pc – [out] output column permutation array with dimensions pc[0:colDim-1]

Returns:

rank of matrix A

int ksl_linalg_lu_full_specified_cmo(const int rowDim, const int colDim, double *A, double eps, int *pr, int *pc, const int specifiedIndex)

Column Major Order LU Decomposition with complete row and column pivoting with one specified coordinate.

ksl_linalg_lu_full_specified_cmo factors a double precision matrix, A[rowDim * colDim], stored in Column Major Order using full row and column pivoting. Matrix A need not have full row or column rank. The integer variable ‘rank’ represents the matrix rank and internally is always one less than the true rank to be consistent with C’s indexing from zero. Upon return from the function, rank will be set to the correct value.

The lower triangular (rank+1) by (rank+1) Lr matrix, except its unity diagonal, is stored below the diagonal in A[0:rank][0:rank]. The upper triangular (rank+1) by (rank+1) Ur matrix is stored on and above the diagonal in A[0:rank][0:rank].

Following the first major matrix decomposition step, A[0:rank][rank+1:colDim] stores the residual matrix UR and A[rank+1:rowDim][0:rank] stores the residual matrix LR.

ksl_linalg_lu_setBMatrix_cmo() computes and stores B = inverse(Ur)*UR and stores it back in A[0:rank][rank+1:colDim]. ksl_linalg_lu_setCMatrix_cmo() computes and stores C = LR*inverse(Lr) and stores it back in A[rank+1:rowDim][0:rank].

The remaining A[rank+1:rowDim][rank+1:colDim-1] submatrix contains numbers whose absolute values are all smaller than eps times the absolutely largest entry in the original matrix. This part of the matrix is not used. The remainig A[rank+1:rowDim][colDim] submatrix could contain numbers whose absolute values are greater than eps times the absolutely largest entry in the original matrix. Adding a check here would be one way to assert that the specified variable has sufficient mobility.

Parameters:
  • rowDim – input row dimension of matrix A.

  • colDim – input column dimension of matrix A.

  • **A – input/output matrix to be factored with dimensions A[0:rowDim-1][0:colDim-1]:

  • eps – input tolerance on the order of machine roundoff.

  • pr – output row permutation array with dimensions pr[0:rowDim-1]

  • pc – output column permutation array with dimensions pc[0:colDim-1]

  • specifiedIndex – input column index of the specified coordinate

Returns:

rank of matrix A

inline void ksl_linalg_lu_cmo(const int rank, const int colDim, double *A)

Fast Column Major Order LU Decomposition with no row or column pivoting for a rectangular matrix

This function LU factors a double precision matrix, A[rank][colDim], using no row or column pivoting. This function should be used only if the left rank by rank submatrix is known to be nonsingular.

rank <= colDim or error.

The lower triangular rank by rank Lr matrix, except the unity diagonal, is stored below the diagonal in A[0:rank-1][0:rank-1]. The upper triangular rank by rank Ur matrix is stored on and above the diagonal in A[0:rank-1][0:rank-1].

Note that this minimal algorithm variation does not compute the product B = inverse(Ur)*UR

Parameters:
  • rank – [in] row dimension of matrix A.

  • colDim – [in] column dimension of matrix A.

  • *A – [in/out] matrix to be factored with dimensions A[0:rank-1][0:colDim-1]:

inline void ksl_linalg_lu_setBMatrix_cmo(const int rowDim, const int colDim, const int rank, double *A)

compute Column Major Order B matrix (inverse(Ur) * UR)

This block overwrites UR with inverse(Ur) * UR If rank == 0 or rank == colDim nothing to process, so exit with message. This loop computes A[0:rank-1][rank:colDim-1] = inverse(Ur)*UR where UR is stored in A[0:rank-1][rank:colDim-1] and Ur is stored in upper part of A[0:rank-1][0:rank-1]. i is the row number in Ur. j is the column number in Ur & row number in UR. k is the column number in UR.

Parameters:
  • rowDim – [in] row dimension of matrix A.

  • colDim – [in] column dimension of matrix A.

  • rank – [in] rank of matrix A.

  • *A – [in/out] matrix with dimensions A[0:rowDim-1][0:colDim-1]:

void ksl_linalg_lu_setCMatrix_cmo(int rowDim, int colDim, int rank, double *A)

compute Column Major Order C matrix

This block overwrites LR with LR*inverse(Lr) If rank==1, the Lr matrix is a 1 by 1 identity matrix, so there is nothing to do here. This loop computes A[rank:rowDim-1][0:rank-1]=LR*inverse(Lr) where LR is stored in A[rank:rowDim-1][0:rank-1] and Lr is stored in A[0:rank-1][0:rank-1]. j is the column number in LR. It ends at 1 because the diagonal entry in row 0 of Lr is 1. i is the row number in LR. k is the column number in Lr

Parameters:
  • rowDim – [in] row dimension of matrix A.

  • colDim – [in] column dimension of matrix A.

  • rank – [in] rank of matrix A.

  • *A – [in/out] matrix with dimensions A[0:rowDim-1][0:colDim-1]:

Cholesky Decomposition

row major order

ksl_linalg_cholesky_rmo()

perform Row Major Order Cholesky (L * L^T) decomposition of a symmetric matrix without pivoting

ksl_linalg_cholesky_forwardElimination_rmo()

used to solve a system of equations

ksl_linalg_cholesky_backwardSubstitution_rmo()

used to solve a system of equations

ksl_linalg_cholesky_solve_rmo()

Solve the system of equations A * x = b where A is Row Major Order and symmetric positive definite.

int ksl_linalg_cholesky_rmo(double *A, const int n)

perform Row Major Order Cholesky (L * L^T) decomposition of a symmetric matrix without pivoting

returns the matrix factor L in the lower triangular portion of matrix A

Parameters:
  • *A – [in/out] matrix with dimensions A[0:n-1][0:n-1]:

  • n – row and column dimension of matrix A.

inline void ksl_linalg_cholesky_forwardElimination_rmo(const double *L, const double *b, double *y, const int n)

used to solve a system of equations

L * y = b

for y using forward elimination where L is Row Major Order and IS NOT unit lower triangular

y = L^-1 * b

Parameters:
  • *L – [in] n by n matrix L[0:n-1][0:n-1]:

  • *b – [in] n by 1 column of right-hand side y[0:n-1].

  • *y – [out] n by 1 column of unknowns.

  • n – [in] dimension of L[][], b[], y[]:

inline void ksl_linalg_cholesky_backwardSubstitution_rmo(const double *L, const double *y, double *x, const int n)

used to solve a system of equations

L^T * x = y

using backward substitution where L is Row Major Order and IS NOT unit lower triangular

x = L^-T * y

Parameters:
  • *L – [in] n by n matrix L[0:n-1][0:n-1]:

  • *y – [in] n by 1 column of right-hand side y[0:n-1].

  • *x – [out] n by 1 column of unknowns.

  • n – [in] dimension of L[][], y[], x[]:

inline void ksl_linalg_cholesky_solve_rmo(const double *A, const double *b, double *x, const int n)

Solve the system of equations A * x = b where A is Row Major Order and symmetric positive definite.

ksl_linalg_cholesky must be called on A prior to calling this function

Parameters:
  • A – [in] n by n matrix A[0:n-1][0:n-1], factored by ksl_linalg_cholesky_rmo()

  • b – [in] n by 1 right-hand side b[0:n-1]

  • x – [out] n by 1 solution x[0:n-1]

  • n – [in] dimension of A, b and x

column major order

ksl_linalg_cholesky_cmo()

perform Column Major Order Cholesky (L * L^T) decomposition of a symmetric matrix without pivoting

ksl_linalg_cholesky_forwardElimination_cmo()

used to solve a system of equations

ksl_linalg_cholesky_backwardSubstitution_cmo()

used to solve a system of equations

ksl_linalg_cholesky_solve_cmo()

solve the system of equations A * x = b where A is Column Major Order and a symmetric positive definite matrix A

int ksl_linalg_cholesky_cmo(double *A, const int n)

perform Column Major Order Cholesky (L * L^T) decomposition of a symmetric matrix without pivoting

returns the matrix factor L in the lower triangular portion of matrix A

Parameters:
  • *A – [in/out] matrix with dimensions A[0:n-1][0:n-1]:

  • n – row and column dimension of matrix A.

inline void ksl_linalg_cholesky_forwardElimination_cmo(const double *L, const double *b, double *y, const int n)

used to solve a system of equations

L * y = b

for y using forward elimination where L is Column Major Order and IS NOT unit lower triangular

y = L^-1 * b

Parameters:
  • *L – [in] n by n matrix L[0:n-1][0:n-1]:

  • *b – [in] n by 1 column of right-hand side y[0:n-1].

  • *y – [out] n by 1 column of unknowns.

  • n – [in] dimension of L[][], b[], y[]:

inline void ksl_linalg_cholesky_backwardSubstitution_cmo(const double *L, const double *y, double *x, const int n)

used to solve a system of equations

L^T * x = y

using backward substitution where L is Column Major Order and IS NOT unit lower triangular

x = L^-T * y

Parameters:
  • *L – [in] n by n matrix L[0:n-1][0:n-1]:

  • *y – [in] n by 1 column of right-hand side y[0:n-1].

  • *x – [out] n by 1 column of unknowns.

  • n – [in] dimension of L[][], y[], x[]:

inline void ksl_linalg_cholesky_solve_cmo(const double *A, const double *b, double *x, const int n)

solve the system of equations A * x = b where A is Column Major Order and a symmetric positive definite matrix A

ksl_linalg_cholesky must be called on A prior to calling this function

Parameters:
  • *A – [in] n by n matrix A[0:n-1][0:n-1] containing Choleski-factored matrix:

  • *b – [in] n by 1 column of right-hand side y[0:n-1].

  • *x – [out] n by 1 column of unknowns.

  • n – [in] dimension of L[][], b[], y[]:

LDLT Decomposition

row major order

ksl_linalg_ldlt_rmo()

computes the Row Major Order L D L^T decomposition of a symmetric positive definite matrix without pivoting

ksl_linalg_ldlt_forwardElimination_rmo()

used to solve a system of equations

ksl_linalg_ldlt_backwardSubstitution_rmo()

used to solve a system of equations

ksl_linalg_ldlt_solve_rmo()

Solve the system of equations A * x = b where A is Row Major Order and a symmetric positive definite matrix A.

int ksl_linalg_ldlt_rmo(double *A, const int n)

computes the Row Major Order L D L^T decomposition of a symmetric positive definite matrix without pivoting

returns the matrix L D L^T in the original matrix A

Parameters:
  • *A – [in/out] matrix with dimensions A[0:n-1][0:n-1]:

  • n – row and column dimension of matrix A.

inline void ksl_linalg_ldlt_forwardElimination_rmo(const double *L, const double *b, double *y, const int n)

used to solve a system of equations

L * y = b

for y using forward elimination where L IS Row Major Order unit lower triangular

y = L^-1 * b

Parameters:
  • *L – [in] n by n matrix L[0:n-1][0:n-1]:

  • *b – [in] n by 1 column of right-hand side y[0:n-1].

  • *y – [out] n by 1 column of unknowns.

  • n – [in] dimension of L[][], b[], y[]:

inline void ksl_linalg_ldlt_backwardSubstitution_rmo(const double *L, const double *b, double *x, const int n)

used to solve a system of equations

L^T * x = y

using backward substitution where L IS Row Major Order unit lower triangular

x = L^-T * y

Parameters:
  • *L – [in] n by n matrix L[0:n-1][0:n-1]:

  • *y – [in] n by 1 column of right-hand side b[0:n-1].

  • *x – [out] n by 1 column of unknowns.

  • n – [in] dimension of L[][], y[], x[]:

inline void ksl_linalg_ldlt_solve_rmo(const double *A, const double *b, double *x, const int n)

Solve the system of equations A * x = b where A is Row Major Order and a symmetric positive definite matrix A.

ksl_linalg_ldlt must be called on A prior to calling this function

Parameters:
  • *A – [in] n by n matrix A[0:n-1][0:n-1] containing Choleski-factored matrix:

  • *b – [in] n by 1 column of right-hand side y[0:n-1].

  • *x – [out] n by 1 column of unknowns.

  • n – [in] dimension of L[][], b[], y[]:

column major order

ksl_linalg_ldlt_cmo()

computes the Column Major Order L D L^T decomposition of a symmetric matrix without pivoting

ksl_linalg_ldlt_forwardElimination_cmo()

used to solve a system of equations

ksl_linalg_ldlt_backwardSubstitution_cmo()

used to solve a system of equations

ksl_linalg_ldlt_solve_cmo()

solve the system of equations A * x = b where A is Column Major Order and a symmetric positive definite matrix A

int ksl_linalg_ldlt_cmo(double *A, const int n)

computes the Column Major Order L D L^T decomposition of a symmetric matrix without pivoting

returns the matrix L D L^T in the original matrix A

Parameters:
  • *A – [in/out] matrix with dimensions A[0:n-1][0:n-1]:

  • n – row and column dimension of matrix A.

inline void ksl_linalg_ldlt_forwardElimination_cmo(const double *L, const double *b, double *y, const int n)

used to solve a system of equations

L * y = b

for y using forward elimination where L IS Column Major Order unit lower triangular

y = L^-1 * b

Parameters:
  • *L – [in] n by n matrix L[0:n-1][0:n-1]:

  • *b – [in] n by 1 column of right-hand side y[0:n-1].

  • *y – [out] n by 1 column of unknowns.

  • n – [in] dimension of L[][], b[], y[]:

inline void ksl_linalg_ldlt_backwardSubstitution_cmo(const double *L, const double *b, double *x, const int n)

used to solve a system of equations

L^T * x = y

using backward substitution where L IS Column Major Order unit lower triangular

x = L^-T * y

Parameters:
  • *L – [in] n by n matrix L[0:n-1][0:n-1]:

  • *y – [in] n by 1 column of right-hand side y[0:n-1].

  • *x – [out] n by 1 column of unknowns.

  • n – [in] dimension of L[][], y[], x[]:

inline void ksl_linalg_ldlt_solve_cmo(const double *A, double *const b, double *x, const int n)

solve the system of equations A * x = b where A is Column Major Order and a symmetric positive definite matrix A

ksl_linalg_ldlt_cmo must be called on A prior to calling this function

Parameters:
  • *A – [in] n by n matrix A[0:n-1][0:n-1] containing Choleski-factored matrix:

  • *b – [in] n by 1 column of right-hand side y[0:n-1].

  • *x – [out] n by 1 column of unknowns.

  • n – [in] dimension of L[][], b[], y[]:

Symmetric Matrix Inverse

row major order

inline int ksl_linalg_symmetricMatrixInverse_rmo(double *A, const int n)

Compute inverse of a symmetric positive definite matrix A where A is in Row Major Order.

column major order

inline int ksl_linalg_symmetricMatrixInverse_cmo(double *A, const int n)

compute inverse of a symmetric positive definite matrix A where A and A_inverse are in Column Major Order.

Householder QR

row major order

void ksl_linalg_qr_householder_rmo(const int m, const int n, double *A, double *Q)

Householder QR factorization of a row major m by n matrix.

Computes \(A = QR\) with \(Q\) an m by m orthogonal matrix and \(R\) m by n and upper triangular. Column k of A is reflected by \(H_k = I - 2\underline{v}_k\underline{v}_k^{T} /\underline{v}_k^{T}\underline{v}_k\), with \(\underline{v}_k = \underline{a}_k + \mathrm{sign} (a_{kk})\,\lVert\underline{a}_k\rVert\,\underline{e}_k\) over rows k to m - 1, so that \(R = H_{p-1}\cdots H_0 A\) and \(Q = H_0\cdots H_{p-1}\), p = min(n, m - 1). A column that is already zero below the diagonal is skipped.

Parameters:
  • m – [in] row dimension

  • n – [in] column dimension

  • A – [in,out] m by n matrix, replaced by R

  • Q – [out] m by m orthogonal factor

column major order

void ksl_linalg_qr_householder_cmo(const int m, const int n, double *A, double *Q)

Column major version of ksl_linalg_qr_householder_rmo().

Singular Value Decomposition (one-sided Jacobi)

row major order

int ksl_linalg_svd_jacobi_rmo(const int m, const int n, double *A, double *sigma, double *V)

Singular value decomposition of a row major m by n matrix by one-sided (Hestenes) Jacobi rotations.

Rotates pairs of columns of A until they are orthogonal: on return \(W = AV\) has orthogonal columns in nonincreasing order of length, the singular values \(\sigma_j = \lVert\underline{w}_j\rVert\), and V is n by n orthogonal, so that \(A = W V^{T}\) and \(\underline{u}_j = \underline{w}_j/\sigma_j\) for \(\sigma_j > 0\). For columns i and j with \(b_{ii} = \underline{w}_i^{T}\underline{w}_i \ge b_{jj}\) and \(b_{ij} = \underline{w}_i^{T}\underline{w}_j\), the rotation has \(\alpha = 2b_{ij}\), \(\beta = b_{ii} - b_{jj}\), \(\gamma = \mathrm{sign}(\beta) \sqrt{\alpha^{2} + \beta^{2}}\), \(c = \sqrt{(\beta + \gamma)/2\gamma}\) and \(s = \alpha/(2\gamma c)\). The columns of V for zero singular values span the null space of A. Sweeps stop when every pair satisfies \(|b_{ij}| \le \varepsilon\sqrt{b_{ii}b_{jj}}\) or \(b_{jj} \le \varepsilon^{2} b_{ii}\) (the shorter column negligible, so that the rotation is within \(\varepsilon\) of the identity), with \(\varepsilon = 10^{-15}\), or after 60 sweeps.

Parameters:
  • m – [in] row dimension

  • n – [in] column dimension

  • A – [in,out] m by n matrix, replaced by W = A V

  • sigma – [out] n singular values, nonincreasing

  • V – [out] n by n orthogonal matrix

Returns:

number of sweeps, or -1 if 60 sweeps did not converge

column major order

int ksl_linalg_svd_jacobi_cmo(const int m, const int n, double *A, double *sigma, double *V)

Column major version of ksl_linalg_svd_jacobi_rmo().

Null Spaces and Mobility Numbers

ksl_linalg_lu_nullSpace_rmo()

Null space basis of a row major matrix factored by ksl_linalg_lu_full_rmo().

ksl_linalg_lu_leftNullSpace_rmo()

Left null space basis of a row major matrix factored by ksl_linalg_lu_full_rmo().

ksl_linalg_mobilityNumbers_rmo()

Mobility numbers of n coordinates from a null space basis.

void ksl_linalg_lu_nullSpace_rmo(const int rank, const int colDim, const double *A, const int *pc, double *Z)

Null space basis of a row major matrix factored by ksl_linalg_lu_full_rmo().

With the rank r, the column permutation pc, and B in A[0:r-1][r:n-1] from ksl_linalg_lu_setBMatrix_rmo(), the n by (n - r) basis is

\[\begin{split} Z = P_c^{T}\begin{bmatrix} -B \\ I \end{bmatrix}, \end{split}\]

row pc[i] of Z holding row i of \(-B\) for i < r and row pc[r + j] the unit row \(\underline{e}_j^{T}\), so that \(AZ = 0\) for the original A.

Parameters:
void ksl_linalg_lu_leftNullSpace_rmo(const int rowDim, const int colDim, const int rank, const double *A, const int *pr, double *Y)

Left null space basis of a row major matrix factored by ksl_linalg_lu_full_rmo().

With the rank r, the row permutation pr, and C in A[r:m-1][0:r-1] from ksl_linalg_lu_setCMatrix_rmo(), the (m - r) by m basis is

\[ Y = \begin{bmatrix} -C & I \end{bmatrix} P_r, \]

column pr[j] of Y holding column j of \(-C\) for j < r and column pr[r + i] the unit column \(\underline{e}_i\), so that \(YA = 0\) for the original A.

Parameters:
void ksl_linalg_mobilityNumbers_rmo(const int n, const int k, double *Z, double *mu)

Mobility numbers of n coordinates from a null space basis.

Orthonormalizes the columns of the n by k basis Z in place with ksl_linalg_gramSchmidt() and returns the diagonal of the projector onto the null space,

\[ \mu_i = \bigl(ZZ^{T}\bigr)_{ii} = \sum_{j=0}^{k-1} z_{ij}^{2}, \qquad 0 \le \mu_i \le 1, \qquad \sum_{i} \mu_i = k, \]

which does not depend on the basis chosen. \(\mu_i = 0\) for a coordinate that cannot move and \(\mu_i = 1\) for one that moves independently of all others.

Parameters:
  • n – [in] number of coordinates (rows of Z)

  • k – [in] dimension of the null space (columns of Z), full column rank

  • Z – [in,out] n by k row major basis, orthonormalized in place

  • mu – [out] n mobility numbers