29 for (i = 0; i < v.size(); i++)
66 for (i = 0; i < Nrows(); i++)
67 for (j = 0; j < Ncols(); j++)
68 opp[i][j] = -_mat[i][j];
81 for (i = 0; i < Nrows(); i++)
82 for (j = 0; j < Ncols(); j++)
83 sum[i][j] = _mat[i][j]+mat[i][j];
95 for (i = 0; i < _mat.size(); i++)
96 for (j = 0; j < _mat[0].size(); j++)
97 _mat[i][j] += mat[i][j];
108 for (i = 0; i < Nrows(); i++)
109 for (j = 0; j < Ncols(); j++)
110 diff[i][j] = _mat[i][j]-mat[i][j];
122 for (i = 0; i < _mat.size(); i++)
123 for (j = 0; j < _mat[0].size(); j++)
124 _mat[i][j] -= mat[i][j];
135 for (i = 0; i < Nrows(); i++)
136 for (j = 0; j < mat.
Ncols(); j++)
137 for (k = 0; k < Ncols(); k++)
138 prod[i][j] += _mat[i][k]*mat[k][j];
149 std::vector<std::vector<T>> temp = _mat;
150 Resize(Nrows(), mat.
Ncols());
152 for (i = 0; i < temp.size(); i++)
153 for (j = 0; j < mat.
Ncols(); j++) {
155 for (k = 0; k < temp[i].size(); k++)
156 _mat[i][j] += temp[i][k]*mat[k][j];
165 for (i = 0; i < Nrows(); i++)
166 for (j = 0; j < Ncols(); j++)
167 prod[i][j] = factor*_mat[i][j];
176 for (i = 0; i < Nrows(); i++)
177 for (j = 0; j < Ncols(); j++)
178 _mat[i][j] *= factor;
186 for (i = 0; i < mat.
Nrows(); i++)
187 for (j = 0; j < mat.
Ncols(); j++)
188 prod[i][j] = factor*mat[i][j];
201 for (i = 0; i < Nrows(); i++)
202 for (j = 0; j < Ncols(); j++)
203 prod[i] += _mat[i][j]*vec[j];
216 for (i = 0; i < Nrows(); i++)
217 for (j = 0; j < Ncols(); j++)
218 ratio[i][j] = _mat[i][j]/factor;
230 for (i = 0; i < Nrows(); i++)
231 for (j = 0; j < Ncols(); j++)
232 _mat[i][j] /= factor;
247 template<
typename T> std::ostream& operator<<(std::ostream& os, const Matrix<T>& mat) {
251 for (i = 0; i < mat.
Nrows(); i++) {
254 for (j = 0; j < mat.
Ncols(); j++) {
256 if ( j < mat.Ncols()-1 )
259 if ( i < mat.Nrows()-1 )
273 std::vector<std::vector<T>> sub = _mat;
274 sub.erase(sub.begin()+i);
276 for (k = 0; k < sub.size(); k++)
277 sub[k].erase(sub[k].begin()+j);
280 return pow(-1, i+j)*
Matrix<T>(sub).Determinant();
289 for (i = 0; i < Nrows(); i++)
290 for (j = 0; j < Ncols(); j++)
291 cof[i][j] = Cofactor(i, j);
294 "Could not initialize"+std::string(e.
what()),
295 "Matrix<T>::CofactorMatrix"));
304 std::vector<T> column(Nrows());
306 for (j = 0; j < Nrows(); j++)
307 column[j] = _mat[j][i];
319 if ( Nrows() == 1 ) {
321 }
else if ( Nrows() == 2 ) {
322 return _mat[0][0]*_mat[1][1] - _mat[0][1]*_mat[1][0];
323 }
else if ( Nrows() == 3 ) {
324 return _mat[0][0]*(_mat[1][1]*_mat[2][2]-_mat[1][2]*_mat[2][1])
325 - _mat[0][1]*(_mat[1][0]*_mat[2][2]-_mat[1][2]*_mat[2][0])
326 + _mat[0][2]*(_mat[1][0]*_mat[2][1]-_mat[1][1]*_mat[2][0]);
335 const double eps)
const {
342 size_t dim = _mat.size();
345 std::vector<T> tempvals(dim);
346 for (i = 0; i < dim; i++)
348 std::vector<T> eigenvals(dim);
350 std::vector<std::vector<T>> Mk = _mat;
352 for (i = 0; i < dim; i++)
366 for (i = 0; i < Mk.size(); i++) {
367 eigenvals[i] = Mk[i][i];
368 if ( fabs((eigenvals[i]-tempvals[i])/tempvals[i]) > eps )
371 tempvals = eigenvals;
375 "Could not eigendecompose"+std::string(e.
what()),
376 "Matrix<T>::EigenVectors"));
387 for (i = 0; i < Nrows(); i++)
388 for (j = 0; j < Ncols(); j++)
396 for (i = 0; i < Nrows(); i++)
397 for (j = 0; j < Ncols(); j++)
398 _mat[i][j] = (i == j) ? 1 : 0;
418 for (i = 0; i < n; i++) {
426 for (k = 0; k < n; k++) {
430 max = fabs(_mat[k][k]);
431 for (j = k+1; j < n; j++) {
432 if ( max < fabs(_mat[j][k]) ) {
433 max = fabs(_mat[j][k]);
441 "The matrix is singular",
"Matrix<T>::Invert"));
447 std::swap(_mat[pivot], _mat[k]);
451 for (i = 0; i < 2*n; i++)
455 for (i = 0; i < n; i++) {
458 for (j = k+1; j < 2*n; j++)
459 _mat[i][j] -= temp*_mat[k][j];
466 for (i = 0; i < n; i++)
467 _mat[i].erase(_mat[i].begin(), _mat[i].begin()+n);
474 for (i = 0; i < Nrows(); i++)
475 for (j = i+1; j < Ncols(); j++)
476 if ( _mat[i][j] != _mat[j][i] )
487 if ( !_mat.size() ) {
490 return _mat[0].size();
495 std::cerr <<
"Matrix (" << Nrows() <<
", " << Ncols() <<
") is as follows" << std::endl;
497 for (i = 0; i < Nrows(); i++) {
498 for (j = 0; j < Ncols(); j++)
499 std::cerr << _mat[i][j] <<
"\t";
500 std::cerr << std::endl;
508 for (j = 0; j < Nrows(); j++)
509 _mat[j].erase(_mat[j].begin()+i);
515 _mat.erase(_mat.begin()+i);
522 for (i = 0; i < m; i++)
526 template<
typename T>
const std::vector<T>&
Matrix<T>::Row(
const size_t i)
const {
549 "The matrix is singular",
"Matrix<T>::Solve"));
550 return std::vector<T>(n);
552 std::vector<std::vector<T>> L(decLU.
L()), U(decLU.
U());
553 std::vector<T> pivots(decLU.
Pivots());
556 std::vector<T> bprime(n);
558 for (i = 0; i < n; i++)
559 bprime[i] = b[pivots[i]];
565 for (k = 0; k < (int)n; k++) {
567 for (j = 0; j < k; j++)
569 x[k] = (bprime[k] - s)/L[k][k];
573 for (k = n-1; k > -1; k--) {
575 for (j = k+1; j < (int)n; j++)
592 for (i = 0; i < Ncols(); i++)
593 for (j = 0; j < Nrows(); j++)
594 trans[i][j] = _mat[j][i];
const bool & Complete()
Gets and returns whether the LU decomposition was successful or not.
Definition: DecompLU.hh:63
const char * what() const
Return char buffer of message+" at "+location, memory remains owned by Exception. ...
Definition: Exception.hh:52
const size_t Ncols() const
Returns the number of columns.
Definition: Matrix-inl.hh:486
Defines the LU decomposition algorithm of a square matrix.
Definition: DecompLU.hh:19
void IsGreater(const std::string &location, const std::string &what, const T &value, const T &limit, const bool strictly=false)
Asserts that a value is greater than another.
Definition: Assert.hh:10
Matrix & operator=(const Matrix &mat)
Equality operator.
Definition: Matrix-inl.hh:43
Matrix operator*(const Matrix &mat) const
Overload the matrix product.
Definition: Matrix-inl.hh:127
Matrix operator/(const T &factor) const
Overload the matrix ratio with a scalar.
Definition: Matrix-inl.hh:208
void RemoveColumn(const size_t i)
Removes the i^th column of the matrix.
Definition: Matrix-inl.hh:504
void IsNonZero(const std::string &location, const std::string &what, const T &value)
Asserts that a value is non zero.
Definition: Assert.hh:2
void operator-=(const Matrix &mat)
Overload the matrix substraction.
Definition: Matrix-inl.hh:115
bool operator!() const
Boolean not operator.
Definition: Matrix-inl.hh:56
Object that can host any type of data in a vector arrangement.
Definition: Vector.hh:17
std::vector< std::vector< T > > _mat
Underlying std::vector<std::vector>>
Definition: Matrix.hh:217
void IsSquare(const std::string &location, const std::string &what, const std::vector< std::vector< T >> &matrix)
Asserts that a matrix is square.
Definition: Assert.hh:116
Matrix< T > Inverse() const
Returns the inverse of the matrix.
Definition: Matrix-inl.hh:401
void operator/=(const T &factor)
Overload the matrix ratio with a scalar.
Definition: Matrix-inl.hh:223
void Identity()
Sets the matrix to an identity matrix.
Definition: Matrix-inl.hh:392
const std::vector< T > & Row(const size_t i) const
Returns the i^th row of the matrix as a vector.
Definition: Matrix-inl.hh:526
void Resize(const size_t m, const size_t n)
Resizes the matrix to chosen dimension.
Definition: Matrix-inl.hh:518
Matrix operator-() const
Overload the unary minus for matrix algebra.
Definition: Matrix-inl.hh:61
bool IsSymmetric() const
Checks whether the matrix is symmetric or not.
Definition: Matrix-inl.hh:470
Matrix< T > CofactorMatrix() const
Returns the inverse of the matrix.
Definition: Matrix-inl.hh:283
const std::vector< std::vector< T > > & L()
Gets and returns the lower triangular matrix.
Definition: DecompLU.hh:72
std::vector< T > Column(const size_t i) const
Returns the i^th column of the matrix as a vector.
Definition: Matrix-inl.hh:301
Global exception object for all non-standard exceptions thrown by the code.
Definition: Exception.hh:30
Defines the QR decomposition algorithm of a square matrix.
Definition: DecompQR.hh:15
const std::vector< T > & operator[](const size_t i) const
Overload the subscript operator, return an std::vector row.
Definition: Matrix-inl.hh:235
void Fill(const T &value)
Fills the matrix with a certain value.
Definition: Matrix-inl.hh:384
Matrix()
Default constructor, initilizes an empty matrix of dimension 0x0.
Definition: Matrix-inl.hh:1
const std::vector< T > & std() const
Returns the vector as an std::vector.
Definition: Vector-inl.hh:270
const size_t Nrows() const
Returns the number of rows.
Definition: Matrix-inl.hh:482
Matrix operator+(const Matrix &mat) const
Overload the matrix sum.
Definition: Matrix-inl.hh:73
void Invert()
Inverts the matrix in place.
Definition: Matrix-inl.hh:409
Object that can host any type of data in an matrix arrangement.
Definition: Matrix.hh:21
const std::vector< std::vector< T > > & U()
Gets and returns the upper triangular matrix.
Definition: DecompLU.hh:75
const std::vector< std::vector< T > > & std() const
Returns the underlying std vector.
Definition: Matrix-inl.hh:583
T Determinant() const
Returns the deterimant of the matrix.
Definition: Matrix-inl.hh:312
void RemoveRow(const size_t i)
Removes the i^th row of the matrix.
Definition: Matrix-inl.hh:512
~Matrix()
Destructor.
Definition: Matrix-inl.hh:41
void IsWithinBounds(const std::string &location, const size_t n, const size_t i)
Asserts that the index that is accessed is within bounds.
Definition: Assert.cc:5
std::vector< T > Solve(const std::vector< T > &b) const
Solves A*x=b, provided with b.
Definition: Matrix-inl.hh:538
void Print() const
Print the matrix.
Definition: Matrix-inl.hh:494
void Multiplicable(const std::string &location, const std::string &what, const std::vector< std::vector< T >> &ma, const std::vector< std::vector< T >> &mb)
Asserts that two matrices are multiplicable.
Definition: Assert.hh:105
void IsNotEmpty(const std::string &location, const std::string &what, const std::vector< T > &vector)
Asserts that a vector is not empty.
Definition: Assert.hh:53
void SameSize(const std::string &location, const std::string &what, const std::vector< T > &va, const std::vector< T > &vb)
Asserts that two vectors have the same size.
Definition: Assert.hh:83
Matrix< T > Transpose() const
Returns the matrix transpose.
Definition: Matrix-inl.hh:588
const std::vector< std::vector< T > > & Q()
Gets and returns the lower triangular matrix.
Definition: DecompQR.hh:65
const std::vector< T > & Pivots()
Gets and returns the permutation vector, contains the ids of the rows.
Definition: DecompLU.hh:69
void operator*=(const Matrix &mat)
Overload the matrix product equality.
Definition: Matrix-inl.hh:143
T Cofactor(const size_t i, const size_t j) const
Returns the ith-jth cofactor of the matrix.
Definition: Matrix-inl.hh:266
Matrix< T > EigenVectors(std::vector< T > &lambdas, const double eps=1e-6) const
Returns the matrix of which the columns are the eigenvectors of the matrix.
Definition: Matrix-inl.hh:334
void operator+=(const Matrix &mat)
Overload the matrix sum.
Definition: Matrix-inl.hh:88
T Determinant()
Returns the determinant of the decomposed matrix.
Definition: DecompLU-inl.hh:113