7#include "complex_number.h"
25 std::vector<std::vector<T>>
49 , data(r, std::vector<T>(c))
59 Matrix(
const std::vector<std::vector<T>>& mat);
77 const T&
operator()(
int row,
int column)
const;
93 const std::vector<T>&
operator[](
int i)
const {
return data[i]; }
164 for (
int i = 0; i < m.rows; ++i) {
165 for (
int j = 0; j < m.cols; ++j) {
166 os << std::setw(12) << std::fixed << std::setprecision(6)
169 if (i < m.rows - 1) {
311 std::vector<T>
matVec(
const std::vector<T>& v)
const;
321 T
dot(
const std::vector<T>& a,
const std::vector<T>& b)
const;
345 std::vector<T>& alpha,
346 std::vector<T>& beta,
359 const std::vector<T>& beta,
390 double norm(
const std::vector<T>& v)
const;
A templated matrix class supporting various linear algebra operations.
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.
Matrix< T > eigenvectors() const
Computes the eigenvectors of the matrix.
int getCols() const
Gets the number of columns in the matrix.
std::vector< T > matVec(const std::vector< T > &v) const
Performs matrix-vector multiplication.
T rank() const
Computes the rank of the matrix.
Matrix< T > getMinor(int row, int col) const
Gets the minor matrix by removing a specified row and column.
double norm(const std::vector< T > &v) const
Computes the Euclidean (L2) norm of a vector.
double spectralNorm() const
Computes the spectral norm (2-norm) of the matrix.
Matrix< T > operator*(const Matrix< T > &other) const
Matrix multiplication.
std::pair< Matrix< T >, Matrix< T > > QRDecomposition() const
Performs QR decomposition of the matrix using Gram-Schmidt orthogonalization.
void lanczos(int m, std::vector< T > &alpha, std::vector< T > &beta, Matrix< T > &Q) const
Performs the Lanczos algorithm for tridiagonalization.
const std::vector< T > & operator[](int i) const
Accesses a row of the matrix (const).
Matrix< T > operator-(const Matrix< T > &other) const
Matrix subtraction.
std::vector< T > & operator[](int i)
Accesses a row of the matrix (non-const).
std::string toString() const
Converts the matrix to a formatted string representation.
Matrix< T > transpose() const
Computes the transpose of the matrix.
T dot(const std::vector< T > &a, const std::vector< T > &b) const
Computes the dot product of two vectors.
double frobeniusNorm() const
Computes the Frobenius norm of the matrix.
Matrix< T > adjoint() const
Computes the adjoint (adjugate) matrix.
static Matrix< T > ones(int r, int c)
Creates an r × c matrix filled with ones.
Matrix< T > operator/=(const Matrix< T > &other) const
Element-wise division.
void normalize(std::vector< T > &v) const
Normalizes a vector to unit length (in-place).
friend std::ostream & operator<<(std::ostream &os, const Matrix< T > &m)
Stream output operator for formatted matrix display.
std::pair< Matrix< T >, Matrix< T > > LUDecomposition() const
Performs LU decomposition of the matrix.
Matrix()
Default constructor creating an empty 0×0 matrix.
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.
static Matrix< T > zeros(int r, int c)
Creates an r × c matrix filled with zeros.
Matrix< T > inverse() const
Computes the inverse of the matrix.
std::vector< T > eigenvalues() const
Computes the eigenvalues of the matrix.
T & operator()(int row, int column)
Accesses matrix element at specified position (non-const).
Matrix< T > operator+(const Matrix< T > &other) const
Matrix addition.
Matrix< T > operator-=(const Matrix< T > &other) const
In-place subtraction.
static Matrix< T > identity(int n)
Creates an n × n identity matrix.
T determinant() const
Computes the determinant of the matrix.
bool isSymmetric() const
Checks if the matrix is symmetric.
int getRows() const
Gets the number of rows in the matrix.
Matrix(int r, int c)
Constructs a matrix with specified dimensions, initialized with default values.