SynAPSE  0.6.0
Synergistic Analysis of Phase Space Evolution
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends
Matrix-inl.hh
1 template<typename T> Matrix<T>::Matrix() :
2  _mat(0) {
3 }
4 
5 template<typename T> Matrix<T>::Matrix(const size_t n) :
6  _mat(0) {
7 
8  Resize(n, n);
9 }
10 
11 template<typename T> Matrix<T>::Matrix(const size_t m, const size_t n) :
12  _mat(0) {
13 
14  Resize(m, n);
15 }
16 
17 template<typename T> Matrix<T>::Matrix(const size_t m, const size_t n, const T& value) :
18  _mat(0) {
19 
20  Resize(m, n);
21  Fill(value);
22 }
23 
24 template<typename T> Matrix<T>::Matrix(const std::vector<T>& v) :
25  _mat(0) {
26 
27  Resize(v.size(), 1);
28  size_t i;
29  for (i = 0; i < v.size(); i++)
30  _mat[i][0] = v[i];
31 }
32 
33 template<typename T> Matrix<T>::Matrix(const std::vector<std::vector<T>>& mat) :
34  _mat(mat) {
35 }
36 
37 template<typename T> Matrix<T>::Matrix(const Matrix& mat) {
38  *this = mat;
39 }
40 
41 template<typename T> Matrix<T>::~Matrix() {}
42 
43 template<typename T> Matrix<T>& Matrix<T>::operator=(const Matrix<T>& mat) {
44  if ( this == &mat )
45  return *this;
46 
47  _mat = mat._mat;
48  return *this;
49 }
50 
51 template<typename T> Matrix<T>::operator bool() const {
52 
53  return _mat.size();
54 }
55 
56 template<typename T> bool Matrix<T>::operator!() const {
57 
58  return !_mat.size();
59 }
60 
61 template<typename T> Matrix<T> Matrix<T>::operator-() const {
62 
63  // Change the sign of every elements
64  Matrix<T> opp(Nrows(), Ncols());
65  size_t i, j;
66  for (i = 0; i < Nrows(); i++)
67  for (j = 0; j < Ncols(); j++)
68  opp[i][j] = -_mat[i][j];
69 
70  return opp;
71 }
72 
73 template<typename T> Matrix<T> Matrix<T>::operator+(const Matrix<T>& mat) const {
74 
75  // Check that the sizes of the matrices to be added are compatible
76  Assert::SameSize("Matrix<T>::operator+", "Matrices", _mat, mat.std());
77 
78  // Add the elements one-to-one
79  Matrix<T> sum(Nrows(), Ncols());
80  size_t 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];
84 
85  return sum;
86 }
87 
88 template<typename T> void Matrix<T>::operator+=(const Matrix<T>& mat) {
89 
90  // Check that the sizes of the matrices to be added are compatible
91  Assert::SameSize("Matrix<T>::operator+=", "Matrices", _mat, mat.std());
92 
93  // Add the elements one-to-one
94  size_t 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];
98 }
99 
100 template<typename T> Matrix<T> Matrix<T>::operator-(const Matrix<T>& mat) const {
101 
102  // Check that the sizes of the matrices to be substracted are compatible
103  Assert::SameSize("Matrix<T>::operator-", "Matrices", _mat, mat.std());
104 
105  // Substract the elements one-to-one
106  Matrix<T> diff(Nrows(), Ncols());
107  size_t 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];
111 
112  return diff;
113 }
114 
115 template<typename T> void Matrix<T>::operator-=(const Matrix<T>& mat) {
116 
117  // Check that the sizes of the matrices to be substracted are compatible
118  Assert::SameSize("Matrix<T>::operator-=", "Matrices", _mat, mat.std());
119 
120  // Substract the elements one-to-one
121  size_t 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];
125 }
126 
127 template<typename T> Matrix<T> Matrix<T>::operator*(const Matrix<T>& mat) const {
128 
129  // Check that the sizes of the matrices to be multiplied are compatible
130  Assert::Multiplicable("Matrix<T>::operator*", "Matrices", _mat, mat.std());
131 
132  // Multiply the two matrices
133  Matrix<T> prod(Nrows(), mat.Ncols());
134  size_t i, j, k;
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];
139 
140  return prod;
141 }
142 
143 template<typename T> void Matrix<T>::operator*=(const Matrix<T>& mat) {
144 
145  // Check that the sizes of the matrices to be multiplied are compatible
146  Assert::Multiplicable("Matrix<T>::operator*=", "Matrices", _mat, mat.std());
147 
148  // Update the parent matrix
149  std::vector<std::vector<T>> temp = _mat;
150  Resize(Nrows(), mat.Ncols());
151  size_t i, j, k;
152  for (i = 0; i < temp.size(); i++)
153  for (j = 0; j < mat.Ncols(); j++) {
154  _mat[i][j] = 0;
155  for (k = 0; k < temp[i].size(); k++)
156  _mat[i][j] += temp[i][k]*mat[k][j];
157  }
158 }
159 
160 template<typename T> Matrix<T> Matrix<T>::operator*(const T& factor) const {
161 
162  // Multiply the matrix with a scalar to its right
163  Matrix<T> prod(Nrows(), Ncols());
164  size_t i, j;
165  for (i = 0; i < Nrows(); i++)
166  for (j = 0; j < Ncols(); j++)
167  prod[i][j] = factor*_mat[i][j];
168 
169  return prod;
170 }
171 
172 template<typename T> void Matrix<T>::operator*=(const T& factor) {
173 
174  // Multiply in place the matrix with a scalar to its right
175  size_t i, j;
176  for (i = 0; i < Nrows(); i++)
177  for (j = 0; j < Ncols(); j++)
178  _mat[i][j] *= factor;
179 }
180 
181 template<typename T> Matrix<T> operator*(const T& factor, const Matrix<T> mat) {
182 
183  // Multiply the matrix with a scalar to its left
184  Matrix<T> prod(mat.Nrows(), mat.Ncols());
185  size_t i, j;
186  for (i = 0; i < mat.Nrows(); i++)
187  for (j = 0; j < mat.Ncols(); j++)
188  prod[i][j] = factor*mat[i][j];
189 
190  return prod;
191 }
192 
193 template<typename T> Vector<T> Matrix<T>::operator*(const Vector<T>& vec) const {
194 
195  // Check that the sizes of the matrix and vector to be multiplied are compatible
196  Assert::SameSize("Matrix<T>::operator*", "Matrix and vector", _mat[0], vec.std());
197 
198  // Multiply the matrix with a vector to its right
199  Vector<T> prod(Nrows());
200  size_t i, j;
201  for (i = 0; i < Nrows(); i++)
202  for (j = 0; j < Ncols(); j++)
203  prod[i] += _mat[i][j]*vec[j];
204 
205  return prod;
206 }
207 
208 template<typename T> Matrix<T> Matrix<T>::operator/(const T& factor) const {
209 
210  // Check that the divisor is non-zero
211  Assert::IsNonZero("Matrix<T>::operator/", "Divisor", factor);
212 
213  // Multiply the matrix with a scalar to its right
214  Matrix<T> ratio(Nrows(), Ncols());
215  size_t i, j;
216  for (i = 0; i < Nrows(); i++)
217  for (j = 0; j < Ncols(); j++)
218  ratio[i][j] = _mat[i][j]/factor;
219 
220  return ratio;
221 }
222 
223 template<typename T> void Matrix<T>::operator/=(const T& factor) {
224 
225  // Check that the divisor is non-zero
226  Assert::IsNonZero("Matrix<T>::operator/=", "Divisor", factor);
227 
228  // Multiply the matrix with a scalar to its right
229  size_t i, j;
230  for (i = 0; i < Nrows(); i++)
231  for (j = 0; j < Ncols(); j++)
232  _mat[i][j] /= factor;
233 }
234 
235 template<typename T> const std::vector<T>& Matrix<T>::operator[](const size_t i) const {
236 
237  Assert::IsWithinBounds("Matrix<T>::operator[]", Nrows(), i);
238  return _mat[i];
239 }
240 
241 template<typename T> std::vector<T>& Matrix<T>::operator[](const size_t i) {
242 
243  Assert::IsWithinBounds("Matrix<T>::operator[]", Nrows(), i);
244  return _mat[i];
245 }
246 
247 template<typename T> std::ostream& operator<<(std::ostream& os, const Matrix<T>& mat) {
248 
249  size_t i, j;
250  os << "[[";
251  for (i = 0; i < mat.Nrows(); i++) {
252  if ( i )
253  os << "[";
254  for (j = 0; j < mat.Ncols(); j++) {
255  os << mat[i][j];
256  if ( j < mat.Ncols()-1 )
257  os << ", ";
258  }
259  if ( i < mat.Nrows()-1 )
260  os << "], ";
261  }
262  os << "]]";
263  return os;
264 }
265 
266 template<typename T> T Matrix<T>::Cofactor(const size_t i, const size_t j) const {
267 
268  // Check that the matrix is square and of at least dimension 1
269  Assert::IsSquare("Matrix<T>::Cofactor", "Matrix", _mat);
270  Assert::IsGreater("Matrix<T>::Cofactor", "Dimension", Nrows(), size_t(2));
271 
272  // Copy the matrix into a new vector of vector and simply remove the undesired elements
273  std::vector<std::vector<T>> sub = _mat;
274  sub.erase(sub.begin()+i);
275  size_t k;
276  for (k = 0; k < sub.size(); k++)
277  sub[k].erase(sub[k].begin()+j);
278 
279  // The cofactor is the determinant of sub with a prefactor
280  return pow(-1, i+j)*Matrix<T>(sub).Determinant();
281 }
282 
283 template<typename T> Matrix<T> Matrix<T>::CofactorMatrix() const {
284 
285  // Define a matrix of dimension n*n and fill it with cofactors
286  Matrix<T> cof(Nrows(), Ncols());
287  try {
288  size_t i, j;
289  for (i = 0; i < Nrows(); i++)
290  for (j = 0; j < Ncols(); j++)
291  cof[i][j] = Cofactor(i, j);
292  } catch ( Exceptions::Exception& e ) {
293  throw(Exceptions::Exception(Exceptions::nonRecoverable,
294  "Could not initialize"+std::string(e.what()),
295  "Matrix<T>::CofactorMatrix"));
296  }
297 
298  return cof;
299 }
300 
301 template<typename T> std::vector<T> Matrix<T>::Column(const size_t i) const {
302 
303  Assert::IsWithinBounds("Matrix<T>::Column", Ncols(), i);
304  std::vector<T> column(Nrows());
305  size_t j;
306  for (j = 0; j < Nrows(); j++)
307  column[j] = _mat[j][i];
308 
309  return column;
310 }
311 
312 template<typename T> T Matrix<T>::Determinant() const {
313 
314  // Cannot compute the determinant of a non square matrix
315  Assert::IsNotEmpty("Matrix<T>::Determinant", "Matrix", _mat);
316  Assert::IsSquare("Matrix<T>::Determinant", "Matrix", _mat);
317 
318  // Get rid of the simple cases without using the heavy artillery
319  if ( Nrows() == 1 ) {
320  return _mat[0][0];
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]);
327  }
328 
329  // For larger matrices, use LU decomposition, fastest
330  DecompLU<T> decLU(_mat);
331  return decLU.Determinant();
332 }
333 
334 template<typename T> Matrix<T> Matrix<T>::EigenVectors(std::vector<T>& lambdas,
335  const double eps) const {
336 
337  // Check that the matrix is square
338  Assert::IsNotEmpty("Matrix<T>::EigenVectors", "Matrix", _mat);
339  Assert::IsSquare("Matrix<T>::EigenVectors", "Matrix", _mat);
340 
341  // Initialize the containers for the decomposition
342  size_t dim = _mat.size(); // Dimension of the square matrix
343  size_t i;
344 
345  std::vector<T> tempvals(dim); // Array to store the temporary eigenvalues
346  for (i = 0; i < dim; i++)
347  tempvals[i] = 1.;
348  std::vector<T> eigenvals(dim);
349 
350  std::vector<std::vector<T>> Mk = _mat; // k^th iteration of the similar matrix
351  Matrix<T> Qk, Q(dim, dim, 0); // k^th iteration of the Q matrix and its composite
352  for (i = 0; i < dim; i++)
353  Q[i][i] = 1;
354 
355  // QR decompose the Matrix M, compute Qk^T*M*Qk, repeat until convergence
356  try {
357  bool done = false;
358  while ( !done ) {
359  DecompQR<T> decQR(Mk);
360  Qk = Matrix<T>(decQR.Q());
361  Q *= Qk;
362  Mk = (Qk.Transpose()*Matrix<T>(Mk)*Qk).std();
363 
364  // Update and check the evolution of the eigenvalues
365  done = true;
366  for (i = 0; i < Mk.size(); i++) {
367  eigenvals[i] = Mk[i][i];
368  if ( fabs((eigenvals[i]-tempvals[i])/tempvals[i]) > eps )
369  done = false;
370  }
371  tempvals = eigenvals;
372  }
373  } catch ( Exceptions::Exception& e ) {
374  throw(Exceptions::Exception(Exceptions::nonRecoverable,
375  "Could not eigendecompose"+std::string(e.what()),
376  "Matrix<T>::EigenVectors"));
377  }
378 
379  // Return the eigen values and vectors
380  lambdas = eigenvals;
381  return Q;
382 }
383 
384 template<typename T> void Matrix<T>::Fill(const T& value) {
385 
386  size_t i, j;
387  for (i = 0; i < Nrows(); i++)
388  for (j = 0; j < Ncols(); j++)
389  _mat[i][j] = value;
390 }
391 
392 template<typename T> void Matrix<T>::Identity() {
393 
394  Assert::IsSquare("Matrix<T>::Identity", "Matrix", _mat);
395  size_t i, j;
396  for (i = 0; i < Nrows(); i++)
397  for (j = 0; j < Ncols(); j++)
398  _mat[i][j] = (i == j) ? 1 : 0;
399 }
400 
401 template<typename T> Matrix<T> Matrix<T>::Inverse() const {
402 
403  // Initialize a matrix from the base matrix, invert it in place and return
404  Matrix<T> inv(_mat);
405  inv.Invert();
406  return inv;
407 }
408 
409 template<typename T> void Matrix<T>::Invert() {
410 
411  // Check that the matrix to invert is square and non-zero dimensional
412  Assert::IsNotEmpty("Matrix<T>::Invert", "Matrix", _mat);
413  Assert::IsSquare("Matrix<T>::Invert", "Matrix", _mat);
414 
415  // Initialize the augmented matrix by adding an identity to it
416  size_t i, j, k;
417  size_t n = Nrows();
418  for (i = 0; i < n; i++) {
419  _mat[i].resize(2*n);
420  _mat[i][n+i] = 1;
421  }
422 
423  // Perform the Gauss-Jordan elimination
424  size_t pivot;
425  T max, temp;
426  for (k = 0; k < n; k++) {
427 
428  // Find the pivot column, the one with the highest k^th element on the
429  pivot = 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]);
434  pivot = j;
435  }
436  }
437 
438  // If the the pivot element is 0, return that the matrix is singular, increment otherwise
439  if ( !max ) {
440  throw(Exceptions::Exception(Exceptions::nonRecoverable,
441  "The matrix is singular", "Matrix<T>::Invert"));
442  return;
443  }
444 
445  // Swap the k^th and pivot^th lines if not identical
446  if ( pivot != k )
447  std::swap(_mat[pivot], _mat[k]);
448 
449  // Divide the pivot line by its element on the diagonal
450  temp = _mat[k][k];
451  for (i = 0; i < 2*n; i++)
452  _mat[k][i] /= temp;
453 
454  // Simplify all the lines below the current pivot row
455  for (i = 0; i < n; i++) {
456  if ( i != k ) {
457  temp = _mat[i][k];
458  for (j = k+1; j < 2*n; j++)
459  _mat[i][j] -= temp*_mat[k][j];
460  }
461  _mat[i][k] = 0;
462  }
463  }
464 
465  // Remove the first n columns of the augmented matrix
466  for (i = 0; i < n; i++)
467  _mat[i].erase(_mat[i].begin(), _mat[i].begin()+n);
468 }
469 
470 template<typename T> bool Matrix<T>::IsSymmetric() const {
471 
472  Assert::IsSquare("Matrix<T>::IsSymmetric", "Matrix", _mat);
473  size_t i, j;
474  for (i = 0; i < Nrows(); i++)
475  for (j = i+1; j < Ncols(); j++)
476  if ( _mat[i][j] != _mat[j][i] )
477  return false;
478 
479  return true;
480 }
481 
482 template<typename T> const size_t Matrix<T>::Nrows() const {
483  return _mat.size();
484 }
485 
486 template<typename T> const size_t Matrix<T>::Ncols() const {
487  if ( !_mat.size() ) {
488  return _mat.size();
489  } else {
490  return _mat[0].size();
491  }
492 }
493 
494 template<typename T> void Matrix<T>::Print() const {
495  std::cerr << "Matrix (" << Nrows() << ", " << Ncols() << ") is as follows" << std::endl;
496  size_t i, j;
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;
501  }
502 }
503 
504 template<typename T> void Matrix<T>::RemoveColumn(const size_t i) {
505 
506  Assert::IsWithinBounds("Matrix<T>::RemoveColumn", Ncols(), i);
507  size_t j;
508  for (j = 0; j < Nrows(); j++)
509  _mat[j].erase(_mat[j].begin()+i);
510 }
511 
512 template<typename T> void Matrix<T>::RemoveRow(const size_t i) {
513 
514  Assert::IsWithinBounds("Matrix<T>::RemoveRow", Nrows(), i);
515  _mat.erase(_mat.begin()+i);
516 }
517 
518 template<typename T> void Matrix<T>::Resize(const size_t m, const size_t n) {
519 
520  _mat.resize(m);
521  size_t i;
522  for (i = 0; i < m; i++)
523  _mat[i].resize(n);
524 }
525 
526 template<typename T> const std::vector<T>& Matrix<T>::Row(const size_t i) const {
527 
528  Assert::IsWithinBounds("Matrix<T>::Row", Nrows(), i);
529  return _mat[i];
530 }
531 
532 template<typename T> std::vector<T>& Matrix<T>::Row(const size_t i) {
533 
534  Assert::IsWithinBounds("Matrix<T>::Row", Nrows(), i);
535  return _mat[i];
536 }
537 
538 template<typename T> std::vector<T> Matrix<T>::Solve(const std::vector<T>& b) const {
539 
540  // Check that the matrix to invert is square and non-zero dimensional
541  Assert::IsNotEmpty("Matrix<T>::Solve", "Matrix", _mat);
542  Assert::IsSquare("Matrix<T>::Solve", "Matrix", _mat);
543 
544  // Find the LU decomposition of the matrix. If the matrix is singular, do not bother
545  size_t n = Nrows();
546  DecompLU<T> decLU(_mat);
547  if ( !decLU.Complete() ) {
548  throw(Exceptions::Exception(Exceptions::nonRecoverable,
549  "The matrix is singular", "Matrix<T>::Solve"));
550  return std::vector<T>(n);
551  }
552  std::vector<std::vector<T>> L(decLU.L()), U(decLU.U());
553  std::vector<T> pivots(decLU.Pivots());
554 
555  // Take care of the permutation, define a new b'
556  std::vector<T> bprime(n);
557  size_t i;
558  for (i = 0; i < n; i++)
559  bprime[i] = b[pivots[i]];
560 
561  // Solve Ld = b'
562  std::vector<T> x(n);
563  T s;
564  int j, k;
565  for (k = 0; k < (int)n; k++) {
566  s = 0;
567  for (j = 0; j < k; j++)
568  s += L[k][j]*x[j];
569  x[k] = (bprime[k] - s)/L[k][k];
570  }
571 
572  // Back substitution to solve Ux = d
573  for (k = n-1; k > -1; k--) {
574  s = 0;
575  for (j = k+1; j < (int)n; j++)
576  s += U[k][j]*x[j];
577  x[k] = x[k] - s;
578  }
579 
580  return x;
581 }
582 
583 template<typename T> const std::vector<std::vector<T>>& Matrix<T>::std() const {
584 
585  return _mat;
586 }
587 
588 template<typename T> Matrix<T> Matrix<T>::Transpose() const {
589 
590  Matrix<T> trans(Ncols(), Nrows());
591  size_t i, j;
592  for (i = 0; i < Ncols(); i++)
593  for (j = 0; j < Nrows(); j++)
594  trans[i][j] = _mat[j][i];
595 
596  return trans;
597 }
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