From 0b8a907d1f99b20cc789806ff70394a18cbea1d9 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Sun, 13 Aug 2017 17:38:57 +0100 Subject: Updating docs --- matrix_8hpp_source.html | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'matrix_8hpp_source.html') diff --git a/matrix_8hpp_source.html b/matrix_8hpp_source.html index 03a28e8a..26233d2f 100644 --- a/matrix_8hpp_source.html +++ b/matrix_8hpp_source.html @@ -68,20 +68,20 @@ $(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 
9 
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 
32 namespace yage
33 {
34 
35 template<int Rows, int Cols, class Type> class Matrix;
36 
44 namespace detail
45 {
46 
55 template<int Rows, int Cols, class Type> class Row
56 {
57 private:
58  Matrix<Rows, Cols, Type> *parent_;
59  int index_;
60 
61 public:
62  Row<Rows, Cols, Type>(Matrix<Rows, Cols, Type> *parent, int index) :
63  parent_(parent), index_(index)
64  {}
65 
66  Type& operator[](int col)
67  {
68  // the index is the y-position of the element in the matrix
69  return parent_->data_[index_*Cols+col];
70  }
71 
72  const Type& operator[](int col) const
73  {
74  return parent_->data_[index_*Cols+col];
75  }
76 };
77 
78 } // detail
79 
88 template<int Rows=4, int Cols=4, class Type=double> class Matrix
89 {
90  // friended with the row class so that it can access protected member data
91  friend class detail::Row<Rows, Cols, Type>;
92 protected:
94  std::vector<Type> data_;
95 
96 public:
98  Matrix<Rows, Cols, Type>() : data_(Rows*Cols) {}
99 
101  int rowSize() const
102  {
103  return Rows;
104  }
105 
107  int colSize() const
108  {
109  return Cols;
110  }
111 
119  {
120  Matrix<1, Cols, Type> rowMatrix;
121  for(int i=0; i<Cols; ++i)
122  {
123  rowMatrix[0][i]=data_[row][i];
124  }
125  return rowMatrix;
126  }
127 
128  // returns the column in a column matrix
129  Matrix<Rows, 1, Type> getCol(int col) const
130  {
131  Matrix<Rows, 1, Type> colMatrix;
132  for(int i=0; i<Rows; ++i)
133  {
134  colMatrix[i][0]=data_[i][col];
135  }
136  return colMatrix;
137  }
138 
139  // iterator support for begin
140  typename std::vector<Type>::iterator begin()
141  {
142  return data_.begin();
143  }
144 
145  // iterator support for end
146  typename std::vector<Type>::iterator end()
147  {
148  return data_.end();
149  }
150 
151  // prints out the matrix, but can also be implemented by other classes to print data
152  // differently
153  virtual std::string toString() const
154  {
155  std::stringstream ss;
156  ss<<'[';
157  for(int i=0; i<Rows-1; ++i)
158  {
159  ss<<'[';
160  for(int j=0; j<Cols-1; ++j)
161  {
162  ss<<data_[i*Cols+j]<<' ';
163  }
164  ss<<data_[(Rows-1)*Cols+Cols-1]<<"],";
165  }
166  ss<<'[';
167  for(int j=0; j<Cols-1; ++j)
168  {
169  ss<<data_[(Rows-1)*Cols+j]<<' ';
170  }
171  ss<<data_[(Rows-1)*Cols+Cols-1]<<"]]";
172  return ss.str();
173  }
174 
175  detail::Row<Rows, Cols, Type> operator[](int row)
176  {
177  return detail::Row<Rows, Cols, Type>(this, row);
178  }
179 
180  detail::Row<Rows, Cols, Type> operator[](int row) const
181  {
182  // TODO got to fix this
183  return detail::Row<Rows, Cols, Type>((Matrix<Rows, Cols, Type>*)this, row);
184  }
185 
186  Matrix<Rows, Cols, Type>& operator=(const Matrix<Rows, Cols, Type> &other)
187  {
188  if(this!=&other)
189  {
190  data_=other.data_;
191  }
192  return *this;
193  }
194 
195  Matrix<Rows, Cols, Type>& operator+=(const Matrix<Rows, Cols, Type> &rhs)
196  {
197  std::vector<Type> out;
198  out.reserve(data_.size());
199  std::transform(data_.begin(), data_.end(), rhs.data_.begin(), std::back_inserter(out),
200  [] (Type a, Type b) {
201  return a+b;
202  });
203  data_=std::move(out);
204  return *this;
205  }
206 
207  Matrix<Rows, Cols, Type>& operator-=(const Matrix<Rows, Cols, Type> &rhs)
208  {
209  std::vector<Type> out;
210  out.reserve(data_.size());
211  std::transform(data_.begin(), data_.end(), rhs.begin(), std::back_inserter(out),
212  [] (Type a, Type b) {
213  return a-b;
214  });
215  data_=std::move(out);
216  return *this;
217  }
218 };
219 
220 template<int M, int N, class T>
221 Matrix<M, N, T> operator+(Matrix<M, N, T> lhs, const Matrix<M, N, T> &rhs)
222 {
223  lhs+=rhs;
224  return lhs;
225 }
226 
227 template<int M, int N, class T>
228 Matrix<M, N, T> operator-(Matrix<M, N, T> lhs, const Matrix<M, N, T> &rhs)
229 {
230  lhs-=rhs;
231  return lhs;
232 }
233 
234 template<int M, int N, class T>
235 Matrix<M, N, T> operator+(Matrix<M, N, T> lhs, const T &rhs)
236 {
237  for(auto &data : lhs)
238  {
239  data+=rhs;
240  }
241  return lhs;
242 }
243 
244 template<int M, int N, class T>
245 Matrix<M, N, T> operator+(const T &lhs, Matrix<M, N, T> rhs)
246 {
247  for(auto &data : rhs)
248  {
249  data+=lhs;
250  }
251  return rhs;
252 }
253 
254 template<int M, int N, class T>
255 Matrix<M, N, T> operator-(Matrix<M, N, T> lhs, const T &rhs)
256 {
257  for(auto &data : lhs)
258  {
259  data-=rhs;
260  }
261  return lhs;
262 }
263 
264 template<int M, int N, class T>
265 Matrix<M, N, T> operator-(const T &lhs, Matrix<M, N, T> rhs)
266 {
267  for(auto &data : rhs)
268  {
269  data=lhs-data;
270  }
271  return rhs;
272 }
273 
274 template<int M, int N, class T>
275 Matrix<M, N, T> operator*(Matrix<M, N, T> lhs, const T &rhs)
276 {
277  for(auto &data : lhs)
278  {
279  data*=rhs;
280  }
281  return lhs;
282 }
283 
284 template<int M, int N, class T>
285 Matrix<M, N, T> operator*(const T &lhs, Matrix<M, N, T> rhs)
286 {
287  for(auto &data : rhs)
288  {
289  data*=lhs;
290  }
291  return rhs;
292 }
293 
294 template<int M, int N, class T>
295 Matrix<M, N, T> operator/(Matrix<M, N, T> lhs, const T &rhs)
296 {
297  for(auto &data : lhs)
298  {
299  data/=rhs;
300  }
301  return lhs;
302 }
303 
304 template<int M, int N, class T>
305 bool operator==(const Matrix<M, N, T> &lhs, const Matrix<M, N, T> &rhs)
306 {
307  for(int i=0; i<M; ++i)
308  for(int j=0; j<N; ++j)
309  if(lhs[i][j]!=rhs[i][j])
310  return false;
311  return true;
312 }
313 
314 template<int M, int N, class T>
315 std::ostream& operator<<(std::ostream &os, const Matrix<M, N, T> &mat)
316 {
317  return os<<mat.toString();
318 }
319 
320 template<int Rows=2, class Type=double> class Vector : public Matrix<Rows, 1, Type>
321 {
322 public:
323  Vector<Rows, Type>() : Matrix<Rows, 1, Type>() {}
324  explicit Vector<Rows, Type>(const Matrix<Rows, 1, Type> &other) : Matrix<Rows, 1, Type>(other) {}
325 
326  Type& operator[](int col)
327  {
328  return this->data_[col];
329  }
330 
331  const Type& operator[](int col) const
332  {
333  return this->data_[col];
334  }
335 
336  virtual std::string toString() const
337  {
338  std::stringstream ss;
339  ss<<"[";
340  for(std::size_t i=0; i<this->data_.size()-1; ++i)
341  {
342  ss<<this->data_[i]<<" ";
343  }
344  ss<<this->data_[this->data_.size()-1]<<"]";
345  return ss.str();
346  }
347 };
348 
353 template<class Type=double> class Vector2 : public Vector<2, Type>
354 {
355 public:
356  Vector2<Type>() : Vector<2, Type>() {}
357 
358  Vector2<Type>(Type x, Type y)
359  {
360  this->data_[0]=x;
361  this->data_[1]=y;
362  }
363 
364  Vector2<Type>(const Matrix<2, 1, Type> &other) : Vector<2, Type>(other) {}
365 
366  Type& x()
367  {
368  return this->data_[0];
369  }
370 
371  const Type& x() const
372  {
373  return this->data_[0];
374  }
375 
376  Type& y()
377  {
378  return this->data_[1];
379  }
380 
381  const Type& y() const
382  {
383  return this->data_[1];
384  }
385 };
386 
389 
391 namespace matrix
392 {
393 
398 template<int M, int N, class T> Matrix<N, M, T> transpose(const Matrix<M, N, T> &m)
399 {
400  Matrix<N, M, T> trans;
401  for(int i=0; i<M; ++i)
402  {
403  for(int j=0; j<N; ++j)
404  {
405  trans[j][i]=m[i][j];
406  }
407  }
408  return trans;
409 }
410 
415 template<int R, class T> T dot(const Matrix<R, 1, T> &m1, const Matrix<R, 1, T> &m2)
416 {
417  T sum=0;
418  for(int i=0; i<R; ++i)
419  {
420  sum += m1[i][0]*m2[i][0];
421  }
422  return sum;
423 }
424 
431 template<int M, int N, int P, int Q, class T>
433 {
434  if(N!=P)
435  {
436  throw std::runtime_error("Matrices don't have the right dimensions for multiplication");
437  }
438 
439  Matrix<M, Q, T> res;
440 
441  for(int i=0; i<M; ++i)
442  {
443  for(int j=0; j<Q; ++j)
444  {
445  res[i][j] = dot(transpose(m1.getRow(i)), m2.getCol(j));
446  }
447  }
448 
449  return res;
450 }
451 
452 } // matrix
453 
454 } // yage
455 
456 #endif
int rowSize() const
Returns the row size of the Matrix.
Definition: matrix.hpp:101
-
int colSize() const
Returns the column size of the Matrixxs.
Definition: matrix.hpp:107
-
2D Vector class.
Definition: matrix.hpp:353
-
std::vector< Type > data_
Vector containing the data of the matrix.
Definition: matrix.hpp:94
-
Matrix< M, Q, T > multiply(const Matrix< M, N, T > &m1, const Matrix< P, Q, T > &m2)
Multiplies two matrices together.
Definition: matrix.hpp:432
-
Matrix< N, M, T > transpose(const Matrix< M, N, T > &m)
Transposes a matrix and returns the result.
Definition: matrix.hpp:398
-
Base Matrix class used by other similar classes.
Definition: matrix.hpp:35
-
T dot(const Matrix< R, 1, T > &m1, const Matrix< R, 1, T > &m2)
Returns the dot product between two vectors.
Definition: matrix.hpp:415
-
Matrix< 1, Cols, Type > getRow(int row) const
Return the row specified row as a Matrix with only one row.
Definition: matrix.hpp:118
+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 
124  // iterator support for begin
125  typename std::vector<Type>::iterator begin() { return data_.begin(); }
126 
127  // iterator support for end
128  typename std::vector<Type>::iterator end() { return data_.end(); }
129 
130  // prints out the matrix, but can also be implemented by other classes to
131  // print data differently
132  virtual std::string toString() const {
133  std::stringstream ss;
134  ss << '[';
135  for (int i = 0; i < Rows - 1; ++i) {
136  ss << '[';
137  for (int j = 0; j < Cols - 1; ++j) {
138  ss << data_[i * Cols + j] << ' ';
139  }
140  ss << data_[(Rows - 1) * Cols + Cols - 1] << "],";
141  }
142  ss << '[';
143  for (int j = 0; j < Cols - 1; ++j) {
144  ss << data_[(Rows - 1) * Cols + j] << ' ';
145  }
146  ss << data_[(Rows - 1) * Cols + Cols - 1] << "]]";
147  return ss.str();
148  }
149 
150  detail::Row<Rows, Cols, Type> operator[](int row) {
151  return detail::Row<Rows, Cols, Type>(this, row);
152  }
153 
154  detail::Row<Rows, Cols, Type> operator[](int row) const {
155  // TODO got to fix this
156  return detail::Row<Rows, Cols, Type>((Matrix<Rows, Cols, Type>*)this,
157  row);
158  }
159 
160  Matrix<Rows, Cols, Type>& operator+=(const Matrix<Rows, Cols, Type>& rhs) {
161  std::vector<Type> out;
162  out.reserve(data_.size());
163  std::transform(data_.begin(), data_.end(), rhs.data_.begin(),
164  std::back_inserter(out),
165  [](Type a, Type b) { return a + b; });
166  data_ = std::move(out);
167  return *this;
168  }
169 
170  Matrix<Rows, Cols, Type>& operator-=(const Matrix<Rows, Cols, Type>& rhs) {
171  std::vector<Type> out;
172  out.reserve(data_.size());
173  std::transform(data_.begin(), data_.end(), rhs.begin(),
174  std::back_inserter(out),
175  [](Type a, Type b) { return a - b; });
176  data_ = std::move(out);
177  return *this;
178  }
179 };
180 
181 template <int M, int N, class T>
182 Matrix<M, N, T> operator+(Matrix<M, N, T> lhs, const Matrix<M, N, T>& rhs) {
183  lhs += rhs;
184  return lhs;
185 }
186 
187 template <int M, int N, class T>
188 Matrix<M, N, T> operator-(Matrix<M, N, T> lhs, const Matrix<M, N, T>& rhs) {
189  lhs -= rhs;
190  return lhs;
191 }
192 
193 template <int M, int N, class T>
194 Matrix<M, N, T> operator+(Matrix<M, N, T> lhs, const T& rhs) {
195  for (auto& data : lhs) {
196  data += rhs;
197  }
198  return lhs;
199 }
200 
201 template <int M, int N, class T>
202 Matrix<M, N, T> operator+(const T& lhs, Matrix<M, N, T> rhs) {
203  for (auto& data : rhs) {
204  data += lhs;
205  }
206  return rhs;
207 }
208 
209 template <int M, int N, class T>
210 Matrix<M, N, T> operator-(Matrix<M, N, T> lhs, const T& rhs) {
211  for (auto& data : lhs) {
212  data -= rhs;
213  }
214  return lhs;
215 }
216 
217 template <int M, int N, class T>
218 Matrix<M, N, T> operator-(const T& lhs, Matrix<M, N, T> rhs) {
219  for (auto& data : rhs) {
220  data = lhs - data;
221  }
222  return rhs;
223 }
224 
225 template <int M, int N, class T>
226 Matrix<M, N, T> operator*(Matrix<M, N, T> lhs, const T& rhs) {
227  for (auto& data : lhs) {
228  data *= rhs;
229  }
230  return lhs;
231 }
232 
233 template <int M, int N, class T>
234 Matrix<M, N, T> operator*(const T& lhs, Matrix<M, N, T> rhs) {
235  for (auto& data : rhs) {
236  data *= lhs;
237  }
238  return rhs;
239 }
240 
241 template <int M, int N, class T>
242 Matrix<M, N, T> operator/(Matrix<M, N, T> lhs, const T& rhs) {
243  for (auto& data : lhs) {
244  data /= rhs;
245  }
246  return lhs;
247 }
248 
249 template <int M, int N, class T>
250 bool operator==(const Matrix<M, N, T>& lhs, const Matrix<M, N, T>& rhs) {
251  for (int i = 0; i < M; ++i)
252  for (int j = 0; j < N; ++j)
253  if (lhs[i][j] != rhs[i][j]) return false;
254  return true;
255 }
256 
257 template <int M, int N, class T>
258 std::ostream& operator<<(std::ostream& os, const Matrix<M, N, T>& mat) {
259  return os << mat.toString();
260 }
261 
262 template <int Rows = 2, class Type = double>
263 class Vector : public Matrix<Rows, 1, Type> {
264 public:
265  Vector<Rows, Type>() : Matrix<Rows, 1, Type>() {}
266  Vector<Rows, Type>(const Matrix<Rows, 1, Type>& other)
267  : Matrix<Rows, 1, Type>(other) {}
268  Vector<Rows, Type>(const std::vector<Type>& data)
269  : Matrix<Rows, 1, Type>(data) {}
270 
271  Type& operator[](int col) { return this->data_[col]; }
272 
273  const Type& operator[](int col) const { return this->data_[col]; }
274 
275  virtual std::string toString() const {
276  std::stringstream ss;
277  ss << "[";
278  for (std::size_t i = 0; i < this->data_.size() - 1; ++i) {
279  ss << this->data_[i] << " ";
280  }
281  ss << this->data_[this->data_.size() - 1] << "]";
282  return ss.str();
283  }
284 };
285 
290 template <class Type = double>
291 class Vector2 : public Vector<2, Type> {
292 public:
293  Vector2<Type>() : Vector<2, Type>() {}
294  Vector2<Type>(const std::vector<Type>& data) : Vector<2, Type>(data) {}
295 
296  Vector2<Type>(Type x, Type y) {
297  this->data_[0] = x;
298  this->data_[1] = y;
299  }
300 
301  Vector2<Type>(const Matrix<2, 1, Type>& other) : Vector<2, Type>(other) {}
302 
303  Type& x() { return this->data_[0]; }
304 
305  const Type& x() const { return this->data_[0]; }
306 
307  Type& y() { return this->data_[1]; }
308 
309  const Type& y() const { return this->data_[1]; }
310 };
311 
314 
316 namespace matrix {
317 
322 template <int M, int N, class T>
324  Matrix<N, M, T> trans;
325  for (int i = 0; i < M; ++i) {
326  for (int j = 0; j < N; ++j) {
327  trans[j][i] = m[i][j];
328  }
329  }
330  return trans;
331 }
332 
337 template <int R, class T>
338 T dot(const Matrix<R, 1, T>& m1, const Matrix<R, 1, T>& m2) {
339  T sum = 0;
340  for (int i = 0; i < R; ++i) {
341  sum += m1[i][0] * m2[i][0];
342  }
343  return sum;
344 }
345 
352 template <int M, int N, int P, int Q, class T>
354  if (N != P) {
355  throw std::runtime_error(
356  "Matrices don't have the right dimensions for multiplication");
357  }
358 
359  Matrix<M, Q, T> res;
360 
361  for (int i = 0; i < M; ++i) {
362  for (int j = 0; j < Q; ++j) {
363  res[i][j] = dot(transpose(m1.getRow(i)), m2.getCol(j));
364  }
365  }
366 
367  return res;
368 }
369 
370 } // matrix
371 
372 } // yage
373 
374 #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:291
+
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:353
+
Matrix< N, M, T > transpose(const Matrix< M, N, T > &m)
Transposes a matrix and returns the result.
Definition: matrix.hpp:323
+
Base Matrix class used by other similar classes.
Definition: matrix.hpp:32
+
T dot(const Matrix< R, 1, T > &m1, const Matrix< R, 1, T > &m2)
Returns the dot product between two vectors.
Definition: matrix.hpp:338
+
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
-- cgit