Theoretica
A C++ numerical and automatic mathematical library
algebra.h File Reference

Linear algebra routines. More...

Go to the source code of this file.

Namespaces

 theoretica
 Main namespace of the library which contains all functions and objects.
 
 theoretica::algebra
 Linear algebra routines.
 

Functions

template<typename Matrix >
Matrix & theoretica::algebra::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 & theoretica::algebra::vec_error (Vector &v)
 Overwrite the given vector with the error vector with NaN values. More...
 
template<typename Matrix >
Matrix & theoretica::algebra::make_identity (Matrix &m)
 Overwrite a matrix with the identity matrix. More...
 
template<typename Matrix >
Matrix & theoretica::algebra::mat_zeroes (Matrix &m)
 Overwrite a matrix with all zeroes. More...
 
template<typename Vector >
Vector & theoretica::algebra::vec_zeroes (Vector &v)
 Overwrite a vector with all zeroes. More...
 
template<typename Matrix1 , typename Matrix2 >
Matrix1 & theoretica::algebra::mat_copy (Matrix1 &dest, const Matrix2 &src)
 Copy a matrix by overwriting another. More...
 
template<typename Vector1 , typename Vector2 >
Vector1 & theoretica::algebra::vec_copy (Vector1 &dest, const Vector2 &src)
 Copy a vector by overwriting another. More...
 
template<typename Matrix >
Matrix & theoretica::algebra::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 & theoretica::algebra::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 & theoretica::algebra::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 theoretica::algebra::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 theoretica::algebra::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 theoretica::algebra::sqr_norm (const Vector &v)
 Returns the square of the Euclidean/Hermitian norm of the given vector. More...
 
template<typename Vector >
auto theoretica::algebra::norm (const Vector &v)
 Returns the Euclidean/Hermitian norm of the given vector. More...
 
template<typename Vector >
Vector theoretica::algebra::normalize (const Vector &v)
 Returns the normalized vector. More...
 
template<typename Vector >
Vector & theoretica::algebra::make_normalized (Vector &v)
 Normalize a given vector overwriting it. More...
 
template<typename Vector1 , typename Vector2 >
auto theoretica::algebra::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 theoretica::algebra::cross (const Vector1 &v1, const Vector2 &v2)
 Compute the cross product between two tridimensional vectors. More...
 
template<typename Matrix , typename MatrixT = Matrix>
MatrixT theoretica::algebra::transpose (const Matrix &m)
 Returns the transpose of the given matrix. More...
 
template<typename Matrix >
Matrix & theoretica::algebra::make_transposed (Matrix &m)
 Transpose the given matrix. More...
 
template<typename Matrix1 , typename Matrix2 >
Matrix1 & theoretica::algebra::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 theoretica::algebra::hermitian (const Matrix &m)
 Returns the hermitian of the given matrix. More...
 
template<typename Matrix >
Matrix & theoretica::algebra::make_hermitian (Matrix &m)
 Compute the hermitian of a given matrix and overwrite it. More...
 
template<typename Matrix1 , typename Matrix2 >
Matrix1 & theoretica::algebra::hermitian (Matrix1 &dest, const Matrix2 &src)
 Hermitian (conjugate transpose) of a matrix. More...
 
template<typename Matrix >
auto theoretica::algebra::trace (const Matrix &m)
 Compute the trace of the given matrix. More...
 
template<typename Matrix >
auto theoretica::algebra::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 & theoretica::algebra::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 & theoretica::algebra::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 & theoretica::algebra::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 & theoretica::algebra::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 & theoretica::algebra::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 theoretica::algebra::transform (const Matrix &A, const Vector &v)
 Returns the matrix transformation of a vector. More...
 
template<typename Matrix , typename Vector1 , typename Vector2 >
Vector1 & theoretica::algebra::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 & theoretica::algebra::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 & theoretica::algebra::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 & theoretica::algebra::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 & theoretica::algebra::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 & theoretica::algebra::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 & theoretica::algebra::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 theoretica::algebra::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 & theoretica::algebra::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 theoretica::algebra::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 theoretica::algebra::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 theoretica::algebra::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 & theoretica::algebra::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 & theoretica::algebra::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 & theoretica::algebra::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 & theoretica::algebra::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 theoretica::algebra::is_square (const Matrix &m)
 Returns whether the matrix is square. More...
 
template<typename Matrix >
bool theoretica::algebra::is_diagonal (const Matrix &m, real tolerance=10 *MACH_EPSILON)
 Returns whether the matrix is diagonal. More...
 
template<typename Matrix >
bool theoretica::algebra::is_symmetric (const Matrix &m, real tolerance=ALGEBRA_ELEMENT_TOL)
 Returns whether the matrix is symmetric. More...
 
template<typename Matrix >
bool theoretica::algebra::is_lower_triangular (const Matrix &m, real tolerance=ALGEBRA_ELEMENT_TOL)
 Returns whether the matrix is lower triangular. More...
 
template<typename Matrix >
bool theoretica::algebra::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 theoretica::algebra::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 theoretica::algebra::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 theoretica::algebra::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 & theoretica::algebra::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 theoretica::algebra::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 & theoretica::algebra::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 theoretica::algebra::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 theoretica::algebra::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 theoretica::algebra::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 & theoretica::algebra::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 theoretica::algebra::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 theoretica::algebra::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 theoretica::algebra::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 theoretica::algebra::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 & theoretica::algebra::inverse (Matrix1 &dest, const Matrix2 &src)
 Invert the given matrix. More...
 
template<typename Matrix , typename MatrixInv = Matrix>
MatrixInv theoretica::algebra::inverse (const Matrix &m)
 Returns the inverse of the given matrix. More...
 
template<typename Matrix >
Matrix & theoretica::algebra::invert (Matrix &m)
 Invert the given matrix and overwrite it. More...
 
template<typename Matrix >
auto theoretica::algebra::det (const Matrix &A)
 Compute the determinant of a square matrix. More...
 
template<typename Matrix , typename Vector >
auto theoretica::algebra::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 theoretica::algebra::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 theoretica::algebra::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 theoretica::algebra::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 theoretica::algebra::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 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)
 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 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)
 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 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)
 Compute an eigenvalue of a square matrix and its corresponding eigenvector (eigenpair) using the Rayleigh quotient iteration method. More...
 

Detailed Description

Linear algebra routines.

This file implements all linear algebra routines of the library, using templates and type traits. The Matrix type must be a class with these methods:

  • operator()() Get the element at the i-th row and j-th column.
  • rows() Get the number of rows of the matrix (not defined for vectors)
  • cols() Get the number of columns of the matrix (not defined for vectors)
  • resize() Change or set the size of the matrix (may throw for statically allocated matrices) The Vector type must be a class with these methods:
  • operator[]() Get the element at index i by reference or const reference
  • size() Get the total number of elements of the vector
  • resize() Change or set the size of the vector (may throw for statically allocated vectors)