Theoretica
A C++ numerical and automatic mathematical library
|
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... | |
Linear algebra routines.
|
inline |
Apply a matrix transformation to a vector and store the result in the vector.
Equivalent to the operation v = A * v
A | The matrix transformation |
v | The vector to transform |
|
inline |
Compute the Canberra distance between two vectors.
v1 | The first vector |
v2 | The second vector |
|
inline |
Compute the Chebyshev distance between two vectors: \(d(\vec v_1, \vec v_2) = L_{\infty}(\vec v_1 - \vec v_2)\).
v1 | The first vector |
v2 | The second vector |
|
inline |
Compute the cosine distance between two vectors.
v1 | The first vector |
v2 | The second vector |
|
inline |
Compute the cross product between two tridimensional vectors.
v1 | The first tridimensional vector |
w | The second tridimensional vector |
|
inline |
Decompose a symmetric positive definite matrix into a triangular matrix so that \(A = L L^T\) using Cholesky decomposition.
A | The matrix to decompose |
|
inline |
Decompose a symmetric positive definite matrix in-place, overwriting the starting matrix, without using additional space.
A | The symmetric, positive definite matrix to decompose and overwrite with the lower triangular matrix. |
|
inline |
Decompose a square matrix to two triangular matrices, L and U where L is lower and U is upper, so that \(A = LU\).
A | The matrix to decompose |
L | The matrix to overwrite with the lower triangular one |
U | The matrix to overwrite with the upper triangular one |
|
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.
A | The matrix to decompose and overwrite |
|
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.
A | The matrix to compute the density of |
tolerance | The minimum tolerance in absolute value to consider an element non-zero. |
|
inline |
Compute the determinant of a square matrix.
In-place LU decomposition is used to reduce the matrix to triangular form.
A | The matrix to compute the determinant of |
|
inline |
Returns the a matrix with the given diagonal.
The function without any parameters is used for statically allocated matrix types.
v | The vector of the diagonal elements |
rows | The number of rows of the matrix |
cols | The number of columns of the matrix |
|
inline |
Construct a matrix with the given vector as diagonal and zeroes everywhere else, overwriting the given matrix.
The | vector of diagonal elements |
res | The matrix to overwrite |
|
inline |
Compute the product of the elements of the main diagonal of a generic matrix.
m | The input matrix |
|
inline |
Compute the discrete distance between two vectors.
v1 | The first vector |
v2 | The second vector |
tolerance | The minimum absolute difference between elements to consider them different (defaults to MACH_EPSILON). |
|
inline |
Compute the distance between two complex numbers: \(d(z_1, z_2) = |z_1 - z_2|\).
z1 | The first complex value |
z2 | The second complex value |
|
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)^*\).
v1 | The first vector |
v2 | The second vector |
|
inline |
Compute the Euclidean distance between two vectors: \(d(\vec v_1, \vec v_2) = L_2(\vec v_1 - \vec v_2)\).
v1 | The first vector |
v2 | The second vector |
Compute the Euclidian distance between two values: \(d(a, b) = |a - b|\).
a | The first real value |
b | The second real value |
|
inline |
Computes the dot product between two vectors, using the Hermitian form if needed.
v | The first vector |
w | The second vector |
|
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.
A | The matrix to find the smallest eigenvalue of |
x | The starting vector (a random vector is a good choice) |
v | The vector to overwrite with the eigenvector |
tolerance | The minimum difference in norm between subsequent steps to stop the algorithm at (defaults to ALGEBRA_EIGEN_TOL). |
max_iter | The maximum number of iterations to use (defaults to 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).
A | The matrix to find the biggest eigenvalue of |
x | The starting vector (a random vector is a good choice) |
v | The vector to overwrite with the eigenvector |
tolerance | The minimum difference in norm between subsequent steps to stop the algorithm at (defaults to ALGEBRA_EIGEN_TOL). |
max_iter | The maximum number of iterations to use (defaults to ALGEBRA_EIGEN_ITER). |
|
inline |
Compute an eigenvalue of a square matrix and its corresponding eigenvector (eigenpair) using the Rayleigh quotient iteration method.
A | The matrix to compute the eigenvalue of |
lambda | The starting value for the Rayleigh quotient |
x | The starting approximation for the eigenvector (a random vector is a good choice). |
v | The vector to overwrite with the eigenvector |
tolerance | The minimum difference in norm between subsequent steps to stop the algorithm at (defaults to ALGEBRA_EIGEN_TOL). |
max_iter | The maximum number of iterations to use (defaults to 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.
A | The matrix to find the smallest eigenvalue of |
x | The starting vector (a random vector is a good choice) |
tolerance | The minimum difference in norm between subsequent steps to stop the algorithm at (defaults to ALGEBRA_EIGEN_TOL). |
max_iter | The maximum number of iterations to use (defaults to ALGEBRA_EIGEN_ITER). |
|
inline |
Find the biggest eigenvalue in module \(|\lambda_i|\) of a square matrix using the power method (Von Mises iteration).
A | The matrix to find the biggest eigenvalue of |
x | The starting vector (a random vector is a good choice) |
tolerance | The minimum difference in norm between subsequent steps to stop the algorithm at (defaults to ALGEBRA_EIGEN_TOL). |
max_iter | The maximum number of iterations to use (defaults to ALGEBRA_EIGEN_ITER). |
|
inline |
Compute an eigenvalue of a square matrix using the Rayleigh quotient iteration method.
A | The matrix to compute the eigenvalue of |
lambda | The starting value for the Rayleigh quotient |
x | The starting approximation for the eigenvector (a random vector is a good choice). |
tolerance | The minimum difference in norm between subsequent steps to stop the algorithm at (defaults to ALGEBRA_EIGEN_TOL). |
max_iter | The maximum number of iterations to use (defaults to ALGEBRA_EIGEN_ITER). |
|
inline |
Find the eigenvector associated with a given approximated eigenvalue using the inverse power method.
A | The matrix to find the eigenvector of |
lambda | The eigenvalue |
x | The starting vector (a random vector is a good choice) |
tolerance | The minimum difference in norm between subsequent steps to stop the algorithm at (defaults to ALGEBRA_EIGEN_TOL). |
max_iter | The maximum number of iterations to use (defaults to ALGEBRA_EIGEN_ITER). |
|
inline |
Distances.
Compute the Euclidean distance between two vectors: \(d(\vec v_1, \vec v_2) = L_2(\vec v_1 - \vec v_2)\)
v1 | The first vector |
v2 | The second vector |
Compute the Euclidian distance between two real values: \(d(a, b) = |a - b|\).
a | The first real value |
b | The second real value |
|
inline |
Compute the Hamming distance between two vectors.
v1 | The first vector |
v2 | The second vector |
tolerance | The minimum absolute difference between elements to consider them different (defaults to MACH_EPSILON). |
|
inline |
Returns the hermitian of the given matrix.
Equivalent to the operation m^T
m | The matrix to compute the hermitian of |
|
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.
dest | The matrix to overwrite |
src | The matrix to compute the hermitian of |
|
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)^*\).
v1 | The first vector |
v2 | The second vector |
|
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).
rows | The number of rows (and columns) of the resulting matrix |
|
inline |
Returns the identity matrix.
Size parameters are used only for dynamically allocated matrix types.
|
inline |
Returns the inverse of the given matrix.
Equivalent to the operation \(m^-1\)
m | The matrix to invert |
|
inline |
Invert the given matrix.
Equivalent to the operation dest = src^-1
dest | The matrix to overwrite |
src | The matrix to invert |
|
inline |
Invert the given matrix and overwrite it.
Equivalent to the operation m = m^-1
m | The matrix to invert |
|
inline |
Returns whether the matrix is diagonal.
m | The matrix to consider |
tolerance | The tolerance to allow for in the comparison, defaults to 10 * MACH_EPSILON |
|
inline |
Returns whether the matrix is lower triangular.
m | The matrix to consider |
tolerance | The tolerance to allow for in the comparison, defaults to ALGEBRA_ELEMENT_TOL |
|
inline |
Returns whether the matrix is square.
m | The matrix to consider |
|
inline |
Returns whether the matrix is symmetric.
m | The matrix to consider |
tolerance | The tolerance to allow for in the comparison, defaults to ALGEBRA_ELEMENT_TOL |
|
inline |
Returns whether the matrix is upper triangular.
m | The matrix to consider |
tolerance | The tolerance to allow for in the comparison, defaults to ALGEBRA_ELEMENT_TOL |
|
inline |
Compute the L1 norm of a vector: \(L_1(\vec v) = \Sigma_i \ |v_i|\).
v | The vector to compute the norm of |
|
inline |
Compute the L2 norm of a vector: \(L_2(\vec v) = \sqrt{\Sigma_i \ v_i^2}\).
v | The vector to compute the norm of |
|
inline |
Compute the Linf norm of a vector: \(L_{\infty}(\vec v) = max(|v_i|)\).
v | The vector to compute the norm of |
|
inline |
Norms.
Compute the Lp norm of a vector: \(L_p(\vec v) = (\Sigma_i \ |v_i|^p)^{1/p}\)
v | The vector to compute the norm of |
p | The power of the Lp norm |
|
inline |
Compute the hermitian of a given matrix and overwrite it.
Equivalent to the operation m = m^H
m | The matrix to compute the hermitian of |
|
inline |
Overwrite a matrix with the identity matrix.
m | The matrix to overwrite |
|
inline |
Normalize a given vector overwriting it.
v | The vector to normalize |
|
inline |
Transpose the given matrix.
m | The matrix to transpose |
|
inline |
Compute the Manhattan distance between two vectors: \(d(\vec v_1, \vec v_2) = L_1(\vec v_1 - \vec v_2)\).
v1 | The first vector |
v2 | The second vector |
|
inline |
Copy a matrix by overwriting another.
dest | The matrix to overwrite |
src | The matrix to copy |
|
inline |
Subtract two matrices and store the result in the first matrix.
Equivalent to the operation A = A - B
A | The first matrix to store the result |
B | The second matrix |
|
inline |
Subtract two matrices and store the result in another matrix Equivalent to the operation res = A - B.
A | The first matrix |
B | The second matrix |
|
inline |
Checks whether two matrices are equal.
A | The first matrix |
B | The second matrix |
tolerance | The tolerance to allow for in the comparison, defaults to 10 * MACH_EPSILON |
|
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.
m | The matrix to overwrite |
|
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
A | The first matrix to combine and store the result |
B | The second matrix to combine |
|
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
res | The matrix to overwrite with the result |
alpha | The first scalar parameter |
A | The first matrix to combine |
beta | The second scalar parameter |
B | The second matrix to combine |
|
inline |
Multiply two matrices and store the result in the first matrix, equivalent to the operation \(R = A B\).
A | The first matrix to multiply and store the result |
B | The second matrix to multiply |
|
inline |
Multiply two matrices and store the result in another matrix, equivalent to the operation \(R = A B\).
R | The matrix to overwrite with the result |
A | The first matrix to multiply |
B | The second matrix to multiply |
|
inline |
Multiply a matrix by the transpose of another matrix, equivalent to the operation \(R = A B^T\).
A | The first matrix to multiply |
B | The second matrix to transpose and multiply by |
|
inline |
Multiply a matrix by a scalar of any compatible type.
a | A scalar value |
m | The matrix to multiply |
|
inline |
Multiply a matrix by a scalar of any compatible type which can be cast to the type of element of the output matrix.
dest | The matrix to overwrite with the result |
a | A scalar value |
src | The matrix to multiply |
|
inline |
Shift the diagonal of a matrix by the given amount, overwriting the matrix itself, as \((A + \sigma I)\).
A | The matrix to shift the diagonal of |
sigma | The amount to shift |
|
inline |
Sum two matrices and store the result in the first matrix.
Equivalent to the operation A = A + B
A | The first matrix to add and store the result |
B | The second matrix to add |
|
inline |
Sum two matrices and store the result in another matrix Equivalent to the operation res = A + B.
A | The first matrix to add |
B | The second matrix to add |
|
inline |
Swap two columns of a matrix, given the matrix and the two indices of the columns.
A | The matrix to swap the columns of |
col1 | The index of the first column to swap |
col2 | The index of the other column to swap |
|
inline |
Swap two rows of a matrix, given the matrix and the two indices of the rows.
A | The matrix to swap the rows of |
row1 | The index of the first row to swap |
row2 | The index of the other row to swap |
|
inline |
Multiply the transpose of a matrix by another matrix, equivalent to the operation \(R = A^T B\).
A | The matrix to transpose and then multiply |
B | The second matrix to multiply by |
|
inline |
Overwrite a matrix with all zeroes.
m | The matrix to overwrite |
|
inline |
Compute the Minkowski distance between two vectors: \(d(\vec v_1, \vec v_2) = L_p(\vec v_1 - \vec v_2)\).
v1 | The first vector |
v2 | The second vector |
p | The power of the distance |
Compute the Minkowski distance between two values: \(d(a, b) = L_p(a - b)\).
a | The first real value |
b | The second real value |
p | The power of the distance |
|
inline |
Returns the Euclidean/Hermitian norm of the given vector.
v | The vector to compute the norm of |
|
inline |
Returns the normalized vector.
v | The vector to normalize |
|
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.
v_i | The first element of the pair |
w_i | The second element of the pair, which will be conjugated if needed |
|
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.
v_i | The first element of the pair |
w_i | The second element of the pair, which will be conjugated if needed |
|
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.
A | The matrix |
x | The vector |
|
inline |
Returns a matrix representing a 2D rotation.
theta | The angle of rotation |
|
inline |
Returns a matrix representing a 3D rotation around a given axis.
theta | Angle of rotation |
axis | Axis of rotation |
|
inline |
Returns a matrix representing a 3D rotation around the x axis.
theta | Angle of rotation |
|
inline |
Returns a matrix representing a 3D rotation around the y axis.
theta | Angle of rotation |
|
inline |
Returns a matrix representing a 3D rotation around the z axis.
theta | Angle of rotation |
|
inline |
Solve the linear system \(A \vec x = \vec b\), finding \(\vec x\) using the best available algorithm.
A | The matrix of the linear system |
b | The known vector |
|
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\).
L | The already computed Cholesky decomposition of the symmetric positive definite matrix describing the system. |
b | The known vector |
|
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.
L | The lower triangular matrix |
U | The upper triangular matrix |
b | The known vector |
|
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.
A | The matrix of the linear system |
b | The known vector |
|
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.
A | The matrix of the linear system, after in-place LU decomposition |
b | The known vector, to be overwritten with the solution |
|
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.
T | The triangular matrix. |
b | The known vector. |
|
inline |
Solve the linear system \(L \vec x = b\) for lower triangular \(L\).
L | The lower triangular matrix. |
b | The known vector. |
|
inline |
Solve the linear system \(U \vec x = b\) for upper triangular \(U\).
U | The upper triangular matrix. |
b | The known vector. |
|
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.
A | The matrix to compute the sparsity of |
tolerance | The minimum tolerance in absolute value to consider an element non-zero. |
|
inline |
Sphere inversion of a point with respect to a sphere of radius r centered at a point c.
p | The vector to transform |
c | The center of the sphere |
r | The radius of the sphere |
|
inline |
Returns the square of the Euclidean/Hermitian norm of the given vector.
v | The vector to compute the norm of |
|
inline |
Compute the trace of the given matrix.
m | A matrix of any type |
|
inline |
Returns the matrix transformation of a vector.
Equivalent to the operation A * v
A | The matrix transformation |
v | The vector to transform |
|
inline |
Apply a matrix transformation to a vector and store the result in the vector.
Equivalent to the operation v = A * v
res | The matrix to overwrite with the result |
A | The matrix transformation |
v | The vector to transform |
|
inline |
Returns a translation matrix, that is, an NxN matrix which describes a translation in N-1 dimensions.
v | The translation vector of dimension N-1 |
|
inline |
Returns the transpose of the given matrix.
Equivalent to the operation m^T
m | The matrix to transpose |
|
inline |
Compute the transpose matrix and write the result to another matrix.
Equivalent to the operation dest = src^T
dest | The matrix to overwrite |
src | The matrix to transpose |
|
inline |
Copy a vector by overwriting another.
Equivalent to the operation dest = src
dest | The vector to overwrite |
src | The vector to copy |
|
inline |
Subtract two vectors and store the result in another vector Equivalent to the operation res = v1 - v2.
v1 | The first vector |
v2 | The second vector |
|
inline |
Subtract two vectors and store the result in the first vector.
Equivalent to the operation v1 = v1 - v2
v1 | The first vector to store the result |
v2 | The second vector |
|
inline |
Overwrite the given vector with the error vector with NaN values.
This function is used to signal an error.
v | The vector to overwrite |
|
inline |
Multiply a vector by a scalar of any compatible type.
a | A scalar value |
v | The vector to multiply |
|
inline |
Multiply a vector by a scalar of any compatible type which can be cast to the type of element of the output vector.
dest | The vector to overwrite with the result |
a | A scalar value |
src | The vector to multiply |
|
inline |
Sum two vectors and store the result in another vector Equivalent to the operation res = v1 + v2.
v1 | The first vector to add |
v2 | The second vector to add |
|
inline |
Sum two vectors and store the result in the first vector.
Equivalent to the operation v1 = v1 + v2
v1 | The first vector to add and store the result |
v2 | The second vector to add |
|
inline |
Overwrite a vector with all zeroes.
v | The vector to overwrite |