Theoretica
A C++ numerical and automatic mathematical library
theoretica::algebra Namespace Reference

Linear algebra routines. More...

Functions

template<typename Matrix >
Matrix & mat_error (Matrix &m)
 Overwrite the given matrix with the error matrix with NaN values on the diagonal and zeroes everywhere else. More...
 
template<typename Vector >
Vector & vec_error (Vector &v)
 Overwrite the given vector with the error vector with NaN values. More...
 
template<typename Matrix >
Matrix & make_identity (Matrix &m)
 Overwrite a matrix with the identity matrix. More...
 
template<typename Matrix >
Matrix & mat_zeroes (Matrix &m)
 Overwrite a matrix with all zeroes. More...
 
template<typename Vector >
Vector & vec_zeroes (Vector &v)
 Overwrite a vector with all zeroes. More...
 
template<typename Matrix1 , typename Matrix2 >
Matrix1 & mat_copy (Matrix1 &dest, const Matrix2 &src)
 Copy a matrix by overwriting another. More...
 
template<typename Vector1 , typename Vector2 >
Vector1 & vec_copy (Vector1 &dest, const Vector2 &src)
 Copy a vector by overwriting another. More...
 
template<typename Matrix >
Matrix & mat_swap_rows (Matrix &A, unsigned int row1, unsigned int row2)
 Swap two rows of a matrix, given the matrix and the two indices of the rows. More...
 
template<typename Matrix >
Matrix & mat_swap_cols (Matrix &A, unsigned int col1, unsigned int col2)
 Swap two columns of a matrix, given the matrix and the two indices of the columns. More...
 
template<typename Matrix , typename Type = matrix_element_t<Matrix>>
Matrix & mat_shift_diagonal (Matrix &A, const Type &sigma)
 Shift the diagonal of a matrix by the given amount, overwriting the matrix itself, as \((A + \sigma I)\). More...
 
template<typename Type >
auto pair_inner_product (const Type &v_i, const Type &w_i)
 Compute the contribution of the inner product between a pair of elements of two vectors, automatically selecting whether to compute the conjugate or not. More...
 
template<typename Type >
auto pair_inner_product (const complex< Type > &v_i, const complex< Type > &w_i)
 Compute the contribution of the inner product between a pair of elements of two vectors, automatically selecting whether to compute the conjugate or not. More...
 
template<typename Vector >
auto sqr_norm (const Vector &v)
 Returns the square of the Euclidean/Hermitian norm of the given vector. More...
 
template<typename Vector >
auto norm (const Vector &v)
 Returns the Euclidean/Hermitian norm of the given vector. More...
 
template<typename Vector >
Vector normalize (const Vector &v)
 Returns the normalized vector. More...
 
template<typename Vector >
Vector & make_normalized (Vector &v)
 Normalize a given vector overwriting it. More...
 
template<typename Vector1 , typename Vector2 >
auto dot (const Vector1 &v, const Vector2 &w)
 Computes the dot product between two vectors, using the Hermitian form if needed. More...
 
template<typename Vector1 , typename Vector2 >
Vector1 cross (const Vector1 &v1, const Vector2 &v2)
 Compute the cross product between two tridimensional vectors. More...
 
template<typename Matrix , typename MatrixT = Matrix>
MatrixT transpose (const Matrix &m)
 Returns the transpose of the given matrix. More...
 
template<typename Matrix >
Matrix & make_transposed (Matrix &m)
 Transpose the given matrix. More...
 
template<typename Matrix1 , typename Matrix2 >
Matrix1 & transpose (Matrix1 &dest, const Matrix2 &src)
 Compute the transpose matrix and write the result to another matrix. More...
 
template<typename Matrix , typename MatrixT = Matrix>
MatrixT hermitian (const Matrix &m)
 Returns the hermitian of the given matrix. More...
 
template<typename Matrix >
Matrix & make_hermitian (Matrix &m)
 Compute the hermitian of a given matrix and overwrite it. More...
 
template<typename Matrix1 , typename Matrix2 >
Matrix1 & hermitian (Matrix1 &dest, const Matrix2 &src)
 Hermitian (conjugate transpose) of a matrix. More...
 
template<typename Matrix >
auto trace (const Matrix &m)
 Compute the trace of the given matrix. More...
 
template<typename Matrix >
auto diagonal_product (const Matrix &m)
 Compute the product of the elements of the main diagonal of a generic matrix. More...
 
template<typename Field , typename Matrix >
Matrix & mat_scalmul (Field a, Matrix &m)
 Multiply a matrix by a scalar of any compatible type. More...
 
template<typename Field , typename Matrix1 , typename Matrix2 >
Matrix1 & mat_scalmul (Matrix1 &dest, Field a, const Matrix2 &src)
 Multiply a matrix by a scalar of any compatible type which can be cast to the type of element of the output matrix. More...
 
template<typename Field , typename Vector >
Vector & vec_scalmul (Field a, Vector &v)
 Multiply a vector by a scalar of any compatible type. More...
 
template<typename Field , typename Vector1 , typename Vector2 >
Vector1 & vec_scalmul (Vector1 &dest, Field a, const Vector2 &src)
 Multiply a vector by a scalar of any compatible type which can be cast to the type of element of the output vector. More...
 
template<typename Matrix , typename Vector >
Vector & apply_transform (const Matrix &A, Vector &v)
 Apply a matrix transformation to a vector and store the result in the vector. More...
 
template<typename Matrix , typename Vector >
Vector transform (const Matrix &A, const Vector &v)
 Returns the matrix transformation of a vector. More...
 
template<typename Matrix , typename Vector1 , typename Vector2 >
Vector1 & transform (Vector1 &res, const Matrix &A, const Vector2 &v)
 Apply a matrix transformation to a vector and store the result in the vector. More...
 
template<typename Matrix1 , typename Matrix2 >
Matrix2 & mat_sum (Matrix1 &A, const Matrix2 &B)
 Sum two matrices and store the result in the first matrix. More...
 
template<typename Matrix1 , typename Matrix2 , typename Matrix3 >
Matrix1 & mat_sum (Matrix1 &res, const Matrix2 &A, const Matrix3 &B)
 Sum two matrices and store the result in another matrix Equivalent to the operation res = A + B. More...
 
template<typename Matrix1 , typename Matrix2 >
Matrix2 & mat_diff (Matrix1 &A, const Matrix2 &B)
 Subtract two matrices and store the result in the first matrix. More...
 
template<typename Matrix1 , typename Matrix2 , typename Matrix3 >
Matrix1 & mat_diff (Matrix1 &res, const Matrix2 &A, const Matrix3 &B)
 Subtract two matrices and store the result in another matrix Equivalent to the operation res = A - B. More...
 
template<typename Field1 , typename Matrix1 , typename Field2 , typename Matrix2 >
Matrix2 & mat_lincomb (Field1 alpha, Matrix1 &A, Field2 beta, const Matrix2 &B)
 Compute the linear combination of two matrices and store the result in the first matrix. More...
 
template<typename Matrix1 , typename Field1 , typename Matrix2 , typename Field2 , typename Matrix3 >
Matrix1 & mat_lincomb (Matrix1 &res, Field1 alpha, const Matrix2 &A, Field2 beta, const Matrix3 &B)
 Compute the linear combination of two matrices and store the result in the first matrix. More...
 
template<typename Matrix1 , typename Matrix2 , typename Matrix3 = Matrix1>
Matrix3 mat_mul (const Matrix1 &A, const Matrix2 &B)
 Multiply two matrices and store the result in the first matrix, equivalent to the operation \(R = A B\). More...
 
template<typename Matrix1 , typename Matrix2 , typename Matrix3 >
Matrix1 & mat_mul (Matrix1 &R, const Matrix2 &A, const Matrix3 &B)
 Multiply two matrices and store the result in another matrix, equivalent to the operation \(R = A B\). More...
 
template<typename Matrix1 , typename Matrix2 , typename Matrix3 = Matrix1>
Matrix3 mat_transpose_mul (const Matrix1 &A, const Matrix2 &B)
 Multiply the transpose of a matrix by another matrix, equivalent to the operation \(R = A^T B\). More...
 
template<typename Matrix1 , typename Matrix2 , typename Matrix3 = Matrix1>
Matrix3 mat_mul_transpose (const Matrix1 &A, const Matrix2 &B)
 Multiply a matrix by the transpose of another matrix, equivalent to the operation \(R = A B^T\). More...
 
template<typename Matrix1 , typename Matrix2 >
bool mat_equals (const Matrix1 &A, const Matrix2 &B, real tolerance=10 *MACH_EPSILON)
 Checks whether two matrices are equal. More...
 
template<typename Vector1 , typename Vector2 >
Vector2 & vec_sum (Vector1 &v1, const Vector2 &v2)
 Sum two vectors and store the result in the first vector. More...
 
