From c7dad1e929c0101386fbcdaa194e402d68f74752 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Wed, 23 Aug 2017 22:56:53 +0100 Subject: Updating docs --- matrix_8hpp_source.html | 90 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 67 insertions(+), 23 deletions(-) (limited to 'matrix_8hpp_source.html') diff --git a/matrix_8hpp_source.html b/matrix_8hpp_source.html index 2d51d501..7e0cdd85 100644 --- a/matrix_8hpp_source.html +++ b/matrix_8hpp_source.html @@ -9,6 +9,13 @@ + + + + + @@ -44,6 +51,21 @@ $(function() { }); + +
+ +
+
+
+ +
- -
matrix.hpp
-Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2  * matrix.hpp
3  *
4  * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com> -- MIT License
5  * See file LICENSE for more details
6  * ----------------------------------------------------------------------------
7  */
8 
19 #ifndef YAGE_MATH_MATRIX_HPP
20 #define YAGE_MATH_MATRIX_HPP
21 
22 #include <algorithm>
23 #include <exception>
24 #include <iostream>
25 #include <sstream>
26 #include <string>
27 #include <vector>
28 
29 namespace yage {
30 
31 template <int Rows, int Cols, class Type>
32 class Matrix;
33 
41 namespace detail {
42 
51 template <int Rows, int Cols, class Type>
52 class Row {
53 private:
54  Matrix<Rows, Cols, Type>* parent_;
55  int index_;
56 
57 public:
58  Row<Rows, Cols, Type>(Matrix<Rows, Cols, Type>* parent, int index)
59  : parent_(parent), index_(index) {}
60 
61  Type& operator[](int col) {
62  // the index is the y-position of the element in the matrix
63  return parent_->data_[index_ * Cols + col];
64  }
65 
66  const Type& operator[](int col) const {
67  return parent_->data_[index_ * Cols + col];
68  }
69 };
70 
71 } // detail
72 
81 template <int Rows = 4, int Cols = 4, class Type = double>
82 class Matrix {
83  // friended with the row class so that it can access protected member data
84  friend class detail::Row<Rows, Cols, Type>;
85 
86 protected:
88  std::vector<Type> data_;
89 
90 public:
92  Matrix<Rows, Cols, Type>() : data_(Rows * Cols) {}
93  Matrix<Rows, Cols, Type>(const std::vector<Type>& data) : data_(data) {}
94 
96  int rowSize() const { return Rows; }
97 
99  int colSize() const { return Cols; }
100 
107  Matrix<1, Cols, Type> getRow(int row) const {
108  Matrix<1, Cols, Type> rowMatrix;
109  for (int i = 0; i < Cols; ++i) {
110  rowMatrix[0][i] = data_[row][i];
111  }
112  return rowMatrix;
113  }
114 
115  // returns the column in a column matrix
116  Matrix<Rows, 1, Type> getCol(int col) const {
117  Matrix<Rows, 1, Type> colMatrix;
118  for (int i = 0; i < Rows; ++i) {
119  colMatrix[i][0] = data_[i][col];
120  }
121  return colMatrix;
122  }
123 
125  typename std::vector<Type>::iterator begin() { return data_.begin(); }
126 
128  typename std::vector<Type>::iterator end() { return data_.end(); }
129 
133  virtual std::string toString() const {
134  std::stringstream ss;
135  ss << '[';
136  for (int i = 0; i < Rows - 1; ++i) {
137  ss << '[';
138  for (int j = 0; j < Cols - 1; ++j) {
139  ss << data_[i * Cols + j] << ' ';
140  }
141  ss << data_[(Rows - 1) * Cols + Cols - 1] << "],";
142  }
143  ss << '[';
144  for (int j = 0; j < Cols - 1; ++j) {
145  ss << data_[(Rows - 1) * Cols + j] << ' ';
146  }
147  ss << data_[(Rows - 1) * Cols + Cols - 1] << "]]";
148  return ss.str();
149  }
150 
151  detail::Row<Rows, Cols, Type> operator[](int row) {
152  return detail::Row<Rows, Cols, Type>(this, row);
153  }
154 
155  detail::Row<Rows, Cols, Type> operator[](int row) const {
156  // TODO got to fix this
157  return detail::Row<Rows, Cols, Type>((Matrix<Rows, Cols, Type>*)this,
158  row);
159  }
160 
161  Matrix<Rows, Cols, Type>& operator+=(const Matrix<Rows, Cols, Type>& rhs) {
162  std::vector<Type> out;
163  out.reserve(data_.size());
164  std::transform(data_.begin(), data_.end(), rhs.data_.begin(),
165  std::back_inserter(out),
166  [](Type a, Type b) { return a + b; });
167  data_ = std::move(out);
168  return *this;
169  }
170 
171  Matrix<Rows, Cols, Type>& operator-=(const Matrix<Rows, Cols, Type>& rhs) {
172  std::vector<Type> out;
173  out.reserve(data_.size());
174  std::transform(data_.begin(), data_.end(), rhs.begin(),
175  std::back_inserter(out),
176  [](Type a, Type b) { return a - b; });
177  data_ = std::move(out);
178  return *this;
179  }
180 };
181 
182 template <int M, int N, class T>
183 Matrix<M, N, T> operator+(Matrix<M, N, T> lhs, const Matrix<M, N, T>& rhs) {
184  lhs += rhs;
185  return lhs;
186 }
187 
188 template <int M, int N, class T>
189 Matrix<M, N, T> operator-(Matrix<M, N, T> lhs, const Matrix<M, N, T>& rhs) {
190  lhs -= rhs;
191  return lhs;
192 }
193 
194 template <int M, int N, class T>
195 Matrix<M, N, T> operator+(Matrix<M, N, T> lhs, const T& rhs) {
196  for (auto& data : lhs) {
197  data += rhs;
198  }
199  return lhs;
200 }
201 
202 template <int M, int N, class T>
203 Matrix<M, N, T> operator+(const T& lhs, Matrix<M, N, T> rhs) {
204  for (auto& data : rhs) {
205  data += lhs;
206  }
207  return rhs;
208 }
209 
210 template <int M, int N, class T>
211 Matrix<M, N, T> operator-(Matrix<M, N, T> lhs, const T& rhs) {
212  for (auto& data : lhs) {
213  data -= rhs;
214  }
215  return lhs;
216 }
217 
218 template <int M, int N, class T>
219 Matrix<M, N, T> operator-(const T& lhs, Matrix<M, N, T> rhs) {
220  for (auto& data : rhs) {
221  data = lhs - data;
222  }
223  return rhs;
224 }
225 
226 template <int M, int N, class T>
227 Matrix<M, N, T> operator*(Matrix<M, N, T> lhs, const T& rhs) {
228  for (auto& data : lhs) {
229  data *= rhs;
230  }
231  return lhs;
232 }
233 
234 template <int M, int N, class T>
235 Matrix<M, N, T> operator*(const T& lhs, Matrix<M, N, T> rhs) {
236  for (auto& data : rhs) {
237  data *= lhs;
238  }
239  return rhs;
240 }
241 
242 template <int M, int N, class T>
243 Matrix<M, N, T> operator/(Matrix<M, N, T> lhs, const T& rhs) {
244  for (auto& data : lhs) {
245  data /= rhs;
246  }
247  return lhs;
248 }
249 
250 template <int M, int N, class T>
251 bool operator==(const Matrix<M, N, T>& lhs, const Matrix<M, N, T>& rhs) {
252  for (int i = 0; i < M; ++i)
253  for (int j = 0; j < N; ++j)
254  if (lhs[i][j] != rhs[i][j]) return false;
255  return true;
256 }
257 
258 template <int M, int N, class T>
259 std::ostream& operator<<(std::ostream& os, const Matrix<M, N, T>& mat) {
260  return os << mat.toString();
261 }
262 
263 template <int Rows = 2, class Type = double>
264 class Vector : public Matrix<Rows, 1, Type> {
265 public:
266  Vector<Rows, Type>() : Matrix<Rows, 1, Type>() {}
267  Vector<Rows, Type>(const Matrix<Rows, 1, Type>& other)
268  : Matrix<Rows, 1, Type>(other) {}
269  Vector<Rows, Type>(const std::vector<Type>& data)
270  : Matrix<Rows, 1, Type>(data) {}
271 
272  Type& operator[](int col) { return this->data_[col]; }
273 
274  const Type& operator[](int col) const { return this->data_[col]; }
275 
276  virtual std::string toString() const {
277  std::stringstream ss;
278  ss << "[";
279  for (std::size_t i = 0; i < this->data_.size() - 1; ++i) {
280  ss << this->data_[i] << " ";
281  }
282  ss << this->data_[this->data_.size() - 1] << "]";
283  return ss.str();
284  }
285 };
286 
291 template <class Type = double>
292 class Vector2 : public Vector<2, Type> {
293 public:
294  Vector2<Type>() : Vector<2, Type>() {}
295  Vector2<Type>(const std::vector<Type>& data) : Vector<2, Type>(data) {}
296 
297  Vector2<Type>(Type x, Type y) {
298  this->data_[0] = x;
299  this->data_[1] = y;
300  }
301 
302  Vector2<Type>(const Matrix<2, 1, Type>& other) : Vector<2, Type>(other) {}
303 
304  Type& x() { return this->data_[0]; }
305 
306  const Type& x() const { return this->data_[0]; }
307 
308  Type& y() { return this->data_[1]; }
309 
310  const Type& y() const { return this->data_[1]; }
311 };
312 
315 
317 namespace matrix {
318 
323 template <int M, int N, class T>
325  Matrix<N, M, T> trans;
326  for (int i = 0; i < M; ++i) {
327  for (int j = 0; j < N; ++j) {
328  trans[j][i] = m[i][j];
329  }
330  }
331  return trans;
332 }
333 
338 template <int R, class T>
339 T dot(const Matrix<R, 1, T>& m1, const Matrix<R, 1, T>& m2) {
340  T sum = 0;
341  for (int i = 0; i < R; ++i) {
342  sum += m1[i][0] * m2[i][0];
343  }
344  return sum;
345 }
346 
353 template <int M, int N, int P, int Q, class T>
355  if (N != P) {
356  throw std::runtime_error(
357  "Matrices don't have the right dimensions for multiplication");
358  }
359 
360  Matrix<M, Q, T> res;
361 
362  for (int i = 0; i < M; ++i) {
363  for (int j = 0; j < Q; ++j) {
364  res[i][j] = dot(transpose(m1.getRow(i)), m2.getCol(j));
365  }
366  }
367 
368  return res;
369 }
370 
371 } // matrix
372 
373 } // yage
374 
375 #endif
int rowSize() const
Returns the row size of the Matrix.
Definition: matrix.hpp:96
-
int colSize() const
Returns the column size of the Matrixxs.
Definition: matrix.hpp:99
-
2D Vector class.
Definition: matrix.hpp:292
-
std::vector< Type >::iterator end()
iterator support for end
Definition: matrix.hpp:128
-
std::vector< Type > data_
Vector containing the data of the matrix.
Definition: matrix.hpp:88
-
Matrix< M, Q, T > multiply(const Matrix< M, N, T > &m1, const Matrix< P, Q, T > &m2)
Multiplies two matrices together.
Definition: matrix.hpp:354
-
Matrix< N, M, T > transpose(const Matrix< M, N, T > &m)
Transposes a matrix and returns the result.
Definition: matrix.hpp:324
-
Base Matrix class used by other similar classes.
Definition: matrix.hpp:32
-
virtual std::string toString() const
prints out the matrix, but can also be implemented by other classes to print data differently ...
Definition: matrix.hpp:133
-
T dot(const Matrix< R, 1, T > &m1, const Matrix< R, 1, T > &m2)
Returns the dot product between two vectors.
Definition: matrix.hpp:339
-
Matrix< 1, Cols, Type > getRow(int row) const
Return the row specified row as a Matrix with only one row.
Definition: matrix.hpp:107
-
Definition: camera2d.hpp:17
-
std::vector< Type >::iterator begin()
iterator support for begin
Definition: matrix.hpp:125
+Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2  * matrix.hpp
3  *
4  * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com> -- MIT License
5  * See file LICENSE for more details
6  * ----------------------------------------------------------------------------
7  */
8 
10 
21 #ifndef YAGE_MATH_MATRIX_HPP
22 #define YAGE_MATH_MATRIX_HPP
23 
24 #include <algorithm>
25 #include <exception>
26 #include <iostream>
27 #include <sstream>
28 #include <string>
29 #include <vector>
30 
31 namespace yage {
32 
33 template <int Rows, int Cols, class Type>
34 class Matrix;
35 
43 namespace detail {
44 
53 template <int Rows, int Cols, class Type>
54 class Row {
55 private:
57  int index_;
58 
59 public:
61  : parent_(parent), index_(index) {}
62 
63  Type& operator[](int col) {
64  // the index is the y-position of the element in the matrix
65  return parent_->data_[index_ * Cols + col];
66  }
67 
68  const Type& operator[](int col) const {
69  return parent_->data_[index_ * Cols + col];
70  }
71 };
72 
73 } // detail
74 
83 template <int Rows = 4, int Cols = 4, class Type = double>
84 class Matrix {
85  // friended with the row class so that it can access protected member data
86  friend class detail::Row<Rows, Cols, Type>;
87 
88 protected:
90  std::vector<Type> data_;
91 
92 public:
94  Matrix<Rows, Cols, Type>() : data_(Rows * Cols) {}
95  Matrix<Rows, Cols, Type>(const std::vector<Type>& data) : data_(data) {}
96 
98  int rowSize() const { return Rows; }
99 
101  int colSize() const { return Cols; }
102 
109  Matrix<1, Cols, Type> getRow(int row) const {
110  Matrix<1, Cols, Type> rowMatrix;
111  for (int i = 0; i < Cols; ++i) {
112  rowMatrix[0][i] = data_[row][i];
113  }
114  return rowMatrix;
115  }
116 
117  // returns the column in a column matrix
118  Matrix<Rows, 1, Type> getCol(int col) const {
119  Matrix<Rows, 1, Type> colMatrix;
120  for (int i = 0; i < Rows; ++i) {
121  colMatrix[i][0] = data_[i][col];
122  }
123  return colMatrix;
124  }
125 
127  typename std::vector<Type>::iterator begin() { return data_.begin(); }
128 
130  typename std::vector<Type>::iterator end() { return data_.end(); }
131 
135  virtual std::string toString() const {
136  std::stringstream ss;
137  ss << '[';
138  for (int i = 0; i < Rows - 1; ++i) {
139  ss << '[';
140  for (int j = 0; j < Cols - 1; ++j) {
141  ss << data_[i * Cols + j] << ' ';
142  }
143  ss << data_[(Rows - 1) * Cols + Cols - 1] << "],";
144  }
145  ss << '[';
146  for (int j = 0; j < Cols - 1; ++j) {
147  ss << data_[(Rows - 1) * Cols + j] << ' ';
148  }
149  ss << data_[(Rows - 1) * Cols + Cols - 1] << "]]";
150  return ss.str();
151  }
152 
154  return detail::Row<Rows, Cols, Type>(this, row);
155  }
156 
158  // TODO got to fix this
160  row);
161  }
162 
164  std::vector<Type> out;
165  out.reserve(data_.size());
166  std::transform(data_.begin(), data_.end(), rhs.data_.begin(),
167  std::back_inserter(out),
168  [](Type a, Type b) { return a + b; });
169  data_ = std::move(out);
170  return *this;
171  }
172 
174  std::vector<Type> out;
175  out.reserve(data_.size());
176  std::transform(data_.begin(), data_.end(), rhs.begin(),
177  std::back_inserter(out),
178  [](Type a, Type b) { return a - b; });
179  data_ = std::move(out);
180  return *this;
181  }
182 };
183 
184 template <int M, int N, class T>
186  lhs += rhs;
187  return lhs;
188 }
189 
190 template <int M, int N, class T>
192  lhs -= rhs;
193  return lhs;
194 }
195 
196 template <int M, int N, class T>
198  for (auto& data : lhs) {
199  data += rhs;
200  }
201  return lhs;
202 }
203 
204 template <int M, int N, class T>
206  for (auto& data : rhs) {
207  data += lhs;
208  }
209  return rhs;
210 }
211 
212 template <int M, int N, class T>
214  for (auto& data : lhs) {
215  data -= rhs;
216  }
217  return lhs;
218 }
219 
220 template <int M, int N, class T>
222  for (auto& data : rhs) {
223  data = lhs - data;
224  }
225  return rhs;
226 }
227 
228 template <int M, int N, class T>
230  for (auto& data : lhs) {
231  data *= rhs;
232  }
233  return lhs;
234 }
235 
236 template <int M, int N, class T>
238  for (auto& data : rhs) {
239  data *= lhs;
240  }
241  return rhs;
242 }
243 
244 template <int M, int N, class T>
246  for (auto& data : lhs) {
247  data /= rhs;
248  }
249  return lhs;
250 }
251 
252 template <int M, int N, class T>
253 bool operator==(const Matrix<M, N, T>& lhs, const Matrix<M, N, T>& rhs) {
254  for (int i = 0; i < M; ++i)
255  for (int j = 0; j < N; ++j)
256  if (lhs[i][j] != rhs[i][j]) return false;
257  return true;
258 }
259 
260 template <int M, int N, class T>
261 std::ostream& operator<<(std::ostream& os, const Matrix<M, N, T>& mat) {
262  return os << mat.toString();
263 }
264 
265 template <int Rows = 2, class Type = double>
266 class Vector : public Matrix<Rows, 1, Type> {
267 public:
270  : Matrix<Rows, 1, Type>(other) {}
271  Vector<Rows, Type>(const std::vector<Type>& data)
272  : Matrix<Rows, 1, Type>(data) {}
273 
274  Type& operator[](int col) { return this->data_[col]; }
275 
276  const Type& operator[](int col) const { return this->data_[col]; }
277 
278  std::string toString() const override override override {
279  std::stringstream ss;
280  ss << "[";
281  for (std::size_t i = 0; i < this->data_.size() - 1; ++i) {
282  ss << this->data_[i] << " ";
283  }
284  ss << this->data_[this->data_.size() - 1] << "]";
285  return ss.str();
286  }
287 };
288 
293 template <class Type = double>
294 class Vector2 : public Vector<2, Type> {
295 public:
297  Vector2<Type>(const std::vector<Type>& data) : Vector<2, Type>(data) {}
298 
299  Vector2<Type>(Type x, Type y) {
300  this->data_[0] = x;
301  this->data_[1] = y;
302  }
303 
305 
306  Type& x() { return this->data_[0]; }
307 
308  const Type& x() const { return this->data_[0]; }
309 
310  Type& y() { return this->data_[1]; }
311 
312  const Type& y() const { return this->data_[1]; }
313 };
314 
317 
319 namespace matrix {
320 
325 template <int M, int N, class T>
327  Matrix<N, M, T> trans;
328  for (int i = 0; i < M; ++i) {
329  for (int j = 0; j < N; ++j) {
330  trans[j][i] = m[i][j];
331  }
332  }
333  return trans;
334 }
335 
340 template <int R, class T>
341 T dot(const Matrix<R, 1, T>& m1, const Matrix<R, 1, T>& m2) {
342  T sum = 0;
343  for (int i = 0; i < R; ++i) {
344  sum += m1[i][0] * m2[i][0];
345  }
346  return sum;
347 }
348 
355 template <int M, int N, int P, int Q, class T>
357  if (N != P) {
358  throw std::runtime_error(
359  "Matrices don't have the right dimensions for multiplication");
360  }
361 
362  Matrix<M, Q, T> res;
363 
364  for (int i = 0; i < M; ++i) {
365  for (int j = 0; j < Q; ++j) {
366  res[i][j] = dot(transpose(m1.getRow(i)), m2.getCol(j));
367  }
368  }
369 
370  return res;
371 }
372 
373 } // matrix
374 
375 } // yage
376 
377 #endif
Matrix< M, N, T > operator/(Matrix< M, N, T > lhs, const T &rhs)
Definition: matrix.hpp:245
+
const Type & y() const
Definition: matrix.hpp:312
+
Matrix< M, N, T > operator*(Matrix< M, N, T > lhs, const T &rhs)
Definition: matrix.hpp:229
+
int rowSize() const
Returns the row size of the Matrix.
Definition: matrix.hpp:98
+
int colSize() const
Returns the column size of the Matrixxs.
Definition: matrix.hpp:101
+
2D Vector class.
Definition: matrix.hpp:294
+
bool operator==(const Matrix< M, N, T > &lhs, const Matrix< M, N, T > &rhs)
Definition: matrix.hpp:253
+
std::vector< Type >::iterator end()
iterator support for end
Definition: matrix.hpp:130
+
Matrix< Rows, 1, Type > getCol(int col) const
Definition: matrix.hpp:118
+
Type & operator[](int col)
Definition: matrix.hpp:274
+
std::vector< Type > data_
Vector containing the data of the matrix.
Definition: matrix.hpp:90
+
Matrix< M, Q, T > multiply(const Matrix< M, N, T > &m1, const Matrix< P, Q, T > &m2)
Multiplies two matrices together.
Definition: matrix.hpp:356
+
const Type & operator[](int col) const
Definition: matrix.hpp:68
+
Type & y()
Definition: matrix.hpp:310
+
detail::Row< Rows, Cols, Type > operator[](int row) const
Definition: matrix.hpp:157
+
Definition: matrix.hpp:54
+
Matrix< Rows, Cols, Type > * parent_
Definition: matrix.hpp:56
+
int index_
Definition: matrix.hpp:57
+
Matrix< N, M, T > transpose(const Matrix< M, N, T > &m)
Transposes a matrix and returns the result.
Definition: matrix.hpp:326
+
std::string toString() const override override override
prints out the matrix, but can also be implemented by other classes to print data differently ...
Definition: matrix.hpp:278
+
Matrix< Rows, Cols, Type > & operator-=(const Matrix< Rows, Cols, Type > &rhs)
Definition: matrix.hpp:173
+
Type & x()
Definition: matrix.hpp:306
+
Matrix< M, N, T > operator+(Matrix< M, N, T > lhs, const Matrix< M, N, T > &rhs)
Definition: matrix.hpp:185
+
Definition: matrix.hpp:266
+
Matrix< M, N, T > operator-(Matrix< M, N, T > lhs, const Matrix< M, N, T > &rhs)
Definition: matrix.hpp:191
+
Matrix< Rows, Cols, Type > & operator+=(const Matrix< Rows, Cols, Type > &rhs)
Definition: matrix.hpp:163
+
Type & operator[](int col)
Definition: matrix.hpp:63
+
Base Matrix class used by other similar classes.
Definition: matrix.hpp:34
+
virtual std::string toString() const
prints out the matrix, but can also be implemented by other classes to print data differently ...
Definition: matrix.hpp:135
+
T dot(const Matrix< R, 1, T > &m1, const Matrix< R, 1, T > &m2)
Returns the dot product between two vectors.
Definition: matrix.hpp:341
+
Matrix< 1, Cols, Type > getRow(int row) const
Return the row specified row as a Matrix with only one row.
Definition: matrix.hpp:109
+
const Type & x() const
Definition: matrix.hpp:308
+
Templated matrix class.
Definition: camera2d.hpp:17
+
std::vector< Type >::iterator begin()
iterator support for begin
Definition: matrix.hpp:127
+
detail::Row< Rows, Cols, Type > operator[](int row)
Definition: matrix.hpp:153
+
const Type & operator[](int col) const
Definition: matrix.hpp:276
+
- + -- cgit