Functions
Matrix Mathematics
Linear Algebra
Collaboration diagram for Matrix Mathematics:

Functions

static int inv2 (const double *a, double *b)
static int inv3 (const double *a, double *b)
static int inv4 (const double *a, double *b)
void matrix_add_sc (u32 n, u32 m, const double *a, const double *b, double gamma, double *c)
int matrix_ataati (u32 n, u32 m, const double *a, double *b)
int matrix_ataiat (u32 n, u32 m, const double *a, double *b)
int matrix_atawati (u32 n, u32 m, const double *a, const double *w, double *b)
int matrix_atwaiat (u32 n, u32 m, const double *a, const double *w, double *b)
void matrix_copy (u32 n, u32 m, const double *a, double *b)
int matrix_inverse (u32 n, const double const *a, double *b)
void matrix_multiply (u32 n, u32 m, u32 p, const double *a, const double *b, double *c)
int matrix_pseudoinverse (u32 n, u32 m, const double *a, double *b)
void matrix_transpose (u32 n, u32 m, const double *a, double *b)
s32 qrdecomp (const double *a, u32 rows, u32 cols, double *qt, double *r)
s32 qrdecomp_square (const double *a, u32 rows, double *qt, double *r)
s32 qrsolve (const double *a, u32 rows, u32 cols, const double *b, double *x)
void qtmult (const double *qt, u32 n, const double *b, double *x)
static void row_swap (double *a, double *b, u32 size)
static int rref (u32 order, u32 cols, double *m)
void rsolve (const double *r, u32 rows, u32 cols, const double *b, double *x)

Detailed Description

Routines for working with matrices.


Function Documentation

static int inv2 ( const double *  a,
double *  b 
) [inline, static]

Invert a 2x2 matrix. Calculate the inverse of a 2x2 matrix: $ b := a^{-1} $

Parameters:
aThe matrix to invert (input)
bWhere to put the inverse (output)
Returns:
-1 if a is singular; 0 otherwise.

Definition at line 384 of file linear_algebra.c.

static int inv3 ( const double *  a,
double *  b 
) [inline, static]

Invert a 3x3 matrix. Calculate the inverse of a 3x3 matrix: $ b := a^{-1} $

Parameters:
aThe matrix to invert (input)
bWhere to put the inverse (output)
Returns:
-1 if a is singular; 0 otherwise.

Definition at line 403 of file linear_algebra.c.

static int inv4 ( const double *  a,
double *  b 
) [inline, static]

Invert a 4x4 matrix. Calculate the inverse of a 4x4 matrix: $ b := a^{-1} $

Parameters:
aThe matrix to invert (input)
bWhere to put the inverse (output)
Returns:
-1 if a is singular; 0 otherwise.

Definition at line 434 of file linear_algebra.c.

void matrix_add_sc ( u32  n,
u32  m,
const double *  a,
const double *  b,
double  gamma,
double *  c 
)

Add a matrix to a scaled matrix. Add two matrices: $ C := A + \gamma B $, where $ A $, $ B $ and $C$ are matrices on $\mathbb{R}^{n \times m}$ and $\gamma$ is a scalar coefficient.

Parameters:
nNumber of rows in a, b and c
mNumber of columns in a, b and c
aFirst matrix (unscaled)
bSecond matrix (will be scaled)
gammaCoefficient for second matrix
cOutput (sum) matrix

Definition at line 805 of file linear_algebra.c.

int matrix_ataati ( u32  n,
u32  m,
const double *  a,
double *  b 
) [inline]

Compute $ B := A^{T} (A A^{T})^{-1} $. Compute $ B := A^{T} (A A^{T})^{-1} $, where $ A $ is a matrix on $\mathbb{R}^{n \times m}$ and $B$ is (therefore) a matrix on $\mathbb{R}^{n \times n}$, for $ n < m $.

Parameters:
nNumber of rows in a and rows and columns in b
mNumber of columns in a
aInput matrix
bOutput matrix
Returns:
-1 if n >= m or singular; 0 otherwise

Definition at line 762 of file linear_algebra.c.

int matrix_ataiat ( u32  n,
u32  m,
const double *  a,
double *  b 
) [inline]

Compute $ B := (A^{T} A)^{-1} A^{T} $. Compute $ B := (A^{T} A)^{-1} A^{T} $, where $ A $ is a matrix on $\mathbb{R}^{n \times m}$ and $B$ is (therefore) a matrix on $\mathbb{R}^{m \times m}$, for $ n > m $.

Parameters:
nNumber of rows in a
mNumber of columns in a and rows and columns in b
aInput matrix
bOutput matrix
Returns:
-1 if n < m; 0 otherwise

Definition at line 743 of file linear_algebra.c.