template<typename Vector1 , typename Vector2 , typename Vector3 >
Vector1 & vec_sum (Vector1 &res, const Vector2 &v1, const Vector3 &v2)
 Sum two vectors and store the result in another vector Equivalent to the operation res = v1 + v2. More...
 
template<typename Vector1 , typename Vector2 >
Vector2 & vec_diff (Vector1 &v1, const Vector2 &v2)
 Subtract two vectors and store the result in the first vector. More...
 
template<typename Vector1 , typename Vector2 , typename Vector3 >
Vector1 & vec_diff (Vector1 &res, const Vector2 &v1, const Vector3 &v2)
 Subtract two vectors and store the result in another vector Equivalent to the operation res = v1 - v2. More...
 
template<typename Matrix >
bool is_square (const Matrix &m)
 Returns whether the matrix is square. More...
 
template<typename Matrix >
bool is_diagonal (const Matrix &m, real tolerance=10 *MACH_EPSILON)
 Returns whether the matrix is diagonal. More...
 
template<typename Matrix >
bool is_symmetric (const Matrix &m, real tolerance=ALGEBRA_ELEMENT_TOL)
 Returns whether the matrix is symmetric. More...
 
template<typename Matrix >
bool is_lower_triangular (const Matrix &m, real tolerance=ALGEBRA_ELEMENT_TOL)
 Returns whether the matrix is lower triangular. More...
 
template<typename Matrix >
bool is_upper_triangular (const Matrix &m, real tolerance=ALGEBRA_ELEMENT_TOL)
 Returns whether the matrix is upper triangular. More...
 
template<typename Matrix , enable_matrix< Matrix > = true>
real density (const Matrix &A, real tolerance=1E-12)
 Compute the density of a matrix, counting the proportion of non-zero (bigger in module than the given tolerance) elements with respect to the total number of elements. More...
 
template<typename Matrix , enable_matrix< Matrix > = true>
real sparsity (const Matrix &A, real tolerance=1E-12)
 Compute the sparsity of a matrix, counting the proportion of zero (smaller in module than the given tolerance) elements with respect to the total number of elements. More...
 
template<typename Matrix1 , typename Matrix2 , typename Matrix3 >
void decompose_lu (const Matrix1 &A, Matrix2 &L, Matrix3 &U)
 Decompose a square matrix to two triangular matrices, L and U where L is lower and U is upper, so that \(A = LU\). More...
 
template<typename Matrix >
Matrix & decompose_lu_inplace (Matrix &A)
 Decompose a square matrix to two triangular matrices, L and U where L is lower and U is upper, so that \(A = LU\) overwriting the matrix A with the elements of both matrices, omitting the diagonal of L (equal to all ones). More...
 
template<typename Matrix >
Matrix decompose_cholesky (const Matrix &A)
 Decompose a symmetric positive definite matrix into a triangular matrix so that \(A = L L^T\) using Cholesky decomposition. More...
 
template<typename Matrix >
Matrix & decompose_cholesky_inplace (Matrix &A)
 Decompose a symmetric positive definite matrix in-place, overwriting the starting matrix, without using additional space. More...
 
template<typename Matrix , typename Vector >
Vector solve_triangular_lower (const Matrix &L, const Vector &b)
 Solve the linear system \(L \vec x = b\) for lower triangular \(L\). More...
 
template<typename Matrix , typename Vector >
Vector solve_triangular_upper (const Matrix &U, const Vector &b)
 Solve the linear system \(U \vec x = b\) for upper triangular \(U\). More...
 
template<typename Matrix , typename Vector >
Vector solve_triangular (const Matrix &T, const Vector &b)
 Solve the linear system \(T \vec x = b\) for triangular \(T\). More...
 
template<typename Matrix , typename Vector >
Vector & solve_lu_inplace (const Matrix &A, Vector &b)
 Solve the linear system \(A \vec x = \vec b\), finding \(\vec x\), where the matrix A has already undergone in-place LU decomposition. More...
 
template<typename Matrix , typename Vector >
Vector solve_lu (Matrix A, Vector b)
 Solve the linear system \(A \vec x = \vec b\), finding \(\vec x\). More...
 
template<typename Matrix1 , typename Matrix2 , typename Vector >
Vector solve_lu (const Matrix1 &L, const Matrix2 &U, const Vector &b)
 Use the LU decomposition of a matrix to solve its associated linear system, solving \(A \vec x = \vec b\) for \(\vec b\). More...
 
template<typename Matrix , typename Vector >
Vector solve_cholesky (const Matrix &L, const Vector &b)
 Solve a linear system \(A \vec x = \vec b\) defined by a symmetric positive definite matrix, using the Cholesky decomposition \(L\) constructed so that \(A = LL^T\). More...
 
template<typename Matrix , typename Vector >
Vector solve (const Matrix &A, const Vector &b)
 Solve the linear system \(A \vec x = \vec b\), finding \(\vec x\) using the best available algorithm. More...
 
template<typename Matrix1 , typename Matrix2 >
Matrix1 & inverse (Matrix1 &dest, const Matrix2 &src)
 Invert the given matrix. More...
 
template<typename Matrix , typename MatrixInv = Matrix>
MatrixInv inverse (const Matrix &m)
 Returns the inverse of the given matrix. More...
 
template<typename Matrix >
Matrix & invert (Matrix &m)
 Invert the given matrix and overwrite it. More...
 
template<typename Matrix >
auto det (const Matrix &A)
 Compute the determinant of a square matrix. More...
 
template<typename Matrix , typename Vector >
auto rayleigh_quotient (const Matrix &A, const Vector &x)
 Compute the Rayleigh quotient \(\frac{x^T A x}{x^T x}\) of a vector with respect to a matrix. More...
 
template<typename Matrix , typename Vector >
auto eigenvalue_power (const Matrix &A, const Vector &x, real tolerance=ALGEBRA_EIGEN_TOL, unsigned int max_iter=ALGEBRA_EIGEN_ITER)
 Find the biggest eigenvalue in module \(|\lambda_i|\) of a square matrix using the power method (Von Mises iteration). More...
 
template<typename Matrix , typename Vector1 , typename Vector2 = Vector1>
auto eigenpair_power (const Matrix &A, const Vector1 &x, Vector2 &v, real tolerance=ALGEBRA_EIGEN_TOL, unsigned int max_iter=ALGEBRA_EIGEN_ITER)
 Find the biggest eigenvalue in module \(|\lambda_i|\) of a square matrix and its corresponding eigenvector (eigenpair), using the power method (Von Mises iteration). More...
 
template<typename Matrix , typename Vector >
auto eigenvalue_inverse (const Matrix &A, const Vector &x, real tolerance=ALGEBRA_EIGEN_TOL, unsigned int max_iter=ALGEBRA_EIGEN_ITER)
 Find the eigenvalue with the smallest inverse \(\frac{1}{|\lambda_i|}\) of a square matrix, using the inverse power method with parameter equal to 0. More...
 
template<typename Matrix , typename Vector1 , typename Vector2 >
auto eigenpair_inverse (const Matrix &A, const Vector1 &x, Vector2 &v, real tolerance=ALGEBRA_EIGEN_TOL, unsigned int max_iter=ALGEBRA_EIGEN_ITER)
 Find the eigenvalue with the smallest inverse \(\frac{1}{|\lambda_i|}\) of a square matrix and its corresponding eigenvector, using the inverse power method with parameter equal to 0. More...
 
template<typename Matrix , typename Vector , typename T = matrix_element_t<Matrix>>
Vector eigenvector_inverse (const Matrix &A, const T &lambda, const Vector &x, real tolerance=ALGEBRA_EIGEN_TOL, unsigned int max_iter=ALGEBRA_EIGEN_ITER)
 Find the eigenvector associated with a given approximated eigenvalue using the inverse power method. More...
 
template<typename Matrix , typename Vector , typename T = matrix_element_t<Matrix>>
auto eigenvalue_rayleigh (const Matrix &A, const T &lambda, const Vector &x, real tolerance=ALGEBRA_EIGEN_TOL, unsigned int max_iter=ALGEBRA_EIGEN_ITER)
 Compute an eigenvalue of a square matrix using the Rayleigh quotient iteration method. More...
 
template<typename Matrix , typename Vector1 , typename Vector2 , typename T = matrix_element_t<Matrix>>
auto eigenpair_rayleigh (const Matrix &A, const T &lambda, const Vector1 &x, Vector2 &v, real tolerance=ALGEBRA_EIGEN_TOL, unsigned int max_iter=ALGEBRA_EIGEN_ITER)
 Compute an eigenvalue of a square matrix and its corresponding eigenvector (eigenpair) using the Rayleigh quotient iteration method. More...
 
template<typename Vector >
real lp_norm (const Vector &v, unsigned int p)
 Norms. More...
 
template<typename Vector >
real l1_norm (const Vector &v)
 Compute the L1 norm of a vector: \(L_1(\vec v) = \Sigma_i \ |v_i|\). More...
 
