|
sokobo
|
A templated matrix class supporting various linear algebra operations. More...
#include <matrix.h>
Public Member Functions | |
| Matrix () | |
| Default constructor creating an empty 0×0 matrix. | |
| Matrix (int r, int c) | |
| Constructs a matrix with specified dimensions, initialized with default values. | |
| Matrix (const std::vector< std::vector< T > > &mat) | |
| Constructs a matrix from a 2D vector. | |
| T & | operator() (int row, int column) |
| Accesses matrix element at specified position (non-const). | |
| const T & | operator() (int row, int column) const |
| Accesses matrix element at specified position (const). | |
| std::vector< T > & | operator[] (int i) |
| Accesses a row of the matrix (non-const). | |
| const std::vector< T > & | operator[] (int i) const |
| Accesses a row of the matrix (const). | |
| Matrix< T > | operator+ (const Matrix< T > &other) const |
| Matrix addition. | |
| Matrix< T > | operator- (const Matrix< T > &other) const |
| Matrix subtraction. | |
| Matrix< T > | operator* (const Matrix< T > &other) const |
| Matrix multiplication. | |
| Matrix< T > | operator* (const T &scalar) const |
| Scalar multiplication. | |
| Matrix< T > | operator/= (const Matrix< T > &other) const |
| Element-wise division. | |
| Matrix< T > | operator-= (const Matrix< T > &other) const |
| In-place subtraction. | |
| T | determinant () const |
| Computes the determinant of the matrix. | |
| Matrix< T > | inverse () const |
| Computes the inverse of the matrix. | |
| Matrix< T > | transpose () const |
| Computes the transpose of the matrix. | |
| std::vector< T > | eigenvalues () const |
| Computes the eigenvalues of the matrix. | |
| Matrix< T > | eigenvectors () const |
| Computes the eigenvectors of the matrix. | |
| Matrix< T > | adjoint () const |
| Computes the adjoint (adjugate) matrix. | |
| Matrix< T > | submatrix (int row_start, int row_end, int col_start, int col_end) const |
| Extracts a submatrix from specified row and column ranges. | |
| T | rank () const |
| Computes the rank of the matrix. | |
| bool | isSymmetric () const |
| Checks if the matrix is symmetric. | |
| std::pair< Matrix< T >, Matrix< T > > | LUDecomposition () const |
| Performs LU decomposition of the matrix. | |
| std::pair< Matrix< T >, Matrix< T > > | QRDecomposition () const |
| Performs QR decomposition of the matrix using Gram-Schmidt orthogonalization. | |
| std::vector< T > | matVec (const std::vector< T > &v) const |
| Performs matrix-vector multiplication. | |
| T | dot (const std::vector< T > &a, const std::vector< T > &b) const |
| Computes the dot product of two vectors. | |
| void | normalize (std::vector< T > &v) const |
| Normalizes a vector to unit length (in-place). | |
| void | lanczos (int m, std::vector< T > &alpha, std::vector< T > &beta, Matrix< T > &Q) const |
| Performs the Lanczos algorithm for tridiagonalization. | |
| Matrix< T > | buildTridiagonal (const std::vector< T > &alpha, const std::vector< T > &beta, int m) const |
| Constructs a tridiagonal matrix from diagonal and off-diagonal elements. | |
| double | frobeniusNorm () const |
| Computes the Frobenius norm of the matrix. | |
| double | spectralNorm () const |
| Computes the spectral norm (2-norm) of the matrix. | |
| double | norm (const std::vector< T > &v) const |
| Computes the Euclidean (L2) norm of a vector. | |
| Matrix< T > | getMinor (int row, int col) const |
| Gets the minor matrix by removing a specified row and column. | |
| std::string | toString () const |
| Converts the matrix to a formatted string representation. | |
| int | getRows () const |
| Gets the number of rows in the matrix. | |
| int | getCols () const |
| Gets the number of columns in the matrix. | |
Static Public Member Functions | |
| static Matrix< T > | identity (int n) |
| Creates an n × n identity matrix. | |
| static Matrix< T > | zeros (int r, int c) |
| Creates an r × c matrix filled with zeros. | |
| static Matrix< T > | ones (int r, int c) |
| Creates an r × c matrix filled with ones. | |
Friends | |
| std::ostream & | operator<< (std::ostream &os, const Matrix< T > &m) |
| Stream output operator for formatted matrix display. | |
A templated matrix class supporting various linear algebra operations.
This class provides a comprehensive implementation of matrix operations including basic arithmetic, advanced operations (determinant, inverse, eigenvalues), matrix decompositions (LU, QR), and iterative algorithms (Lanczos). Supports both real arithmetic types and complex number types.
| T | The element type. Can be arithmetic types (int, float, double) or complex number types (ComplexNumber). |
Constructs a matrix from a 2D vector.
| mat | A 2D vector containing the matrix data |
| std::invalid_argument | if rows have inconsistent lengths |
Definition at line 113 of file matrix.cpp.
Computes the adjoint (adjugate) matrix.
The adjoint is the transpose of the cofactor matrix.
| std::invalid_argument | if matrix is not square |
Definition at line 403 of file matrix.cpp.
| Matrix< T > Matrix< T >::buildTridiagonal | ( | const std::vector< T > & | alpha, |
| const std::vector< T > & | beta, | ||
| int | m | ||
| ) | const |
Constructs a tridiagonal matrix from diagonal and off-diagonal elements.
| alpha | Diagonal elements |
| beta | Off-diagonal elements |
| m | Dimension of the tridiagonal matrix |
Definition at line 847 of file matrix.cpp.
| T Matrix< T >::determinant |
Computes the determinant of the matrix.
The calculation follows these rules based on the matrix dimension \( n \times n \):
\[ \det(A) = a_{1,1} \]
\[ \det(A) = a_{0,0}a_{1,1} - a_{0,1}a_{1,0} \]
\[ \det(A) = \det(L)\det(U) = \prod_{i=0}^{n-1} u_{i,i} \]
*(Note: \( \det(L) = 1 \) as it is a unit triangular matrix)*.| std::invalid_argument | If the matrix is not square ( \( rows \neq cols \)). |
Definition at line 276 of file matrix.cpp.
| T Matrix< T >::dot | ( | const std::vector< T > & | a, |
| const std::vector< T > & | b | ||
| ) | const |
Computes the dot product of two vectors.
| a | First vector |
| b | Second vector |
| std::invalid_argument | if vectors have different sizes |
Definition at line 733 of file matrix.cpp.
| std::vector< T > Matrix< T >::eigenvalues |
Computes the eigenvalues of the matrix.
| std::invalid_argument | if matrix is not square |
Definition at line 427 of file matrix.cpp.
Computes the eigenvectors of the matrix.
| std::invalid_argument | if matrix is not square |
Definition at line 865 of file matrix.cpp.
| double Matrix< T >::frobeniusNorm |
Computes the Frobenius norm of the matrix.
The Frobenius norm is the square root of the sum of squares of all elements.
Definition at line 619 of file matrix.cpp.
|
inline |
Gets the minor matrix by removing a specified row and column.
Used in computing determinants and adjoints via cofactor expansion.
| row | Row to remove (0-based) |
| col | Column to remove (0-based) |
| std::out_of_range | if row or column index is out of bounds |
Definition at line 655 of file matrix.cpp.
|
inline |
Creates an n × n identity matrix.
The identity matrix has ones on the main diagonal and zeros elsewhere.
| n | Dimension of the identity matrix |
Definition at line 706 of file matrix.cpp.
Computes the inverse of the matrix.
| std::invalid_argument | if matrix is not square |
| std::runtime_error | if matrix is singular (determinant is zero) |
Definition at line 303 of file matrix.cpp.
| bool Matrix< T >::isSymmetric |
Checks if the matrix is symmetric.
A matrix is symmetric if A = A^T.
| std::invalid_argument | if matrix is not square |
Definition at line 258 of file matrix.cpp.
| void Matrix< T >::lanczos | ( | int | m, |
| std::vector< T > & | alpha, | ||
| std::vector< T > & | beta, | ||
| Matrix< T > & | Q | ||
| ) | const |
Performs the Lanczos algorithm for tridiagonalization.
The Lanczos algorithm is an iterative method for computing eigenvalues and eigenvectors of large sparse symmetric matrices. It reduces the matrix to tridiagonal form.
| m | Number of Lanczos iterations |
| alpha | Output vector of diagonal elements (size m) |
| beta | Output vector of off-diagonal elements (size m-1) |
| Q | Output matrix of orthonormal Lanczos vectors (n × m) |
| std::invalid_argument | if matrix is not square or m > matrix size |
Definition at line 792 of file matrix.cpp.
Performs LU decomposition of the matrix.
Decomposes the matrix into lower triangular (L) and upper triangular (U) matrices such that A = LU.
| std::invalid_argument | if matrix is not square |
| std::runtime_error | if decomposition fails (singular matrix) |
Definition at line 533 of file matrix.cpp.
| std::vector< T > Matrix< T >::matVec | ( | const std::vector< T > & | v | ) | const |
Performs matrix-vector multiplication.
| v | Vector to multiply with |
| std::invalid_argument | if vector size doesn't match matrix columns |
Definition at line 721 of file matrix.cpp.
| double Matrix< T >::norm | ( | const std::vector< T > & | v | ) | const |
Computes the Euclidean (L2) norm of a vector.
| v | Vector to compute norm for |
Definition at line 743 of file matrix.cpp.
| void Matrix< T >::normalize | ( | std::vector< T > & | v | ) | const |
Normalizes a vector to unit length (in-place).
| v | Vector to normalize |
| std::runtime_error | if vector has zero norm |
Definition at line 749 of file matrix.cpp.
Creates an r × c matrix filled with ones.
| r | Number of rows |
| c | Number of columns |
Definition at line 909 of file matrix.cpp.
| T & Matrix< T >::operator() | ( | int | row, |
| int | column | ||
| ) |
Accesses matrix element at specified position (non-const).
| row | Row index (0-based) |
| column | Column index (0-based) |
Definition at line 134 of file matrix.cpp.
| const T & Matrix< T >::operator() | ( | int | row, |
| int | column | ||
| ) | const |
Accesses matrix element at specified position (const).
| row | Row index (0-based) |
| column | Column index (0-based) |
Definition at line 146 of file matrix.cpp.
Matrix multiplication.
| other | Matrix to multiply with |
| std::invalid_argument | if dimensions are incompatible (this.cols != other.rows) |
Definition at line 190 of file matrix.cpp.
Scalar multiplication.
| scalar | Scalar value to multiply all elements by |
Definition at line 211 of file matrix.cpp.
Matrix addition.
| other | Matrix to add |
| std::invalid_argument | if matrices have different dimensions |
Definition at line 158 of file matrix.cpp.
Matrix subtraction.
| other | Matrix to subtract |
| std::invalid_argument | if matrices have different dimensions |
Definition at line 174 of file matrix.cpp.
In-place subtraction.
| other | Matrix to subtract |
| std::invalid_argument | if matrices have different dimensions |
Element-wise division.
| other | Matrix to divide by |
| std::invalid_argument | if matrices have different dimensions |
| std::runtime_error | if division by zero occurs |
|
inline |
|
inline |
Performs QR decomposition of the matrix using Gram-Schmidt orthogonalization.
Decomposes the matrix into an orthogonal matrix (Q) and an upper triangular matrix (R) such that A = QR.
| std::invalid_argument | if matrix has more columns than rows |
Definition at line 563 of file matrix.cpp.
| T Matrix< T >::rank |
Computes the rank of the matrix.
The rank is the number of linearly independent rows or columns.
Definition at line 223 of file matrix.cpp.
| double Matrix< T >::spectralNorm |
Computes the spectral norm (2-norm) of the matrix.
The spectral norm is the largest singular value, equal to the square root of the largest eigenvalue of A^T * A.
Definition at line 633 of file matrix.cpp.
| Matrix< T > Matrix< T >::submatrix | ( | int | row_start, |
| int | row_end, | ||
| int | col_start, | ||
| int | col_end | ||
| ) | const |
Extracts a submatrix from specified row and column ranges.
Extracts rows [row_start, row_end) and columns [col_start, col_end) using exclusive upper bounds.
| row_start | Starting row index (inclusive) |
| row_end | Ending row index (exclusive) |
| col_start | Starting column index (inclusive) |
| col_end | Ending column index (exclusive) |
| std::out_of_range | if indices are out of bounds |
Definition at line 763 of file matrix.cpp.
| std::string Matrix< T >::toString |
Converts the matrix to a formatted string representation.
Definition at line 684 of file matrix.cpp.
Computes the transpose of the matrix.
Definition at line 391 of file matrix.cpp.
Creates an r × c matrix filled with zeros.
| r | Number of rows |
| c | Number of columns |
Definition at line 899 of file matrix.cpp.
|
friend |