Hamiltonian Monte Carlo

linearalgebra.cpp File Reference
#include <vector>
#include <iostream>
#include <cmath>
Include dependency graph for linearalgebra.cpp:

Go to the source code of this file.

Functions

std::vector< double > VectorDifference (std::vector< double > A, std::vector< double > B)
 Vector difference 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 > VectorSum (std::vector< double > A, std::vector< double > B)
 Vector sum between two vectors. More...
 
std::vector< double > MatrixVectorProduct (std::vector< std::vector< double > > M, std::vector< double > A)
 
double VectorVectorProduct (std::vector< double > A, std::vector< double > B)
 Dot product of vectors. More...
 
std::vector< double > GetMatrixColumn (std::vector< std::vector< double >> M, int column)
 Function to get a column from a matrix. More...
 
std::vector< double > GetMatrixRow (std::vector< std::vector< double >> M, int row)
 Function to get a row from a matrix. More...
 
std::vector< std::vector< double > > TransposeMatrix (std::vector< std::vector< double > > M)
 
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...
 
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< 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...
 
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+ (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< 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< 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< 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< 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 > NormalizeVector (std::vector< double > A)
 Normalizes a vector to unit length. More...
 

Function Documentation

◆ CholeskyDecompose()

std::vector<std::vector<double> > CholeskyDecompose ( std::vector< std::vector< double >>  A)

Cholesky-decomposition of a positive definite Hermitian \( n \times n\) matrix \( M \).

Parameters
AA positive definite Hermitian \( n \times n\) matrix.
Returns
A lower triangular \( n \times n\) matrix for which holds \( L L^\dagger = M \), where \( L^\dagger \) is the adjoint of \( L \) (which of course simplifies to transposition for real matrices).

Definition at line 275 of file linearalgebra.cpp.

◆ GetMatrixColumn()

std::vector<double> GetMatrixColumn ( std::vector< std::vector< double >>  M,
int  column 
)

Function to get a column from a matrix.

Parameters
MAny \( n \times m \) matrix \(M\).
rowInteger indicating the column. Numbering starts at 0.
Returns
An \( n \) dimensional vector containing the extracted matrix column.

Definition at line 93 of file linearalgebra.cpp.

◆ GetMatrixRow()

std::vector<double> GetMatrixRow ( std::vector< std::vector< double >>  M,
int  row 
)

Function to get a row from a matrix.

Parameters
MAny \( n \times m \) matrix \(M\).
rowInteger indicating the row. Numbering starts at 0.
Returns
An \( m \) dimensional vector containing the extracted matrix row.

Definition at line 110 of file linearalgebra.cpp.

◆ InvertLowerTriangular()

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.

Parameters
LAny lower triangular \( n \times n\) matrix.
Returns
The inverse of \( L \), another lower triangular \( n \times n\) matrix.

Definition at line 325 of file linearalgebra.cpp.

◆ InvertMatrixElements()

std::vector<std::vector<double> > InvertMatrixElements ( std::vector< std::vector< double >>  M)

Function to take the inverse of the individual matrix elements.

Parameters
MAny \( n \times m \) matrix \(M\).
Returns
The inverted elements \( 1/M_{ij}\) in a std::vector of std::vector of double.

Definition at line 158 of file linearalgebra.cpp.

◆ MatrixMatrixProduct()

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.

Parameters
MAny \( n \times m \) matrix \(M\).
NAny \( m \times p \) matrix \(N\).
Returns
A \( n \times p \) matrix P defined by \( P_{ij} = \sum_{k=1}^m M_{ik} N_{kj} \).

Definition at line 184 of file linearalgebra.cpp.

◆ MatrixMatrixSum()

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.

Parameters
MAny \( n \times m \) matrix \(M\).
NAny \( n \times m \) matrix \(N\).
Returns
A \( n \times m \) matrix \( S \) containing the sum of the entries of \(M\) and \(N\).

Definition at line 213 of file linearalgebra.cpp.

◆ MatrixTrace()

std::vector<double> MatrixTrace ( std::vector< std::vector< double >>  M)

Function to the trace of a square \(n \times n\) matrix \(M\).

Parameters
MAny square matrix.
Returns
The trace of \( M \).

Definition at line 144 of file linearalgebra.cpp.

◆ MatrixVectorProduct()

std::vector<double> MatrixVectorProduct ( std::vector< std::vector< double > >  M,
std::vector< double >  A 
)

Definition at line 54 of file linearalgebra.cpp.

◆ NormalizeVector()

std::vector<double> NormalizeVector ( std::vector< double >  A)

Normalizes a vector to unit length.

Parameters
AAny vector \( A \).
Returns
Vector \( B' \) where \( \sqrt{\sum_i {B_{i}}^{2} }= 1\) and \( A \cdot B = |A| |B| = |A|\) .

Definition at line 345 of file linearalgebra.cpp.

◆ operator*() [1/5]

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.

◆ operator*() [2/5]

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.

◆ operator*() [3/5]

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.

◆ operator*() [4/5]

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.

◆ operator*() [5/5]

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.

◆ operator+() [1/2]

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.

◆ operator+() [2/2]

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.

◆ operator-()

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.

◆ operator/()

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.

◆ SolveLowerTriangular()

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.

Parameters
LAny lower triangular \( n \times n\) matrix.
xAny \( n\) dimensional vector.
Returns
Solution y of the system, an \( n\) dimensional vector.

Definition at line 303 of file linearalgebra.cpp.

◆ TransposeMatrix()

std::vector<std::vector<double> > TransposeMatrix ( std::vector< std::vector< double > >  M)

Definition at line 127 of file linearalgebra.cpp.

◆ VectorDifference()

std::vector<double> VectorDifference ( std::vector< double >  A,
std::vector< double >  B 
)

Vector difference between two vectors.

Parameters
AVector \( A \) of dimension \( n\).
BVector \( B \) of dimension \( n\).
Returns
Vector \( C \) where \( C_i = A_i - B_i \).

Definition at line 11 of file linearalgebra.cpp.

◆ VectorScalarProduct()

std::vector<double> VectorScalarProduct ( std::vector< double >  A,
double  b 
)

Vector scalar prodcut of vector and scalar.

Parameters
AAny vector \( A \).
BScalar \( b \).
Returns
Vector \( C \) where \( C_{i} = A_{i} \cdot b \).

Definition at line 29 of file linearalgebra.cpp.

◆ VectorSum()

std::vector<double> VectorSum ( std::vector< double >  A,
std::vector< double >  B 
)

Vector sum between two vectors.

Parameters
AVector \( A \) of dimension \( n\).
BVector \( B \) of dimension \( n\).
Returns
Vector \( C \) where \( C_i = A_{i} + B_i \).

Definition at line 36 of file linearalgebra.cpp.

◆ VectorToDiagonal()

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} \).

Parameters
AAny Any \( n \) dimensional vector.
Returns
Diagonal \( n \times n \) matrix \(M\).

Definition at line 169 of file linearalgebra.cpp.

◆ VectorVectorProduct()

double VectorVectorProduct ( std::vector< double >  A,
std::vector< double >  B 
)

Dot product of vectors.

Parameters
AVector \( A \) of dimension \( n\).
BVector \( B \) of dimension \( n\).
Returns
Dot product \( c \) of \( A \) and \( B \) where where \( c = A_i \cdot B_i \) (float).

Definition at line 78 of file linearalgebra.cpp.