template<typename Vector >
real l2_norm (const Vector &v)
 Compute the L2 norm of a vector: \(L_2(\vec v) = \sqrt{\Sigma_i \ v_i^2}\). More...
 
template<typename Vector >
real linf_norm (const Vector &v)
 Compute the Linf norm of a vector: \(L_{\infty}(\vec v) = max(|v_i|)\). More...
 
template<typename Vector >
real euclidean_distance (const Vector &v1, const Vector &v2)
 Distances. More...
 
template<unsigned int N>
real distance (const vec< real, N > &v1, const vec< real, N > &v2)
 Compute the Euclidean distance between two vectors: \(d(\vec v_1, \vec v_2) = L_2(\vec v_1 - \vec v_2)\). More...
 
real euclidean_distance (real a, real b)
 Compute the Euclidian distance between two real values: \(d(a, b) = |a - b|\). More...
 
real distance (real a, real b)
 Compute the Euclidian distance between two values: \(d(a, b) = |a - b|\). More...
 
template<typename T >
complex< T > distance (complex< T > z1, complex< T > z2)
 Compute the distance between two complex numbers: \(d(z_1, z_2) = |z_1 - z_2|\). More...
 
template<typename Vector >
real minkowski_distance (const Vector &v1, const Vector &v2, unsigned int p)
 Compute the Minkowski distance between two vectors: \(d(\vec v_1, \vec v_2) = L_p(\vec v_1 - \vec v_2)\). More...
 
real minkowski_distance (real a, real b, unsigned int p)
 Compute the Minkowski distance between two values: \(d(a, b) = L_p(a - b)\). More...
 
template<typename Vector , typename T = real>
auto hermitian_distance (const Vector &v1, const Vector &v2)
 Compute the Hermitian distance between two vectors: \(d(\vec v_1, \vec v_2) = (\vec v_1 - \vec v_2) \cdot (\vec v_1 - \vec v_2)^*\). More...
 
template<unsigned int N, typename T >
complex< T > distance (const vec< complex< T >, N > &v1, const vec< complex< T >, N > &v2)
 Compute the Hermitian distance between two vectors: \(d(\vec v_1, \vec v_2) = (\vec v_1 - \vec v_2) \cdot (\vec v_1 - \vec v_2)^*\). More...
 
template<typename Vector >
real manhattan_distance (const Vector &v1, const Vector &v2)
 Compute the Manhattan distance between two vectors: \(d(\vec v_1, \vec v_2) = L_1(\vec v_1 - \vec v_2)\). More...
 
template<typename Vector >
real chebyshev_distance (const Vector &v1, const Vector &v2)
 Compute the Chebyshev distance between two vectors: \(d(\vec v_1, \vec v_2) = L_{\infty}(\vec v_1 - \vec v_2)\). More...
 
template<typename Vector >
real discrete_distance (const Vector &v1, const Vector &v2, real tolerance=MACH_EPSILON)
 Compute the discrete distance between two vectors. More...
 
template<typename Vector >
real canberra_distance (const Vector &v1, const Vector &v2)
 Compute the Canberra distance between two vectors. More...
 
template<typename Vector >
real cosine_distance (const Vector &v1, const Vector &v2)
 Compute the cosine distance between two vectors. More...
 
template<typename Vector >
real hamming_distance (const Vector &v1, const Vector &v2, real tolerance=MACH_EPSILON)
 Compute the Hamming distance between two vectors. More...
 
template<typename Matrix >
Matrix identity (unsigned int rows=0, unsigned int cols=0)
 Returns the identity matrix. More...
 
template<typename Vector , typename Matrix >
Matrix & diagonal (Matrix &res, const Vector &v)
 Construct a matrix with the given vector as diagonal and zeroes everywhere else, overwriting the given matrix. More...
 
template<typename Matrix , typename Vector >
Matrix diagonal (const Vector &v, unsigned int rows=0, unsigned int cols=0)
 Returns the a matrix with the given diagonal. More...
 
template<typename Matrix , typename Vector >
Matrix translation (const Vector &v, unsigned int rows=0, unsigned int cols=0)
 Returns a translation matrix, that is, an NxN matrix which describes a translation in N-1 dimensions. More...
 
template<typename Matrix >
Matrix rotation_2d (real theta, unsigned int rows=0, unsigned int cols=0)
 Returns a matrix representing a 2D rotation. More...
 
template<typename Matrix , typename Vector >
Matrix rotation_3d (real theta, const Vector &axis, unsigned int rows=0, unsigned int cols=0)
 Returns a matrix representing a 3D rotation around a given axis. More...
 
template<typename Matrix >
Matrix rotation_3d_xaxis (real theta, unsigned int rows=0, unsigned int cols=0)
 Returns a matrix representing a 3D rotation around the x axis. More...
 
template<typename Matrix >
Matrix rotation_3d_yaxis (real theta, unsigned int rows=0, unsigned int cols=0)
 Returns a matrix representing a 3D rotation around the y axis. More...
 
template<typename Matrix >
Matrix rotation_3d_zaxis (real theta, unsigned int rows=0, unsigned int cols=0)
 Returns a matrix representing a 3D rotation around the z axis. More...
 
template<typename Matrix >
Matrix perspective (real left, real right, real bottom, real top, real near, real far, unsigned int rows=0, unsigned int cols=0)
 Returns a perspective projection matrix.
 
template<typename Matrix >
Matrix perspective_fov (real fov, real aspect, real near, real far, unsigned int rows=0, unsigned int cols=0)
 Returns a perspective projection matrix, using the Field of View as parameter.
 
template<typename Matrix >
Matrix ortho (real left, real right, real bottom, real top, real near, real far, unsigned int rows=0, unsigned int cols=0)
 Returns an orthogonal projection matrix.
 
template<typename Matrix , typename Vector1 , typename Vector2 , typename Vector3 >
Matrix look_at (Vector1 camera, Vector2 target, Vector3 up)
 Return a transformation matrix that points the field of view towards a given point from the <camera> point.
 
template<typename Matrix >
Matrix symplectic (unsigned int rows=0, unsigned int cols=0)
 Generate a NxN symplectic matrix where N is even.
 
template<typename Matrix >
Matrix hilbert (unsigned int rows=0)
 Construct the Hilbert matrix of arbitrary dimension. More...
 
template<typename Vector1 , typename Vector2 >
Vector1 sphere_inversion (const Vector1 &p, const Vector2 &c=Vector2(0), real r=1)
 Sphere inversion of a point with respect to a sphere of radius r centered at a point c. More...
 

Detailed Description

Linear algebra routines.

Function Documentation

◆ apply_transform()

template<typename Matrix , typename Vector >
Vector& theoretica::algebra::apply_transform ( const Matrix &  A,
Vector &  v 
)
inline

Apply a matrix transformation to a vector and store the result in the vector.

Equivalent to the operation v = A * v

Parameters
AThe matrix transformation
vThe vector to transform
Returns
A reference to the overwritten vector

◆ canberra_distance()

template<typename Vector >
real theoretica::algebra::canberra_distance ( const Vector &  v1,
const Vector &  v2 
)
inline

Compute the Canberra distance between two vectors.

Parameters
v1The first vector
v2The second vector
Returns
The Canberra distance between v1 and v2

◆ chebyshev_distance()

template<typename Vector >
real theoretica::algebra::chebyshev_distance ( const Vector &  v1,
const Vector &  v2 
)
inline

Compute the Chebyshev distance between two vectors: \(d(\vec v_1, \vec v_2) = L_{\infty}(\vec v_1 - \vec v_2)\).

Parameters
v1The first vector
v2The second vector
Returns
The Chebyshev distance between v1 and v2

◆ cosine_distance()

template<typename Vector >
real theoretica::algebra::cosine_distance ( const Vector &  v1,
const Vector &  v2 
)
inline

Compute the cosine distance between two vectors.

Parameters
v1The first vector
v2The second vector
Returns
The cosine distance between v1 and v2

◆ cross()

template<typename Vector1 , typename Vector2 >
Vector1 theoretica::algebra::cross ( const Vector1 &  v1,
const Vector2 &  v2 
)
inline

Compute the cross product between two tridimensional vectors.

Parameters
v1The first tridimensional vector
wThe second tridimensional vector
Returns
The cross product of the two vectors

◆ decompose_cholesky()

template<typename Matrix >
Matrix theoretica::algebra::decompose_cholesky ( const Matrix &  A)
inline

Decompose a symmetric positive definite matrix into a triangular matrix so that \(A = L L^T\) using Cholesky decomposition.

Parameters
AThe matrix to decompose
Returns
The Cholesky decomposition of the matrix

◆ decompose_cholesky_inplace()

template<typename Matrix >
Matrix& theoretica::algebra::decompose_cholesky_inplace ( Matrix &  A)
inline

Decompose a symmetric positive definite matrix in-place, overwriting the starting matrix, without using additional space.

Parameters
AThe symmetric, positive definite matrix to decompose and overwrite with the lower triangular matrix.

