MeteoIODoc 20240503.aefd3c94
Matrix.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-3.0-or-later
2/***********************************************************************************/
3/* Copyright 2010 WSL Institute for Snow and Avalanche Research SLF-DAVOS */
4/***********************************************************************************/
5/* This file is part of MeteoIO.
6 MeteoIO is free software: you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 MeteoIO is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with MeteoIO. If not, see <http://www.gnu.org/licenses/>.
18*/
19#ifndef MATRIX_H
20#define MATRIX_H
21
22#include <vector>
23#include <iostream>
24
25namespace mio {
26
42class Matrix {
43 public:
44 Matrix() : vecData(), ncols(0), nrows(0) {}
45
51 Matrix(const int& rows, const int& cols);
52 Matrix(const size_t& rows, const size_t& cols) : vecData(rows*cols), ncols(cols), nrows(rows) {}
53
60 Matrix(const size_t& rows, const size_t& cols, const double& init) : vecData(rows*cols, init), ncols(cols), nrows(rows) {}
61
67 Matrix(const size_t& n, const double& init);
68
75 Matrix(const size_t& rows, const size_t& cols, const std::vector<double> data) : vecData(data), ncols(cols), nrows(rows) {}
76
82 void identity(const size_t& n, const double& init = 1.);
83
84 void resize(const size_t& rows, const size_t& cols);
85 void resize(const size_t& rows, const size_t& cols, const double& init);
86 void resize(const size_t& rows, const size_t& cols, const std::vector<double>& data);
87
93 void size(size_t& rows, size_t& cols) const;
94
95 size_t getNx() const {return ncols;}
96 size_t getNy() const {return nrows;}
97
101 void clear();
102
107 void random(const double& range);
108
109 double& operator ()(const size_t& x, const size_t& y);
110 double operator ()(const size_t& x, const size_t& y) const;
111
116 double scalar() const;
117 static double scalar(const Matrix& m);
118
123 static double dot(const Matrix& A, const Matrix& B);
124
129 Matrix getT() const;
130 static Matrix T(const Matrix& m);
131 void T();
132
143 Matrix getInv() const;
144 bool inv();
145
151 Matrix getRow(const size_t i) const;
152
158 void setRow(const size_t i, const Matrix& row);
159
165 Matrix getCol(const size_t j) const;
166
172 void setCol(const size_t j, const Matrix& col);
173
178 Matrix getDiagonal() const;
179
190 Matrix extract(size_t r_low, size_t r_high, size_t c_low, size_t c_high) const;
191
198 double maxCoeff(size_t& max_row, size_t& max_col) const;
199
208 static Matrix solve(const Matrix& A, const Matrix& B);
209
219 static bool solve(const Matrix& A, const Matrix& B, Matrix& X);
220
231 static Matrix TDMA_solve(const Matrix& A, const Matrix& B);
232
244 static bool TDMA_solve(const Matrix& A, const Matrix& B, Matrix& X);
245
250 double det() const;
251
260 static void gaussElimination(Matrix& M, std::vector<size_t>& p);
261
272 static bool gaussSolve(Matrix& M, Matrix& A, Matrix& X);
273
283 static bool gaussSolve(const Matrix& M, const Matrix& A, Matrix& X);
284
292 static bool gaussInverse(Matrix& M);
293
301 static bool gaussInverse(const Matrix& M, Matrix& Inv);
302
310 static double gaussDet(Matrix& M);
311
318 static double normEuclid(const Matrix& vv);
319
328 bool LU(Matrix& L, Matrix& U) const;
329
336 void partialPivoting(std::vector<size_t>& pivot_idx);
337 void partialPivoting();
338
339 void maximalPivoting();
340
346 //void bidiagonalize();
347
357 static unsigned int eigenvaluesJacobi(Matrix& AA, Matrix& DD);
358 static double jacobiEpsilon(Matrix& AA);
359
369 static void svdJacobi(const Matrix& MM, Matrix& UU, Matrix& SS, Matrix& VV);
370 static void sortEigenvalues(Matrix& EE, Matrix& VV);
371 void swapCols(const size_t &j1, const size_t &j2); //used by svd
372
373 const std::string toString(const int& precision=2, const bool& prettify=true) const;
374
375 Matrix& operator+=(const Matrix& rhs);
376 const Matrix operator+(const Matrix& rhs) const;
377 Matrix& operator+=(const double& rhs);
378 const Matrix operator+(const double& rhs) const;
379
380 Matrix& operator-=(const Matrix& rhs);
381 const Matrix operator-(const Matrix& rhs) const;
382 Matrix& operator-=(const double& rhs);
383 const Matrix operator-(const double& rhs) const;
384
385 Matrix& operator*=(const Matrix& rhs);
386 const Matrix operator*(const Matrix& rhs) const;
387 Matrix& operator*=(const double& rhs);
388 const Matrix operator*(const double& rhs) const;
389
390 Matrix& operator/=(const double& rhs);
391 const Matrix operator/(const double& rhs) const;
392
393 bool operator==(const Matrix&) const;
394 bool operator!=(const Matrix&) const;
395
400 bool isIdentity() const;
401 static bool isIdentity(const Matrix& A);
402
403 static const double epsilon, epsilon_mtr;
404
405 protected:
406 std::vector<double> vecData;
407 size_t ncols;
408 size_t nrows;
409
410 size_t findMaxInCol(const size_t &col);
411 size_t findMaxInRow(const size_t &row);
412 void swapRows(const size_t &i1, const size_t &i2);
413
414};
415
416} //end namespace
417
418#endif
This class implements the basic operations on matrices. Elements are access in matrix notation: that ...
Definition: Matrix.h:42
const Matrix operator+(const Matrix &rhs) const
Definition: Matrix.cc:213
static Matrix TDMA_solve(const Matrix &A, const Matrix &B)
Solving system of equations using Thomas Algorithm The following function solves a A·X=B with X and B...
Definition: Matrix.cc:803
Matrix getDiagonal() const
Copy the matrix diagonal to a new 1-column-matrix.
Definition: Matrix.cc:649
static double dot(const Matrix &A, const Matrix &B)
Dot product.
Definition: Matrix.cc:362
bool operator!=(const Matrix &) const
Operator that tests for inequality.
Definition: Matrix.cc:190
static bool gaussSolve(Matrix &M, Matrix &A, Matrix &X)
Equation system solver via Gaussian elimination. This function solves a set of equations M·X=A via Ga...
Definition: Matrix.cc:841
Matrix(const size_t &rows, const size_t &cols, const double &init)
A constructor that creates a matrix filled with constant values.
Definition: Matrix.h:60
bool LU(Matrix &L, Matrix &U) const
matrix LU decomposition. Perform LU decomposition by the Dolittle algorithm, (cf http://math....
Definition: Matrix.cc:430
Matrix()
Definition: Matrix.h:44
static void gaussElimination(Matrix &M, std::vector< size_t > &p)
Gaussian elimimination with partial pivoting. This function performs row reduction with (virtual) row...
Definition: Matrix.cc:813
double scalar() const
Converts a 1x1 matrix to a scalar.
Definition: Matrix.cc:351
bool operator==(const Matrix &) const
Operator that tests for equality.
Definition: Matrix.cc:176
Matrix getCol(const size_t j) const
Copy a matrix column to a new 1-column-matrix.
Definition: Matrix.cc:614
static Matrix solve(const Matrix &A, const Matrix &B)
matrix solving for A·X=B. It first performs LU decomposition and then solves A·X=B by backward and fo...
Definition: Matrix.cc:745
Matrix & operator+=(const Matrix &rhs)
Definition: Matrix.cc:195
std::vector< double > vecData
Definition: Matrix.h:406
void setCol(const size_t j, const Matrix &col)
Set a matrix column from a 1-column-matrix.
Definition: Matrix.cc:630
bool inv()
Definition: Matrix.cc:524
double & operator()(const size_t &x, const size_t &y)
Definition: Matrix.cc:122
void clear()
free the memory and set the matrix dimensions to (0,0)
Definition: Matrix.cc:108
void T()
Definition: Matrix.cc:407
size_t getNy() const
Definition: Matrix.h:96
static double jacobiEpsilon(Matrix &AA)
Definition: Matrix.cc:1148
void resize(const size_t &rows, const size_t &cols)
Definition: Matrix.cc:71
const Matrix operator/(const double &rhs) const
Definition: Matrix.cc:338
const Matrix operator*(const Matrix &rhs) const
Definition: Matrix.cc:308
size_t ncols
Definition: Matrix.h:407
Matrix & operator*=(const Matrix &rhs)
Definition: Matrix.cc:279
static void svdJacobi(const Matrix &MM, Matrix &UU, Matrix &SS, Matrix &VV)
Singular Value Decomposition. This function calculates the SVD of a matrix. It uses an eigenvector se...
Definition: Matrix.cc:1161
Matrix getInv() const
matrix invert. It first performs LU decomposition (Dolittle algorithm) and then computes the inverse ...
Definition: Matrix.cc:468
static const double epsilon
Definition: Matrix.h:403
Matrix getRow(const size_t i) const
Copy a matrix row to a new 1-row-matrix.
Definition: Matrix.cc:579
Matrix & operator-=(const Matrix &rhs)
Definition: Matrix.cc:238
Matrix(const size_t &rows, const size_t &cols, const std::vector< double > data)
A constructor that takes a data vector to fill the matrix.
Definition: Matrix.h:75
Matrix(const size_t &rows, const size_t &cols)
Definition: Matrix.h:52
void maximalPivoting()
Definition: Matrix.cc:997
static const double epsilon_mtr
Definition: Matrix.h:403
double det() const
matrix determinant
Definition: Matrix.cc:413
static double normEuclid(const Matrix &vv)
Calculate Euclidean norm (l2) for a vector. The matrix must either be a row or a column vector.
Definition: Matrix.cc:922
bool isIdentity() const
check if a matrix is the identity matrix
Definition: Matrix.cc:938
static void sortEigenvalues(Matrix &EE, Matrix &VV)
Definition: Matrix.cc:1181
void random(const double &range)
fill the matrix with random numbers.
Definition: Matrix.cc:114
double maxCoeff(size_t &max_row, size_t &max_col) const
Find the first maximum value in the matrix.
Definition: Matrix.cc:1057
static bool gaussInverse(Matrix &M)
Matrix inversion via Gaussian elimination. Attention: The original matrix is destroyed!
Definition: Matrix.cc:885
size_t findMaxInRow(const size_t &row)
Definition: Matrix.cc:1042
void size(size_t &rows, size_t &cols) const
get the dimensions of the current object
Definition: Matrix.cc:102
void swapCols(const size_t &j1, const size_t &j2)
Definition: Matrix.cc:1082
void partialPivoting()
Definition: Matrix.cc:991
static double gaussDet(Matrix &M)
Get the matrix determinant via Gaussian elimination. Attention: The original matrix is destroyed!
Definition: Matrix.cc:903
size_t findMaxInCol(const size_t &col)
Definition: Matrix.cc:1026
size_t getNx() const
Definition: Matrix.h:95
Matrix extract(size_t r_low, size_t r_high, size_t c_low, size_t c_high) const
Extract a submatrix to a new matrix. IOUtils::npos for an index means "from beginning" (i....
Definition: Matrix.cc:665
Matrix & operator/=(const double &rhs)
Definition: Matrix.cc:332
static unsigned int eigenvaluesJacobi(Matrix &AA, Matrix &DD)
matrix bidiagonalization This uses Householder's reduction, see Golub, 1970. (see https://secure....
Definition: Matrix.cc:1091
Matrix getT() const
matrix transpose.
Definition: Matrix.cc:396
void setRow(const size_t i, const Matrix &row)
Set a matrix row from a 1-row-matrix.
Definition: Matrix.cc:595
const Matrix operator-(const Matrix &rhs) const
Definition: Matrix.cc:256
size_t nrows
Definition: Matrix.h:408
void swapRows(const size_t &i1, const size_t &i2)
Definition: Matrix.cc:1073
void identity(const size_t &n, const double &init=1.)
Convert the current matrix to a identity matrix of size n.
Definition: Matrix.cc:65
const std::string toString(const int &precision=2, const bool &prettify=true) const
Definition: Matrix.cc:146
Definition: Config.cc:31