Matrix.hpp
Go to the documentation of this file.
1 
6 #pragma once
7 
8 #include <etk/types.hpp>
9 #include <etk/math/Vector2D.hpp>
10 #include <vector>
11 
12 namespace etk {
17  template <typename T> class Matrix {
18  private:
19  uivec2 m_size;
20  std::vector<T> m_data;
21  public:
27  Matrix(const ivec2& _size, T* _defaultVal=nullptr) :
28  m_size(_size),
29  etk::Vector2D<T>(_size.x()* _size.y()) {
30  if (defaultVal == nullptr) {
31  clear();
32  return;
33  }
34  // copy all the elements
35  for(size_t iii = 0;
36  iii <= m_size.x()*m_size.y();
37  ++iii) {
38  // cast and set value :
39  m_data[iii] = T(_defaultVal++);
40  }
41  }
48  Matrix(int32_t _width=0, int32_t _heigh=0, T* _defaultVal=nullptr) :
49  m_size(_width, _heigh),
50  etk::Vector2D<T>(_width*_heigh) {
51  if (_defaultVal == nullptr) {
52  clear();
53  return;
54  }
55  // copy all the elements
56  for(size_t iii = 0;
57  iii <= m_size.x()*m_size.y();
58  ++iii) {
59  // cast and set value :
60  m_data[iii] = T(_defaultVal++);
61  }
62  }
67  template<class ETK_TYPE_MATRIX_2>
69  m_size(_obj.m_size),
70  etk::Vector2D<T>(_obj.m_size.x()* _obj.m_size.y()) {
71  // copy all the elements
72  for(size_t iii = 0;
73  iii <= m_size.x()*m_size.y();
74  ++iii) {
75  // cast and set value :
76  m_data[iii] = T(_obj.m_data[iii]);
77  }
78  }
82  virtual ~Matrix() = default;
83 
89  const Matrix<T>& operator= (const Matrix<T>& _obj ) {
90  // check if it was the same pointer
91  if (this == &_obj ) {
92  return *this;
93  }
94  // copy data :
95  m_size = _obj.m_size;
96  m_data = _obj.m_data;
97  return *this;
98  }
104  const Matrix<T>& operator= (T& _value) {
105  // set data :
106  for(size_t iii = 0;
107  iii <= m_size.x()*m_size.y();
108  ++iii) {
109  m_data = _value;
110  }
111  return *this;
112  }
119  bool operator== (const Matrix<T>& _obj) const {
120  return (m_data == _obj.m_data);
121  }
128  bool operator!= (const Matrix<T>& _obj) const {
129  return (m_data != _obj.m_data);
130  }
141  const Matrix<T>& operator+= (const Matrix<T>& _obj) {
142  if (m_size != _obj.m_size) {
143  //TK_CRITICAL("add 2 Matrix with diffĂ©rent size ... ==> generate the max size of all the 2 matrix");
144  etk::Matrix<T> tmpMatrix(std::max(m_size.x(),_obj.m_size.x()), std::max(m_size.y(),_obj.m_size.y()));
145  for (int32_t jjj=0; jjj< m_size.y(); jjj++) {
146  T* tmpPointer = tmpMatrix[jjj];
147  T* tmpPointerIn = (*this)[jjj];
148  for (int32_t iii=0; iii< m_size.x(); iii++) {
149  tmpPointer[iii] = tmpPointerIn[iii];
150  }
151  }
152  for (int32_t jjj=0; jjj< _obj.m_size.y(); jjj++) {
153  T* tmpPointer = tmpMatrix[jjj];
154  T* tmpPointerIn = _obj[jjj];
155  for (int32_t iii=0; iii< _obj.m_size.x(); iii++) {
156  tmpPointer[iii] += tmpPointerIn[iii];
157  }
158  }
159  // copy in local :
160  m_size = tmpMatrix.m_size;
161  m_data = tmpMatrix.m_data;
162  } else {
163  // copy data for the same size:
164  for (int32_t iii=0; iii< m_data.size(); iii++) {
165  m_data[iii] += _obj.m_data[iii];
166  }
167  }
168  return *this;
169  }
181  Matrix<T> tmpp(*this);
182  tmpp += _obj;
183  return tmpp;
184  }
195  const Matrix<T>& operator-= (const Matrix<T>& _obj) {
196  if (m_size != _obj.m_size) {
197  //TK_CRITICAL("less 2 Matrix with different size ... ==> generate the max size of all the 2 matrix");
198  etk::Matrix<T> tmpMatrix(std::max(m_size.x(),_obj.m_size.x()), std::max(m_size.y(),_obj.m_size.y()));
199  for (int32_t jjj=0; jjj< m_size.y; jjj++) {
200  T* tmpPointer = tmpMatrix[jjj];
201  T* tmpPointerIn = (*this)[jjj];
202  for (int32_t iii=0; iii< m_size.x(); iii++) {
203  tmpPointer[iii] = tmpPointerIn[iii];
204  }
205  }
206  for (int32_t jjj=0; jjj< _obj.m_size.y(); jjj++) {
207  T* tmpPointer = tmpMatrix[jjj];
208  T* tmpPointerIn = _obj[jjj];
209  for (int32_t iii=0; iii< _obj.m_size.x(); iii++) {
210  tmpPointer[iii] -= tmpPointerIn[iii];
211  }
212  }
213  // copy in local :
214  m_size = tmpMatrix.m_size;
215  m_data = tmpMatrix.m_data;
216  } else {
217  // copy data for the same size :
218  for (int32_t iii=0; iii< m_data.size(); iii++) {
219  m_data[iii] -= _obj.m_data[iii];
220  }
221  }
222  return *this;
223  };
235  Matrix<T> tmpp(*this);
236  tmpp += _obj;
237  return tmpp;
238  }
244  const Matrix<T>& operator*= (const Matrix<T>& _obj) {
245  if( m_size.x() != _obj.m_size.y()
246  || m_size.y() != _obj.m_size.x()) {
247  //TK_CRITICAL("Error while multipliying 2 matrix with different size ==> impossible case ...");
248  return *this;
249  }
250  etk::Matrix<T> tmpMatrix(m_size);
251  for (int32_t jjj=0; jjj< _obj.m_size.y(); jjj++) {
252  for (int32_t iii=0; iii< _obj.m_size.x(); iii++) {
253  T tmpVal = 0;
254  for (int32_t kkk=0; kkk< _obj.m_size.x(); kkk++) {
255  tmpVal += (*this)[jjj][iii+kkk] * _obj[jjj+kkk][iii];
256  }
257  tmpMatrix[jjj][iii] = tmpVal;
258  }
259  }
260  // copy in local :
261  m_data = tmpMatrix.m_data;
262  return *this;
263  };
270  Matrix tmpp(*this);
271  tmpp *= _obj;
272  return tmpp;
273  }
274  // TODO : Check if is possible to do elemntValue = mayMatrix[xxx, yyy]
283  const T* operator[] (int32_t _yyy) const {
284  return &m_data[_yyy*m_size.x()];
285  }
294  T* operator[] (int32_t _yyy) {
295  return &m_data[_yyy*m_size.x()];
296  }
305  const T& operator[] (const ivec2& _pos) const {
306  return m_data[_pos.y()*m_size.x() + _pos.x()];
307  }
316  T& operator[] (const ivec2& _pos) {
317  return m_data[_pos.y()*m_size.x() + _pos.x()];
318  }
328  T& operator () (size_t _xxx, size_t _yyy) {
329  return m_data[_yyy*m_size.x() + _xxx];
330  }
336  Matrix<T> tmp(m_size);
337  for (int32_t iii=0; iii<m_data.Size(); iii++) {
338  tmp.m_data[iii] = -m_data[iii];
339  }
340  return tmp;
341  }
347  // create a matrix with the inverted size
348  Matrix<T> tmpMatrix(m_size);
349  for (int32_t jjj=0; jjj< m_size.y(); jjj++) {
350  for (int32_t iii=0; iii< m_size.x(); iii++) {
351  tmpMatrix(jjj,iii) = (*this)(iii,jjj);
352  }
353  }
354  return tmpMatrix;
355  }
362  Matrix<T> tmppp(1,1);
363  // TODO : ...
364  return tmppp;
365  }
371  Matrix<T> fix(int32_t _decalage) const {
372  Matrix<T> tmppp(m_size);
373  T tmpVal = 0;
374  for(int32_t iii=0; iii<m_data.size(); iii++) {
375  tmpVal = m_data[iii];
376  if (tmpVal < 0 && (tmpVal & ~(~0 << _decalage))) {
377  tmpVal = tmpVal >> _decalage;
378  tmpVal++;
379  } else {
380  tmpVal = tmpVal >> _decalage;
381  }
382  tmppp.m_data[iii] = tmpVal;
383  }
384  return tmppp;
385  };
391  Matrix<T> round(int32_t _decalage) const {
392  Matrix<T> tmppp(m_size);
393  for(int32_t iii=0; iii<m_data.size(); iii++) {
394  tmppp.m_data[iii] = ( m_data[iii]+(1<<(_decalage-1)) ) >> _decalage;
395  }
396  return tmppp;
397  };
404  Matrix<T> tmppp(_size);
405  for(int32_t iii=0; iii<m_data.m_size.x() && iii<tmppp.m_size.x(); iii++) {
406  for(int32_t jjj=0; jjj<m_data.m_size.y() && jjj<tmppp.m_size.y(); jjj++) {
407  tmppp(iii,jjj) = (*this)(iii,jjj);
408  }
409  }
410  return tmppp;
411  };
420  Matrix<T> select(int32_t _np, int32_t* _p, int32_t _nq, int32_t* _q) const {
421  if (_np < 1 || _nq < 1) {
422  TK_WARNING("bad index array sizes");
423  }
424  Matrix<T> tmppp(_np, _nq);
425  for (int32_t iii=0; iii<_np; iii++) {
426  for (int32_t jjj=0; jjj<_nq; jjj++) {
427  if( _p[i] < 0
428  || _p[i] >= m_size.x()
429  || _q[i] < 0
430  || _q[i] >= m_size.y()) {
431  TK_WARNING("bad index arrays");
432  }
433  tmppp(iii,jjj) = (*this)(_p[i],_q[j]);
434  }
435  }
436  return tmppp;
437  }
449  if (m_size.x() != m_size.y()) {
450  TK_WARNING("better to do with square Matrix");
451  }
452  for (int32_t iii=0; iii<m_size.x(); iii++) {
453  for (int32_t jjj=iii+1; jjj<m_size.y(); jjj++)
454  m_data[iii*m_size.x() + jjj] = 0;
455  }
456  }
457  };
469  if (m_size.x() != m_size.y()) {
470  TK_WARNING("better to do with square Matrix");
471  }
472  for (int32_t iii=0; iii<m_size.x(); iii++) {
473  for (int32_t jjj=0; jjj<m_size.y() && jjj<iii; jjj++)
474  m_data[iii*m_size.x() + jjj] = 0;
475  }
476  }
477  };
482  void makeRandom(float _range) {
483  for(int32_t iii=0; iii<m_data.size(); iii++) {
484  m_data[iii] = (T)etk::tool::frand(-_range, _range);
485  }
486  };
492  T maxDifference(const Matrix<T>& _input) const {
493  if (m_size != _input.m_size) {
494  TK_WARNING("better to do with same size Matrix");
495  }
496  T max = 0;
497  for(int32_t iii = 0;
498  iii < m_data.size() && iii < _input.m_data.size();
499  ++iii) {
500  T diff = m_data[iii] - _input.m_data[iii];
501  if (diff<0) {
502  diff = -diff;
503  }
504  if (diff > max) {
505  max = diff;
506  }
507  }
508  return max;
509  }
520  void clear() {
521  // copy data for the same size :
522  for (int32_t iii=0; iii< m_size.x()*m_size.y(); iii++) {
523  m_data[iii] = (T)0;
524  }
525  };
535  void identity() {
536  // copy data for the same size :
537  for (int32_t iii=0; iii< std::mim(m_size.x(), m_size.y()); iii++) {
538  (*this)(iii,iii) = (T)1;
539  }
540  };
544  void eye() {
545  clear();
546  identity();
547  };
552  const uivec2& size() const {
553  return m_size;
554  };
555  };
556 }
557 
558 // To siplify the writing of the code ==> this is not compatible with GLSL ...
const T & x() const
Get X value.
Definition: Vector2D.hpp:467
Matrix< T > operator*(const Matrix< T > &_obj)
Operator* Multiplication an other matrix with this one.
Definition: Matrix.hpp:269
void eye()
Clear and set the diagonal at 1.
Definition: Matrix.hpp:544
const Matrix< T > & operator*=(const Matrix< T > &_obj)
Operator*= Multiplication an other matrix with this one.
Definition: Matrix.hpp:244
Matrix(const Matrix< ETK_TYPE_MATRIX_2 > &_obj)
Copy contructor with ETK_TYPE_MATRIX_2 type matrix input.
Definition: Matrix.hpp:68
void clearUpperTriangle()
Clear the Upper triangle of the current Matrix.
Definition: Matrix.hpp:448
Matrix< T > transpose() const
Transpose Matrix.
Definition: Matrix.hpp:346
Matrix< T > operator-() const
Operator- Multiply with -1.
Definition: Matrix.hpp:335
const Matrix< T > & operator+=(const Matrix< T > &_obj)
Operator+= Addition an other matrix with this one.
Definition: Matrix.hpp:141
Matrix< T > round(int32_t _decalage) const
generate a devide of the curent Matrix with the specify power of 2
Definition: Matrix.hpp:391
bool operator==(const Matrix< T > &_obj) const
Equality compare operator with an other object.
Definition: Matrix.hpp:119
const uivec2 & size() const
Get the size of the current Matrix.
Definition: Matrix.hpp:552
basic namespace of the etk library. (it might contain all the etk fuctions/class/structures without m...
Definition: Archive.hpp:16
Matrix< T > resize(etk::Vector2D< int32_t > _size) const
Generate a resised matrix.
Definition: Matrix.hpp:403
Matrix< T > select(int32_t _np, int32_t *_p, int32_t _nq, int32_t *_q) const
Select element in the matrix from a list of element Ids.
Definition: Matrix.hpp:420
T maxDifference(const Matrix< T > &_input) const
Return the maximum of the diff for this Matrix.
Definition: Matrix.hpp:492
Matrix(const ivec2 &_size, T *_defaultVal=nullptr)
Contructor that create a Vector with a specific size and specific raw data.
Definition: Matrix.hpp:27
void makeRandom(float _range)
Generate a compleate random Matrix.
Definition: Matrix.hpp:482
Matrix(int32_t _width=0, int32_t _heigh=0, T *_defaultVal=nullptr)
default contructor that create a Vector with a specific size and specific raw data ...
Definition: Matrix.hpp:48
void identity()
Set the matrix identity.
Definition: Matrix.hpp:535
const Matrix< T > & operator=(const Matrix< T > &_obj)
Operator= Asign the current object with an other object.
Definition: Matrix.hpp:89
void clear()
Clear all the matrix.
Definition: Matrix.hpp:520
const T & y() const
Get Y value.
Definition: Vector2D.hpp:474
Matrix< T > fix(int32_t _decalage) const
generate a devide of the curent Matrix with the specify power of 2
Definition: Matrix.hpp:371
void clearLowerTriangle()
Clear the Lower triangle of the current Matrix.
Definition: Matrix.hpp:468
const Matrix< T > & operator-=(const Matrix< T > &_obj)
Operator+= Addition an other matrix with this one.
Definition: Matrix.hpp:195
2 dimention matrix template to manage simpliest algo
Definition: Matrix.hpp:17
virtual ~Matrix()=default
Virtualisation of destructor.
T & operator()(size_t _xxx, size_t _yyy)
Operator() Access at the element at a specific position.
Definition: Matrix.hpp:328
bool operator!=(const Matrix< T > &_obj) const
In-Equality compare operator with an other object.
Definition: Matrix.hpp:128
Matrix< T > operator+(const Matrix< T > &_obj)
Operator+= Addition an other matrix with this one.
Definition: Matrix.hpp:180
const T * operator[](int32_t _yyy) const
Operator[] Access at the first element (const pointer) of a line.
Definition: Matrix.hpp:283
double frand(double _a, double _b)
Get a random value in a specific range in float.
Matrix< T > convolution(Matrix< T > &_obj) const
Create a convolution on the matrix : set convolution on the lines.
Definition: Matrix.hpp:361