◆ decompose_lu()

template<typename Matrix1 , typename Matrix2 , typename Matrix3 >
void theoretica::algebra::decompose_lu ( const Matrix1 &  A,
Matrix2 &  L,
Matrix3 &  U 
)
inline

Decompose a square matrix to two triangular matrices, L and U where L is lower and U is upper, so that \(A = LU\).

Parameters
AThe matrix to decompose
LThe matrix to overwrite with the lower triangular one
UThe matrix to overwrite with the upper triangular one
Returns
The LU decomposition of the matrix

◆ decompose_lu_inplace()

template<typename Matrix >
Matrix& theoretica::algebra::decompose_lu_inplace ( Matrix &  A)
inline

Decompose a square matrix to two triangular matrices, L and U where L is lower and U is upper, so that \(A = LU\) overwriting the matrix A with the elements of both matrices, omitting the diagonal of L (equal to all ones).

Particularly useful for solving linear systems.

Parameters
AThe matrix to decompose and overwrite
Returns
A reference to the overwritten matrix A

◆ density()

template<typename Matrix , enable_matrix< Matrix > = true>
real theoretica::algebra::density ( const Matrix &  A,
real  tolerance = 1E-12 
)
inline

Compute the density of a matrix, counting the proportion of non-zero (bigger in module than the given tolerance) elements with respect to the total number of elements.

Parameters
AThe matrix to compute the density of
toleranceThe minimum tolerance in absolute value to consider an element non-zero.
Returns
A real number between 0 and 1 representing the proportion of non-zero elements of the matrix.

◆ det()

template<typename Matrix >
auto theoretica::algebra::det ( const Matrix &  A)
inline

Compute the determinant of a square matrix.

In-place LU decomposition is used to reduce the matrix to triangular form.

Parameters
AThe matrix to compute the determinant of
Returns
The determinant of the matrix

◆ diagonal() [1/2]

template<typename Matrix , typename Vector >
Matrix theoretica::algebra::diagonal ( const Vector &  v,
unsigned int  rows = 0,
unsigned int  cols = 0 
)
inline

Returns the a matrix with the given diagonal.

The function without any parameters is used for statically allocated matrix types.

Parameters
vThe vector of the diagonal elements
rowsThe number of rows of the matrix
colsThe number of columns of the matrix
Returns
The diagonal matrix of the given type

◆ diagonal() [2/2]

template<typename Vector , typename Matrix >
Matrix& theoretica::algebra::diagonal ( Matrix &  res,
const Vector &  v 
)
inline

Construct a matrix with the given vector as diagonal and zeroes everywhere else, overwriting the given matrix.

Parameters
Thevector of diagonal elements
resThe matrix to overwrite
Returns
A reference to the overwritten matrix

◆ diagonal_product()

template<typename Matrix >
auto theoretica::algebra::diagonal_product ( const Matrix &  m)
inline

Compute the product of the elements of the main diagonal of a generic matrix.

Parameters
mThe input matrix
Returns
The product of all the elements of the main diagonal of the input matrix

◆ discrete_distance()

template<typename Vector >
real theoretica::algebra::discrete_distance ( const Vector &  v1,
const Vector &  v2,
real  tolerance = MACH_EPSILON 
)
inline

Compute the discrete distance between two vectors.

Parameters
v1The first vector
v2The second vector
toleranceThe minimum absolute difference between elements to consider them different (defaults to MACH_EPSILON).
Returns
The discrete distance between v1 and v2

◆ distance() [1/4]

template<typename T >
complex<T> theoretica::algebra::distance ( complex< T >  z1,
complex< T >  z2 
)
inline

Compute the distance between two complex numbers: \(d(z_1, z_2) = |z_1 - z_2|\).

Parameters
z1The first complex value
z2The second complex value
Returns
The distance between z1 and z2

◆ distance() [2/4]

template<unsigned int N, typename T >
complex<T> theoretica::algebra::distance ( const vec< complex< T >, N > &  v1,
const vec< complex< T >, N > &  v2 
)
inline

Compute the Hermitian distance between two vectors: \(d(\vec v_1, \vec v_2) = (\vec v_1 - \vec v_2) \cdot (\vec v_1 - \vec v_2)^*\).

Parameters
v1The first vector
v2The second vector
Returns
The Hermitian distance between v1 and v2

◆ distance() [3/4]

template<unsigned int N>
real theoretica::algebra::distance ( const vec< real, N > &  v1,
const vec< real, N > &  v2 
)
inline

Compute the Euclidean distance between two vectors: \(d(\vec v_1, \vec v_2) = L_2(\vec v_1 - \vec v_2)\).

Parameters
v1The first vector
v2The second vector
Returns
The Euclidean distance between v1 and v2

◆ distance() [4/4]

real theoretica::algebra::distance ( real  a,
real  b 
)
inline

Compute the Euclidian distance between two values: \(d(a, b) = |a - b|\).

Parameters
aThe first real value
bThe second real value
Returns
The Euclidean distance between a and b

◆ dot()

template<typename Vector1 , typename Vector2 >
auto theoretica::algebra::dot ( const Vector1 &  v,
const Vector2 &  w 
)
inline

Computes the dot product between two vectors, using the Hermitian form if needed.

Parameters
vThe first vector
wThe second vector
Returns
The dot product of the two vectors

◆ eigenpair_inverse()

template<typename Matrix , typename Vector1 , typename Vector2 >
auto theoretica::algebra::eigenpair_inverse ( const Matrix &  A,
const Vector1 &  x,
Vector2 &  v,
real  tolerance = ALGEBRA_EIGEN_TOL,
unsigned int  max_iter = ALGEBRA_EIGEN_ITER 
)
inline

Find the eigenvalue with the smallest inverse \(\frac{1}{|\lambda_i|}\) of a square matrix and its corresponding eigenvector, using the inverse power method with parameter equal to 0.

Parameters
AThe matrix to find the smallest eigenvalue of
xThe starting vector (a random vector is a good choice)
vThe vector to overwrite with the eigenvector
toleranceThe minimum difference in norm between subsequent steps to stop the algorithm at (defaults to ALGEBRA_EIGEN_TOL).
max_iterThe maximum number of iterations to use (defaults to ALGEBRA_EIGEN_ITER).
Returns
The eigenvalue with the smallest inverse of the matrix, or NaN if the algorithm did not converge.

◆ eigenpair_power()

template<typename Matrix , typename Vector1 , typename Vector2 = Vector1>
auto theoretica::algebra::eigenpair_power ( const Matrix &  A,
const Vector1 &  x,
Vector2 &  v,
real  tolerance = ALGEBRA_EIGEN_TOL,
unsigned int  max_iter = ALGEBRA_EIGEN_ITER 
)
inline

Find the biggest eigenvalue in module \(|\lambda_i|\) of a square matrix and its corresponding eigenvector (eigenpair), using the power method (Von Mises iteration).

Parameters
AThe matrix to find the biggest eigenvalue of
xThe starting vector (a random vector is a good choice)
vThe vector to overwrite with the eigenvector
toleranceThe minimum difference in norm between subsequent steps to stop the algorithm at (defaults to ALGEBRA_EIGEN_TOL).
max_iterThe maximum number of iterations to use (defaults to ALGEBRA_EIGEN_ITER).
Returns
The biggest eigenvalue of the matrix, or NaN if the algorithm did not converge.

◆ eigenpair_rayleigh()

template<typename Matrix , typename Vector1 , typename Vector2 , typename T = matrix_element_t<Matrix>>
auto theoretica::algebra::eigenpair_rayleigh ( const Matrix &  A,
const T &  lambda,
const Vector1 &  x,
Vector2 &  v,
real  tolerance = ALGEBRA_EIGEN_TOL,
unsigned int  max_iter = ALGEBRA_EIGEN_ITER 
)
inline

Compute an eigenvalue of a square matrix and its corresponding eigenvector (eigenpair) using the Rayleigh quotient iteration method.

Parameters
AThe matrix to compute the eigenvalue of
lambdaThe starting value for the Rayleigh quotient
xThe starting approximation for the eigenvector (a random vector is a good choice).
vThe vector to overwrite with the eigenvector
toleranceThe minimum difference in norm between subsequent steps to stop the algorithm at (defaults to ALGEBRA_EIGEN_TOL).
max_iterThe maximum number of iterations to use (defaults to ALGEBRA_EIGEN_ITER).
Returns
An approximate eigenvalue of the matrix, or NaN if the algorithm did not converge.

◆ eigenvalue_inverse()

template<typename Matrix , typename Vector >
auto theoretica::algebra::eigenvalue_inverse ( const Matrix &  A,
const Vector &  x,
real  tolerance = ALGEBRA_EIGEN_TOL,
unsigned int  max_iter = ALGEBRA_EIGEN_ITER 
)
inline

Find the eigenvalue with the smallest inverse \(\frac{1}{|\lambda_i|}\) of a square matrix, using the inverse power method with parameter equal to 0.

