From e7770985a2be00cc22c3cd1302afb1fcc16bf94b Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Fri, 28 Jul 2017 00:38:30 +0100 Subject: Updating docs --- matrix_8hpp_source.html | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'matrix_8hpp_source.html') diff --git a/matrix_8hpp_source.html b/matrix_8hpp_source.html index af613af6..ea7481a9 100644 --- a/matrix_8hpp_source.html +++ b/matrix_8hpp_source.html @@ -67,10 +67,11 @@ $(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 
22 #ifndef YAGE_MATH_MATRIX_HPP
23 #define YAGE_MATH_MATRIX_HPP
24 
25 #include <algorithm>
26 #include <exception>
27 #include <iostream>
28 #include <sstream>
29 #include <string>
30 #include <vector>
31 
32 namespace yage
33 {
34 
35 template<int Rows, int Cols, class Type> class Matrix;
36 
37 // includes implementation details that should not be accessible to the user
38 namespace detail
39 {
40 
41 // Row class
42 //
43 // Used to implement the double square bracket operator and be able
44 // to return the value by reference of the array.
45 template<int Rows, int Cols, class Type> class Row
46 {
47 private:
48  Matrix<Rows, Cols, Type> *parent_;
49  int index_;
50 
51 public:
53  parent_(parent), index_(index)
54  {}
55 
56  Type& operator[](int col)
57  {
58  // the index is the y-position of the element in the matrix
59  return parent_->data_[index_*Cols+col];
60  }
61 
62  const Type& operator[](int col) const
63  {
64  return parent_->data_[index_*Cols+col];
65  }
66 };
67 
68 } // detail
69 
70 // Matrix class
71 //
72 // Implements the base Matrix class that is inherited by other classes to make them more
73 // specific.
74 template<int Rows=4, int Cols=4, class Type=double> class Matrix
75 {
76  // friended with the row class so that it can access protected member data
77  friend class detail::Row<Rows, Cols, Type>;
78 protected:
79  std::vector<Type> data_;
80 
81 public:
82  Matrix<Rows, Cols, Type>() : data_(Rows*Cols) {}
83 
84  int rowSize() const
85  {
86  return Rows;
87  }
88 
89  int colSize() const
90  {
91  return Cols;
92  }
93 
94  // returns the row in a row matrix
95  Matrix<1, Cols, Type> getRow(int row) const
96  {
97  Matrix<1, Cols, Type> rowMatrix;
98  for(int i=0; i<Cols; ++i)
99  {
100  rowMatrix[0][i]=data_[row][i];
101  }
102  return rowMatrix;
103  }
104 
105  // returns the column in a column matrix
106  Matrix<Rows, 1, Type> getCol(int col) const
107  {
108  Matrix<Rows, 1, Type> colMatrix;
109  for(int i=0; i<Rows; ++i)
110  {
111  colMatrix[i][0]=data_[i][col];
112  }
113  return colMatrix;
114  }
115 
116  // iterator support for begin
117  typename std::vector<Type>::iterator begin()
118  {
119  return data_.begin();
120  }
121 
122  // iterator support for end
123  typename std::vector<Type>::iterator end()
124  {
125  return data_.end();
126  }
127 
128  // prints out the matrix, but can also be implemented by other classes to print data
129  // differently
130  virtual std::string toString() const
131  {
132  std::stringstream ss;
133  ss<<'[';
134  for(int i=0; i<Rows-1; ++i)
135  {
136  ss<<'[';
137  for(int j=0; j<Cols-1; ++j)
138  {
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  {
146  ss<<data_[(Rows-1)*Cols+j]<<' ';
147  }
148  ss<<data_[(Rows-1)*Cols+Cols-1]<<"]]";
149  return ss.str();
150  }
151 
152  detail::Row<Rows, Cols, Type> operator[](int row)
153  {
154  return detail::Row<Rows, Cols, Type>(this, row);
155  }
156 
157  detail::Row<Rows, Cols, Type> operator[](int row) const
158  {
159  // TODO got to fix this
161  }
162 
163  Matrix<Rows, Cols, Type>& operator=(const Matrix<Rows, Cols, Type> &other)
164  {
165  if(this!=&other)
166  {
167  data_=other.data_;
168  }
169  return *this;
170  }
171 
172  Matrix<Rows, Cols, Type>& operator+=(const Matrix<Rows, Cols, Type> &rhs)
173  {
174  std::vector<Type> out;
175  out.reserve(data_.size());
176  std::transform(data_.begin(), data_.end(), rhs.data_.begin(), std::back_inserter(out),
177  [] (Type a, Type b) {
178  return a+b;
179  });
180  data_=std::move(out);
181  return *this;
182  }
183 
184  Matrix<Rows, Cols, Type>& operator-=(const Matrix<Rows, Cols, Type> &rhs)
185  {
186  std::vector<Type> out;
187  out.reserve(data_.size());
188  std::transform(data_.begin(), data_.end(), rhs.begin(), std::back_inserter(out),
189  [] (Type a, Type b) {
190  return a-b;
191  });
192  data_=std::move(out);
193  return *this;
194  }
195 };
196 
197 template<int M, int N, class T>
198 Matrix<M, N, T> operator+(Matrix<M, N, T> lhs, const Matrix<M, N, T> &rhs)
199 {
200  lhs+=rhs;
201  return lhs;
202 }
203 
204 template<int M, int N, class T>
205 Matrix<M, N, T> operator-(Matrix<M, N, T> lhs, const Matrix<M, N, T> &rhs)
206 {
207  lhs-=rhs;
208  return lhs;
209 }
210 
211 template<int M, int N, class T>
212 Matrix<M, N, T> operator+(Matrix<M, N, T> lhs, const T &rhs)
213 {
214  for(auto &data : lhs)
215  {
216  data+=rhs;
217  }
218  return lhs;
219 }
220 
221 template<int M, int N, class T>
222 Matrix<M, N, T> operator+(const T &lhs, Matrix<M, N, T> rhs)
223 {
224  for(auto &data : rhs)
225  {
226  data+=lhs;
227  }
228  return rhs;
229 }
230 
231 template<int M, int N, class T>
232 Matrix<M, N, T> operator-(Matrix<M, N, T> lhs, const T &rhs)
233 {
234  for(auto &data : lhs)
235  {
236  data-=rhs;
237  }
238  return lhs;
239 }
240 
241 template<int M, int N, class T>
242 Matrix<M, N, T> operator-(const T &lhs, Matrix<M, N, T> rhs)
243 {
244  for(auto &data : rhs)
245  {
246  data=lhs-data;
247  }
248  return rhs;
249 }
250 
251 template<int M, int N, class T>
252 Matrix<M, N, T> operator*(Matrix<M, N, T> lhs, const T &rhs)
253 {
254  for(auto &data : lhs)
255  {
256  data*=rhs;
257  }
258  return lhs;
259 }
260 
261 template<int M, int N, class T>
262 Matrix<M, N, T> operator*(const T &lhs, Matrix<M, N, T> rhs)
263 {
264  for(auto &data : rhs)
265  {
266  data*=lhs;
267  }
268  return rhs;
269 }
270 
271 template<int M, int N, class T>
272 Matrix<M, N, T> operator/(Matrix<M, N, T> lhs, const T &rhs)
273 {
274  for(auto &data : lhs)
275  {
276  data/=rhs;
277  }
278  return lhs;
279 }
280 
281 template<int M, int N, class T>
282 bool operator==(const Matrix<M, N, T> &lhs, const Matrix<M, N, T> &rhs)
283 {
284  for(int i=0; i<M; ++i)
285  for(int j=0; j<N; ++j)
286  if(lhs[i][j]!=rhs[i][j])
287  return false;
288  return true;
289 }
290 
291 template<int M, int N, class T>
292 std::ostream& operator<<(std::ostream &os, const Matrix<M, N, T> &mat)
293 {
294  return os<<mat.toString();
295 }
296 
297 template<int Rows=2, class Type=double> class Vector : public Matrix<Rows, 1, Type>
298 {
299 public:
301  explicit Vector<Rows, Type>(const Matrix<Rows, 1, Type> &other) : Matrix<Rows, 1, Type>(other) {}
302 
303  Type& operator[](int col)
304  {
305  return this->data_[col];
306  }
307 
308  const Type& operator[](int col) const
309  {
310  return this->data_[col];
311  }
312 
313  virtual std::string toString() const
314  {
315  std::stringstream ss;
316  ss<<"[";
317  for(std::size_t i=0; i<this->data_.size()-1; ++i)
318  {
319  ss<<this->data_[i]<<" ";
320  }
321  ss<<this->data_[this->data_.size()-1]<<"]";
322  return ss.str();
323  }
324 };
325 
326 template<class Type=double> class Vector2 : public Vector<2, Type>
327 {
328 public:
330 
331  Vector2<Type>(Type x, Type y)
332  {
333  this->data_[0]=x;
334  this->data_[1]=y;
335  }
336 
337  Vector2<Type>(const Matrix<2, 1, Type> &other) : Vector<2, Type>(other) {}
338 
339  Type& x()
340  {
341  return this->data_[0];
342  }
343 
344  const Type& x() const
345  {
346  return this->data_[0];
347  }
348 
349  Type& y()
350  {
351  return this->data_[1];
352  }
353 
354  const Type& y() const
355  {
356  return this->data_[1];
357  }
358 };
359 
360 typedef Vector2<double> Vector2d;
361 
362 namespace matrix
363 {
364 
365 template<int M, int N, class T> Matrix<N, M, T> transpose(const Matrix<M, N, T> &m)
366 {
367  Matrix<N, M, T> trans;
368  for(int i=0; i<M; ++i)
369  {
370  for(int j=0; j<N; ++j)
371  {
372  trans[j][i]=m[i][j];
373  }
374  }
375  return trans;
376 }
377 
378 template<int R, class T> T dot(const Matrix<R, 1, T> &m1, const Matrix<R, 1, T> &m2)
379 {
380  T sum=0;
381  for(int i=0; i<R; ++i)
382  {
383  sum += m1[i][0]*m2[i][0];
384  }
385  return sum;
386 }
387 
388 template<int M, int N, int P, int Q, class T>
389 Matrix<M, Q, T> multiply(const Matrix<M, N, T> &m1, const Matrix<P, Q, T> &m2)
390 {
391  if(N!=P)
392  {
393  throw std::runtime_error("Matrices don't have the right dimensions for multiplication");
394  }
395 
396  Matrix<M, Q, T> res;
397 
398  for(int i=0; i<M; ++i)
399  {
400  for(int j=0; j<Q; ++j)
401  {
402  res[i][j] = dot(transpose(m1.getRow(i)), m2.getCol(j));
403  }
404  }
405 
406  return res;
407 }
408 
409 } // matrix
410 
411 } // yage
412 
413 #endif
Definition: matrix.hpp:326
-
Definition: matrix.hpp:45
-
Definition: matrix.hpp:297
-
Definition: matrix.hpp:35
+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 
20 #ifndef YAGE_MATH_MATRIX_HPP
21 #define YAGE_MATH_MATRIX_HPP
22 
23 #include <algorithm>
24 #include <exception>
25 #include <iostream>
26 #include <sstream>
27 #include <string>
28 #include <vector>
29 
30 namespace yage
31 {
32 
33 template<int Rows, int Cols, class Type> class Matrix;
34 
43 namespace detail
44 {
45 
46 template<int Rows, int Cols, class Type> class Row
47 {
48 private:
49  Matrix<Rows, Cols, Type> *parent_;
50  int index_;
51 
52 public:
54  parent_(parent), index_(index)
55  {}
56 
57  Type& operator[](int col)
58  {
59  // the index is the y-position of the element in the matrix
60  return parent_->data_[index_*Cols+col];
61  }
62 
63  const Type& operator[](int col) const
64  {
65  return parent_->data_[index_*Cols+col];
66  }
67 };
68 
69 } // detail
70 
80 template<int Rows=4, int Cols=4, class Type=double> class Matrix
81 {
82  // friended with the row class so that it can access protected member data
83  friend class detail::Row<Rows, Cols, Type>;
84 protected:
85  std::vector<Type> data_;
86 
87 public:
88  Matrix<Rows, Cols, Type>() : data_(Rows*Cols) {}
89 
90  int rowSize() const
91  {
92  return Rows;
93  }
94 
95  int colSize() const
96  {
97  return Cols;
98  }
99 
100  // returns the row in a row matrix
101  Matrix<1, Cols, Type> getRow(int row) const
102  {
103  Matrix<1, Cols, Type> rowMatrix;
104  for(int i=0; i<Cols; ++i)
105  {
106  rowMatrix[0][i]=data_[row][i];
107  }
108  return rowMatrix;
109  }
110 
111  // returns the column in a column matrix
112  Matrix<Rows, 1, Type> getCol(int col) const
113  {
114  Matrix<Rows, 1, Type> colMatrix;
115  for(int i=0; i<Rows; ++i)
116  {
117  colMatrix[i][0]=data_[i][col];
118  }
119  return colMatrix;
120  }
121 
122  // iterator support for begin
123  typename std::vector<Type>::iterator begin()
124  {
125  return data_.begin();
126  }
127 
128  // iterator support for end
129  typename std::vector<Type>::iterator end()
130  {
131  return data_.end();
132  }
133 
134  // prints out the matrix, but can also be implemented by other classes to print data
135  // differently
136  virtual std::string toString() const
137  {
138  std::stringstream ss;
139  ss<<'[';
140  for(int i=0; i<Rows-1; ++i)
141  {
142  ss<<'[';
143  for(int j=0; j<Cols-1; ++j)
144  {
145  ss<<data_[i*Cols+j]<<' ';
146  }
147  ss<<data_[(Rows-1)*Cols+Cols-1]<<"],";
148  }
149  ss<<'[';
150  for(int j=0; j<Cols-1; ++j)
151  {
152  ss<<data_[(Rows-1)*Cols+j]<<' ';
153  }
154  ss<<data_[(Rows-1)*Cols+Cols-1]<<"]]";
155  return ss.str();
156  }
157 
158  detail::Row<Rows, Cols, Type> operator[](int row)
159  {
160  return detail::Row<Rows, Cols, Type>(this, row);
161  }
162 
163  detail::Row<Rows, Cols, Type> operator[](int row) const
164  {
165  // TODO got to fix this
167  }
168 
169  Matrix<Rows, Cols, Type>& operator=(const Matrix<Rows, Cols, Type> &other)
170  {
171  if(this!=&other)
172  {
173  data_=other.data_;
174  }
175  return *this;
176  }
177 
178  Matrix<Rows, Cols, Type>& operator+=(const Matrix<Rows, Cols, Type> &rhs)
179  {
180  std::vector<Type> out;
181  out.reserve(data_.size());
182  std::transform(data_.begin(), data_.end(), rhs.data_.begin(), std::back_inserter(out),
183  [] (Type a, Type b) {
184  return a+b;
185  });
186  data_=std::move(out);
187  return *this;
188  }
189 
190  Matrix<Rows, Cols, Type>& operator-=(const Matrix<Rows, Cols, Type> &rhs)
191  {
192  std::vector<Type> out;
193  out.reserve(data_.size());
194  std::transform(data_.begin(), data_.end(), rhs.begin(), std::back_inserter(out),
195  [] (Type a, Type b) {
196  return a-b;
197  });
198  data_=std::move(out);
199  return *this;
200  }
201 };
202 
203 template<int M, int N, class T>
204 Matrix<M, N, T> operator+(Matrix<M, N, T> lhs, const Matrix<M, N, T> &rhs)
205 {
206  lhs+=rhs;
207  return lhs;
208 }
209 
210 template<int M, int N, class T>
211 Matrix<M, N, T> operator-(Matrix<M, N, T> lhs, const Matrix<M, N, T> &rhs)
212 {
213  lhs-=rhs;
214  return lhs;
215 }
216 
217 template<int M, int N, class T>
218 Matrix<M, N, T> operator+(Matrix<M, N, T> lhs, const T &rhs)
219 {
220  for(auto &data : lhs)
221  {
222  data+=rhs;
223  }
224  return lhs;
225 }
226 
227 template<int M, int N, class T>
228 Matrix<M, N, T> operator+(const T &lhs, Matrix<M, N, T> rhs)
229 {
230  for(auto &data : rhs)
231  {
232  data+=lhs;
233  }
234  return rhs;
235 }
236 
237 template<int M, int N, class T>
238 Matrix<M, N, T> operator-(Matrix<M, N, T> lhs, const T &rhs)
239 {
240  for(auto &data : lhs)
241  {
242  data-=rhs;
243  }
244  return lhs;
245 }
246 
247 template<int M, int N, class T>
248 Matrix<M, N, T> operator-(const T &lhs, Matrix<M, N, T> rhs)
249 {
250  for(auto &data : rhs)
251  {
252  data=lhs-data;
253  }
254  return rhs;
255 }
256 
257 template<int M, int N, class T>
258 Matrix<M, N, T> operator*(Matrix<M, N, T> lhs, const T &rhs)
259 {
260  for(auto &data : lhs)
261  {
262  data*=rhs;
263  }
264  return lhs;
265 }
266 
267 template<int M, int N, class T>
268 Matrix<M, N, T> operator*(const T &lhs, Matrix<M, N, T> rhs)
269 {
270  for(auto &data : rhs)
271  {
272  data*=lhs;
273  }
274  return rhs;
275 }
276 
277 template<int M, int N, class T>
278 Matrix<M, N, T> operator/(Matrix<M, N, T> lhs, const T &rhs)
279 {
280  for(auto &data : lhs)
281  {
282  data/=rhs;
283  }
284  return lhs;
285 }
286 
287 template<int M, int N, class T>
288 bool operator==(const Matrix<M, N, T> &lhs, const Matrix<M, N, T> &rhs)
289 {
290  for(int i=0; i<M; ++i)
291  for(int j=0; j<N; ++j)
292  if(lhs[i][j]!=rhs[i][j])
293  return false;
294  return true;
295 }
296 
297 template<int M, int N, class T>
298 std::ostream& operator<<(std::ostream &os, const Matrix<M, N, T> &mat)
299 {
300  return os<<mat.toString();
301 }
302 
303 template<int Rows=2, class Type=double> class Vector : public Matrix<Rows, 1, Type>
304 {
305 public:
307  explicit Vector<Rows, Type>(const Matrix<Rows, 1, Type> &other) : Matrix<Rows, 1, Type>(other) {}
308 
309  Type& operator[](int col)
310  {
311  return this->data_[col];
312  }
313 
314  const Type& operator[](int col) const
315  {
316  return this->data_[col];
317  }
318 
319  virtual std::string toString() const
320  {
321  std::stringstream ss;
322  ss<<"[";
323  for(std::size_t i=0; i<this->data_.size()-1; ++i)
324  {
325  ss<<this->data_[i]<<" ";
326  }
327  ss<<this->data_[this->data_.size()-1]<<"]";
328  return ss.str();
329  }
330 };
331 
332 template<class Type=double> class Vector2 : public Vector<2, Type>
333 {
334 public:
336 
337  Vector2<Type>(Type x, Type y)
338  {
339  this->data_[0]=x;
340  this->data_[1]=y;
341  }
342 
343  Vector2<Type>(const Matrix<2, 1, Type> &other) : Vector<2, Type>(other) {}
344 
345  Type& x()
346  {
347  return this->data_[0];
348  }
349 
350  const Type& x() const
351  {
352  return this->data_[0];
353  }
354 
355  Type& y()
356  {
357  return this->data_[1];
358  }
359 
360  const Type& y() const
361  {
362  return this->data_[1];
363  }
364 };
365 
366 typedef Vector2<double> Vector2d;
367 
368 namespace matrix
369 {
370 
371 template<int M, int N, class T> Matrix<N, M, T> transpose(const Matrix<M, N, T> &m)
372 {
373  Matrix<N, M, T> trans;
374  for(int i=0; i<M; ++i)
375  {
376  for(int j=0; j<N; ++j)
377  {
378  trans[j][i]=m[i][j];
379  }
380  }
381  return trans;
382 }
383 
384 template<int R, class T> T dot(const Matrix<R, 1, T> &m1, const Matrix<R, 1, T> &m2)
385 {
386  T sum=0;
387  for(int i=0; i<R; ++i)
388  {
389  sum += m1[i][0]*m2[i][0];
390  }
391  return sum;
392 }
393 
394 template<int M, int N, int P, int Q, class T>
395 Matrix<M, Q, T> multiply(const Matrix<M, N, T> &m1, const Matrix<P, Q, T> &m2)
396 {
397  if(N!=P)
398  {
399  throw std::runtime_error("Matrices don't have the right dimensions for multiplication");
400  }
401 
402  Matrix<M, Q, T> res;
403 
404  for(int i=0; i<M; ++i)
405  {
406  for(int j=0; j<Q; ++j)
407  {
408  res[i][j] = dot(transpose(m1.getRow(i)), m2.getCol(j));
409  }
410  }
411 
412  return res;
413 }
414 
415 } // matrix
416 
417 } // yage
418 
419 #endif
Definition: matrix.hpp:332
+
Detail namespace.
Definition: matrix.hpp:33
+
Definition: matrix.hpp:46
+
Definition: matrix.hpp:303
+
Base matrix class.
Definition: matrix.hpp:33
Definition: camera2d.hpp:17
-- cgit