MorphoGraphX
Matrix.hpp
Go to the documentation of this file.
1 //
2 // This file is part of MorphoGraphX - http://www.MorphoGraphX.org
3 // Copyright (C) 2012-2015 Richard S. Smith and collaborators.
4 //
5 // If you use MorphoGraphX in your work, please cite:
6 // http://dx.doi.org/10.7554/eLife.05864
7 //
8 // MorphoGraphX is free software, and is licensed under under the terms of the
9 // GNU General (GPL) Public License version 2.0, http://www.gnu.org/licenses.
10 //
11 #ifndef MATRIX_HPP
12 #define MATRIX_HPP
13 
20 #include <Config.hpp>
21 
22 #include <cuda/CudaGlobal.hpp>
23 #include <StaticAssert.hpp>
24 #include <Util.hpp>
25 #include <Vector.hpp>
26 
27 #include <cmath>
28 #ifndef COMPILE_CUDA
29 # include <QTextStream>
30 #endif
31 
32 namespace mgx {
33 namespace util {
34 
35 enum MatrixLayout { C_STYLE, GL_STYLE };
36 
44 template <size_t nRows, size_t nCols, typename T = double> class Matrix {
45 private:
46  Vector<nCols, T> rows[nRows];
47 
48 public:
49  typedef T value_type;
50  typedef T& reference_type;
51  typedef const T& const_reference_type;
52  typedef T* pointer_type;
53  typedef const T* const_pointer_type;
54  typedef T* iterator;
55  typedef const T* const_iterator;
56 
57  static const size_t numcols = nCols;
58  static const size_t numrows = nRows;
59 
63  CU_HOST_DEVICE
64  Matrix(void)
65  {
66  for(size_t i = 0; i < nRows; i++)
67  for(size_t j = 0; j < nCols; j++)
68  rows[i][j] = 0;
69  }
70 
76  template <typename T1> CU_HOST_DEVICE Matrix(const Matrix<nRows, nCols, T1>& mat)
77  {
78  for(size_t i = 0; i < nRows; i++)
79  for(size_t j = 0; j < nCols; j++)
80  rows[i][j] = T(mat(i, j));
81  }
82 
88  template <typename T1> CU_HOST_DEVICE Matrix(const Vector<nCols, T1>* vecs)
89  {
90  for(size_t i = 0; i < nRows; i++)
91  rows[i] = vecs[i];
92  }
93 
103  template <typename T1> CU_HOST_DEVICE Matrix(const T1* values, MatrixLayout layout = C_STYLE)
104  {
105  switch(layout) {
106  case C_STYLE: {
107  for(size_t i = 0; i < nRows; i++) {
108  rows[i] = Vector<nCols, T>(values + (i * nCols));
109  }
110  } break;
111  case GL_STYLE: {
112  for(size_t j = 0; j < nCols; j++)
113  for(size_t i = 0; i < nRows; i++) {
114  rows[i][j] = T(values[i + j * nRows]);
115  }
116  } break;
117  }
118  }
119 
129  CU_HOST_DEVICE
130  Matrix(const T* values, MatrixLayout layout = C_STYLE)
131  {
132  switch(layout) {
133  case C_STYLE: {
134  for(size_t i = 0; i < nRows; i++) {
135  rows[i] = Vector<nCols, T>(values + (i * nCols));
136  }
137  } break;
138  case GL_STYLE: {
139  for(size_t i = 0; i < nRows; i++)
140  for(size_t j = 0; j < nCols; j++) {
141  rows[i][j] = values[i + j * nRows];
142  }
143  } break;
144  }
145  }
146 
152  CU_HOST_DEVICE
153  Matrix(const T& value)
154  {
155  for(size_t i = 0; i < nRows; i++)
156  for(size_t j = 0; j < nCols; j++)
157  rows[i][j] = (i == j) ? value : 0;
158  }
159 
165  CU_HOST_DEVICE
167  return Vector<2, size_t>(nRows, nCols);
168  }
169 
173  CU_HOST_DEVICE
174  static size_t nbRows() {
175  return nRows;
176  }
177 
181  CU_HOST_DEVICE
182  static size_t nbColumns() {
183  return nCols;
184  }
185 
191  CU_HOST_DEVICE
192  const T* c_data() const {
193  return rows[0].c_data();
194  }
195 
201  CU_HOST_DEVICE
202  T* data() {
203  return rows[0].data();
204  }
205 
209  CU_HOST_DEVICE
210  Matrix operator-(void) const
211  {
212  Matrix ans;
213 
214  for(size_t i = 0; i < nRows; i++)
215  ans.rows[i] = -rows[i];
216 
217  return ans;
218  }
219 
223  CU_HOST_DEVICE
224  Matrix operator+(const Matrix& mat) const
225  {
226  Matrix ans;
227 
228  for(size_t i = 0; i < nRows; i++)
229  ans.rows[i] = rows[i] + mat.rows[i];
230 
234  return ans;
235  }
236 
237  CU_HOST_DEVICE
238  Matrix operator-(const Matrix& mat) const
239  {
240  Matrix ans;
241 
242  for(size_t i = 0; i < nRows; i++)
243  ans.rows[i] = rows[i] - mat.rows[i];
244 
245  return ans;
246  }
247 
251  CU_HOST_DEVICE
252  Matrix operator*(const T& scalar) const
253  {
254  Matrix ans;
255 
256  for(size_t i = 0; i < nRows; i++)
257  ans[i] = rows[i] * scalar;
258 
259  return ans;
260  }
261 
265  CU_HOST_DEVICE
266  Matrix operator/(const T& scalar) const
267  {
268  Matrix ans;
269 
270  for(size_t i = 0; i < nRows; i++)
271  ans[i] = rows[i] / scalar;
272 
273  return ans;
274  }
275 
279  CU_HOST_DEVICE
280  friend Matrix operator*(const T& scalar, const Matrix& mat)
281  {
282  Matrix ans;
283 
284  for(size_t i = 0; i < nRows; i++)
285  ans[i] = scalar * mat.rows[i];
286 
287  return ans;
288  }
289 
293  CU_HOST_DEVICE
295  {
296  Vector<nRows, T> ans;
297  for(size_t i = 0; i < nRows; ++i) {
298  T value = 0;
299  for(size_t j = 0; j < nCols; ++j)
300  value += rows[i][j] * vec[j];
301  ans[i] = value;
302  }
303  return ans;
304  }
305 
306  CU_HOST_DEVICE
307  Matrix& operator=(const Matrix& mat)
308  {
309  for(size_t i = 0; i < nRows; i++)
310  rows[i] = mat.rows[i];
311 
312  return (*this);
313  }
314 
315  CU_HOST_DEVICE
316  Matrix& operator+=(const Matrix& mat) {
317  return ((*this) = (*this) + mat);
318  }
319 
320  CU_HOST_DEVICE
321  Matrix& operator-=(const Matrix& mat) {
322  return ((*this) = (*this) - mat);
323  }
324 
325  CU_HOST_DEVICE
326  Matrix& operator*=(const T& scalar) {
327  return ((*this) = (*this) * scalar);
328  }
329 
330  CU_HOST_DEVICE
331  Matrix& operator/=(const T& scalar) {
332  return ((*this) = (*this) / scalar);
333  }
334 
335  CU_HOST_DEVICE
336  Matrix& operator*=(const Matrix& mat) {
337  return ((*this) = (*this) * mat);
338  }
339 
340  bool operator==(const Matrix& mat) const
341  {
342  for(size_t i = 0; i < nRows; i++)
343  if(rows[i] != mat.rows[i])
344  return false;
345 
346  return true;
347  }
348 
349  CU_HOST_DEVICE
350  bool operator!=(const Matrix& mat) const {
351  return (!((*this) == mat));
352  }
353 
354 #ifndef COMPILE_CUDA
355  friend QTextStream& operator<<(QTextStream& out, const Matrix& mat)
356  {
357  for(size_t i = 0; i < nRows; i++) {
358  out << mat.rows[i];
359  if(i != (nRows - 1))
360  out << " ";
361  }
362 
363  return out;
364  }
365 
366  friend QTextStream& operator>>(QTextStream& in, Matrix& mat)
367  {
368  for(size_t i = 0; i < nRows && !in.atEnd(); i++)
369  in >> mat.rows[i];
370  return in;
371  }
372 #endif
373 
374  CU_HOST_DEVICE
375  friend std::ostream& operator<<(std::ostream& out, const Matrix& mat)
376  {
377  for(size_t i = 0; i < nRows; i++) {
378  out << mat.rows[i];
379  if(i != (nRows - 1))
380  out << " ";
381  }
382 
383  return out;
384  }
385 
386  CU_HOST_DEVICE
387  friend std::istream& operator>>(std::istream& in, Matrix& mat)
388  {
389  for(size_t i = 0; i < nRows && in; i++)
390  in >> mat.rows[i];
391  return in;
392  }
393 
399  CU_HOST_DEVICE
401  return rows[idx];
402  }
403 
409  CU_HOST_DEVICE
410  Vector<nCols, T> operator[](size_t idx) const {
411  return rows[idx];
412  }
413 
417  CU_HOST_DEVICE
418  T& operator()(size_t i, size_t j) {
419  return rows[i][j];
420  }
421 
425  CU_HOST_DEVICE
426  T operator()(size_t i, size_t j) const {
427  return rows[i][j];
428  }
429 
433  CU_HOST_DEVICE
434  static Matrix identity()
435  {
436  Matrix mat(1);
437  return mat;
438  }
439 
443  CU_HOST_DEVICE
444  Matrix& zero(void)
445  {
446  for(size_t i = 0; i < nRows; i++)
447  for(size_t j = 0; j < nCols; j++)
448  rows[i][j] = 0.0;
449  return (*this);
450  }
451 
457  CU_HOST_DEVICE
458  Matrix& operator=(const T& value)
459  {
460  for(size_t i = 0; i < nRows; ++i) {
461  for(size_t j = 0; j < nCols; ++j) {
462  if(i == j)
463  rows[i][j] = value;
464  else
465  rows[i][j] = 0;
466  }
467  }
468  return *this;
469  }
470 
474  CU_HOST_DEVICE
476  {
478  for(size_t i = 0; i < nRows; ++i)
479  for(size_t j = 0; j < nCols; ++j)
480  t[i][j] = rows[j][i];
481  return t;
482  }
483 
490  CU_HOST_DEVICE
491  static Matrix<3, 3, T> rotation(const Vector<3, T>& direction, T angle)
492  {
493  T ca = std::cos(angle);
494  T sa = std::sin(angle);
495  Matrix<3, 3, T> r;
496  T x = direction.x();
497  T y = direction.y();
498  T z = direction.z();
499  r[0].set(ca + (1 - ca) * x * x, (1 - ca) * x * y - sa * z, (1 - ca) * z * x + sa * y);
500  r[1].set((1 - ca) * y * x + sa * z, ca + (1 - ca) * y * y, (1 - ca) * z * y - sa * x);
501  r[2].set((1 - ca) * x * z - sa * y, (1 - ca) * y * z + sa * x, ca + (1 - ca) * z * z);
502  return r;
503  }
504 
511  CU_HOST_DEVICE
512  static Matrix<4, 4, T> rotation(const Vector<4, T>& direction, T angle)
513  {
514  T ca = std::cos(angle);
515  T sa = std::sin(angle);
516  Matrix<4, 4, T> r;
517  T x = direction.x() / direction.t();
518  T y = direction.y() / direction.t();
519  T z = direction.z() / direction.t();
520  r[0].set(ca + (1 - ca) * x * x, (1 - ca) * x * y - sa * z, (1 - ca) * z * x + sa * y, 0);
521  r[1].set((1 - ca) * y * x + sa * z, ca + (1 - ca) * y * y, (1 - ca) * z * y - sa * x, 0);
522  r[2].set((1 - ca) * x * z - sa * y, (1 - ca) * y * z + sa * x, ca + (1 - ca) * z * z, 0);
523  r[3].set(0, 0, 0, 1);
524  return r;
525  }
526 
530  CU_HOST_DEVICE
531  T trace() const
532  {
533  T acc = 0;
534  for(size_t i = 0; i < min(nRows, nCols); ++i) {
535  acc += rows[i][i];
536  }
537  return acc;
538  }
539 
540  CU_HOST_DEVICE
541  void fillArray(T* array, MatrixLayout layout = C_STYLE)
542  {
543  switch(layout) {
544  case C_STYLE:
545  memcpy(array, &rows[0][0], sizeof(T) * nRows * nCols);
546  break;
547  case GL_STYLE: {
548  size_t k = 0;
549  for(size_t c = 0; c < nCols; ++c)
550  for(size_t r = 0; r < nRows; ++r, ++k)
551  array[k] = rows[r][c];
552  } break;
553  }
554  }
555 
559  CU_HOST_DEVICE
561  {
562  STATIC_ASSERT(nRows == nCols);
563  Vector<nRows, T> res;
564  for(size_t i = 0; i < nRows; ++i)
565  res[i] = (*this)(i, i);
566  return res;
567  }
568 };
569 
574 template <size_t nRows, size_t nCols, typename T>
575 CU_HOST_DEVICE Vector<nCols, T> operator*(const Vector<nCols, T>& vec, const Matrix<nRows, nCols, T>& mat);
576 
581 template <size_t nRows, size_t nSize, size_t nCols, typename T>
584 
589 template <typename T> T CU_HOST_DEVICE det(const Matrix<1, 1, T>& mat);
594 template <typename T> T CU_HOST_DEVICE det(const Matrix<2, 2, T>& mat);
599 template <typename T> T CU_HOST_DEVICE det(const Matrix<3, 3, T>& mat);
606 template <size_t nRows, typename T> T CU_HOST_DEVICE det(const Matrix<nRows, nRows, T>& mat);
607 
608 template <size_t nRows, typename T>
609 CU_HOST_DEVICE T matrix_minor(const Matrix<nRows, nRows, T>& mat, size_t i, size_t j);
610 
615 template <size_t nRows, typename T> CU_HOST_DEVICE T cofactor(const Matrix<nRows, nRows, T>& mat, size_t i, size_t j);
616 
622 template <typename T> CU_HOST_DEVICE Matrix<1, 1, T> inverse(const Matrix<1, 1, T>& mat);
628 template <typename T> CU_HOST_DEVICE Matrix<2, 2, T> inverse(const Matrix<2, 2, T>& mat);
634 template <typename T> CU_HOST_DEVICE Matrix<3, 3, T> inverse(const Matrix<3, 3, T>& mat);
640 template <typename T> CU_HOST_DEVICE Matrix<4, 4, T> inverse(const Matrix<4, 4, T>& mat);
641 
649 template <size_t nRows, typename T> CU_HOST_DEVICE Matrix<nRows, nRows, T> inverse(const Matrix<nRows, nRows, T>& mat);
650 
656 template <size_t nRows, size_t nCols, typename T>
658 
667 template <size_t nRows, size_t nCols, typename T> CU_HOST_DEVICE T norm(const Matrix<nRows, nCols, T>& mat);
668 
676 template <size_t nRows, size_t nCols, typename T> CU_HOST_DEVICE T normsq(const Matrix<nRows, nCols, T>& mat);
677 
683 template <size_t nRows, size_t nCols, typename T>
684 CU_HOST_DEVICE Matrix<nRows, nCols, T> map(const T& (*fct)(const T &), const Matrix<nRows, nCols, T>& m)
685 {
687  for(size_t i = 0; i < nRows; ++i) {
688  const Vector<nCols, T>& mrow = m[i];
689  Vector<nCols, T>& rrow = result[i];
690  for(size_t j = 0; j < nCols; ++j) {
691  rrow[j] = (*fct)(mrow[j]);
692  }
693  }
694  return result;
695 }
696 
702 template <size_t nRows, size_t nCols, typename T>
703 CU_HOST_DEVICE Matrix<nRows, nCols, T> map(T (*fct)(T), const Matrix<nRows, nCols, T>& m)
704 {
706  for(size_t i = 0; i < nRows; ++i) {
707  const Vector<nCols, T>& mrow = m[i];
708  Vector<nCols, T>& rrow = result[i];
709  for(size_t j = 0; j < nCols; ++j) {
710  rrow[j] = (*fct)(mrow[j]);
711  }
712  }
713  return result;
714 }
715 
721 template <size_t nRows, size_t nCols, typename T>
722 CU_HOST_DEVICE Matrix<nRows, nCols, T> map(T (*fct)(const T&), const Matrix<nRows, nCols, T>& m)
723 {
725  for(size_t i = 0; i < nRows; ++i) {
726  const Vector<nCols, T>& mrow = m[i];
727  Vector<nCols, T>& rrow = result[i];
728  for(size_t j = 0; j < nCols; ++j) {
729  rrow[j] = (*fct)(mrow[j]);
730  }
731  }
732  return result;
733 }
734 
740 template <size_t nRows, size_t nCols, typename T, typename T1>
741 CU_HOST_DEVICE Matrix<nRows, nCols, T> map(const T& (*fct)(const T1 &), const Matrix<nRows, nCols, T1>& m)
742 {
744  for(size_t i = 0; i < nRows; ++i) {
745  const Vector<nCols, T1>& mrow = m[i];
746  Vector<nCols, T>& rrow = result[i];
747  for(size_t j = 0; j < nCols; ++j) {
748  rrow[j] = (*fct)(mrow[j]);
749  }
750  }
751  return result;
752 }
753 
759 template <size_t nRows, size_t nCols, typename T, typename T1>
760 CU_HOST_DEVICE Matrix<nRows, nCols, T> map(T (*fct)(T1), const Matrix<nRows, nCols, T1>& m)
761 {
763  for(size_t i = 0; i < nRows; ++i) {
764  const Vector<nCols, T1>& mrow = m[i];
765  Vector<nCols, T>& rrow = result[i];
766  for(size_t j = 0; j < nCols; ++j) {
767  rrow[j] = (*fct)(mrow[j]);
768  }
769  }
770  return result;
771 }
772 
778 template <size_t nRows, size_t nCols, typename T, typename T1>
779 CU_HOST_DEVICE Matrix<nRows, nCols, T> map(T (*fct)(const T1&), const Matrix<nRows, nCols, T1>& m)
780 {
782  for(size_t i = 0; i < nRows; ++i) {
783  const Vector<nCols, T1>& mrow = m[i];
784  Vector<nCols, T>& rrow = result[i];
785  for(size_t j = 0; j < nCols; ++j) {
786  rrow[j] = (*fct)(mrow[j]);
787  }
788  }
789  return result;
790 }
791 
797 template <size_t nRows, size_t nCols, typename T>
798 CU_HOST_DEVICE Matrix<nRows, nCols, T> map(T (*fct)(T, T), const Matrix<nRows, nCols, T>& m1,
799  const Matrix<nRows, nCols, T>& m2)
800 {
802  for(size_t i = 0; i < nRows; ++i) {
803  const Vector<nCols, T>& mrow1 = m1[i];
804  const Vector<nCols, T>& mrow2 = m2[i];
805  Vector<nCols, T>& rrow = result[i];
806  for(size_t j = 0; j < nCols; ++j) {
807  rrow[j] = (*fct)(mrow1[j], mrow2[j]);
808  }
809  }
810  return result;
811 }
812 
818 template <size_t nRows, size_t nCols, typename T>
819 CU_HOST_DEVICE Matrix<nRows, nCols, T> map(T (*fct)(const T&, const T&), const Matrix<nRows, nCols, T>& m1,
820  const Matrix<nRows, nCols, T>& m2)
821 {
823  for(size_t i = 0; i < nRows; ++i) {
824  const Vector<nCols, T>& mrow1 = m1[i];
825  const Vector<nCols, T>& mrow2 = m2[i];
826  Vector<nCols, T>& rrow = result[i];
827  for(size_t j = 0; j < nCols; ++j) {
828  rrow[j] = (*fct)(mrow1[j], mrow2[j]);
829  }
830  }
831  return result;
832 }
833 
839 template <size_t nRows, size_t nCols, typename T>
840 CU_HOST_DEVICE Matrix<nRows, nCols, T> map(const T& (*fct)(const T &, const T &), const Matrix<nRows, nCols, T>& m1,
841  const Matrix<nRows, nCols, T>& m2)
842 {
844  for(size_t i = 0; i < nRows; ++i) {
845  const Vector<nCols, T>& mrow1 = m1[i];
846  const Vector<nCols, T>& mrow2 = m2[i];
847  Vector<nCols, T>& rrow = result[i];
848  for(size_t j = 0; j < nCols; ++j) {
849  rrow[j] = (*fct)(mrow1[j], mrow2[j]);
850  }
851  }
852  return result;
853 }
854 
860 template <size_t nRows, size_t nCols, typename T, typename T1, typename T2>
861 CU_HOST_DEVICE Matrix<nRows, nCols, T> map(T (*fct)(T1, T2), const Matrix<nRows, nCols, T1>& m1,
862  const Matrix<nRows, nCols, T2>& m2)
863 {
865  for(size_t i = 0; i < nRows; ++i) {
866  const Vector<nCols, T1>& mrow1 = m1[i];
867  const Vector<nCols, T2>& mrow2 = m2[i];
868  Vector<nCols, T>& rrow = result[i];
869  for(size_t j = 0; j < nCols; ++j) {
870  rrow[j] = (*fct)(mrow1[j], mrow2[j]);
871  }
872  }
873  return result;
874 }
875 
881 template <size_t nRows, size_t nCols, typename T, typename T1, typename T2>
882 CU_HOST_DEVICE Matrix<nRows, nCols, T> map(T (*fct)(const T1&, const T2&), const Matrix<nRows, nCols, T1>& m1,
883  const Matrix<nRows, nCols, T2>& m2)
884 {
886  for(size_t i = 0; i < nRows; ++i) {
887  const Vector<nCols, T1>& mrow1 = m1[i];
888  const Vector<nCols, T2>& mrow2 = m2[i];
889  Vector<nCols, T>& rrow = result[i];
890  for(size_t j = 0; j < nCols; ++j) {
891  rrow[j] = (*fct)(mrow1[j], mrow2[j]);
892  }
893  }
894  return result;
895 }
896 
902 template <size_t nRows, size_t nCols, typename T, typename T1, typename T2>
903 CU_HOST_DEVICE Matrix<nRows, nCols, T> map(const T& (*fct)(const T1 &, const T2 &), const Matrix<nRows, nCols, T1>& m1,
904  const Matrix<nRows, nCols, T2>& m2)
905 {
907  for(size_t i = 0; i < nRows; ++i) {
908  const Vector<nCols, T1>& mrow1 = m1[i];
909  const Vector<nCols, T2>& mrow2 = m2[i];
910  Vector<nCols, T>& rrow = result[i];
911  for(size_t j = 0; j < nCols; ++j) {
912  rrow[j] = (*fct)(mrow1[j], mrow2[j]);
913  }
914  }
915  return result;
916 }
917 
918 template <size_t nRows, size_t nCols, typename T>
920 {
921  Vector<nCols, T> ans;
922  for(size_t i = 0; i < nCols; ++i) {
923  T value = 0;
924  for(size_t j = 0; j < nRows; ++j)
925  value += mat(j, i) * vec[j];
926  ans[i] = value;
927  }
928  return ans;
929 }
930 
931 template <size_t nRows, size_t intSize, size_t nCols, typename T>
933  const Matrix<intSize, nCols, T>& mat2)
934 {
936  for(size_t i = 0; i < nRows; i++)
937  for(size_t j = 0; j < nCols; j++) {
938  T acc = 0;
939  for(size_t k = 0; k < intSize; k++)
940  acc += mat1(i, k) * mat2(k, j);
941  ans(i, j) = acc;
942  }
943  return ans;
944 }
945 
946 template <typename T> CU_HOST_DEVICE T det(const Matrix<1, 1, T>& mat) {
947  return mat(0, 0);
948 }
949 
950 template <typename T> CU_HOST_DEVICE T det(const Matrix<2, 2, T>& mat)
951 {
952  return mat(0, 0) * mat(1, 1) - mat(0, 1) * mat(1, 0);
953 }
954 
955 template <typename T> CU_HOST_DEVICE T det(const Matrix<3, 3, T>& mat)
956 {
957  return mat(0, 0) * mat(1, 1) * mat(2, 2) + mat(0, 1) * mat(1, 2) * mat(2, 0) + mat(0, 2) * mat(1, 0) * mat(2, 1)
958  - mat(0, 0) * mat(1, 2) * mat(2, 1) - mat(0, 1) * mat(1, 0) * mat(2, 2) - mat(0, 2) * mat(1, 1) * mat(2, 0);
959 }
960 
961 template <size_t nRows, typename T> CU_HOST_DEVICE T det(const Matrix<nRows, nRows, T>& mat)
962 {
963  STATIC_ASSERT(nRows > 3);
964  T acc = 0;
965  for(size_t i = 0; i < nRows; i++) {
966  acc += mat(i, 0) * cofactor(mat, i, 0);
967  }
968  return acc;
969 }
970 
971 template <size_t nRows, typename T>
972 CU_HOST_DEVICE T matrix_minor(const Matrix<nRows, nRows, T>& mat, size_t i, size_t j)
973 {
974  STATIC_ASSERT(nRows > 0);
975  Matrix<nRows - 1, nRows - 1, T> ans;
976  for(size_t i1 = 0, i2 = 0; i1 < nRows; i1++, i2++) {
977  if(i1 == i) {
978  i2--;
979  } else {
980  for(size_t j1 = 0, j2 = 0; j1 < nRows; j1++, j2++) {
981  if(j1 == j) {
982  j2--;
983  } else {
984  ans(i2, j2) = mat(i1, j1);
985  }
986  }
987  }
988  }
989  return det(ans);
990 }
991 
992 template <size_t nRows, typename T> CU_HOST_DEVICE T cofactor(const Matrix<nRows, nRows, T>& mat, size_t i, size_t j)
993 {
994  T inv = 1;
995  if((i + j) % 2) {
996  inv = -1;
997  }
998  return inv * matrix_minor(mat, i, j);
999 }
1000 
1001 template <typename T> CU_HOST_DEVICE Matrix<1, 1, T> inverse(const Matrix<1, 1, T>& mat)
1002 {
1003  Matrix<1, 1, T> ans;
1004  ans[0][0] = 1 / mat(0, 0);
1005  return ans;
1006 }
1007 
1008 template <typename T> CU_HOST_DEVICE Matrix<2, 2, T> inverse(const Matrix<2, 2, T>& mat)
1009 {
1010  Matrix<2, 2, T> ans;
1011  T d;
1012  d = det(mat);
1013  if(d == 0)
1014  return ans;
1015  ans(0, 0) = mat(1, 1) / d;
1016  ans(0, 1) = mat(0, 1) / -d;
1017  ans(1, 0) = mat(1, 0) / -d;
1018  ans(1, 1) = mat(0, 0) / d;
1019  return ans;
1020 }
1021 
1022 template <typename T> CU_HOST_DEVICE Matrix<3, 3, T> inverse(const Matrix<3, 3, T>& mat)
1023 {
1024  Matrix<3, 3, T> ans;
1025  T d;
1026  d = det(mat);
1027  if(d == 0)
1028  return ans;
1029  ans(0, 0) = (mat(1, 1) * mat(2, 2) - mat(1, 2) * mat(2, 1)) / d;
1030  ans(1, 1) = (mat(0, 0) * mat(2, 2) - mat(0, 2) * mat(2, 0)) / d;
1031  ans(2, 2) = (mat(1, 1) * mat(0, 0) - mat(1, 0) * mat(0, 1)) / d;
1032  ans(1, 0) = (mat(1, 2) * mat(2, 0) - mat(1, 0) * mat(2, 2)) / d;
1033  ans(0, 1) = (mat(2, 1) * mat(0, 2) - mat(0, 1) * mat(2, 2)) / d;
1034  ans(2, 0) = (mat(1, 0) * mat(2, 1) - mat(1, 1) * mat(2, 0)) / d;
1035  ans(0, 2) = (mat(0, 1) * mat(1, 2) - mat(1, 1) * mat(0, 2)) / d;
1036  ans(1, 2) = (mat(0, 2) * mat(1, 0) - mat(0, 0) * mat(1, 2)) / d;
1037  ans(2, 1) = (mat(2, 0) * mat(0, 1) - mat(0, 0) * mat(2, 1)) / d;
1038  return ans;
1039 }
1040 
1041 template <typename T> Matrix<4, 4, T> inverse(const Matrix<4, 4, T>& m)
1042 {
1043  Matrix<4, 4, T> inv;
1044 
1045  inv(0, 0) = (m(1, 1) * m(2, 2) * m(3, 3) - m(1, 1) * m(3, 2) * m(2, 3) - m(1, 2) * m(2, 1) * m(3, 3)
1046  + m(1, 2) * m(3, 1) * m(2, 3) + m(1, 3) * m(2, 1) * m(3, 2) - m(1, 3) * m(3, 1) * m(2, 2));
1047 
1048  inv(0, 1) = (-m(0, 1) * m(2, 2) * m(3, 3) + m(0, 1) * m(3, 2) * m(2, 3) + m(0, 2) * m(2, 1) * m(3, 3)
1049  - m(0, 2) * m(3, 1) * m(2, 3) - m(0, 3) * m(2, 1) * m(3, 2) + m(0, 3) * m(3, 1) * m(2, 2));
1050 
1051  inv(0, 2) = (m(0, 1) * m(1, 2) * m(3, 3) - m(0, 1) * m(3, 2) * m(1, 3) - m(0, 2) * m(1, 1) * m(3, 3)
1052  + m(0, 2) * m(3, 1) * m(1, 3) + m(0, 3) * m(1, 1) * m(3, 2) - m(0, 3) * m(3, 1) * m(1, 2));
1053 
1054  inv(0, 3) = (-m(0, 1) * m(1, 2) * m(2, 3) + m(0, 1) * m(2, 2) * m(1, 3) + m(0, 2) * m(1, 1) * m(2, 3)
1055  - m(0, 2) * m(2, 1) * m(1, 3) - m(0, 3) * m(1, 1) * m(2, 2) + m(0, 3) * m(2, 1) * m(1, 2));
1056 
1057  inv(1, 0) = (-m(1, 0) * m(2, 2) * m(3, 3) + m(1, 0) * m(3, 2) * m(2, 3) + m(1, 2) * m(2, 0) * m(3, 3)
1058  - m(1, 2) * m(3, 0) * m(2, 3) - m(1, 3) * m(2, 0) * m(3, 2) + m(1, 3) * m(3, 0) * m(2, 2));
1059 
1060  inv(1, 1) = (m(0, 0) * m(2, 2) * m(3, 3) - m(0, 0) * m(3, 2) * m(2, 3) - m(0, 2) * m(2, 0) * m(3, 3)
1061  + m(0, 2) * m(3, 0) * m(2, 3) + m(0, 3) * m(2, 0) * m(3, 2) - m(0, 3) * m(3, 0) * m(2, 2));
1062 
1063  inv(1, 2) = (-m(0, 0) * m(1, 2) * m(3, 3) + m(0, 0) * m(3, 2) * m(1, 3) + m(0, 2) * m(1, 0) * m(3, 3)
1064  - m(0, 2) * m(3, 0) * m(1, 3) - m(0, 3) * m(1, 0) * m(3, 2) + m(0, 3) * m(3, 0) * m(1, 2));
1065 
1066  inv(1, 3) = (m(0, 0) * m(1, 2) * m(2, 3) - m(0, 0) * m(2, 2) * m(1, 3) - m(0, 2) * m(1, 0) * m(2, 3)
1067  + m(0, 2) * m(2, 0) * m(1, 3) + m(0, 3) * m(1, 0) * m(2, 2) - m(0, 3) * m(2, 0) * m(1, 2));
1068 
1069  inv(2, 0) = (m(1, 0) * m(2, 1) * m(3, 3) - m(1, 0) * m(3, 1) * m(2, 3) - m(1, 1) * m(2, 0) * m(3, 3)
1070  + m(1, 1) * m(3, 0) * m(2, 3) + m(1, 3) * m(2, 0) * m(3, 1) - m(1, 3) * m(3, 0) * m(2, 1));
1071 
1072  inv(2, 1) = (-m(0, 0) * m(2, 1) * m(3, 3) + m(0, 0) * m(3, 1) * m(2, 3) + m(0, 1) * m(2, 0) * m(3, 3)
1073  - m(0, 1) * m(3, 0) * m(2, 3) - m(0, 3) * m(2, 0) * m(3, 1) + m(0, 3) * m(3, 0) * m(2, 1));
1074 
1075  inv(2, 2) = (m(0, 0) * m(1, 1) * m(3, 3) - m(0, 0) * m(3, 1) * m(1, 3) - m(0, 1) * m(1, 0) * m(3, 3)
1076  + m(0, 1) * m(3, 0) * m(1, 3) + m(0, 3) * m(1, 0) * m(3, 1) - m(0, 3) * m(3, 0) * m(1, 1));
1077 
1078  inv(2, 3) = (-m(0, 0) * m(1, 1) * m(2, 3) + m(0, 0) * m(2, 1) * m(1, 3) + m(0, 1) * m(1, 0) * m(2, 3)
1079  - m(0, 1) * m(2, 0) * m(1, 3) - m(0, 3) * m(1, 0) * m(2, 1) + m(0, 3) * m(2, 0) * m(1, 1));
1080 
1081  inv(3, 0) = (-m(1, 0) * m(2, 1) * m(3, 2) + m(1, 0) * m(3, 1) * m(2, 2) + m(1, 1) * m(2, 0) * m(3, 2)
1082  - m(1, 1) * m(3, 0) * m(2, 2) - m(1, 2) * m(2, 0) * m(3, 1) + m(1, 2) * m(3, 0) * m(2, 1));
1083 
1084  inv(3, 1) = (m(0, 0) * m(2, 1) * m(3, 2) - m(0, 0) * m(3, 1) * m(2, 2) - m(0, 1) * m(2, 0) * m(3, 2)
1085  + m(0, 1) * m(3, 0) * m(2, 2) + m(0, 2) * m(2, 0) * m(3, 1) - m(0, 2) * m(3, 0) * m(2, 1));
1086 
1087  inv(3, 2) = (-m(0, 0) * m(1, 1) * m(3, 2) + m(0, 0) * m(3, 1) * m(1, 2) + m(0, 1) * m(1, 0) * m(3, 2)
1088  - m(0, 1) * m(3, 0) * m(1, 2) - m(0, 2) * m(1, 0) * m(3, 1) + m(0, 2) * m(3, 0) * m(1, 1));
1089 
1090  inv(3, 3) = (m(0, 0) * m(1, 1) * m(2, 2) - m(0, 0) * m(2, 1) * m(1, 2) - m(0, 1) * m(1, 0) * m(2, 2)
1091  + m(0, 1) * m(2, 0) * m(1, 2) + m(0, 2) * m(1, 0) * m(2, 1) - m(0, 2) * m(2, 0) * m(1, 1));
1092 
1093  T det = m(0, 0) * inv(0, 0) + m(1, 0) * inv(0, 1) + m(2, 0) * inv(0, 2) + m(3, 0) * inv(0, 3);
1094 
1095  T inv_det = 1.0 / det;
1096 
1097  return inv_det * inv;
1098 }
1099 
1100 template <size_t nRows, typename T> CU_HOST_DEVICE Matrix<nRows, nRows, T> inverse(const Matrix<nRows, nRows, T>& mat)
1101 {
1103  T d = det(mat);
1104  if(d == 0)
1105  return ans;
1106  for(size_t i = 0; i < nRows; ++i)
1107  for(size_t j = 0; j < nRows; ++j) {
1108  ans(i, j) = cofactor(mat, j, i) / d;
1109  }
1110  return ans;
1111 }
1112 
1113 template <size_t nRows, size_t nCols, typename T>
1115 {
1117  for(size_t i = 0; i < nRows; i++)
1118  for(size_t j = 0; j < nCols; j++)
1119  ans(j, i) = mat(i, j);
1120  return ans;
1121 }
1122 
1123 template <size_t nRows, size_t nCols, typename T> CU_HOST_DEVICE T normsq(const Matrix<nRows, nCols, T>& mat)
1124 {
1125  T acc = 0;
1126  for(size_t i = 0; i < nRows; i++)
1127  for(size_t j = 0; j < nCols; j++) {
1128  const T& v = mat(i, j);
1129  acc += v * v;
1130  }
1131  return acc;
1132 }
1133 
1134 template <size_t nRows, size_t nCols, typename T> CU_HOST_DEVICE T norm(const Matrix<nRows, nCols, T>& mat)
1135 {
1136  return std::sqrt(normsq(mat));
1137 }
1138 
1139 }}
1140 #endif
CU_HOST_DEVICE Matrix & zero(void)
Set the matrix to all zero.
Definition: Matrix.hpp:444
CU_HOST_DEVICE void t(const T &v)
Short access to the fourth element.
Definition: Vector.hpp:688
CU_HOST_DEVICE Matrix< nRows, nCols, T > map(T(*fct)(const T &), const Matrix< nRows, nCols, T > &m)
Apply a unary function to each element of the matrix.
Definition: Matrix.hpp:722
CU_HOST_DEVICE T cofactor(const Matrix< nRows, nRows, T > &mat, size_t i, size_t j)
Returns the cofactor of the matrix for position (i,j)
Definition: Matrix.hpp:992
CU_HOST_DEVICE Matrix(const T1 *values, MatrixLayout layout=C_STYLE)
Fill in the matrix with the values.
Definition: Matrix.hpp:103
CU_HOST_DEVICE const T * c_data() const
Returns a constant raw pointer on the data.
Definition: Vector.hpp:272
CU_HOST_DEVICE Matrix< nRows, nCols, T > map(T(*fct)(const T1 &, const T2 &), const Matrix< nRows, nCols, T1 > &m1, const Matrix< nRows, nCols, T2 > &m2)
Apply a binary function to each element of the matrix.
Definition: Matrix.hpp:882
CU_HOST_DEVICE friend Matrix operator*(const T &scalar, const Matrix &mat)
Matrix-scalar multiplication.
Definition: Matrix.hpp:280
CU_HOST_DEVICE T operator()(size_t i, size_t j) const
Return the value at row i, column j.
Definition: Matrix.hpp:426
CU_HOST_DEVICE Matrix operator-(void) const
Matrix subtraction.
Definition: Matrix.hpp:210
CU_HOST_DEVICE Matrix & operator=(const T &value)
Set the matrix to a diagonal matrix.
Definition: Matrix.hpp:458
static CU_HOST_DEVICE Matrix< 4, 4, T > rotation(const Vector< 4, T > &direction, T angle)
Creates the 4x4 matrix corresponding to a rotation.
Definition: Matrix.hpp:512
CU_HOST_DEVICE T trace() const
Trace of the matrix.
Definition: Matrix.hpp:531
CU_HOST_DEVICE void x(const T &v)
Short access to the first element.
Definition: Vector.hpp:661
CU_HOST_DEVICE const T * c_data() const
Returns a constant raw pointer on the data.
Definition: Matrix.hpp:192
CU_HOST_DEVICE Vector< nCols, T > & operator[](size_t idx)
Returns the nth row.
Definition: Matrix.hpp:400
static CU_HOST_DEVICE Matrix< 3, 3, T > rotation(const Vector< 3, T > &direction, T angle)
Creates the 3x3 matrix corresponding to a rotation.
Definition: Matrix.hpp:491
CU_HOST_DEVICE Matrix< nRows, nCols, T > map(const T &(*fct)(const T &, const T &), const Matrix< nRows, nCols, T > &m1, const Matrix< nRows, nCols, T > &m2)
Apply a binary function to each element of the matrix.
Definition: Matrix.hpp:840
CU_HOST_DEVICE Matrix< nRows, nCols, T > map(T(*fct)(const T1 &), const Matrix< nRows, nCols, T1 > &m)
Apply a unary function to each element of the matrix.
Definition: Matrix.hpp:779
static CU_HOST_DEVICE size_t nbRows()
Returns the number of rows of the matrix.
Definition: Matrix.hpp:174
CU_HOST_DEVICE Matrix(const Vector< nCols, T1 > *vecs)
Fill the matrix with the array of vectors.
Definition: Matrix.hpp:88
T CU_HOST_DEVICE det(const Matrix< 1, 1, T > &mat)
Determinant of the matrix.
Definition: Matrix.hpp:946
CU_HOST_DEVICE Matrix(const Matrix< nRows, nCols, T1 > &mat)
Copy a matrix.
Definition: Matrix.hpp:76
static CU_HOST_DEVICE Vector< 2, size_t > size()
Returns the size of the matrix.
Definition: Matrix.hpp:166
CU_HOST_DEVICE T * data()
Returns a raw pointer on the data.
Definition: Matrix.hpp:202
static CU_HOST_DEVICE size_t nbColumns()
Returns the number of columns of the matrix.
Definition: Matrix.hpp:182
CU_HOST_DEVICE Matrix(const T &value)
Create a diagonal matrix.
Definition: Matrix.hpp:153
CU_HOST_DEVICE Matrix< nRows, nCols, T > map(T(*fct)(T1), const Matrix< nRows, nCols, T1 > &m)
Apply a unary function to each element of the matrix.
Definition: Matrix.hpp:760
CU_HOST_DEVICE Matrix(const T *values, MatrixLayout layout=C_STYLE)
Fill in the matrix with the values.
Definition: Matrix.hpp:130
CU_HOST_DEVICE Matrix< nRows, nCols, T > map(T(*fct)(T, T), const Matrix< nRows, nCols, T > &m1, const Matrix< nRows, nCols, T > &m2)
Apply a binary function to each element of the matrix.
Definition: Matrix.hpp:798
CU_HOST_DEVICE Matrix operator*(const T &scalar) const
Matrix-scalar multiplication.
Definition: Matrix.hpp:252
CU_HOST_DEVICE Matrix operator+(const Matrix &mat) const
Matrix addition.
Definition: Matrix.hpp:224
CU_HOST_DEVICE T * data()
Returns a raw pointer on the data.
Definition: Vector.hpp:234
CU_HOST_DEVICE void y(const T &v)
Short access to the second element.
Definition: Vector.hpp:670
This namespace contains all the API of MorphoGraphX.
Definition: Assert.hpp:26
CU_HOST_DEVICE Matrix< nRows, nCols, T > map(const T &(*fct)(const T1 &), const Matrix< nRows, nCols, T1 > &m)
Apply a unary function to each element of the matrix.
Definition: Matrix.hpp:741
CU_HOST_DEVICE Vector< nRows, T > diag() const
Return the diagonal vector, if the matrix is square.
Definition: Matrix.hpp:560
CU_HOST_DEVICE Matrix< nCols, nRows, T > transpose(const Matrix< nRows, nCols, T > &mat)
Transpose a matrix.
Definition: Matrix.hpp:1114
CU_HOST_DEVICE Matrix< nRows, nCols, T > map(const T &(*fct)(const T1 &, const T2 &), const Matrix< nRows, nCols, T1 > &m1, const Matrix< nRows, nCols, T2 > &m2)
Apply a binary function to each element of the matrix.
Definition: Matrix.hpp:903
CU_HOST_DEVICE Matrix< nCols, nRows, T > operator~()
Transpose the matrix.
Definition: Matrix.hpp:475
#define STATIC_ASSERT(B)
Assertion that works at compile time.
Definition: StaticAssert.hpp:57
CU_HOST_DEVICE Matrix< nRows, nCols, T > map(const T &(*fct)(const T &), const Matrix< nRows, nCols, T > &m)
Apply a unary function to each element of the matrix.
Definition: Matrix.hpp:684
CU_HOST_DEVICE Matrix< nRows, nCols, T > map(T(*fct)(T), const Matrix< nRows, nCols, T > &m)
Apply a unary function to each element of the matrix.
Definition: Matrix.hpp:703
static CU_HOST_DEVICE Matrix identity()
Returns an identity matrix.
Definition: Matrix.hpp:434
CU_HOST_DEVICE Matrix< nRows, nCols, T > map(T(*fct)(T1, T2), const Matrix< nRows, nCols, T1 > &m1, const Matrix< nRows, nCols, T2 > &m2)
Apply a binary function to each element of the matrix.
Definition: Matrix.hpp:861
Define the STATIC_ASSERT macro.
Class representing a fixed-size matrix.
Definition: Matrix.hpp:44
Common definitions and utilities This file is shared by cuda, do not include headers that nvcc can&#39;t ...
CU_HOST_DEVICE T & operator()(size_t i, size_t j)
Return the value at row i, column j.
Definition: Matrix.hpp:418
CU_HOST_DEVICE void z(const T &v)
Short access to the third element.
Definition: Vector.hpp:679
CU_HOST_DEVICE T norm(const Matrix< nRows, nCols, T > &mat)
Return the norm of the matrix.
Definition: Matrix.hpp:1134
CU_HOST_DEVICE Matrix operator/(const T &scalar) const
Matrix-scalar division.
Definition: Matrix.hpp:266
CU_HOST_DEVICE Matrix(void)
Create a matrix filled with 0s.
Definition: Matrix.hpp:64
CU_HOST_DEVICE T normsq(const Matrix< nRows, nCols, T > &mat)
Return the square norm of the matrix.
Definition: Matrix.hpp:1123
CU_HOST_DEVICE Vector< nCols, T > operator[](size_t idx) const
Returns the nth row.
Definition: Matrix.hpp:410
CU_HOST_DEVICE Matrix< 1, 1, T > inverse(const Matrix< 1, 1, T > &mat)
Inverse the matrix.
Definition: Matrix.hpp:1001
CU_HOST_DEVICE Matrix< nRows, nCols, T > map(T(*fct)(const T &, const T &), const Matrix< nRows, nCols, T > &m1, const Matrix< nRows, nCols, T > &m2)
Apply a binary function to each element of the matrix.
Definition: Matrix.hpp:819
CU_HOST_DEVICE Vector< nRows, T > operator*(const Vector< nCols, T > &vec) const
Matrix*Column Vector.
Definition: Matrix.hpp:294
Defines the Vector class template This file is shared by cuda, do not include headers that nvcc can&#39;t...