Parameters
AThe matrix to find the smallest eigenvalue of
xThe starting vector (a random vector is a good choice)
toleranceThe minimum difference in norm between subsequent steps to stop the algorithm at (defaults to ALGEBRA_EIGEN_TOL).
max_iterThe maximum number of iterations to use (defaults to ALGEBRA_EIGEN_ITER).
Returns
The eigenvalue with the smallest inverse of the matrix, or NaN if the algorithm did not converge.

◆ eigenvalue_power()

template<typename Matrix , typename Vector >
auto theoretica::algebra::eigenvalue_power ( const Matrix &  A,
const Vector &  x,
real  tolerance = ALGEBRA_EIGEN_TOL,
unsigned int  max_iter = ALGEBRA_EIGEN_ITER 
)
inline

Find the biggest eigenvalue in module \(|\lambda_i|\) of a square matrix using the power method (Von Mises iteration).

Parameters
AThe matrix to find the biggest eigenvalue of
xThe starting vector (a random vector is a good choice)
toleranceThe minimum difference in norm between subsequent steps to stop the algorithm at (defaults to ALGEBRA_EIGEN_TOL).
max_iterThe maximum number of iterations to use (defaults to ALGEBRA_EIGEN_ITER).
Returns
The biggest eigenvalue of the matrix, or NaN if the algorithm did not converge.

◆ eigenvalue_rayleigh()

template<typename Matrix , typename Vector , typename T = matrix_element_t<Matrix>>
auto theoretica::algebra::eigenvalue_rayleigh ( const Matrix &  A,
const T &  lambda,
const Vector &  x,
real  tolerance = ALGEBRA_EIGEN_TOL,
unsigned int  max_iter = ALGEBRA_EIGEN_ITER 
)
inline

Compute an eigenvalue of a square matrix using the Rayleigh quotient iteration method.

Parameters
AThe matrix to compute the eigenvalue of
lambdaThe starting value for the Rayleigh quotient
xThe starting approximation for the eigenvector (a random vector is a good choice).
toleranceThe minimum difference in norm between subsequent steps to stop the algorithm at (defaults to ALGEBRA_EIGEN_TOL).
max_iterThe maximum number of iterations to use (defaults to ALGEBRA_EIGEN_ITER).
Returns
An approximate eigenvalue of the matrix, or NaN if the algorithm did not converge.

◆ eigenvector_inverse()

template<typename Matrix , typename Vector , typename T = matrix_element_t<Matrix>>
Vector theoretica::algebra::eigenvector_inverse ( const Matrix &  A,
const T &  lambda,
const Vector &  x,
real  tolerance = ALGEBRA_EIGEN_TOL,
unsigned int  max_iter = ALGEBRA_EIGEN_ITER 
)
inline

Find the eigenvector associated with a given approximated eigenvalue using the inverse power method.

Note
The algorithm is unstable when the approximation of the eigenvalue is too close to the true value. A value not too close to the actual eigenvalue and far away from the other eigenvalues should be used.
Parameters
AThe matrix to find the eigenvector of
lambdaThe eigenvalue
xThe starting vector (a random vector is a good choice)
toleranceThe minimum difference in norm between subsequent steps to stop the algorithm at (defaults to ALGEBRA_EIGEN_TOL).
max_iterThe maximum number of iterations to use (defaults to ALGEBRA_EIGEN_ITER).
Returns
The approximate eigenvector associated with the eigenvalue

◆ euclidean_distance() [1/2]

template<typename Vector >
real theoretica::algebra::euclidean_distance ( const Vector &  v1,
const Vector &  v2 
)
inline

Distances.

Compute the Euclidean distance between two vectors: \(d(\vec v_1, \vec v_2) = L_2(\vec v_1 - \vec v_2)\)

Parameters
v1The first vector
v2The second vector
Returns
The Euclidean distance between v1 and v2

◆ euclidean_distance() [2/2]

real theoretica::algebra::euclidean_distance ( real  a,
real  b 
)
inline

Compute the Euclidian distance between two real values: \(d(a, b) = |a - b|\).

Parameters
aThe first real value
bThe second real value
Returns
The Euclidean distance between a and b

◆ hamming_distance()

template<typename Vector >
real theoretica::algebra::hamming_distance ( const Vector &  v1,
const Vector &  v2,
real  tolerance = MACH_EPSILON 
)
inline

Compute the Hamming distance between two vectors.

Parameters
v1The first vector
v2The second vector
toleranceThe minimum absolute difference between elements to consider them different (defaults to MACH_EPSILON).
Returns
The Hamming distance between v1 and v2

◆ hermitian() [1/2]

template<typename Matrix , typename MatrixT = Matrix>
MatrixT theoretica::algebra::hermitian ( const Matrix &  m)
inline

Returns the hermitian of the given matrix.

Equivalent to the operation m^T

Parameters
mThe matrix to compute the hermitian of
Returns
The hermitian of the matrix

◆ hermitian() [2/2]

template<typename Matrix1 , typename Matrix2 >
Matrix1& theoretica::algebra::hermitian ( Matrix1 &  dest,
const Matrix2 &  src 
)
inline

Hermitian (conjugate transpose) of a matrix.

Equivalent to the operation dest = src^H. The base type of the matrix needs to have a compatible conjugate() function.

Parameters
destThe matrix to overwrite
srcThe matrix to compute the hermitian of
Returns
A reference to the overwritten matrix

◆ hermitian_distance()

template<typename Vector , typename T = real>
auto theoretica::algebra::hermitian_distance ( const Vector &  v1,
const Vector &  v2 
)
inline

Compute the Hermitian distance between two vectors: \(d(\vec v_1, \vec v_2) = (\vec v_1 - \vec v_2) \cdot (\vec v_1 - \vec v_2)^*\).

Parameters
v1The first vector
v2The second vector
Returns
The Hermitian distance between v1 and v2

◆ hilbert()

template<typename Matrix >
Matrix theoretica::algebra::hilbert ( unsigned int  rows = 0)
inline

Construct the Hilbert matrix of arbitrary dimension.

The Hilbert matrices are square matrices with particularly high condition number, which makes them ill-conditioned for numerical calculations. The elements of the Hilbert matrix are given by \(H_{ij} = \frac{1}{i + j - 1}\) (for \(i,j\) starting from 1).

Parameters
rowsThe number of rows (and columns) of the resulting matrix
Returns
The Hilbert matrix of the given size

◆ identity()

template<typename Matrix >
Matrix theoretica::algebra::identity ( unsigned int  rows = 0,
unsigned int  cols = 0 
)
inline

Returns the identity matrix.

Size parameters are used only for dynamically allocated matrix types.

Returns
The identity matrix of the given type

◆ inverse() [1/2]

template<typename Matrix , typename MatrixInv = Matrix>
MatrixInv theoretica::algebra::inverse ( const Matrix &  m)
inline

Returns the inverse of the given matrix.

Equivalent to the operation \(m^-1\)

Parameters
mThe matrix to invert
Returns
The inverted matrix

◆ inverse() [2/2]

template<typename Matrix1 , typename Matrix2 >
Matrix1& theoretica::algebra::inverse ( Matrix1 &  dest,
const Matrix2 &  src 
)
inline

Invert the given matrix.

Equivalent to the operation dest = src^-1

Parameters
destThe matrix to overwrite
srcThe matrix to invert
Returns
A reference to the inverted matrix

◆ invert()

template<typename Matrix >
Matrix& theoretica::algebra::invert ( Matrix &  m)
inline

Invert the given matrix and overwrite it.

Equivalent to the operation m = m^-1

Parameters
mThe matrix to invert
Returns
A reference to the inverted matrix

◆ is_diagonal()

template<typename Matrix >
bool theoretica::algebra::is_diagonal ( const Matrix &  m,
real  tolerance = 10 * MACH_EPSILON 
)
inline

Returns whether the matrix is diagonal.

Parameters
mThe matrix to consider
toleranceThe tolerance to allow for in the comparison, defaults to 10 * MACH_EPSILON
Returns
A boolean value

◆ is_lower_triangular()

template<typename Matrix >
bool theoretica::algebra::is_lower_triangular ( const Matrix &  m,
real  tolerance = ALGEBRA_ELEMENT_TOL 
)
inline

Returns whether the matrix is lower triangular.

Parameters
mThe matrix to consider
toleranceThe tolerance to allow for in the comparison, defaults to ALGEBRA_ELEMENT_TOL
Returns
A boolean value

◆ is_square()

template<typename Matrix >
bool theoretica::algebra::is_square ( const Matrix &  m)
inline

Returns whether the matrix is square.

Parameters
mThe matrix to consider
Returns
A boolean value

◆ is_symmetric()

template<typename Matrix >
bool theoretica::algebra::is_symmetric ( const Matrix &  m,
real  tolerance = ALGEBRA_ELEMENT_TOL 
)
inline

Returns whether the matrix is symmetric.

