GEDLIB  1.0
matrix.ipp
Go to the documentation of this file.
1 /***************************************************************************
2 * *
3 * Copyright (C) 2018 by David B. Blumenthal *
4 * *
5 * This file is part of GEDLIB. *
6 * *
7 * GEDLIB is free software: you can redistribute it and/or modify it *
8 * under the terms of the GNU Lesser General Public License as published *
9 * by the Free Software Foundation, either version 3 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * GEDLIB is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU Lesser General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with GEDLIB. If not, see <http://www.gnu.org/licenses/>. *
19 * *
20 ***************************************************************************/
21 
27 #ifndef MATRIX_IPP
28 #define MATRIX_IPP 1
29 
30 namespace ged {
31 
32 template<class ScalarT>
34 Matrix(std::size_t num_rows, std::size_t num_cols, ScalarT val) :
35 matrix_(Eigen::Matrix<ScalarT, Eigen::Dynamic, Eigen::Dynamic>::Constant(num_rows, num_cols, val)) {}
36 
37 template<class ScalarT>
39 Matrix() :
40 matrix_(){}
41 
42 template<class ScalarT>
44 Matrix(const Matrix<ScalarT> & matrix) :
45 matrix_(matrix.matrix_) {}
46 
47 template<class ScalarT>
48 void
50 operator=(const Matrix & matrix) {
51  matrix_ = matrix.matrix_;
52 }
53 
54 template<class ScalarT>
55 const ScalarT &
57 operator() (std::size_t row, std::size_t col) const {
58  return matrix_(row, col);
59 }
60 
61 template<class ScalarT>
62 ScalarT &
64 operator() (std::size_t row, std::size_t col) {
65  return matrix_(row, col);
66 }
67 
68 template<class ScalarT>
69 ScalarT *
71 data() {
72  return matrix_.data();
73 }
74 
75 template<class ScalarT>
76 const ScalarT *
78 data() const {
79  return matrix_.data();
80 }
81 
82 template<class ScalarT>
83 std::size_t
85 num_rows() const {
86  return static_cast<std::size_t>(matrix_.rows());
87 }
88 
89 template<class ScalarT>
90 void
92 resize(std::size_t num_rows, std::size_t num_cols) {
93  matrix_.resize(num_rows, num_cols);
94 }
95 
96 template<class ScalarT>
97 void
99 set_to_val(const ScalarT & val) {
100  for (std::size_t row{0}; row < num_rows(); row++) {
101  for (std::size_t col{0}; col < num_cols(); col++) {
102  (*this)(row, col) = val;
103  }
104  }
105 }
106 
107 template<class ScalarT>
108 std::size_t
110 num_cols() const {
111  return static_cast<std::size_t>(matrix_.cols());
112 }
113 
114 template<class ScalarT>
115 void
117 power(std::size_t n) {
118  if (n <= 1) {
119  return;
120  }
121  Eigen::Matrix<ScalarT, Eigen::Dynamic, Eigen::Dynamic> temp = Eigen::Matrix<ScalarT, Eigen::Dynamic, Eigen::Dynamic>::Ones(num_rows(), num_cols());
122  while (n > 1) {
123  if (n % 2 == 0) {
124  matrix_ *= matrix_;
125  n /= 2;
126  }
127  else {
128  temp *= matrix_;
129  matrix_ *= matrix_;
130  n--;
131  n /= 2;
132  }
133  }
134  matrix_ *= temp;
135 }
136 
137 template<class ScalarT>
138 void
141  matrix_.transpose();
142 }
143 
144 template<class ScalarT>
147 transposed() const {
148  Matrix<ScalarT> transposed_matrix(*this);
149  transposed_matrix.transpose();
150  return transposed_matrix;
151 }
152 
153 template<class ScalarT>
154 void
156 swap(Matrix & rhs) {
157  std::swap(matrix_, rhs.matrix_);
158 }
159 
160 template<class ScalarT>
161 ScalarT
163 max() const {
164  return matrix_.maxCoeff();
165 }
166 
167 template<class ScalarT>
168 ScalarT
170 min() const {
171  return matrix_.minCoeff();
172 }
173 
174 template<class ScalarT>
177 operator*=(const ScalarT & scalar) {
178  matrix_ *= scalar;
179  return (*this);
180 }
181 
182 template<class ScalarT>
185 operator+=(const Matrix<ScalarT> & matrix) {
186  matrix_ += matrix.matrix_;
187  return (*this);
188 }
189 
190 template<class ScalarT>
193 operator-=(const Matrix<ScalarT> & matrix) {
194  matrix_ -= matrix.matrix_;
195  return (*this);
196 }
197 
198 template<class ScalarT>
201 operator*(const ScalarT & scalar) const {
202  Matrix<ScalarT> new_matrix(*this);
203  new_matrix.matrix_ *= scalar;
204  return new_matrix;
205 }
206 
207 template<class ScalarT>
210 operator+(const Matrix<ScalarT> & matrix) const {
211  Matrix<ScalarT> new_matrix(*this);
212  new_matrix.matrix_ += matrix.matrix_;
213  return new_matrix;
214 }
215 
216 template<class ScalarT>
219 operator-(const Matrix<ScalarT> & matrix) const {
220  Matrix<ScalarT> new_matrix(*this);
221  new_matrix.matrix_ -= matrix.matrix_;
222  return new_matrix;
223 }
224 
225 template<class ScalarT>
226 Eigen::Matrix<ScalarT, Eigen::Dynamic, Eigen::Dynamic> &
229  return matrix_;
230 }
231 
232 template<class ScalarT>
233 const Eigen::Matrix<ScalarT, Eigen::Dynamic, Eigen::Dynamic> &
235 matrix() const {
236  return matrix_;
237 }
238 
239 template<class ScalarT>
240 std::ostream & operator<<(std::ostream & os, const Matrix<ScalarT> & matrix) {
241  os << matrix.matrix();
242  return os;
243 }
244 
245 }
246 
247 #endif
A matrix class with basic functionality.
Definition: matrix.hpp:38
void resize(std::size_t num_rows, std::size_t num_cols)
Resizes the matrix.
Definition: matrix.ipp:92
Matrix()
Constructs an empty cost matrix.
Definition: matrix.ipp:39
Eigen::Matrix< ScalarT, Eigen::Dynamic, Eigen::Dynamic > & matrix()
Returns reference to the internal Eigen matrix.
Definition: matrix.ipp:228
Global namespace for GEDLIB.
Implements constant edit cost functions.
Definition: constant.hpp:38
void transpose()
Transposes the matrix.
Definition: matrix.ipp:140
ScalarT * data()
Provides access to internal data.
Definition: matrix.ipp:71