Numerical Linear Algebra
Mathwrist C++ API Series
Abstract
This document details the C++ programming interface of the numerical linear algebra (LA) features implemented in Mathwrist’s Numerical Programming Library (NPL). For a technical overview of algorithms, please refer to our white paper series.
1 Matrix Basics
The Matrix class is one of the most important built-in blocks of the library. The majority part of linear algebra implementation is encapsulated as member functions of the Matrix class defined in C++ header file matrix.h.
1.1 Enumeration of Matrix Forms
Matrix class defines the following enumeration of matrix forms. These forms help to choose appropriate algorithms in certain context.
1.2 Object Creation
A matrix object created by the following constructors has contiguous memory allocated in heap to store elements. All matrix elements are initialized to 0 by the constructor. We shall call such a matrix object as “physically allocated” matrix. Later on in the view section, we will introduce “view represented” matrix objects. No matter what is the actual memory allocation scheme, when a matrix object goes out of scope, its internal resouces are freed by the destructor.
Description:
-
1.
Default contructor to create an empty matrix object.
-
2.
Create a matrix object of nRows number of rows and nCols number of columns;
-
3.
Create a square matrix;
-
4.
Copy constructor, deep copy all elements from the other matrix.
-
5.
Destructor, free all resouces used by the matrix object.
1.3 Static Initialization Methods
Description:
-
1.
Initialize matrix I to an identity matrix of size n.
-
2.
Diagonize a matrix D with a given constant factor lambda.
-
3.
Diagonize a matrix D with a given vector dv. If parameter inverse is passed true, the inverse of vector element of dv are assigned to the diagonal of D.
1.4 Element Access
All functions listed below are Matrix class memeber functions. Matrix row and column index starts from 0.
Description:
-
1.
Return a reference to the element at the -th row and -th column in the matrix. Element can be modified.
-
2.
For read only, return a constant reference to the element at the -th row and -th column in the matrix.
1.5 Dimension and Size Query
All functions listed below are Matrix class memeber functions.
Description:
In the same order of the above code block, the function returns,
-
1.
if the matrix has n_rows number of rows and n_cols number of columns.
-
2.
if the matrix has the same dimension size as the other matrix.
-
3.
if the matrix is empty.
-
4.
if the matrix is a square matrix.
-
5.
if the matrix is a symmetric matrix.
-
6.
if the matrix is a row vector.
-
7.
if the matrix is a column vector.
-
8.
if the matrix is a row or column vector.
-
9.
the total number of rows in the matrix.
-
10.
the total number of columns in the matrix.
-
11.
the total number of elements in the matrix.
1.6 Comparison and Content Query
All functions listed below are Matrix class memeber functions.
Description:
In the same order of the above code block, based on numerical comparison with tolerance (default to ), the function returns if the current matrix object numerically is,
-
1.
equal to the other matrix.
-
2.
equal to the other matrix with a single tolerance eps
-
3.
equal to the other matrix with an element-wise tolerance matrix eps.
-
4.
equal to 0, with a single tolerance eps.
-
5.
equal to 0, with an element-wise tolerance matrix eps.
-
6.
a diagonal matrix, with a single tolerance eps.
-
7.
a lower triangular matrix, with a single tolerance eps.
-
8.
a upper triangular matrix, with a single tolerance eps.
-
9.
an identity matrix, with a single tolerance eps.
1.7 Assignment Operators
All functions listed below are Matrix class memeber functions.
Description:
-
1.
Matrix element-wise copy , where is the current matrix object, is passed by reference from parameter other. If is actually a view representation, it must have the same dimension size as . Otherwise, an exception will be thrown. If is a physically allocated matrix object and has difference size from , it will be resized before copying.
-
2.
Set all elements in the current matrix to value parameter v.
1.8 Manipulation Functions
All functions listed below are Matrix class memeber functions.
Description:
Let denote the current matrix object. In the same order in the above code block,
-
1.
Change the size of to nRows number of rows and nCols number of columns. If the parameter to_zeros flag is passed to true, all elements of are set to 0 after resize. This function involves memory allocation if doesn’t have the desired size. Hence, if is actually a view representation, an exception is thrown when has different size.
-
2.
REQUIRED: Matrix sizes are identical. It element-wise copies all values from the other matrix (passed by reference) to . This function is mostly useful for views because the assignment operator for views doesn’t copy elements, instead view assignment creates a view copy.
-
3.
Sets all elements in equal to 0.
-
4.
Sets all elements in equal to 1.
-
5.
REQUIRED: is a row vector or a column vector. It sets the -th element of to 1 and all other elements to 0, i.e. .
-
6.
Flips the sign of all elements in .
-
7.
REQUIRED: is a square matrix. It sets to be a diagonal matrix where all diagonals are equal to the given parameter value lambda.
-
8.
REQUIRED: is a square matrix. It sets to be a diagonal matrix where the diagonal elements are passed as a vector from parameter diag.
-
9.
REQUIRED: is a square matrix. It sets to be a diagonal matrix where the diagonal elements are passed as a buffer from parameter diag.
-
10.
REQUIRED: is a square matrix. It sets all elements below diagonal to 0 when flag clean_lower is passed as true. When it is false, all elements above diagonal are set to 0.
-
11.
REQUIRED: is a square matrix. When flag upper_to_lower is passed as true, it copies all above diagonal elements to below diagonal elements so that the matrix is symmetric. When flase is passed, it copies all below diagonal elements to above diagonal.
-
12.
REQUIRED: is a square matrix. The other matrix is required to be either a square matrix of the same size, or a vector of same row or column size. When other is a matrix, its diagonal elements are copied to the diagonal of . When it is a vector, its element are copied to the diagonal of .
-
13.
Change the current matrix to its transpose.
-
14.
Let denote the -th column of . This function computes the mean of and then subtract all elements of by this mean.
-
15.
Swaps the -th column of with the -th column.
-
16.
Swaps the -th row of with the -th row.
-
17.
Shift all columns of to the right by 1 column. The content of the original first column is not erased.
-
18.
Shift all columns of to the left by 1 column. The content of the original last column is not erased.
-
19.
Shift all rows to the top by 1 row. The content of the original last (bottom) row is not erased.
-
20.
Shift all rows to the bottom by 1 row. The content of the original first (top) row is not erased.
1.9 Norm, Min and Max Functions
All functions listed below are Matrix class memeber functions.
Description:
Let denote the current matrix object. The above function requires to be a row or column vector of size . In the same order of the above code block,
-
1.
REQUIRED: Parameter is a vector object of size . The function computes the inner product of and .
-
2.
The function computes the vector 1-norm .
-
3.
The function computes the vector 2-norm .
-
4.
The function computes the vector -norm . If the pointer parameter i_max is not null, *i_max holds the index of the max norm element on return.
-
5.
The function returns the min element in the vector. If the pointer parameter _i is not null, *_i holds the index of the min element on return.
-
6.
The function returns the max element in the vector. If the pointer parameter _i is not null, *_i holds the index of the max element on return.
-
7.
The function returns the minimum of . If the pointer parameter _i is not null, *_i holds the index of this element on return.
-
8.
The functions returns the sum of all elements.
-
9.
The function returns the max of .
2 Matrix Arithmetics
All functions listed in this section are Matrix class memeber functions.
2.1 Arithmetic Operators
Let denote the relevant arithmetic operation. The functions listed above perform operation on the current matrix object with operand and returns a reference to . An exception will be thrown if and have incompatible size.
Description:
-
1.
is addition, is passed by reference from parameter other.
-
2.
is addition, has all element value being parameter v.
-
3.
is subtraction, is passed by reference from parameter other.
-
4.
is subtraction, has all element value being parameter v.
-
5.
is multiplication, is passed by reference from parameter other.
-
6.
is multiplication, has all element value being parameter v.
2.2 Element-wise Multiplication and Division
Description:
Mostly used for vectorized arithmetics, these two function perform element-wise operation , where is the current matrix and is passed by reference from parameter other. The operation here is multiplication and division respectively. If and have different size, an exception is thrown.
2.3 Matrix Multiplication and Inverse
Description:
Let denote the current matrix object.
-
1.
computes , where the operand matrix is required to have size . The result matrix is resized to be a matrix.
-
2.
computes , where the result matrix is resized to be a matrix.
-
3.
computes the inverse of the current matrix, where .
Parameters
-
(a)
Inv will be resized to hold the inverse matrix on return.
-
(b)
f indicates what is the form of the current matrix object, which helps to choose appropriate algorithms to compute the inverse. Please refer to our white paper for the algorithm details based on different matrix forms.
-
(a)
3 Transformation Matrices
Linear transformations mathematically can be written as matrix multiplications. Programmatically, transformation matrices often deserve special treatment for both storage and performance efficiency. We gives details on two special transfromations, permutation and Givens rotaion, in this section.
3.1 Permutation
Matrix::Permuation is a nested class of the Matrix class. Conceptually, it represents a square matrix where each row and column has exactly one element equal to 1. All other element are 0s. This allows us to only store the position of those 1-element in order to represent the matrix.
Given a matrix , the left multiplication generates a row interchanged view of . The right multiplication generates a column interchanged view of . We will discuss matrix views in a separate section.
3.1.1 Constructors
Description:
In the same order of the above code block,
-
1.
Creates an empty permutataion object.
-
2.
Creates a permutation object that represents and row or column interchange. Parameter defines the size of the permutation matrix.
-
3.
Creates a permutation object where element . The encoding buffer has size , which also defines the size of the permutation matrix.
-
4.
Same as the constructor that takes an encoding buffer as argument, this function takes a pointer and size parameter instead.
3.1.2 Two Useful Factory Methods:
The functions listed below are Matrix::Permutation class static member functions.
Description:
These two functions can be best explained by examples. Let be a matrix. Let denote the -th row of , . In other words,
If we want to shift rows one row up and move to ’s original position, it involves a sequence of row interchanges. Let be the permutation object that performs this sequence of interchanges.
This permutation object can be obtained by calling the i_shift_row() factory method with arguments i=1 and j=4 as the code example below,
If we want to shift rows one row down and move to ’s position such that
, this can be obtained by
Effectively, when index , i_shift_row shifts all rows with index one row up. When , the shift is one row down. In both cases, the original -th row is simultaneously moved to the -th row.
The i_shift_col() function behaves in a similar way for column interchanges. Now let be the columns of . When parameter , columns shift to left by one column and column moves to the original position of . When , columns shift to right by one column and moves to the original position of .
When parameter , these two function both return a permutation corresponding to an identity matrix.
3.1.3 Multiplications
All functions listed below, except the 6th one, are Matrix::Permutation class member functions. The 6th function is a friend binary operator where the permutation object appears as the right hand side operand of the multiplication.
Description:
Let and be two permutation matrices of rows. Let be a matrix under the permutation operation. In the same order of the above code block,
-
1.
Returns a new permutation .
Generalize this to , where each is a single interchange operation. is equivalent to applying the row interchange sequence . is to apply the column interchanges in reverse order.
-
2.
Performs .
-
3.
REQUIRED: has number of rows. This function returns a row-interchanged view of , where the row interchanges are represented by . Please refer to the Matrix class member multiplication operators, i.e. Matrix::operator*(Permutation&) for column-interchanged permutation.
-
4.
Same as the previous function except that now is applied to a constant matrix and returns a constant view.
-
5.
Return the final position of the -th row after the permutation. This is when appears as the left hand side multiplication operand.
-
6.
Return the final position of the -th column after the permutation. This is when appears as the right hand side multiplication operand.
-
7.
REQUIRED: is square matrix. The function performs symmetric row and column interchanges on and returns a vew that represents the multiplication .
-
8.
Same as the previous function exception now is a constant matrix and the function returns a constant view.
-
9.
REQUIRED: is square matrix. The function performs symmetric row and column interchanges on and returns a vew that represents the multiplication .
-
10.
Same as the previous function exception now is a constant matrix and the function returns a constant view.
3.1.4 Query Functions:
The functions listed below are Matrix::Permutation class member functions.
Description:
Let be the current permutation object. In the same order of the above code block,
-
1.
Compares if effectively performs the same operation as the other permutation.
-
2.
Return the size .
3.1.5 Manipulation Functions:
The functions listed below are Matrix::Permutation class member functions.
Description:
Let be the current permutation object. In the same order of the above code block,
-
1.
Change to size and initialize it to be effectively an identity matrix.
-
2.
Reset to be effectively an identity matrix of the same size.
-
3.
Change to be . For a general where each is a single interchange operation, . The transpose effectively reverses the order of the sequence.
-
4.
Return a new permutation object that is .
3.2 Givens Rotation
A Givens rotation matrix is a unitary matrix with two special index and . It has the following elements,
-
•
All diagonal elements ;
-
•
Diagonal elements
-
•
All off-diagonal elements are 0 except and ;
, where and for some . When left multiplies a column vector . Only the -th and -th element of the result vector are different from .
We represent the Givens rotation matrix by nested class Matrix::Givens defined in header file “matrix.h”.
3.2.1 Constructors:
Description:
-
1.
Default constructor, create an empty object.
-
2.
Initialize the rotation object with index , , and , where . For a column vector where and . This construction of sets and in the result vector .
3.2.2 Rotation Operation:
The functions listed below are Givens class member functions.
Description:
Let be the current rotation object.
-
1.
The left multiplication operator* takes operand mathrix and applies the operation on all columns of . After the rotation, a reference of is returned. The right multiplication operation is implemented as a class member function of the Matrix,
It modifies the -th and -th column elements on all rows of and returns a reference of after rotation.
-
2.
The apply function conceptually performs the operation on vector . Since only the -th and -th element of are changed, and are passed by reference at parameter a and b. then directly modifies the values of and .
3.2.3 Query Functions:
The functions listed below are Givens class member functions.
Description:
These query functions correspond to the constructor that takes as arguments to construct the rotation .
-
1.
Return the index .
-
2.
Return the index .
-
3.
Return .
-
4.
Return .
-
5.
Return .
3.2.4 Manipulation Functions:
The functions listed below are Givens class member functions.
Description:
These two funtions are useful when applying the same rotation to vector elements and where and are offsetted from and .
-
1.
Increase the index and by , i.e. and .
-
2.
Decrease the index and by , i.e. and .
4 Matrix Views
Matrix views are alternative representation of matrices in Mathwrist NPL. They are defined as nested classes of the Matrix class. A view object can be used anywhere in a context where a regular matrix object is expected, i.e. passed as function parameters or expression operands. In addition, a view object provides pointer notation so that it can be used as the pointer of a matrix object to access the Matrix class memeber functions.
Views can be copied by the copy constructor and assignment operator. The copy operation on views effectively creates indentical views instead of copying data elements. There are two view types, Matrix::ViewConst and Matrix::View respectively. A constant view provides read-only access to matrix elements. A non-constant view supports both read and write access to matrix elements. When a modification is applied to the non-constant view, it is the original matrix being modified through the view.
4.1 Constructors:
Description:
-
1.
Creates an empty constant view.
-
2.
Creates a constant view of nRows by nCols matrix where the data elements are provided in a constant buffer in row-wise major.
-
3.
Creates an empty non-constant view.
-
4.
Creates a non-constant view of nRows by nCols matrix where the data elements are read from or written to a given buffer in row-wise major.
Example:
4.2 Conversion Operators:
Description:
-
1.
In the context where a constant matrix is expected, convert a constant view to a constant matrix, i.e. line 11 and 12 in the code example above.
-
2.
In the context where a constant matrix is expected, convert a non-constant view to a constant matrix.
-
3.
In the context where a non-constant matrix is expected, convert a non-constant view to a non-constant matrix.
4.3 Pointer Operators:
Description:
-
1.
Allows a constant view to call Matrix class constant member functions, i.e. line 15 and 16 in the code example above.
-
2.
Allows a non-constant view to call Matrix class constant member functions.
-
3.
Allows a non-constant view to call Matrix class non-constant member functions.
4.4 Assignment Operators:
Description:
These two functions are the member funcitons of only Matrix::View class. They copy elements from a source to the current view. If the current view mapps to an original matrix, the content of the original matrix is modified.
-
1.
REQUIRED: The current view has same size as the source matrix . This function copies all elements from m to the current view.
-
2.
This function sets all elements in the current view equal to the parameter value v.
Example:
The above code example illustrates the assignment operation to a view object v. Note that if view v has different size from matrix n, the assignment at line# 12 will trigger an exception.
4.5 Copy Operation:
The copy constructor and assignment operator from views to views are not implemented in the library. It is the compiler’s default version (shallow copy) that is used when we copy two objects of the same view type. In which case we obtain two identical views. The code example below illustrates this.
Example:
Seconly, because View and ViewConst are two different types, copy from View to ViewConst will cause compile time error. On the other hand, copy from ViewCont to View can be resolved by C++ standard implicit type conversion rules. First, the ViewConst class has a conversion operator that can take a ViewConst object to anywhere const Matrix& is expected. Secondly, the View class has an assignment operator to perform element-wise copy from a const Matrix& matrix source. Therefore, copy from ViewCont to View involves an implicit conversion and then an element wise copy operation. The code example below illustrates this.
Example:
4.6 Element Access:
Description:
Access to the matrix element at the -th row and -th column (the same way as calling from a regular matrix object).
-
1.
Read only access from a constant view.
-
2.
Read only access from a non-constant view.
-
3.
Read and write access from a non-constant view.
4.7 Transposed View:
Description:
Note that these functions are member functions of class ViewConst, View and Matrix respectively.
Let be a matrix and be a view of . The function calls and both return a new view that represents the transpose matrix .
-
1.
is a constant view. The transpose of is always a constant view.
-
2.
is a non-constant view. The transpose of is a non-constant view.
-
3.
is a non-constant view. The transpose of can be a constant view when has constant modifier.
-
4.
is a non-constant matrix. The transpose of is a non-constant view.
-
5.
The transpose of is a constant view when has constant modifier.
Example 1:
Example 2:
4.8 Permutated View:
Description:
Note that these functions are member functions of class ViewConst, View and Matrix respectively.
Let be a matrix and be a view of . Let be a permutation. These multiplication operators return a new view that is equivalent to . For the permutated view equivalent to , please refer to the multiplication operators defined in class Matrix::Permutation.
REQUIRED: , and have compatible size for multiplication.
-
1.
Return a permutated view that is still a constant view.
-
2.
Return a permutated view that is a non-constant view. Elements in this permutated view can be modified.
-
3.
Return a permutated view that could be a constant view when has constant modifier.
-
4.
Return a non-constant view (elements can be modified).
-
5.
Same as above, except that the return is a constant view.
4.9 Arithmetic Operators:
Description:
These functions are the member functions of class View, since they modify the content of the underlying matrix. Let be the view representation of a matrix.
-
1.
Performs . REQUIRED: The operand matrix has same size as .
-
2.
Add all element in by the same number .
-
3.
Performs . REQUIRED: The operand matrix has same size as .
-
4.
Subtract all element in by the same number .
-
5.
Performs . REQUIRED: The operand matrix has compatible size.
-
6.
Scale all element in by the same number .
-
7.
Performs . REQUIRED: The operand Givens rotation matrix has compatible size.
4.10 Sub Matrix Views:
The functions listed below are Matrix class member functions.
Description:
All these functions are member functions of class Matrix. Let be the current matrix object. In the same order of the above code block,
-
1.
Return a non-constant view of the -th row of .
-
2.
Same as above, except that the return is a constant view.
-
3.
Return a non-constant view of the -th column of .
-
4.
Same as above, except that the return is a constant view.
-
5.
Return a non-constant view of the sub matrix of . The sub matrix starts from row and column (inclusive). It stops at row and column (exclusive).
-
6.
Same as above, except that the return is a constant view.
-
7.
Return itself as a non-constant view.
-
8.
Same as above, except that the return is a constant view.
4.11 Matrix Expressions
Matrix expressions are nested classes of class Matrix, all designed as derived types from the View base class. Because they are views, an expression object can appear anywhere a regular matrix is expected. In extra, they have their own memory to store data elements. Expression objects are created by the following operators.
Description:
Note that these operators are library scope operators. They can be exposed to the global scope with the using statement at a client application.
Let and be two matrix objects. Let be a scalar.
REQUIRED: and have compatible size relevent to the expression.
In the same order of the above code block,
-
1.
Negation: unary operator - on operand creates a negation expression .
-
2.
Addition: binary operator + on operands and creates an addition expression .
-
3.
Subtraction: binary operator - on operands and creates an subtraction expression .
-
4.
Multiplication: binary operator * on operands and creates an multiplication expression .
-
5.
Multiplication: binary operator * on operands and creates an multiplication expression .
-
6.
Multiplication: binary operator * on operands and creates an multiplication expression .
5 Matrix Iterators
Matrix element iterators allow more efficient element access than the Matrix class member function operator()(size_t i, size_t j). In order to use iterators, we first need obtain an iterator support from a matrix or a view object. An iterator support is effectively a unique pointer of some iterator traits. If we are calling the iterator support funtion from a regular matrix object or from a view object other than permutated views, this traits pointer will be not null. We can then ask for iterators from the traits.
5.1 Iterator Supports:
Iterator supports are defined as the following inside the Matrix class:
Iterator traits is a template nested struct of the Matrix class. The template type argument _Element is double for iterator_traits and const double for const_iterator_traits. The implementation details of iterator traits is not a focus here. From programming interface perspective, it is sufficient to know that:
- 1.
- 2.
Each of these two iterator traits types has the following member functions that return row or column traverse iterators.
Class Matrix has two member functions to return iterator supports.
-
1.
From a non-constant matrix object, we can obtain non-const iterator support, then non-const iterators to read or write elements.
-
2.
From a constant matrix, we can only obtain const iterator support, then const iterators for read only.
5.2 Iterator Operations:
Let it denote an iterator object obtained from an iterator traits. Let _Element be the template type argument that instantiates the relevant iterator traits. Let T denote the traits as abbreviation. Table 1 summarizes iterator operations. When iterator it reaches to a row end or column end, those non-comparison operators have undefined behavior.
Operator | Syntax | Inside class definition | Remark |
Deference | it | _Element& T::operator(); | Return a reference to the matrix |
element it points to. | |||
Pre-increment | it | _Element& T::operator(); | Move it to the next element |
in the same row if it is a row | |||
iterator, or the next element in | |||
the same column if it is a column | |||
iterator. | |||
After the move, return a reference | |||
to the element that it now points | |||
to. | |||
Pre-decrement | it | _Element& T::operator(); | Move it to the previous element |
in the same row if it is a row | |||
iterator, or the previous element in | |||
the same column if it is a column | |||
iterator. | |||
After the move, return a reference | |||
to the element that it now points | |||
to. | |||
Post-increment | it | _Element T::operator(int); | Same as pre-increment, except that |
the operation first saves the element | |||
value and then move. The operation | |||
returns the saved previous element | |||
value. | |||
Post-decrement | it | _Element T::operator(int); | Same as pre-decrement, except that |
the operation first saves the element | |||
value and then move. The operation | |||
returns the saved previous element | |||
value. | |||
Jump-increment | it += n | void T::operator+=(size_t); | Move it to the element in the same |
row and n column greater if it is a | |||
row iterator, | |||
or the element in the same column | |||
and n row greater if it is a | |||
column iterator. | |||
Jump-decrement | it -= n | void T::operator-=(size_t); | Move it to the element in the same |
row and n column smaller if it is a | |||
row iterator, | |||
or the element in the same column | |||
and n row smaller if it is a | |||
column iterator. | |||
Comparision | it1 it2 | bool T::operator( | Compare if two iterators pointing |
Equal | const self_type&) const; | to the same element. | |
Comparision | it1 it2 | bool T::operator( | Compare if two iterators pointing |
Not Equal | const self_type&) const; | to different elements. | |
Comparision | it1 it2 | bool T::operator( | Compare if iterator it1 pointing |
Less | const self_type&) const; | to an element that has smaller | |
memory address than the element | |||
pointed by it2. |
6 Matrix Factorization
All functions listed in this section are Matrix class member functions. Let be the matrix object to factorize.
6.1 Eigen
Description:
This function computes the eigen vectors and eigen values of .
REQUIRED: is a square matrix.
Parameters:
-
1.
eigen_vect will be resized to matrix that holds the right eigen vectors in columns.
-
2.
eigen_value will be resized to to hold the eigen values on return.
-
3.
f indicates if is symmetric.
-
4.
eigen_img is relevant to non-symmetric matrix. If supplied, it will be resized to hold the imaginary part of eigen values.
6.2 SVD
Description:
Given a matrix , this function computes a compact form of singular value decomposition . Let . has size . has size . has size .
Parameters:
-
1.
U is resized to to hold matrix on return.
-
2.
S is resized to to hold on return. All elements in are non-negative and are arranged in descending order.
-
3.
V is resized to to hold the matrix , not on return.
6.3 Cholesky
Description:
For a positive definite matrix , this function computes the Cholesky decomposition . If is not positive definite, the function returns false.
Parameters:
L will be resized and hold the lower triangular matrix .
6.4 Reduced Cholesky
Description:
Given a positive definite matrix and a matrix , , this function computes the Cholesky factor,
It returns false if is actually not positive definite. Note that when is indefinite, it is possible that could still be positive definite. This reduced Cholesky function only works when is positive definite.
Parameters:
L will be resized to and hold the lower triangular matrix .
6.5 Modified Cholesky
Description:
Given a matrix that could be indefinite, this function finds a miniminzed norm diagonal remedy matrix such that
Parameters:
-
1.
L will be resized to and hold the lower triangular matrix .
-
2.
_E is an optional argument. When provided, the deferenced matrix object (*_E) will be resized to to holds the diagonal values of .
6.6 Partial Cholesky
Description:
Given a matrix that could be indefinite, this function finds a permutation and computes the partial Cholesky factoring,
, where and have the following block structure.
is a lower triangular unitary matrix. is a diagonal matrix with positive elements. The function returns the size of .
Parameters:
-
1.
L will be resized to to hold matrix .
-
2.
B will be resized to to holds matrix .
-
3.
P will be resized to hold the permutation .
-
4.
nu is a number between 0 and 1 to control the termination condition. Please refer to our white paper on this subject for details.
6.7 LU
Description:
Given a general matrix , this function computes the LU decomposition , where is the permutation introduced by pivoting, is , is and . is lower triangular if or lower trapezoidal if . is upper triangular if or upper trapezoidal if .
Parameters:
-
1.
L will be resized to to hold matrix .
-
2.
U will be resized to to hold matrix .
-
3.
P will hold the permutation on return.
6.8 QR
Description:
Given a general matrix , this function computes a compact form of QR decomposition , where is , is and . has orthonomal vectors in columns. is upper triangular if or upper trapezoidal if .
Parameters:
-
1.
R will be resized to to hold matrix .
-
2.
_Q is optional. If provided, the deferenced matrix object (*_Q) will be resized to to hold matrix ,
6.9 Full QR
Description:
Given a general matrix , this function computes a full QR decomposition , where is and is . has orthonomal vectors in columns. If , has upper triangular at the top and the rest rows be 0. If , is upper trapezoidal.
Parameters:
-
1.
R will be resized to to hold matrix .
-
2.
_Q is optional. If provided, the deferenced matrix object (*_Q) will be resized to to hold matrix ,
7 Linear System
LinearSystem class includes a group of static functions and two nested classes LinearSystem::Tridiagonal and LinearSystem::LU to solve a linear system . The C++ header file is linear_system.h.
7.1 General Solver
Description:
This function is the most general solver and allows to be a matrix. Depending on the nature of , could be an exact solution or a least square solution. Parameter formA describes the form of , which is used to choose appropriate algorithms. Please refer to our white paper for technical details. On function return, parameter x is resized to to hold the solution .
7.2 Special Cases
7.2.1 Forward Substitution
When is a lower triangular matrix , these two functions solves and respectively. is a unit vector with the -th element equal to 1 and all other elements are 0. Parameter x is resized to hold solution on return.
7.2.2 Backward Substitution
When is a upper triangular matrix . The first function solves , is passed as parameter y on call. On return, parameter y holds the solution vector . REQUIRED: Parameter y is a -element vector.
The second function solves . is a unit vector with the -th element equal to 1 and all other elements are 0. Parameter y is resized to hold the solution on return.
7.2.3 Conjugate Gradient Update
Description:
When is a large positive definite matrix, the conjugate gradient (CG) method can solve the linear system by generating a sequence of improved guess , . At each iteration, it also outputs the by-product of a residual vector and a search direction to be taken at the -th iteration.
Initially, users need choose an initial guess . The residual vector should be computed as . The initial search direction should be taken as . Subsequently, are updated by the function. User can stop the update when is considered as small enough. In the worst case, the CG method finds the exact solution after iterations.
REQUIRED: Parameter x_k, p_k and r_k for , and are -element column vectors.
7.2.4 Range Space Solver
Description:
Given a matrix , , if the QR decomposition of is known, we can use this range space solver to solve when parameter transpose is false, or when parameter transpose is passed true.
When , is underdetermined. This solution vector being found is in the span of the range space of . On the other hand, is overdetermined, the computed is a least square solution.
Parameter x is resized to hold the solution on return.
7.2.5 Tridiagonal
When is a tridiagonal matrix, LinearSystem::Tridiagonal solver can be used to compute the solution in floating point operations.
LinearSystem::Tridiagonal a nested class of LinearSystem class. It has the following member functions.
Constructor and Member Functions:
Description:
-
1.
Constructor, create a solver for tridiagonal system.
-
2.
Return the size of the system.
-
3.
Resize the solver to a new size.
-
4.
Return the value of the sub-diagonal element at the -th row.
-
5.
Return a reference to the sub-diagonal element at the -th row.
-
6.
Return the value of the diagonal element at the -th row.
-
7.
Return a referece to the diagonal element at the -th row.
-
8.
Return the value of the super-diagonal element at the -th row.
-
9.
Return a reference to the super-diagonal element at the -th row.
-
10.
Compute the multiplication . Parameter Y is resized to hold the multiplication result on return. REQUIRED: : Parameter X for matrix has rows.
-
11.
Solve the linear system , where could be a matrix. Parameter X is resized to hold the solution matrix on return. Column is the solution of right hand side column . REQUIRED: Parameter B for matrix has rows.
7.2.6 LU Update
LinearSystem::LU class is a nested class of the LinearSystem class. It is useful in the situation when
-
1.
is a matrix and;
-
2.
Subsequently is obtained by pivoting a column of with another vector and
-
3.
We want to repeatedly solve .
We initially compute the LU decomposition of . The LU decomposition of can economically updated from and , hence obtaining the solution of updated linear system.
Constructor and Member Functions:
Description:
-
1.
Default constructor, create an empty object.
-
2.
Copy constructor, deep copy all LU update solver internal details.
-
3.
Initialize the LU update solver for a system with at most number of pivoting operations to perform.
-
4.
Destructor, free all internal resouces.
-
5.
Resize the solver to a new linear system and number of pivoting.
-
6.
Parameter A is takens as , compute the initial LU decomposition. REQUIRED: A has correct matrix size.
-
7.
Parameter D is takens as , which is a diagonal matrix. Initialize to an identity matrix, and . REQUIRED: D is a -element column vector.
-
8.
Perform the pivoting operation to swap the -th column in the current with a vector to obtain . and are updated. REQUIRED: p is -element column vector.
-
9.
Return a constant reference to the current basis matrix .
-
10.
If parameter trans is passed false, solve the linear system ; otherwise, solve . Parameter x is resized and holds the solution on return. REQUIRED: b is a -element vector.