Parameters
mThe matrix to consider
toleranceThe tolerance to allow for in the comparison, defaults to ALGEBRA_ELEMENT_TOL
Returns
A boolean value

◆ is_upper_triangular()

template<typename Matrix >
bool theoretica::algebra::is_upper_triangular ( const Matrix &  m,
real  tolerance = ALGEBRA_ELEMENT_TOL 
)
inline

Returns whether the matrix is upper triangular.

Parameters
mThe matrix to consider
toleranceThe tolerance to allow for in the comparison, defaults to ALGEBRA_ELEMENT_TOL
Returns
A boolean value

◆ l1_norm()

template<typename Vector >
real theoretica::algebra::l1_norm ( const Vector &  v)
inline

Compute the L1 norm of a vector: \(L_1(\vec v) = \Sigma_i \ |v_i|\).

Parameters
vThe vector to compute the norm of
Returns
The L1 norm of v

◆ l2_norm()

template<typename Vector >
real theoretica::algebra::l2_norm ( const Vector &  v)
inline

Compute the L2 norm of a vector: \(L_2(\vec v) = \sqrt{\Sigma_i \ v_i^2}\).

Parameters
vThe vector to compute the norm of
Returns
The L2 norm of v

◆ linf_norm()

template<typename Vector >
real theoretica::algebra::linf_norm ( const Vector &  v)
inline

Compute the Linf norm of a vector: \(L_{\infty}(\vec v) = max(|v_i|)\).

Parameters
vThe vector to compute the norm of
Returns
The Linf norm of v

◆ lp_norm()

template<typename Vector >
real theoretica::algebra::lp_norm ( const Vector &  v,
unsigned int  p 
)
inline

Norms.

Compute the Lp norm of a vector: \(L_p(\vec v) = (\Sigma_i \ |v_i|^p)^{1/p}\)

Parameters
vThe vector to compute the norm of
pThe power of the Lp norm
Returns
The Lp norm of v

◆ make_hermitian()

template<typename Matrix >
Matrix& theoretica::algebra::make_hermitian ( Matrix &  m)
inline

Compute the hermitian of a given matrix and overwrite it.

Equivalent to the operation m = m^H

Parameters
mThe matrix to compute the hermitian of
Returns
A reference to the overwritten matrix

◆ make_identity()

template<typename Matrix >
Matrix& theoretica::algebra::make_identity ( Matrix &  m)
inline

Overwrite a matrix with the identity matrix.

Parameters
mThe matrix to overwrite
Returns
A reference to the overwritten matrix

◆ make_normalized()

template<typename Vector >
Vector& theoretica::algebra::make_normalized ( Vector &  v)
inline

Normalize a given vector overwriting it.

Parameters
vThe vector to normalize
Returns
A reference to the overwritten vector

◆ make_transposed()

template<typename Matrix >
Matrix& theoretica::algebra::make_transposed ( Matrix &  m)
inline

Transpose the given matrix.

Parameters
mThe matrix to transpose
Returns
A reference to the transposed matrix

◆ manhattan_distance()

template<typename Vector >
real theoretica::algebra::manhattan_distance ( const Vector &  v1,
const Vector &  v2 
)
inline

Compute the Manhattan distance between two vectors: \(d(\vec v_1, \vec v_2) = L_1(\vec v_1 - \vec v_2)\).

Parameters
v1The first vector
v2The second vector
Returns
The Manhattan distance between v1 and v2

◆ mat_copy()

template<typename Matrix1 , typename Matrix2 >
Matrix1& theoretica::algebra::mat_copy ( Matrix1 &  dest,
const Matrix2 &  src 
)
inline

Copy a matrix by overwriting another.

Parameters
destThe matrix to overwrite
srcThe matrix to copy
Returns
A reference to the overwritten matrix

◆ mat_diff() [1/2]

template<typename Matrix1 , typename Matrix2 >
Matrix2& theoretica::algebra::mat_diff ( Matrix1 &  A,
const Matrix2 &  B 
)
inline

Subtract two matrices and store the result in the first matrix.

Equivalent to the operation A = A - B

Parameters
AThe first matrix to store the result
BThe second matrix
Returns
A reference to the overwritten matrix

◆ mat_diff() [2/2]

template<typename Matrix1 , typename Matrix2 , typename Matrix3 >
Matrix1& theoretica::algebra::mat_diff ( Matrix1 &  res,
const Matrix2 &  A,
const Matrix3 &  B 
)
inline

Subtract two matrices and store the result in another matrix Equivalent to the operation res = A - B.

Parameters
AThe first matrix
BThe second matrix
Returns
A reference to the overwritten matrix

◆ mat_equals()

template<typename Matrix1 , typename Matrix2 >
bool theoretica::algebra::mat_equals ( const Matrix1 &  A,
const Matrix2 &  B,
real  tolerance = 10 * MACH_EPSILON 
)
inline

Checks whether two matrices are equal.

Parameters
AThe first matrix
BThe second matrix
toleranceThe tolerance to allow for in the comparison, defaults to 10 * MACH_EPSILON
Returns
A boolean value

◆ mat_error()

template<typename Matrix >
Matrix& theoretica::algebra::mat_error ( Matrix &  m)
inline

Overwrite the given matrix with the error matrix with NaN values on the diagonal and zeroes everywhere else.

This function is used to signal an error.

Parameters
mThe matrix to overwrite
Returns
A reference to the overwritten matrix

◆ mat_lincomb() [1/2]

template<typename Field1 , typename Matrix1 , typename Field2 , typename Matrix2 >
Matrix2& theoretica::algebra::mat_lincomb ( Field1  alpha,
Matrix1 &  A,
Field2  beta,
const Matrix2 &  B 
)
inline

Compute the linear combination of two matrices and store the result in the first matrix.

Equivalent to the operation A = alpha * A + beta * B

Parameters
AThe first matrix to combine and store the result
BThe second matrix to combine
Returns
A reference to the overwritten matrix

◆ mat_lincomb() [2/2]

template<typename Matrix1 , typename Field1 , typename Matrix2 , typename Field2 , typename Matrix3 >
Matrix1& theoretica::algebra::mat_lincomb ( Matrix1 &  res,
Field1  alpha,
const Matrix2 &  A,
Field2  beta,
const Matrix3 &  B 
)
inline

Compute the linear combination of two matrices and store the result in the first matrix.

Equivalent to the operation res = alpha * A + beta * B

Parameters
resThe matrix to overwrite with the result
alphaThe first scalar parameter
AThe first matrix to combine
betaThe second scalar parameter
BThe second matrix to combine
Returns
A reference to the overwritten matrix

◆ mat_mul() [1/2]

template<typename Matrix1 , typename Matrix2 , typename Matrix3 = Matrix1>
Matrix3 theoretica::algebra::mat_mul ( const Matrix1 &  A,
const Matrix2 &  B 
)
inline

Multiply two matrices and store the result in the first matrix, equivalent to the operation \(R = A B\).

Parameters
AThe first matrix to multiply and store the result
BThe second matrix to multiply
Returns
The resulting matrix

◆ mat_mul() [2/2]

template<typename Matrix1 , typename Matrix2 , typename Matrix3 >
Matrix1& theoretica::algebra::mat_mul ( Matrix1 &  R,
const Matrix2 &  A,
const Matrix3 &  B 
)
inline

Multiply two matrices and store the result in another matrix, equivalent to the operation \(R = A B\).

Parameters
RThe matrix to overwrite with the result
AThe first matrix to multiply
BThe second matrix to multiply
Returns
A reference to the modified matrix

◆ mat_mul_transpose()

template<typename Matrix1 , typename Matrix2 , typename Matrix3 = Matrix1>
Matrix3 theoretica::algebra::mat_mul_transpose ( const Matrix1 &  A,
const Matrix2 &  B 
)
inline

Multiply a matrix by the transpose of another matrix, equivalent to the operation \(R = A B^T\).

Note
This function is faster then writing A * algebra::transpose(B) and should be preferred.
Parameters
AThe first matrix to multiply
BThe second matrix to transpose and multiply by
Returns
The result of the multiplication by the first matrix and the transpose of second matrix.

◆ mat_scalmul() [1/2]

template<typename Field , typename Matrix >
Matrix& theoretica::algebra::mat_scalmul ( Field  a,
Matrix &  m 
)
inline

Multiply a matrix by a scalar of any compatible type.

Parameters
aA scalar value
mThe matrix to multiply
Returns
A reference to the multiplied matrix

◆ mat_scalmul() [2/2]

template<typename Field , typename Matrix1 , typename Matrix2 >
Matrix1& theoretica::algebra::mat_scalmul ( Matrix1 &  dest,
Field  a,
const Matrix2 &  src 
)
inline

Multiply a matrix by a scalar of any compatible type which can be cast to the type of element of the output matrix.

Parameters
destThe matrix to overwrite with the result
aA scalar value
srcThe matrix to multiply
Returns
A reference to the resulting matrix

