Linear algebra functions operating on standard library containers. More...
Go to the source code of this file.
Functions | |
std::vector< double > | MatrixVectorProduct (std::vector< std::vector< double >> M, std::vector< double > A) |
Function incorporating the standard matrix-vector product. The result is a column vector. More... | |
std::vector< std::vector< double > > | MatrixMatrixProduct (std::vector< std::vector< double >> M, std::vector< std::vector< double >> N) |
Function incorporating the standard matrix-matrix product, producing a new matrix. Matrix M should have as many columns as N has rows, otherwise an exception is thrown. More... | |
std::vector< std::vector< double > > | MatrixMatrixSum (std::vector< std::vector< double >> M, std::vector< std::vector< double >> N) |
A function to calculate the sum of the entries of two matrices. More... | |
std::vector< double > | GetMatrixRow (std::vector< std::vector< double >> M, int row) |
Function to get a row from a matrix. More... | |
std::vector< double > | GetMatrixColumn (std::vector< std::vector< double >> M, int column) |
Function to get a column from a matrix. More... | |
std::vector< std::vector< double > > | TransposeMatrix (std::vector< std::vector< double >> M) |
Function to transpose a matrix M of size i x j into a matrix N of size j x i, where \(M_{ij} = N_{ji}\). More... | |
std::vector< double > | MatrixTrace (std::vector< std::vector< double >> M) |
Function to the trace of a square \(n \times n\) matrix \(M\). More... | |
std::vector< std::vector< double > > | InvertMatrixElements (std::vector< std::vector< double >> M) |
Function to take the inverse of the individual matrix elements. More... | |
std::vector< std::vector< double > > | VectorToDiagonal (std::vector< double > A) |
Function which takes a std::vector of double to make a diagonal matrix of it, such that \( A_{i} = M_{ii} \). More... | |
double | VectorVectorProduct (std::vector< double > A, std::vector< double > B) |
Dot product of vectors. More... | |
std::vector< double > | VectorDifference (std::vector< double > A, std::vector< double > B) |
Vector difference between two vectors. More... | |
std::vector< double > | VectorSum (std::vector< double > A, std::vector< double > B) |
Vector sum between two vectors. More... | |
std::vector< double > | VectorScalarProduct (std::vector< double > A, double b) |
Vector scalar prodcut of vector and scalar. More... | |
std::vector< double > | NormalizeVector (std::vector< double > A) |
Normalizes a vector to unit length. More... | |
std::vector< double > | SolveLowerTriangular (std::vector< std::vector< double >> L, std::vector< double > x) |
Solve linear equation \( L y = x\) where \( L \) is a lower triangular \( n \times n\) matrix and \( x \) and \( y \) are \( n\) dimensional vectors. Uses forward and backward substitution to iteratively solve the system. More... | |
std::vector< std::vector< double > > | InvertLowerTriangular (std::vector< std::vector< double >> L) |
Invert a lower triangular \( n \times n\) matrix by use of solving the system \( L L^{-1} = I\) per column of \( I \) using SolveLowerTriangular(), the identity matrix. More... | |
std::vector< std::vector< double > > | CholeskyDecompose (std::vector< std::vector< double >> A) |
Cholesky-decomposition of a positive definite Hermitian \( n \times n\) matrix \( M \). More... | |
std::vector< std::vector< double > > | operator* (std::vector< std::vector< double >> M, std::vector< std::vector< double >> N) |
Operator form of MatrixMatrixProduct(), using std library forwarding. More... | |
double | operator* (std::vector< double > A, std::vector< double > B) |
Operator form of VectorVectorProduct(), using std library forwarding. More... | |
std::vector< double > | operator* (std::vector< std::vector< double >> M, std::vector< double > A) |
Operator form of MatrixVectorProduct(), using std library forwarding. More... | |
std::vector< double > | operator* (double b, std::vector< double > A) |
Operator form of VectorScalarProduct(), using std library forwarding. More... | |
std::vector< double > | operator* (std::vector< double > A, double b) |
Operator form of VectorScalarProduct(), using std library forwarding. More... | |
std::vector< double > | operator/ (std::vector< double > A, double b) |
Operator form of vector by scalar division. Uses VectorScalarProduct() with std library forwarding. More... | |
std::vector< double > | operator+ (std::vector< double > A, std::vector< double > B) |
Operator form of VectorSum(), using std library forwarding. More... | |
std::vector< double > | operator- (std::vector< double > A, std::vector< double > B) |
Operator form of VectorDifference(), using std library forwarding. More... | |
std::vector< std::vector< double > > | operator+ (std::vector< std::vector< double >> M, std::vector< std::vector< double >> N) |
Operator form of MatrixMatrixSum(), using std library forwarding. More... | |
Linear algebra functions operating on standard library containers.
Lars Gebraad (larsg) ebra ad@gm ail. com
A kind of library for all kinds of linear algebra functions. Incorporates basic operations such as multiplication and dot products (with respective operator overloading) as well as some more specialized functions such as Cholesky decomposition and lower triangular matrix inversion.
This set of function operate on containers defined in the standard library. Most of the objects passed as input or output within this set of functions are std::vector's, of either std::vector<doubles> or doubles itself. These containers act as vectors and matrices. The benefit of these is that they are memory optimized, allow for (nearly) perfect forwarding and other easy manipulation.
When I refer to matrices or vectors, the actual C++ objects will be these containers (of containers) of doubles. The first elements have index zero.
Note that if there are nested containers, the convention is that the first index (the outer container) represents rows, where the inner containers represent row elements. There is no easy way to directly access a column, except when usen GetMatrixColumn().
Definition in file linearalgebra.hpp.
std::vector<std::vector<double> > CholeskyDecompose | ( | std::vector< std::vector< double >> | A | ) |
Cholesky-decomposition of a positive definite Hermitian \( n \times n\) matrix \( M \).
A | A positive definite Hermitian \( n \times n\) matrix. |
Definition at line 275 of file linearalgebra.cpp.
std::vector<double> GetMatrixColumn | ( | std::vector< std::vector< double >> | M, |
int | column | ||
) |
Function to get a column from a matrix.
M | Any \( n \times m \) matrix \(M\). |
row | Integer indicating the column. Numbering starts at 0. |
Definition at line 93 of file linearalgebra.cpp.
std::vector<double> GetMatrixRow | ( | std::vector< std::vector< double >> | M, |
int | row | ||
) |
Function to get a row from a matrix.
M | Any \( n \times m \) matrix \(M\). |
row | Integer indicating the row. Numbering starts at 0. |
Definition at line 110 of file linearalgebra.cpp.
std::vector<std::vector<double> > InvertLowerTriangular | ( | std::vector< std::vector< double >> | L | ) |
Invert a lower triangular \( n \times n\) matrix by use of solving the system \( L L^{-1} = I\) per column of \( I \) using SolveLowerTriangular(), the identity matrix.
L | Any lower triangular \( n \times n\) matrix. |
Definition at line 325 of file linearalgebra.cpp.
std::vector<std::vector<double> > InvertMatrixElements | ( | std::vector< std::vector< double >> | M | ) |
Function to take the inverse of the individual matrix elements.
M | Any \( n \times m \) matrix \(M\). |
Definition at line 158 of file linearalgebra.cpp.
std::vector<std::vector<double> > MatrixMatrixProduct | ( | std::vector< std::vector< double >> | M, |
std::vector< std::vector< double >> | N | ||
) |
Function incorporating the standard matrix-matrix product, producing a new matrix. Matrix M should have as many columns as N has rows, otherwise an exception is thrown.
M | Any \( n \times m \) matrix \(M\). |
N | Any \( m \times p \) matrix \(N\). |
Definition at line 184 of file linearalgebra.cpp.
std::vector<std::vector<double> > MatrixMatrixSum | ( | std::vector< std::vector< double >> | M, |
std::vector< std::vector< double >> | N | ||
) |
A function to calculate the sum of the entries of two matrices.
M | Any \( n \times m \) matrix \(M\). |
N | Any \( n \times m \) matrix \(N\). |
Definition at line 213 of file linearalgebra.cpp.
std::vector<double> MatrixTrace | ( | std::vector< std::vector< double >> | M | ) |
Function to the trace of a square \(n \times n\) matrix \(M\).
M | Any square matrix. |
Definition at line 144 of file linearalgebra.cpp.
std::vector<double> MatrixVectorProduct | ( | std::vector< std::vector< double >> | M, |
std::vector< double > | A | ||
) |
Function incorporating the standard matrix-vector product. The result is a column vector.
M | Any \( n \times m \) matrix \(M\). |
A | Any \( m \) dimensional vector A. |
std::vector<double> NormalizeVector | ( | std::vector< double > | A | ) |
Normalizes a vector to unit length.
A | Any vector \( A \). |
Definition at line 345 of file linearalgebra.cpp.
std::vector<std::vector<double> > operator* | ( | std::vector< std::vector< double >> | M, |
std::vector< std::vector< double >> | N | ||
) |
Operator form of MatrixMatrixProduct(), using std library forwarding.
Definition at line 245 of file linearalgebra.cpp.
double operator* | ( | std::vector< double > | A, |
std::vector< double > | B | ||
) |
Operator form of VectorVectorProduct(), using std library forwarding.
Definition at line 250 of file linearalgebra.cpp.
std::vector<double> operator* | ( | std::vector< std::vector< double >> | M, |
std::vector< double > | A | ||
) |
Operator form of MatrixVectorProduct(), using std library forwarding.
Definition at line 254 of file linearalgebra.cpp.
std::vector<double> operator* | ( | double | b, |
std::vector< double > | A | ||
) |
Operator form of VectorScalarProduct(), using std library forwarding.
Definition at line 267 of file linearalgebra.cpp.
std::vector<double> operator* | ( | std::vector< double > | A, |
double | b | ||
) |
Operator form of VectorScalarProduct(), using std library forwarding.
Definition at line 271 of file linearalgebra.cpp.
std::vector<double> operator+ | ( | std::vector< double > | A, |
std::vector< double > | B | ||
) |
Operator form of VectorSum(), using std library forwarding.
Definition at line 259 of file linearalgebra.cpp.
std::vector<std::vector<double> > operator+ | ( | std::vector< std::vector< double >> | M, |
std::vector< std::vector< double >> | N | ||
) |
Operator form of MatrixMatrixSum(), using std library forwarding.
Definition at line 240 of file linearalgebra.cpp.
std::vector<double> operator- | ( | std::vector< double > | A, |
std::vector< double > | B | ||
) |
Operator form of VectorDifference(), using std library forwarding.
Definition at line 263 of file linearalgebra.cpp.
std::vector<double> operator/ | ( | std::vector< double > | A, |
double | b | ||
) |
Operator form of vector by scalar division. Uses VectorScalarProduct() with std library forwarding.
Definition at line 341 of file linearalgebra.cpp.
std::vector<double> SolveLowerTriangular | ( | std::vector< std::vector< double >> | L, |
std::vector< double > | x | ||
) |
Solve linear equation \( L y = x\) where \( L \) is a lower triangular \( n \times n\) matrix and \( x \) and \( y \) are \( n\) dimensional vectors. Uses forward and backward substitution to iteratively solve the system.
L | Any lower triangular \( n \times n\) matrix. |
x | Any \( n\) dimensional vector. |
Definition at line 303 of file linearalgebra.cpp.
std::vector<std::vector<double> > TransposeMatrix | ( | std::vector< std::vector< double >> | M | ) |
Function to transpose a matrix M of size i x j into a matrix N of size j x i, where \(M_{ij} = N_{ji}\).
M | Any \( n \times m \) matrix \(M\). |
std::vector<double> VectorDifference | ( | std::vector< double > | A, |
std::vector< double > | B | ||
) |
Vector difference between two vectors.
A | Vector \( A \) of dimension \( n\). |
B | Vector \( B \) of dimension \( n\). |
Definition at line 11 of file linearalgebra.cpp.
std::vector<double> VectorScalarProduct | ( | std::vector< double > | A, |
double | b | ||
) |
Vector scalar prodcut of vector and scalar.
A | Any vector \( A \). |
B | Scalar \( b \). |
Definition at line 29 of file linearalgebra.cpp.
std::vector<double> VectorSum | ( | std::vector< double > | A, |
std::vector< double > | B | ||
) |
Vector sum between two vectors.
A | Vector \( A \) of dimension \( n\). |
B | Vector \( B \) of dimension \( n\). |
Definition at line 36 of file linearalgebra.cpp.
std::vector<std::vector<double> > VectorToDiagonal | ( | std::vector< double > | A | ) |
Function which takes a std::vector of double to make a diagonal matrix of it, such that \( A_{i} = M_{ii} \).
A | Any Any \( n \) dimensional vector. |
Definition at line 169 of file linearalgebra.cpp.
double VectorVectorProduct | ( | std::vector< double > | A, |
std::vector< double > | B | ||
) |
Dot product of vectors.
A | Vector \( A \) of dimension \( n\). |
B | Vector \( B \) of dimension \( n\). |
Definition at line 78 of file linearalgebra.cpp.