sokobo
Loading...
Searching...
No Matches
matrix.h
1#pragma once
2#include <iomanip>
3#include <iostream>
4#include <string>
5#include <vector>
6
7#include "complex_number.h"
8#include "vector.h"
9
21template<typename T>
22class Matrix
23{
24private:
25 std::vector<std::vector<T>>
26 data;
27 int rows;
28 int cols;
29
30public:
35 : rows(0)
36 , cols(0)
37 , data {} {};
38
46 Matrix(int r, int c)
47 : rows(r)
48 , cols(c)
49 , data(r, std::vector<T>(c))
50 {
51 }
52
59 Matrix(const std::vector<std::vector<T>>& mat);
60
68 T& operator()(int row, int column);
69
77 const T& operator()(int row, int column) const;
78
85 std::vector<T>& operator[](int i) { return data[i]; }
86
93 const std::vector<T>& operator[](int i) const { return data[i]; }
94
95 // ==================== Basic Operations ====================
96
104 Matrix<T> operator+(const Matrix<T>& other) const;
105
113 Matrix<T> operator-(const Matrix<T>& other) const;
114
123 Matrix<T> operator*(const Matrix<T>& other) const;
124
131 Matrix<T> operator*(const T& scalar) const;
132
141 Matrix<T> operator/=(const Matrix<T>& other) const;
142
150 Matrix<T> operator-=(const Matrix<T>& other) const;
151
162 friend std::ostream& operator<<(std::ostream& os, const Matrix<T>& m)
163 {
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)
167 << m.data[i][j];
168 }
169 if (i < m.rows - 1) {
170 os << "\n";
171 }
172 }
173 return os;
174 }
175
176 // ==================== Advanced Operations ====================
195 T determinant() const;
196
204 Matrix<T> inverse() const;
205
211 Matrix<T> transpose() const;
212
219 std::vector<T> eigenvalues() const;
220
227 Matrix<T> eigenvectors() const;
228
237 Matrix<T> adjoint() const;
238
252 Matrix<T> submatrix(int row_start,
253 int row_end,
254 int col_start,
255 int col_end) const;
256
264 T rank() const;
265
274 bool isSymmetric() const;
275
276 // ==================== Decompositions ====================
277
288 std::pair<Matrix<T>, Matrix<T>> LUDecomposition() const;
289
300 std::pair<Matrix<T>, Matrix<T>> QRDecomposition() const;
301
302 // ==================== Vector Operations ====================
303
311 std::vector<T> matVec(const std::vector<T>& v) const;
312
321 T dot(const std::vector<T>& a, const std::vector<T>& b) const;
322
329 void normalize(std::vector<T>& v) const;
330
344 void lanczos(int m,
345 std::vector<T>& alpha,
346 std::vector<T>& beta,
347 Matrix<T>& Q) const;
348
358 Matrix<T> buildTridiagonal(const std::vector<T>& alpha,
359 const std::vector<T>& beta,
360 int m) const;
361
362 // ==================== Matrix Norms ====================
363
372 double frobeniusNorm() const;
373
382 double spectralNorm() const;
383
390 double norm(const std::vector<T>& v) const;
391
392 // ==================== Utility ====================
393
404 Matrix<T> getMinor(int row, int col) const;
405
411 std::string toString() const;
412
418 int getRows() const { return rows; }
419
425 int getCols() const { return cols; }
426
427 // ==================== Static Factory Methods ====================
428
437 static Matrix<T> identity(int n);
438
446 static Matrix<T> zeros(int r, int c);
447
455 static Matrix<T> ones(int r, int c);
456};
A templated matrix class supporting various linear algebra operations.
Definition: matrix.h:23
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.
Definition: matrix.cpp:763
Matrix< T > eigenvectors() const
Computes the eigenvectors of the matrix.
Definition: matrix.cpp:865
int getCols() const
Gets the number of columns in the matrix.
Definition: matrix.h:425
std::vector< T > matVec(const std::vector< T > &v) const
Performs matrix-vector multiplication.
Definition: matrix.cpp:721
T rank() const
Computes the rank of the matrix.
Definition: matrix.cpp:223
Matrix< T > getMinor(int row, int col) const
Gets the minor matrix by removing a specified row and column.
Definition: matrix.cpp:655
double norm(const std::vector< T > &v) const
Computes the Euclidean (L2) norm of a vector.
Definition: matrix.cpp:743
double spectralNorm() const
Computes the spectral norm (2-norm) of the matrix.
Definition: matrix.cpp:633
Matrix< T > operator*(const Matrix< T > &other) const
Matrix multiplication.
Definition: matrix.cpp:190
std::pair< Matrix< T >, Matrix< T > > QRDecomposition() const
Performs QR decomposition of the matrix using Gram-Schmidt orthogonalization.
Definition: matrix.cpp:563
void lanczos(int m, std::vector< T > &alpha, std::vector< T > &beta, Matrix< T > &Q) const
Performs the Lanczos algorithm for tridiagonalization.
Definition: matrix.cpp:792
const std::vector< T > & operator[](int i) const
Accesses a row of the matrix (const).
Definition: matrix.h:93
Matrix< T > operator-(const Matrix< T > &other) const
Matrix subtraction.
Definition: matrix.cpp:174
std::vector< T > & operator[](int i)
Accesses a row of the matrix (non-const).
Definition: matrix.h:85
std::string toString() const
Converts the matrix to a formatted string representation.
Definition: matrix.cpp:684
Matrix< T > transpose() const
Computes the transpose of the matrix.
Definition: matrix.cpp:391
T dot(const std::vector< T > &a, const std::vector< T > &b) const
Computes the dot product of two vectors.
Definition: matrix.cpp:733
double frobeniusNorm() const
Computes the Frobenius norm of the matrix.
Definition: matrix.cpp:619
Matrix< T > adjoint() const
Computes the adjoint (adjugate) matrix.
Definition: matrix.cpp:403
static Matrix< T > ones(int r, int c)
Creates an r × c matrix filled with ones.
Definition: matrix.cpp:909
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).
Definition: matrix.cpp:749
friend std::ostream & operator<<(std::ostream &os, const Matrix< T > &m)
Stream output operator for formatted matrix display.
Definition: matrix.h:162
std::pair< Matrix< T >, Matrix< T > > LUDecomposition() const
Performs LU decomposition of the matrix.
Definition: matrix.cpp:533
Matrix()
Default constructor creating an empty 0×0 matrix.
Definition: matrix.h:34
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.
Definition: matrix.cpp:847
static Matrix< T > zeros(int r, int c)
Creates an r × c matrix filled with zeros.
Definition: matrix.cpp:899
Matrix< T > inverse() const
Computes the inverse of the matrix.
Definition: matrix.cpp:303
std::vector< T > eigenvalues() const
Computes the eigenvalues of the matrix.
Definition: matrix.cpp:427
T & operator()(int row, int column)
Accesses matrix element at specified position (non-const).
Definition: matrix.cpp:134
Matrix< T > operator+(const Matrix< T > &other) const
Matrix addition.
Definition: matrix.cpp:158
Matrix< T > operator-=(const Matrix< T > &other) const
In-place subtraction.
static Matrix< T > identity(int n)
Creates an n × n identity matrix.
Definition: matrix.cpp:706
T determinant() const
Computes the determinant of the matrix.
Definition: matrix.cpp:276
bool isSymmetric() const
Checks if the matrix is symmetric.
Definition: matrix.cpp:258
int getRows() const
Gets the number of rows in the matrix.
Definition: matrix.h:418
Matrix(int r, int c)
Constructs a matrix with specified dimensions, initialized with default values.
Definition: matrix.h:46