◆ mat_shift_diagonal()

template<typename Matrix , typename Type = matrix_element_t<Matrix>>
Matrix& theoretica::algebra::mat_shift_diagonal ( Matrix &  A,
const Type &  sigma 
)
inline

Shift the diagonal of a matrix by the given amount, overwriting the matrix itself, as \((A + \sigma I)\).

Parameters
AThe matrix to shift the diagonal of
sigmaThe amount to shift
Returns
A reference to the modified matrix

◆ mat_sum() [1/2]

template<typename Matrix1 , typename Matrix2 >
Matrix2& theoretica::algebra::mat_sum ( Matrix1 &  A,
const Matrix2 &  B 
)
inline

Sum two matrices and store the result in the first matrix.

Equivalent to the operation A = A + B

Parameters
AThe first matrix to add and store the result
BThe second matrix to add
Returns
A reference to the overwritten matrix

◆ mat_sum() [2/2]

template<typename Matrix1 , typename Matrix2 , typename Matrix3 >
Matrix1& theoretica::algebra::mat_sum ( Matrix1 &  res,
const Matrix2 &  A,
const Matrix3 &  B 
)
inline

Sum two matrices and store the result in another matrix Equivalent to the operation res = A + B.

Parameters
AThe first matrix to add
BThe second matrix to add
Returns
A reference to the overwritten matrix

◆ mat_swap_cols()

template<typename Matrix >
Matrix& theoretica::algebra::mat_swap_cols ( Matrix &  A,
unsigned int  col1,
unsigned int  col2 
)
inline

Swap two columns of a matrix, given the matrix and the two indices of the columns.

Parameters
AThe matrix to swap the columns of
col1The index of the first column to swap
col2The index of the other column to swap

◆ mat_swap_rows()

template<typename Matrix >
Matrix& theoretica::algebra::mat_swap_rows ( Matrix &  A,
unsigned int  row1,
unsigned int  row2 
)
inline

Swap two rows of a matrix, given the matrix and the two indices of the rows.

Parameters
AThe matrix to swap the rows of
row1The index of the first row to swap
row2The index of the other row to swap

◆ mat_transpose_mul()

template<typename Matrix1 , typename Matrix2 , typename Matrix3 = Matrix1>
Matrix3 theoretica::algebra::mat_transpose_mul ( const Matrix1 &  A,
const Matrix2 &  B 
)
inline

Multiply the transpose of a matrix by another matrix, equivalent to the operation \(R = A^T B\).

Note
This function is faster then writing algebra::transpose(A) * B and should be preferred.
Parameters
AThe matrix to transpose and then multiply
BThe second matrix to multiply by
Returns
The result of the multiplication by the transpose of the first matrix and the second matrix.

◆ mat_zeroes()

template<typename Matrix >
Matrix& theoretica::algebra::mat_zeroes ( Matrix &  m)
inline

Overwrite a matrix with all zeroes.

Parameters
mThe matrix to overwrite
Returns
A reference to the overwritten matrix

◆ minkowski_distance() [1/2]

template<typename Vector >
real theoretica::algebra::minkowski_distance ( const Vector &  v1,
const Vector &  v2,
unsigned int  p 
)
inline

Compute the Minkowski distance between two vectors: \(d(\vec v_1, \vec v_2) = L_p(\vec v_1 - \vec v_2)\).

Parameters
v1The first vector
v2The second vector
pThe power of the distance
Returns
The Minkowski distance of power p between v1 and v2

◆ minkowski_distance() [2/2]

real theoretica::algebra::minkowski_distance ( real  a,
real  b,
unsigned int  p 
)
inline

Compute the Minkowski distance between two values: \(d(a, b) = L_p(a - b)\).

Parameters
aThe first real value
bThe second real value
pThe power of the distance
Returns
The Minkowski distance of power p between a and b

◆ norm()

template<typename Vector >
auto theoretica::algebra::norm ( const Vector &  v)
inline

Returns the Euclidean/Hermitian norm of the given vector.

Parameters
vThe vector to compute the norm of
Returns
The norm of the given vector

◆ normalize()

template<typename Vector >
Vector theoretica::algebra::normalize ( const Vector &  v)
inline

Returns the normalized vector.

Parameters
vThe vector to normalize
Returns
The normalized vector

◆ pair_inner_product() [1/2]

template<typename Type >
auto theoretica::algebra::pair_inner_product ( const complex< Type > &  v_i,
const complex< Type > &  w_i 
)
inline

Compute the contribution of the inner product between a pair of elements of two vectors, automatically selecting whether to compute the conjugate or not.

Parameters
v_iThe first element of the pair
w_iThe second element of the pair, which will be conjugated if needed
Returns
The contribution of the element pair

◆ pair_inner_product() [2/2]

template<typename Type >
auto theoretica::algebra::pair_inner_product ( const Type &  v_i,
const Type &  w_i 
)
inline

Compute the contribution of the inner product between a pair of elements of two vectors, automatically selecting whether to compute the conjugate or not.

Parameters
v_iThe first element of the pair
w_iThe second element of the pair, which will be conjugated if needed
Returns
The contribution of the element pair

◆ rayleigh_quotient()

template<typename Matrix , typename Vector >
auto theoretica::algebra::rayleigh_quotient ( const Matrix &  A,
const Vector &  x 
)
inline

Compute the Rayleigh quotient \(\frac{x^T A x}{x^T x}\) of a vector with respect to a matrix.

This value is particularly useful in the context of eigensolvers.

Parameters
AThe matrix
xThe vector
Returns
The Rayleigh quotient of x with respect to A

◆ rotation_2d()

template<typename Matrix >
Matrix theoretica::algebra::rotation_2d ( real  theta,
unsigned int  rows = 0,
unsigned int  cols = 0 
)
inline

Returns a matrix representing a 2D rotation.

Parameters
thetaThe angle of rotation
Returns
The rotation matrix

◆ rotation_3d()

template<typename Matrix , typename Vector >
Matrix theoretica::algebra::rotation_3d ( real  theta,
const Vector &  axis,
unsigned int  rows = 0,
unsigned int  cols = 0 
)
inline

Returns a matrix representing a 3D rotation around a given axis.

Parameters
thetaAngle of rotation
axisAxis of rotation
Returns
A matrix representing the rotation of theta radians around the given axis.

◆ rotation_3d_xaxis()

template<typename Matrix >
Matrix theoretica::algebra::rotation_3d_xaxis ( real  theta,
unsigned int  rows = 0,
unsigned int  cols = 0 
)
inline

Returns a matrix representing a 3D rotation around the x axis.

Parameters
thetaAngle of rotation
Returns
A matrix representing the rotation of theta radians around the x axis.

◆ rotation_3d_yaxis()

template<typename Matrix >
Matrix theoretica::algebra::rotation_3d_yaxis ( real  theta,
unsigned int  rows = 0,
unsigned int  cols = 0 
)
inline

Returns a matrix representing a 3D rotation around the y axis.

Parameters
thetaAngle of rotation
Returns
A matrix representing the rotation of theta radians around the y axis.

◆ rotation_3d_zaxis()

template<typename Matrix >
Matrix theoretica::algebra::rotation_3d_zaxis ( real  theta,
unsigned int  rows = 0,
unsigned int  cols = 0 
)
inline

Returns a matrix representing a 3D rotation around the z axis.

Parameters
thetaAngle of rotation
Returns
A matrix representing the rotation of theta radians around the z axis.

◆ solve()

template<typename Matrix , typename Vector >
Vector theoretica::algebra::solve ( const Matrix &  A,
const Vector &  b 
)
inline

Solve the linear system \(A \vec x = \vec b\), finding \(\vec x\) using the best available algorithm.

Parameters
AThe matrix of the linear system
bThe known vector
Returns
The unknown vector \(\vec x\)

◆ solve_cholesky()

template<typename Matrix , typename Vector >
Vector theoretica::algebra::solve_cholesky ( const Matrix &  L,
const Vector &  b 
)
inline

Solve a linear system \(A \vec x = \vec b\) defined by a symmetric positive definite matrix, using the Cholesky decomposition \(L\) constructed so that \(A = LL^T\).

Parameters
LThe already computed Cholesky decomposition of the symmetric positive definite matrix describing the system.
bThe known vector
Returns
The unknown vector \(\vec x\)

◆ solve_lu() [1/2]

template<typename Matrix1 , typename Matrix2 , typename Vector >
Vector theoretica::algebra::solve_lu ( const Matrix1 &  L,
const Matrix2 &  U,
const Vector &  b 
)
inline

Use the LU decomposition of a matrix to solve its associated linear system, solving \(A \vec x = \vec b\) for \(\vec b\).

When solving the same linear system over and over, it is advantageous to compute its LU decomposition using decompose_lu and then use the decomposition to solve the system for different known vectors, reducing the overall computational cost.

Parameters
LThe lower triangular matrix
UThe upper triangular matrix
bThe known vector
Returns
The vector solution \(\vec x\).