int matrix_atawati ( u32  n,
u32  m,
const double *  a,
const double *  w,
double *  b 
) [inline]

Compute $ B := A^T (A W A^{T})^{-1} $. Compute $ B := A^T (A W A^{T})^{-1} $, where $ A $ is a matrix on $\mathbb{R}^{n \times m}$, $ W $ is a diagonal weighting matrix on $\mathbb{R}^{m \times m}$ and $B$ is (therefore) a matrix on $\mathbb{R}^{n \times n}$, for $ n < m $.

Parameters:
nNumber of rows in a and rows and columns in b
mNumber of columns in a
aInput matrix
wDiagonal vector of weighting matrix
bOutput matrix
Returns:
-1 if n <= m or singular; 0 otherwise

Definition at line 696 of file linear_algebra.c.

int matrix_atwaiat ( u32  n,
u32  m,
const double *  a,
const double *  w,
double *  b 
) [inline]

Compute $ B := (A^{T} W A)^{-1} A^{T} $. Compute $ B := (A^{T} W A)^{-1} A^{T} $, where $ A $ is a matrix on $\mathbb{R}^{n \times m}$, $ W $ is a diagonal weighting matrix on $\mathbb{R}^{n \times n}$ and $B$ is (therefore) a matrix on $\mathbb{R}^{m \times m}$, for $ n > m $.

Parameters:
nNumber of rows in a
mNumber of columns in a and rows and columns in b
aInput matrix
wDiagonal vector of weighting matrix
bOutput matrix
Returns:
-1 if n <= m or singular; 0 otherwise

Definition at line 647 of file linear_algebra.c.

void matrix_copy ( u32  n,
u32  m,
const double *  a,
double *  b 
)

Copy a matrix. Copy a matrix: $ B := A $, where $A$ and $B$ are matrices on $\mathbb{R}^{n \times m}$.

Parameters:
nNumber of rows in $A$ and $B$
mNumber of columns in $A$ and $B$
aMatrix to copy
bCopied (output) matrix

Definition at line 840 of file linear_algebra.c.

int matrix_inverse ( u32  n,
const double const *  a,
double *  b 
) [inline]

Invert a square matrix. Calculate the inverse of a square matrix: $ B := A^{-1} $, where $A$ and $B$ are matrices on $\mathbb{R}^{n \times n}$. For matrices size 4x4 and smaller, this is done by autogenerated hard-coded routines. For larger matrices, this is done by Gauss-Jordan elimination (which is $ O(n^{3}) $).

Parameters:
nThe rank of a and b
aThe matrix to invert (input)
bWhere to put the inverse (output)
Returns:
-1 if a is singular; 0 otherwise.

Definition at line 534 of file linear_algebra.c.

void matrix_multiply ( u32  n,
u32  m,
u32  p,
const double *  a,
const double *  b,
double *  c 
) [inline]

Multiply two matrices. Multiply two matrices: $ C := AB $, where $ A $ is a matrix on $\mathbb{R}^{n \times m}$, $B$ is a matrix on $\mathbb{R}^{m \times p}$ and $C$ is (therefore) a matrix in $\mathbb{R}^{n \times p}$.

Parameters:
nNumber of rows in a and c
mNumber of columns in a and rows in b
pNumber of columns in b and c
aFirst matrix to multiply
bSecond matrix to multiply
cOutput matrix

Definition at line 782 of file linear_algebra.c.

int matrix_pseudoinverse ( u32  n,
u32  m,
const double *  a,
double *  b 
)

Invert a non-square matrix (least-squares or least-norm solution). If $ A $ is of full rank, calculate the Moore-Penrose pseudoinverse $ A^{+} $ of a square matrix:

\[ A \in \mathbb{R}^{n \times m} \]

\[ B := A^{+} = \begin{cases} (A^{T} A)^{-1} A^{T} & \text{if } n > m \\ A^{T} (A A^{T})^{-1} & \text{if } m > n \\ A^{-1} & \text{if } n = m \end{cases} \]

If $ n > m $, then $ A $ must be of full column rank, and $ A^{+} $ solves the linear least-squares (overconstrained) problem:

