MeteoIODoc  MeteoIODoc-2.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Matrix.h
Go to the documentation of this file.
1 /***********************************************************************************/
2 /* Copyright 2010 WSL Institute for Snow and Avalanche Research SLF-DAVOS */
3 /***********************************************************************************/
4 /* This file is part of MeteoIO.
5  MeteoIO is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  MeteoIO is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with MeteoIO. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #ifndef MATRIX_H
19 #define MATRIX_H
20 
21 #include <vector>
22 #include <iostream>
23 
24 namespace mio {
25 
41 class Matrix {
42  public:
43  Matrix() : vecData(), ncols(0), nrows(0) {}
44 
50  Matrix(const int& rows, const int& cols);
51  Matrix(const size_t& rows, const size_t& cols) : vecData(rows*cols), ncols(cols), nrows(rows) {}
52 
59  Matrix(const size_t& rows, const size_t& cols, const double& init) : vecData(rows*cols, init), ncols(cols), nrows(rows) {}
60 
66  Matrix(const size_t& n, const double& init);
67 
72  Matrix(const Matrix& init) : vecData(init.vecData), ncols(init.ncols), nrows(init.nrows) {}
73 
79  void identity(const size_t& n, const double& init);
80 
81  void resize(const size_t& rows, const size_t& cols);
82  void resize(const size_t& rows, const size_t& cols, const double& init);
83 
89  void size(size_t& rows, size_t& cols) const;
90 
94  void clear();
95 
100  void random(const double& range);
101 
102  double& operator ()(const size_t& x, const size_t& y);
103  double operator ()(const size_t& x, const size_t& y) const;
104 
109  double scalar() const;
110  static double scalar(const Matrix& m);
111 
116  static double dot(const Matrix& A, const Matrix& B);
117 
122  Matrix getT() const;
123  static Matrix T(const Matrix& m);
124  void T();
125 
133  Matrix getInv() const;
134  bool inv();
135 
144  static Matrix solve(const Matrix& A, const Matrix& B);
145 
155  static bool solve(const Matrix& A, const Matrix& B, Matrix& X);
156 
167  static Matrix TDMA_solve(const Matrix& A, const Matrix& B);
168 
180  static bool TDMA_solve(const Matrix& A, const Matrix& B, Matrix& X);
181 
186  double det() const;
187 
196  bool LU(Matrix& L, Matrix& U) const;
197 
204  void partialPivoting(std::vector<size_t>& pivot_idx);
205  void partialPivoting();
206 
207  void maximalPivoting();
208 
214  //void bidiagonalize();
215 
216  const std::string toString() const;
217 
218  Matrix& operator+=(const Matrix& rhs);
219  const Matrix operator+(const Matrix& rhs) const;
220  Matrix& operator+=(const double& rhs);
221  const Matrix operator+(const double& rhs) const;
222 
223  Matrix& operator-=(const Matrix& rhs);
224  const Matrix operator-(const Matrix& rhs) const;
225  Matrix& operator-=(const double& rhs);
226  const Matrix operator-(const double& rhs) const;
227 
228  Matrix& operator*=(const Matrix& rhs);
229  const Matrix operator*(const Matrix& rhs) const;
230  Matrix& operator*=(const double& rhs);
231  const Matrix operator*(const double& rhs) const;
232 
233  Matrix& operator/=(const double& rhs);
234  const Matrix operator/(const double& rhs) const;
235 
236  bool operator==(const Matrix&) const;
237  bool operator!=(const Matrix&) const;
238 
243  bool isIdentity() const;
244  static bool isIdentity(const Matrix& A);
245 
246  static const double epsilon, epsilon_mtr;
247 
248  protected:
249  std::vector<double> vecData;
250  size_t ncols;
251  size_t nrows;
252 
253  size_t findMaxInCol(const size_t &col);
254  size_t findMaxInRow(const size_t &row);
255  void swapRows(const size_t &i1, const size_t &i2);
256 };
257 
258 } //end namespace
259 
260 #endif
void partialPivoting()
Definition: Matrix.cc:683
double det() const
matrix determinant
Definition: Matrix.cc:341
double scalar() const
Converts a 1x1 matrix to a scalar.
Definition: Matrix.cc:284
Matrix & operator+=(const Matrix &rhs)
Definition: Matrix.cc:143
bool LU(Matrix &L, Matrix &U) const
matrix LU decomposition. Perform LU decomposition by the Dolittle algorithm, (cf http://math.fullerton.edu/mathews/numerical/linear/dol/dol.html)
Definition: Matrix.cc:357
Matrix()
Definition: Matrix.h:43
const Matrix operator/(const double &rhs) const
Definition: Matrix.cc:273
Matrix & operator*=(const Matrix &rhs)
Definition: Matrix.cc:219
const std::string toString() const
matrix bidiagonalization This uses Householder's reduction, see Golub, 1970. (see https://secure...
Definition: Matrix.cc:105
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:59
bool operator==(const Matrix &) const
Operator that tests for equality.
Definition: Matrix.cc:126
static const double epsilon_mtr
Definition: Matrix.h:246
static double dot(const Matrix &A, const Matrix &B)
Dot product.
Definition: Matrix.cc:294
void size(size_t &rows, size_t &cols) const
get the dimensions of the current object
Definition: Matrix.cc:66
void swapRows(const size_t &i1, const size_t &i2)
Definition: Matrix.cc:746
size_t findMaxInCol(const size_t &col)
Definition: Matrix.cc:716
const Matrix operator+(const Matrix &rhs) const
Definition: Matrix.cc:160
size_t findMaxInRow(const size_t &row)
Definition: Matrix.cc:731
bool inv()
Definition: Matrix.cc:450
const Matrix operator-(const Matrix &rhs) const
Definition: Matrix.cc:199
bool isIdentity() const
check if a matrix is the identity matrix
Definition: Matrix.cc:633
Matrix(const size_t &rows, const size_t &cols)
Definition: Matrix.h:51
Matrix(const Matrix &init)
Copy constructor.
Definition: Matrix.h:72
size_t ncols
Definition: Matrix.h:250
void maximalPivoting()
Definition: Matrix.cc:688
static const double epsilon
Definition: Matrix.h:246
Matrix getT() const
matrix transpose.
Definition: Matrix.cc:326
void resize(const size_t &rows, const size_t &cols)
Definition: Matrix.cc:52
void identity(const size_t &n, const double &init)
Convert the current matrix to a identity matrix of size n.
Definition: Matrix.cc:47
double & operator()(const size_t &x, const size_t &y)
Definition: Matrix.cc:83
void T()
Definition: Matrix.cc:336
Matrix getInv() const
matrix invert. It first performs LU decomposition and then computes the inverse by backward and forwa...
Definition: Matrix.cc:395
void clear()
free the memory and set the matrix dimensions to (0,0)
Definition: Matrix.cc:71
bool operator!=(const Matrix &) const
Operator that tests for inequality.
Definition: Matrix.cc:139
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:624
size_t nrows
Definition: Matrix.h:251
std::vector< double > vecData
Definition: Matrix.h:249
Matrix & operator-=(const Matrix &rhs)
Definition: Matrix.cc:182
const Matrix operator*(const Matrix &rhs) const
Definition: Matrix.cc:247
This class implements the basic operations on matrices. Elements are access in matrix notation: that ...
Definition: Matrix.h:41
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:566
void random(const double &range)
fill the matrix with random numbers.
Definition: Matrix.cc:76
Matrix & operator/=(const double &rhs)
Definition: Matrix.cc:268