◆ solve_lu() [2/2]

template<typename Matrix , typename Vector >
Vector theoretica::algebra::solve_lu ( Matrix  A,
Vector  b 
)
inline

Solve the linear system \(A \vec x = \vec b\), finding \(\vec x\).

In-place LU decomposition is used on A, followed by forward and backward elimination.

Parameters
AThe matrix of the linear system
bThe known vector
Returns
The unknown vector

◆ solve_lu_inplace()

template<typename Matrix , typename Vector >
Vector& theoretica::algebra::solve_lu_inplace ( const Matrix &  A,
Vector &  b 
)
inline

Solve the linear system \(A \vec x = \vec b\), finding \(\vec x\), where the matrix A has already undergone in-place LU decomposition.

Forward and backward elimination is used to solve the system in place. This routine is particularly efficient for solving linear systems multiple times with the same matrix but different vectors. The input vector is overwritten, as to not use any additional memory.

Parameters
AThe matrix of the linear system, after in-place LU decomposition
bThe known vector, to be overwritten with the solution
Returns
A reference to the overwritten vector solution

◆ solve_triangular()

template<typename Matrix , typename Vector >
Vector theoretica::algebra::solve_triangular ( const Matrix &  T,
const Vector &  b 
)
inline

Solve the linear system \(T \vec x = b\) for triangular \(T\).

The correct solver is selected depending on the elements of \(T\), if the property of the matrix is known a priori, calling the specific function is more efficient.

Parameters
TThe triangular matrix.
bThe known vector.

◆ solve_triangular_lower()

template<typename Matrix , typename Vector >
Vector theoretica::algebra::solve_triangular_lower ( const Matrix &  L,
const Vector &  b 
)
inline

Solve the linear system \(L \vec x = b\) for lower triangular \(L\).

Note
No check is performed on the triangularity of \(L\).
Parameters
LThe lower triangular matrix.
bThe known vector.

◆ solve_triangular_upper()

template<typename Matrix , typename Vector >
Vector theoretica::algebra::solve_triangular_upper ( const Matrix &  U,
const Vector &  b 
)
inline

Solve the linear system \(U \vec x = b\) for upper triangular \(U\).

Note
No check is performed on the triangularity of \(U\).
Parameters
UThe upper triangular matrix.
bThe known vector.

◆ sparsity()

template<typename Matrix , enable_matrix< Matrix > = true>
real theoretica::algebra::sparsity ( const Matrix &  A,
real  tolerance = 1E-12 
)
inline

Compute the sparsity of a matrix, counting the proportion of zero (smaller in module than the given tolerance) elements with respect to the total number of elements.

Parameters
AThe matrix to compute the sparsity of
toleranceThe minimum tolerance in absolute value to consider an element non-zero.
Returns
A real number between 0 and 1 representing the proportion of zero elements of the matrix.

◆ sphere_inversion()

template<typename Vector1 , typename Vector2 >
Vector1 theoretica::algebra::sphere_inversion ( const Vector1 &  p,
const Vector2 &  c = Vector2(0),
real  r = 1 
)
inline

Sphere inversion of a point with respect to a sphere of radius r centered at a point c.

Parameters
pThe vector to transform
cThe center of the sphere
rThe radius of the sphere

◆ sqr_norm()

template<typename Vector >
auto theoretica::algebra::sqr_norm ( const Vector &  v)
inline

Returns the square of the Euclidean/Hermitian norm of the given vector.

Parameters
vThe vector to compute the norm of
Returns
The norm of the given vector

◆ trace()

template<typename Matrix >
auto theoretica::algebra::trace ( const Matrix &  m)
inline

Compute the trace of the given matrix.

Parameters
mA matrix of any type
Returns
The trace of the matrix

◆ transform() [1/2]

template<typename Matrix , typename Vector >
Vector theoretica::algebra::transform ( const Matrix &  A,
const Vector &  v 
)
inline

Returns the matrix transformation of a vector.

Equivalent to the operation A * v

Parameters
AThe matrix transformation
vThe vector to transform
Returns
The transformed vector

◆ transform() [2/2]

template<typename Matrix , typename Vector1 , typename Vector2 >
Vector1& theoretica::algebra::transform ( Vector1 &  res,
const Matrix &  A,
const Vector2 &  v 
)
inline

Apply a matrix transformation to a vector and store the result in the vector.

Equivalent to the operation v = A * v

Parameters
resThe matrix to overwrite with the result
AThe matrix transformation
vThe vector to transform
Returns
A reference to the overwritten vector

◆ translation()

template<typename Matrix , typename Vector >
Matrix theoretica::algebra::translation ( const Vector &  v,
unsigned int  rows = 0,
unsigned int  cols = 0 
)
inline

Returns a translation matrix, that is, an NxN matrix which describes a translation in N-1 dimensions.

Parameters
vThe translation vector of dimension N-1
Returns
The translation matrix

◆ transpose() [1/2]

template<typename Matrix , typename MatrixT = Matrix>
MatrixT theoretica::algebra::transpose ( const Matrix &  m)
inline

Returns the transpose of the given matrix.

Equivalent to the operation m^T

Parameters
mThe matrix to transpose
Returns
The transposed matrix

◆ transpose() [2/2]

template<typename Matrix1 , typename Matrix2 >
Matrix1& theoretica::algebra::transpose ( Matrix1 &  dest,
const Matrix2 &  src 
)
inline

Compute the transpose matrix and write the result to another matrix.

Equivalent to the operation dest = src^T

Parameters
destThe matrix to overwrite
srcThe matrix to transpose
Returns
A reference to the overwritten matrix

◆ vec_copy()

template<typename Vector1 , typename Vector2 >
Vector1& theoretica::algebra::vec_copy ( Vector1 &  dest,
const Vector2 &  src 
)
inline

Copy a vector by overwriting another.

Equivalent to the operation dest = src

Parameters
destThe vector to overwrite
srcThe vector to copy
Returns
A reference to the overwritten matrix

◆ vec_diff() [1/2]

template<typename Vector1 , typename Vector2 , typename Vector3 >
Vector1& theoretica::algebra::vec_diff ( Vector1 &  res,
const Vector2 &  v1,
const Vector3 &  v2 
)
inline

Subtract two vectors and store the result in another vector Equivalent to the operation res = v1 - v2.

Parameters
v1The first vector
v2The second vector
Returns
A reference to the overwritten vector

◆ vec_diff() [2/2]

template<typename Vector1 , typename Vector2 >
Vector2& theoretica::algebra::vec_diff ( Vector1 &  v1,
const Vector2 &  v2 
)
inline

Subtract two vectors and store the result in the first vector.

Equivalent to the operation v1 = v1 - v2

Parameters
v1The first vector to store the result
v2The second vector
Returns
A reference to the overwritten vector

◆ vec_error()

template<typename Vector >
Vector& theoretica::algebra::vec_error ( Vector &  v)
inline

Overwrite the given vector with the error vector with NaN values.

This function is used to signal an error.

Parameters
vThe vector to overwrite
Returns
A reference to the overwritten vector

◆ vec_scalmul() [1/2]

template<typename Field , typename Vector >
Vector& theoretica::algebra::vec_scalmul ( Field  a,
Vector &  v 
)
inline

Multiply a vector by a scalar of any compatible type.

Parameters
aA scalar value
vThe vector to multiply
Returns
A reference to the multiplied vector

◆ vec_scalmul() [2/2]

template<typename Field , typename Vector1 , typename Vector2 >
Vector1& theoretica::algebra::vec_scalmul ( Vector1 &  dest,
Field  a,
const Vector2 &  src 
)
inline

Multiply a vector by a scalar of any compatible type which can be cast to the type of element of the output vector.

Parameters
destThe vector to overwrite with the result
aA scalar value
srcThe vector to multiply
Returns
A reference to the resulting vector

◆ vec_sum() [1/2]

template<typename Vector1 , typename Vector2 , typename Vector3 >
Vector1& theoretica::algebra::vec_sum ( Vector1 &  res,
const Vector2 &  v1,
const Vector3 &  v2 
)
inline

Sum two vectors and store the result in another vector Equivalent to the operation res = v1 + v2.

Parameters
v1The first vector to add
v2The second vector to add
Returns
A reference to the overwritten vector

◆ vec_sum() [2/2]

template<typename Vector1 , typename Vector2 >
Vector2& theoretica::algebra::vec_sum ( Vector1 &  v1,
const Vector2 &  v2 
)
inline

Sum two vectors and store the result in the first vector.

Equivalent to the operation v1 = v1 + v2

Parameters
v1The first vector to add and store the result
v2The second vector to add
Returns
A reference to the overwritten vector

◆ vec_zeroes()

template<typename Vector >
Vector& theoretica::algebra::vec_zeroes ( Vector &  v)
inline

Overwrite a vector with all zeroes.

Parameters
vThe vector to overwrite
Returns
A reference to the overwritten vector