\[ x' = A^{+} b = \underset{x}{min} \|Ax - b\|_{2} \]

If $ m > n $, then $ A $ must be of full row rank, and $ A^{+} $ solves the linear least-norm (underconstrained) problem:

\[ x' = A^{+} b = \underset{x}{min} \|x\|_{2} \text{s.t. } Ax = b \]

If $ m = n $, then $ A $ must be of full rank, and $ A^{+} = A^{-1} $.

Parameters:
nThe number of rows in a
mThe number of columns in a
aThe matrix to invert (input)
bWhere to put the inverse (output)
Returns:
-1 if a is singular; 0 otherwise.

Definition at line 622 of file linear_algebra.c.

void matrix_transpose ( u32  n,
u32  m,
const double *  a,
double *  b 
)

Transpose a matrix. Transpose a matrix: $ B := A^{T} $, where $A$ is a matrix on $\mathbb{R}^{n \times m}$ and $B$ is (therefore) a matrix on $ \mathbb{R}^{m \times n}$.

Parameters:
nNumber of rows in $A$ and columns in $B$
mNumber of rows in $B$ and columns in $A$
aMatrix to transpose
bTransposed (output) matrix

Definition at line 823 of file linear_algebra.c.

s32 qrdecomp ( const double *  a,
u32  rows,
u32  cols,
double *  qt,
double *  r 
)

QR decomposition of a matrix. Compute the QR decomposition of an arbitrary matrix $ A \in \mathbb{R}^{N \times M} $: $ A = Q \cdot R $ where $ Q \in \mathbb{R}^{N \times N} $ is an orthogonal matrix and $ R \in \mathbb{R}^{N \times M} $ is an upper-triangular matrix.

For an overdetermined (least-squares) problem, $ R $ will be of the form

\[ \begin{bmatrix} R' \\ 0 \end{bmatrix} \]

where $ R' $ is an upper-triangular matrix on $ \mathbb{R}^{M \times M} $.

Parameters:
AThe matrix $ A $ to decompose (input)
rowsHow many rows in A
colsHow many columns in A
qt$ Q^{T} $ (output)
r$ R $ (output)
Returns:
-1 if A is singular; 0 otherwise;

Definition at line 103 of file linear_algebra.c.

s32 qrdecomp_square ( const double *  a,
u32  rows,
double *  qt,
double *  r 
)

QR decomposition of a square matrix. Compute the QR decomposition of a square matrix $ A \in \mathbb{R}^{N \times N} $: $ A = Q \cdot R $ where $ Q \in \mathbb{R}^{N \times N} $ is an orthogonal matrix and $ R \in \mathbb{R}^{N \times N} $ is an upper-triangular matrix.

Parameters:
AThe matrix $ A $ to decompose (input)
rowsHow many rows in A
qt$ Q^{T} $ (output)
r$ R $ (output)
Returns:
-1 if A is singular; 0 otherwise;

Definition at line 257 of file linear_algebra.c.

s32 qrsolve ( const double *  a,
u32  rows,
u32  cols,
const double *  b,
double *  x 
)

Solve a linear system using the QR decomposition. Solve the linear system $ Ax = b $ using the QR decomposition and backward substitution, where $ A $ is a matrix on $ \mathbb{R}^{N \times N} $ and $ x $ and $ b $ are vectors on $ \mathbb{R}^{N} $.

Parameters:
aMatrix $ A $ (input)
rowsNumber of rows in a
colsNumber of columns in a
bVector $ b $ (input)
xVector $ x $ (output)
Returns:
-1 if a is singular; 0 otherwise.

Definition at line 365 of file linear_algebra.c.

void qtmult ( const double *  qt,
u32  n,
const double *  b,
double *  x 
)

Solve Qx = b for x. Since $ Q \in \mathbb{R}^{N \times N} $ is an orthogonal matrix, $ Q^{T} Q = I $ and therefore $ x = Q^{T} b $. This function computes $ x \in \mathbb{R}^{N} $ in this way.

Parameters:
qt$ Q^{T} $ to be used to solve for x (input)
nsize of qt (it is square)
b$ b $ to be used to solve for x (input)
xresult of the linear solve (output)

Definition at line 317 of file linear_algebra.c.

static void row_swap ( double *  a,
double *  b,
u32  size 
) [static]

Definition at line 468 of file linear_algebra.c.

static int rref ( u32  order,
u32  cols,
double *  m 
) [static]

Definition at line 479 of file linear_algebra.c.

void rsolve ( const double *  r,
u32  rows,
u32  cols,
const double *  b,
double *  x 
)

Solve Rx = b for x. Solve $ Rx = b $ for $ x \in \mathbb{R}^{N} $. Since $ R \in \mathbb{R}^{N \times M} $ is upper-triangular, this can be done efficiently by back-substitution. This function has two important properties: it must never be called with an $ R $ that results from the decomposition of a singular matrix, and it is safe to pass the same pointer for $ b $ and $ x $.

Parameters:
rUpper-triangular $ R $ (input)
rowsNumber of rows in r
colsNumber of columns in r
bVector $ b $ to solve against, from qtmult()
xSolution vector $ x $ (output)

Definition at line 341 of file linear_algebra.c.



enu
Author(s): Mike Purvis
autogenerated on Fri Jan 3 2014 11:21:07