MatDecompose#
-
namespace lin#
Functions
-
template<typename T, std::size_t Rows, std::size_t Cols>
auto lu_crout(const Matrix<T, Rows, Cols> &A) -> tuple<Matrix<T, Rows, Cols>, Matrix<T, Rows, Cols>># Crout’s LU decomposition implementation. This function returns a tuple of two matrices L and U such that matmul(L,U) = A.
- Template Parameters:
T – The type of the matrix
Rows – The number of rows
Cols – The number of columns
- Parameters:
A – The matrix to be decomposed
- Returns:
tuple<Matrix<T, Rows, Cols>, Matrix<T, Rows, Cols>>
-
template<typename T, std::size_t Rows, std::size_t Cols, typename = typename std::enable_if_t<std::is_floating_point_v<T>>>
auto lu_gaussian(const Matrix<T, Rows, Cols> &A) -> tuple<Matrix<T, Rows, Cols>, Matrix<T, Rows, Cols>># This is a Gaussian elimination implementation of LU Decomposition. WARNING: The current implementation is brittle for integer types as it may result to segfaults due to division by zero (which is why a conditional flag is temporarily set to call this method when T is a floating type). Future efforts will be made to investigate further on how to handle this.
- Template Parameters:
T – The type of the matrix
Rows – The number of rows
Cols – The number of columns
std::enable_if_t<std::is_floating_point_v<T>> –
- Parameters:
A – The matrix to be decomposed
- Returns:
tuple<Matrix<T, Rows, Cols>, Matrix<T, Rows, Cols>>
-
template<typename T, std::size_t Rows, std::size_t Cols>
auto plu(Matrix<T, Rows, Cols> A) -> tuple<Matrix<T, Rows, Cols>, Matrix<T, Rows, Cols>, Matrix<T, Rows, Cols>># PLU decomposition is a variant of LU decomposition where the matrix A is decomposed into three matrices P, L, and U such that A = PLU. The current implementation needs to be refactored for imposing matrix properties. See: https://www.cfm.brown.edu/people/dobrush/cs52/Mathematica/Part2/PLU.html.
- Template Parameters:
T – The type of the matrix
Rows – The number of rows
Cols – The number of columns
- Parameters:
A – The matrix to be decomposed
- Returns:
tuple<Matrix<T, Rows, Cols>, Matrix<T, Rows, Cols>, Matrix<T, Rows, Cols>>
-
template<typename T, std::size_t Rows, std::size_t Cols>
auto ldl(const Matrix<T, Rows, Cols> &A) -> tuple<Matrix<T, Rows, Cols>, vector<T>>#
-
template<typename T, std::size_t Rows, std::size_t Cols>
constexpr auto cholesky(const Matrix<T, Rows, Cols> &A) -> Matrix<T, Rows, Cols># Cholesky decomposition implementation. This function returns a matrix L such that matmul(L, L^T) = A. Note that this function assumes that the matrix is symmetric positive definite. Future efforts will be added to check this condition before proceeding with the decomposition.
- Template Parameters:
T – The type of the matrix
Rows – The number of rows
Cols – The number of columns
- Parameters:
A – The matrix to be decomposed
- Returns:
Matrix<T, Rows, Cols>
-
template<typename T, std::size_t Rows, std::size_t Cols, typename = typename std::enable_if_t<(Rows * Cols > 4)>>
constexpr auto det(const Matrix<T, Rows, Cols> &M, std::function<tuple<Matrix<T, Rows, Cols>, Matrix<T, Rows, Cols>>(const Matrix<T, Rows, Cols>&)> LUMethod =[](const Matrix< T, Rows, Cols > &M) { return lu_gaussian(M);}
) -> double# This function returns the determinant of a matrix. This also keeps the LUMethod open to the client.
- Template Parameters:
T –
- Parameters:
M –
LUMethod –
- Returns:
double
-
template<typename T, std::size_t Rows, std::size_t Cols>
MatFunc#
Contains linalg functions for processing matrices.
- Author
ddamiana
- Version
0.1
- Date
2023-05-27
- Copyright
Copyright (c) 2023
Functions
-
template<typename T, std::size_t Rows, std::size_t Cols>
auto transpose(const Matrix<T, Rows, Cols> &A) -> Matrix<T, Cols, Rows># General transpose function that returns a new matrix.
- Template Parameters:
T – The type of the matrix
Rows – The number of rows
Cols – The number of columns
- Parameters:
A – Base matrix to be transposed
- Returns:
Matrix<T, Cols, Rows>
-
template<typename Type, std::size_t Rows, std::size_t Cols, typename = typename std::enable_if_t<Rows == Cols>>
auto T(Matrix<Type, Rows, Cols> &A) -> void# Inplace transpose function. Only works with square matrices.
- Template Parameters:
Type – type of the matrix
Rows – The number of rows
Cols – The number of columns
- Parameters:
A – matrix to be transposed
-
template<typename T, std::size_t Rows, std::size_t Cols>
auto triu(const Matrix<T, Rows, Cols> &A) -> Matrix<T, Rows, Cols># Returns a copy of upper triangular matrix.
- Template Parameters:
T –
- Parameters:
A –
- Returns:
Matrix<T>
-
template<typename T, std::size_t Rows, std::size_t Cols>
auto mask_triu(Matrix<T, Rows, Cols> &A) -> void# Masks the matrix’s upper triangular part.
- Template Parameters:
T – The type of the matrix
Rows – The number of rows
Cols – The number of columns
- Parameters:
A –
-
template<typename T, std::size_t Rows, std::size_t Cols>
auto tril(const Matrix<T, Rows, Cols> &A) -> Matrix<T, Rows, Cols># Returns the lower triangular part of the matrix.
- Template Parameters:
T – The type of the matrix
Rows – The number of rows
Cols – The number of columns
- Parameters:
A – The matrix to be modified
- Returns:
Matrix<T>
-
template<typename T, std::size_t Rows, std::size_t Cols>
auto mask_tril(Matrix<T, Rows, Cols> &A) -> void# Modifies the matrix to mask the lower triangular part.
- Template Parameters:
T – The type of the matrix
Rows – The number of rows
Cols – The number of columns
- Parameters:
A – The matrix to be modified
-
template<typename T, std::size_t Rows, std::size_t Cols>
auto diag(const Matrix<T, Rows, Cols> &A) -> Matrix<T, Rows, Cols># Returns a diagonal matrix.
- Template Parameters:
T – The type of the matrix
Rows – The number of rows
Cols – The number of columns
- Parameters:
A – Base matrix
- Returns:
Matrix<T, Rows, Cols>
-
template<typename T, std::size_t Rows, std::size_t Cols>
auto mask_diag(Matrix<T, Rows, Cols> &A) -> void# Modifies the matrix to mask the diagonal part.
- Template Parameters:
T – The type of the matrix
Rows – The number of rows
Cols – The number of columns
- Parameters:
A – Base matrix
-
template<typename T, std::size_t Rows, std::size_t Cols>
auto trace(const Matrix<T, Rows, Cols> &M) -> T# Returns the trace of the Matrix; this function requires a square matrix.
- Template Parameters:
T – The type of the matrix
- Parameters:
M – The matrix to be processed
- Returns:
T The trace of the matrix
-
template<typename T, std::size_t Rows, std::size_t Cols>
constexpr auto minor_submatrix(const Matrix<T, Rows, Cols> &M, std::size_t row = 0, std::size_t col = 0) -> Matrix<T, Rows - 1, Cols - 1># Returns a Minor submatrix of M. Note that Matrixindexing is still zero-based.
- Template Parameters:
T –
- Parameters:
M –
row –
col –
- Returns:
Matrix<T>
-
template<typename T, std::size_t Rows, std::size_t Cols>
constexpr auto minor(const Matrix<T, Rows, Cols> &M, std::size_t row = 0, std::size_t col = 0) -> double# Returns the minor determinant of matrix M.
- Template Parameters:
T –
- Parameters:
M –
row –
col –
- Returns:
double
-
template<typename T, std::size_t Rows, std::size_t Cols>
constexpr auto minor_matrix(const Matrix<T, Rows, Cols> &M) -> Matrix<double, Rows, Cols>#
-
template<typename T, std::size_t Rows, std::size_t Cols>
constexpr auto cofactor_submatrix(const Matrix<T, Rows, Cols> &M, std::size_t row = 0, std::size_t col = 0) -> Matrix<T, Rows - 1, Cols - 1># Returns a cofactor submatrix of $M$.
- Template Parameters:
T –
- Parameters:
M –
row –
col –
- Returns:
Matrix<T>
MatOps#
Contains operator overload for Matrix, Vector, and Scalar types. Note: These methods will soon be deprecated in favor of using Expression Templates.
- Author
ddamiana
- Version
0.1
- Date
2023-05-27
- Copyright
Copyright (c) 2023
-
namespace lin
Functions
-
template<typename T, std::size_t Rows, std::size_t Cols>
void swap_rows(Matrix<T, Rows, Cols> &t_mat, std::size_t row1, std::size_t row2)# Helper function to swap two rows in a matrix.
- Template Parameters:
T – The type of the matrix
Rows – The number of rows
Cols – The number of columns
- Parameters:
t_mat – The matrix
row1 – The row to be swapped
row2 – The row to be swapped
-
template<typename T, std::size_t Rows, std::size_t Cols>
void swap_cols(Matrix<T, Rows, Cols> &t_mat, std::size_t col1, std::size_t col2)# Helper function to swap two columns in a matrix.
- Template Parameters:
T – The type of the matrix
Rows – The number of rows
Cols – The number of columns
- Parameters:
t_mat – The matrix
col1 – The column to be swapped
col2 – The column to be swapped
-
template<typename T, typename U = T, std::size_t Rows, std::size_t Cols>
constexpr auto operator+(const Matrix<T, Rows, Cols> &lhs, const Matrix<U, Rows, Cols> &rhs) -> Matrix<common_type_t<T, U>, Rows, Cols>#
-
template<typename T, typename U = T, std::size_t Rows, std::size_t Cols>
constexpr auto operator-(const Matrix<T, Rows, Cols> &lhs, const Matrix<U, Rows, Cols> &rhs) -> Matrix<common_type_t<T, U>, Rows, Cols>#
-
template<typename T, typename U = T, std::size_t Rows, std::size_t Cols>
constexpr auto operator*(const Matrix<T, Rows, Cols> &lhs, const Matrix<U, Rows, Cols> &rhs) -> Matrix<common_type_t<T, U>, Rows, Cols>#
-
template<typename T, typename U = T, std::size_t Rows, std::size_t Cols>
constexpr auto operator/(const Matrix<T, Rows, Cols> &lhs, const Matrix<U, Rows, Cols> &rhs) -> Matrix<common_type_t<T, U, double>, Rows, Cols># Returns the division of two matrices. Will likely return a matrix of doubles for arithmetic types. NOTE: Implementation will be modified in the future.
- Template Parameters:
T – The type of the matrix
U – The type of the matrix
Rows – The number of rows
Cols – The number of columns
- Parameters:
lhs – The left hand side matrix
rhs – The right hand side matrix
- Returns:
Matrix<common_type_t<T, U, double>, Rows, Cols> The division of the two matrices.
-
template<typename T, std::size_t Rows, std::size_t Cols>
constexpr auto operator+=(Matrix<T, Rows, Cols> &lhs, const Matrix<T, Rows, Cols> &rhs) -> Matrix<T, Rows, Cols>&#
-
template<typename T, std::size_t Rows, std::size_t Cols>
constexpr auto operator-=(Matrix<T, Rows, Cols> &lhs, const Matrix<T, Rows, Cols> &rhs) -> Matrix<T, Rows, Cols>&#
-
template<typename T, std::size_t Rows, std::size_t Cols>
constexpr auto operator*=(Matrix<T, Rows, Cols> &lhs, const Matrix<T, Rows, Cols> &rhs) -> Matrix<T, Rows, Cols>&#
-
template<typename T, std::size_t Rows, std::size_t Cols>
constexpr auto operator/=(Matrix<T, Rows, Cols> &lhs, const Matrix<T, Rows, Cols> &rhs) -> Matrix<T, Rows, Cols>&#
-
template<typename T, std::size_t Rows, std::size_t Cols>
constexpr auto operator+(const Matrix<T, Rows, Cols> &lhs, const vector<T> &rhs) -> Matrix<T, Rows, Cols># This implementation is inspired from Numpy’s broadcasting feature and is not a standard linear algebra operation. See: https://numpy.org/doc/stable/user/basics.broadcasting.html.
- Template Parameters:
T – The type of the matrix
Rows – The number of rows
Cols – The number of columns
- Parameters:
lhs – The left hand side matrix
rhs – The right hand side scalar
- Returns:
Matrix<T, Rows, Cols>
-
template<typename T, std::size_t Rows, std::size_t Cols>
constexpr auto operator-(const Matrix<T, Rows, Cols> &lhs, const vector<T> &rhs) -> Matrix<T, Rows, Cols># This implementation is inspired from Numpy’s broadcasting feature and is not a standard linear algebra operation. See: https://numpy.org/doc/stable/user/basics.broadcasting.html.
- Template Parameters:
T – The type of the matrix
Rows – The number of rows
Cols – The number of columns
- Parameters:
lhs – The left hand side matrix
rhs – The right hand side scalar
- Returns:
Matrix<T, Rows, Cols>
-
template<typename T, std::size_t Rows, std::size_t Cols>
constexpr auto operator*(const Matrix<T, Rows, Cols> &lhs, const vector<T> &rhs) -> vector<T>#
-
template<typename T, std::size_t Rows, std::size_t Cols>
constexpr auto operator+=(Matrix<T, Rows, Cols> &t_mat, const vector<T> &t_vec) -> Matrix<T, Rows, Cols>&# This implementation is inspired from Numpy’s broadcasting feature and is not a standard linear algebra operation. This is modified for inplace operations. See: https://numpy.org/doc/stable/user/basics.broadcasting.html.
- Template Parameters:
T – The type of the matrix
Rows – The number of rows
Cols – The number of columns
- Parameters:
t_mat – The left hand side matrix
t_vec – The right hand side scalar
- Returns:
Matrix<T, Rows, Cols>& The division of the two matrices.
-
template<typename T, std::size_t Rows, std::size_t Cols>
constexpr auto operator-=(Matrix<T, Rows, Cols> &t_mat, const vector<T> &t_vec) -> Matrix<T, Rows, Cols>&# This implementation is inspired from Numpy’s broadcasting feature and is not a standard linear algebra operation. This is modified for inplace operations. See: https://numpy.org/doc/stable/user/basics.broadcasting.html.
- Template Parameters:
T – The type of the matrix
Rows – The number of rows
Cols – The number of columns
- Parameters:
t_mat – The left hand side matrix
t_vec – The right hand side scalar
- Returns:
Matrix<T, Rows, Cols>& The division of the two matrices.
-
template<typename T, std::size_t Rows, std::size_t Cols>
constexpr auto operator+(const Matrix<T, Rows, Cols> &t_mat, T t_scalar) -> Matrix<T, Rows, Cols>#
-
template<typename T, std::size_t Rows, std::size_t Cols>
constexpr auto operator-(const Matrix<T, Rows, Cols> &t_mat, T t_scalar) -> Matrix<T, Rows, Cols>#
-
template<typename T, std::size_t Rows, std::size_t Cols>
constexpr auto operator*(const Matrix<T, Rows, Cols> &t_mat, T t_scalar) -> Matrix<T, Rows, Cols>#
-
template<typename T, std::size_t Rows, std::size_t Cols>
constexpr auto operator/(const Matrix<T, Rows, Cols> &t_mat, T t_scalar) -> Matrix<T, Rows, Cols>#
-
template<typename T, std::size_t Rows, std::size_t Cols>
constexpr auto operator+=(Matrix<T, Rows, Cols> &t_mat, T t_scalar) -> Matrix<T, Rows, Cols>&#
-
template<typename T, std::size_t Rows, std::size_t Cols>
constexpr auto operator-=(Matrix<T, Rows, Cols> &t_mat, T t_scalar) -> Matrix<T, Rows, Cols>&#
-
template<typename T, std::size_t Rows, std::size_t Cols>
constexpr auto operator*=(Matrix<T, Rows, Cols> &t_mat, T t_scalar) -> Matrix<T, Rows, Cols>&#
-
template<typename T, std::size_t Rows, std::size_t Cols>
constexpr auto operator/=(Matrix<T, Rows, Cols> &t_mat, T t_scalar) -> Matrix<T, Rows, Cols>&#
-
template<typename T, std::size_t Rows, std::size_t Cols>
MatPred#
-
namespace lin
Functions
-
template<typename T, std::size_t Rows, std::size_t Cols>
constexpr auto is_tril(const Matrix<T, Rows, Cols> &M) noexcept -> bool#
-
template<typename T, std::size_t Rows, std::size_t Cols>
constexpr auto is_triu(const Matrix<T, Rows, Cols> &M) noexcept -> bool#
-
template<typename T, std::size_t Rows, std::size_t Cols>
constexpr auto is_diag(const Matrix<T, Rows, Cols> &M) noexcept -> bool#
-
template<typename T, std::size_t Rows, std::size_t Cols>
constexpr auto is_antidiag(const Matrix<T, Rows, Cols> &M) noexcept -> bool#
-
template<typename T, std::size_t Rows, std::size_t Cols>
constexpr auto is_square(const Matrix<T, Rows, Cols> &M) noexcept -> bool#
-
template<typename T, std::size_t Rows, std::size_t Cols>
constexpr auto is_invertible(const Matrix<T, Rows, Cols> &M) noexcept -> bool#
-
template<typename T, std::size_t Rows, std::size_t Cols>
constexpr auto is_echelon(const Matrix<T, Rows, Cols> &M) noexcept -> bool# Checks if a matrix is in echelon form. Row Echelon form implies that a matrix has the following properties:
The leading first entry in each row must be 1.
The leading entry on each subsequent row must be a new column to the right
All rows where all entries are zero below, rows were not all entires are 0.
- Template Parameters:
T –
- Parameters:
M –
- Returns:
true
- Returns:
false
-
template<typename T, std::size_t Rows, std::size_t Cols>
Matrix#
-
namespace lin
Functions
-
template<typename T, std::size_t Rows, std::size_t Cols>
constexpr auto zero_mat() -> Matrix<T, Rows, Cols># Constructor for zero matrix which pertains to T{} constructor for compound objects.
- Template Parameters:
T – The type of the matrix
Rows – The number of rows
Cols – The number of columns
- Returns:
Matrix<T, Rows, Cols>
-
template<typename T, std::size_t N, typename = typename std::enable_if_t<std::is_arithmetic_v<T>>>
constexpr auto id() -> Matrix<T, N, N># Constructor for identity matrix.
- Template Parameters:
T –
- Parameters:
n –
- Returns:
Matrix<T>
-
template<typename T, std::size_t Rows, std::size_t Cols, typename = typename std::enable_if_t<std::is_arithmetic_v<T>>>
constexpr auto scalar_id(T t_value) -> Matrix<T, Rows, Cols>#
-
template<typename T, std::size_t Rows, std::size_t Cols>
class Matrix# Public Types
Public Functions
-
inline Matrix()#
-
inline constexpr auto cbegin() const noexcept -> const_iterator#
-
inline constexpr auto cend() const noexcept -> const_iterator#
-
inline auto rows() const noexcept -> std::size_t#
-
inline auto cols() const noexcept -> std::size_t#
-
inline auto dims() const noexcept -> std::tuple<std::size_t, std::size_t>#
-
inline auto empty() const noexcept -> bool#
-
inline Matrix()#
-
template<typename T, std::size_t Rows, std::size_t Cols>
Utils#
Contains utility functions.
- Author
ddamiana
- Version
0.1
- Date
2023-05-27
- Copyright
Copyright (c) 2023
VecFunc#
Contains linalg functions for processing vectors.
- Author
ddamiana
- Version
0.1
- Date
2023-05-27
- Copyright
Copyright (c) 2023
-
namespace lin
Functions
-
template<typename T, typename = typename std::enable_if_t<std::is_arithmetic_v<T>>>
constexpr auto ones(size_t t_size) -> vector<T>#
-
template<typename T, typename = typename std::enable_if_t<std::is_arithmetic_v<T>>>
constexpr auto zeros(size_t t_size) -> vector<T>#
-
template<typename T, typename = typename std::enable_if_t<std::is_arithmetic_v<T>>>
constexpr auto lp_norm(const vector<T> &v, float p) -> float#
-
template<typename T, typename = typename std::enable_if_t<std::is_arithmetic_v<T>>>
constexpr auto prod(const std::vector<T> &v) -> T#
-
template<typename T>
constexpr auto get_angle(const vector<T> &lhs, const vector<T> &rhs) -> T# Get the angle between two vectors. Both vectors must be of the same size.
- Template Parameters:
T –
- Parameters:
lhs –
rhs –
- Returns:
T
-
template<typename T, typename = typename std::enable_if_t<std::is_arithmetic_v<T>>>
auto normalize(const vector<T> &v)# Returns the normalized vector.
- Template Parameters:
T – type of the vector
std::enable_if_t<std::is_arithmetic_v<T>> –
- Parameters:
v – vector to normalize
- Returns:
auto
-
template<typename T, typename U = T>
auto cross(const vector<T> &lhs, const vector<U> &rhs) -> vector<common_type_t<T, U>># Implementation of the cross product. Note: current implementation only works with 3d vectors.
- Template Parameters:
T – The type of the vector
U – The type of the vector
- Parameters:
lhs – The left hand side vector
rhs – The right hand side vector
- Returns:
vector<common_type_t<T, U>> The cross product of the two vectors
-
template<typename T, typename = typename std::enable_if_t<std::is_arithmetic_v<T>>>
VecOps#
Contains operator overload for Vector and Scalar types.
- Author
ddamiana
- Version
0.1
- Date
2023-05-27
- Copyright
Copyright (c) 2023
-
namespace lin
Functions
-
template<typename T, typename U = T>
constexpr auto operator+(const vector<T> &lhs, const vector<U> &rhs) -> vector<common_type_t<T, U>>#
-
template<typename T, typename U = T>
constexpr auto operator-(const vector<T> &lhs, const vector<U> &rhs) -> vector<common_type_t<T, U>>#
-
template<typename T, typename U = T>
constexpr auto operator*(const vector<T> &lhs, const vector<U> &rhs) -> vector<common_type_t<T, U>>#
-
template<typename T, typename U = T>
constexpr auto operator/(const vector<T> &lhs, const vector<U> &rhs) -> vector<common_type_t<T, U>>#
-
template<typename T, typename U = T>
constexpr auto operator+(const vector<T> &lhs, U rhs) -> vector<common_type_t<T, U>>#
-
template<typename T, typename U = T>
constexpr auto operator-(const vector<T> &lhs, U rhs) -> vector<common_type_t<T, U>>#
-
template<typename T, typename U = T>
constexpr auto operator*(const vector<T> &lhs, U rhs) -> vector<common_type_t<T, U>>#
-
template<typename T, typename U = T>