Unfit  3.1.1
Data fitting and optimization software
Matrix.hpp
1 // Unfit: Data fitting and optimization software
2 //
3 // Copyright (C) 2012- Dr Martin Buist & Dr Alberto Corrias
4 // Contacts: martin.buist _at_ nus.edu.sg; alberto _at_ nus.edu.sg
5 //
6 // See the 'Contributors' file for a list of those who have contributed
7 // to this work.
8 //
9 // This program is free software: you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 3 of the License, or
12 // (at your option) any later version.
13 //
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with this program. If not, see <http://www.gnu.org/licenses/>.
21 //
22 #ifndef UNFIT_INCLUDE_MATRIX_HPP_
23 #define UNFIT_INCLUDE_MATRIX_HPP_
24 
25 #include <vector>
26 
27 namespace Unfit
28 {
33 class Matrix
34 {
35  public:
41  Matrix();
42 
53  Matrix(std::size_t num_rows, std::size_t num_cols, double the_value);
54 
68  Matrix(std::size_t num_rows, std::size_t num_cols,
69  const std::vector<double> &the_values);
70 
78  void Assign(std::size_t num_rows, std::size_t num_cols, double the_value);
79 
87  void Assign(std::size_t num_rows, std::size_t num_cols,
88  const std::vector<double> &matrix_values);
89 
98  double GetValue(std::size_t row, std::size_t col) const noexcept;
99 
108  void SetValue(std::size_t row, std::size_t col, double the_value);
109 
116  std::size_t GetNumberOfColumns() const noexcept;
117 
124  std::size_t GetNumberOfRows() const noexcept;
125 
131  std::size_t Size() const noexcept;
132 
138  bool Empty() const noexcept;
139 
161  void AddConstantToDiagonal(double value, std::size_t offset = 0,
162  bool upper_triangle = true);
163 
170  std::vector<double> values_;
171 
172  private:
174  std::size_t number_of_rows_;
176  std::size_t number_of_columns_;
177 };
178 
179 // Here are some functions that operate on matrices and vectors that we need.
180 // For simplicity these have all been collected here.
181 
190 std::vector<double> InnerProduct(const Matrix &m, const std::vector<double> &v);
191 
200 double InnerProduct(const std::vector<double> &v1,
201  const std::vector<double> &v2);
202 
215 Matrix InnerProduct(const Matrix &m, bool transpose);
216 
225 std::vector<double> Scale(const std::vector<double> &v,
226  const double scale_factor);
227 
238 std::vector<double> AddSubtractVectors(const std::vector<double> &v1,
239  const std::vector<double> &v2, const bool isadd = true);
240 
253 std::vector<double> AddScaleVectors(const std::vector<double> &v1,
254  const std::vector<double> &v2, const double scale_factor);
255 
262 Matrix Transpose(Matrix &m);
263 
271 double FindL2NormOfVector(const std::vector<double> &v);
272 
280 double SumOfSquares(const std::vector<double> &v);
281 
292 double MaxDiagonalMat(const Matrix &m);
293 
294 } // namespace Unfit
295 
296 #endif
std::size_t GetNumberOfRows() const noexcept
Definition: Matrix.cpp:99
std::size_t GetNumberOfColumns() const noexcept
Definition: Matrix.cpp:94
std::size_t number_of_columns_
Definition: Matrix.hpp:176
Matrix()
Definition: Matrix.cpp:30
Definition: Matrix.hpp:33
Definition: Bounds.hpp:27
std::size_t number_of_rows_
Definition: Matrix.hpp:174
std::vector< double > values_
Definition: Matrix.hpp:170
bool Empty() const noexcept
Definition: Matrix.cpp:109
void SetValue(std::size_t row, std::size_t col, double the_value)
Definition: Matrix.cpp:87
double GetValue(std::size_t row, std::size_t col) const noexcept
Definition: Matrix.cpp:81
void AddConstantToDiagonal(double value, std::size_t offset=0, bool upper_triangle=true)
Definition: Matrix.cpp:114
void Assign(std::size_t num_rows, std::size_t num_cols, double the_value)
Definition: Matrix.cpp:58
std::size_t Size() const noexcept
Definition: Matrix.cpp:104