FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
tiny_algebra.hpp
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5
6#pragma once
7
8#include <iostream>
9
10#include <ios>
13#include <kernel/util/dist.hpp>
14#include <limits>
15#ifndef __CUDA_ARCH__
16#include <kernel/util/math.hpp>
17#endif
18
19// includes, system
20#ifndef __CUDACC__
21#include <initializer_list>
22#else
23#include <kernel/util/cuda_math.cuh>
24#endif
25
26namespace FEAT
27{
34 namespace Tiny
35 {
53 template<
54 typename T_,
55 int n_,
56 int s_ = n_>
57 class Vector DOXY({});
58
79 template<
80 typename T_,
81 int m_,
82 int n_,
83 int sm_ = m_,
84 int sn_ = n_>
85 class Matrix DOXY({});
86
113 template<
114 typename T_,
115 int l_,
116 int m_,
117 int n_,
118 int sl_ = l_,
119 int sm_ = m_,
120 int sn_ = n_>
121 class Tensor3 DOXY({});
122
124 namespace Intern
125 {
134 template<typename T_>
135 struct DataTypeExtractor
136 {
138 typedef T_ MyDataType;
140 static constexpr int level = 0;
141 };
142
157 template<typename T_, int n_, int s_>
158 struct DataTypeExtractor<Vector<T_, n_, s_>>
159 {
161 typedef typename DataTypeExtractor<T_>::MyDataType MyDataType;
163 static constexpr int level = DataTypeExtractor<T_>::level+1;
164 };
165
166 // Same for Matrix
167 template<typename T_, int m_, int n_, int sm_, int sn_>
168 struct DataTypeExtractor<Matrix<T_, m_, n_, sm_, sn_>>
169 {
171 typedef typename DataTypeExtractor<T_>::MyDataType MyDataType;
173 static constexpr int level = DataTypeExtractor<T_>::level+1;
174 };
175
176 // Same for Tensor3
177 template<typename T_, int l_, int m_, int n_, int sl_, int sm_, int sn_>
178 struct DataTypeExtractor<Tensor3<T_, l_, m_, n_, sl_, sm_, sn_>>
179 {
181 typedef typename DataTypeExtractor<T_>::MyDataType MyDataType;
183 static constexpr int level = DataTypeExtractor<T_>::level+1;
184 };
185
186 // forward declarations of helper classes
187 template<int m_, int n_>
188 struct DetHelper;
189
190 template<int m_, int n_>
191 struct VolHelper;
192
193 template<int m_, int n_>
194 struct InverseHelper;
195
196 template<int m_, int n_>
197 struct CofactorHelper;
198
199#ifdef __CUDACC__
200 template<int m_, int n_>
201 struct CudaGroupedInverseHelper;
202#endif
203 } // namespace Intern
205
206 /* ************************************************************************************************************* */
207 /* ************************************************************************************************************* */
208 // Tiny Vector implementation
209 /* ************************************************************************************************************* */
210 /* ************************************************************************************************************* */
211
212 template<typename T_, int n_, int s_>
213 class Vector
214 {
215 static_assert(n_ > 0, "invalid vector length");
216 static_assert(s_ >= n_, "invalid vector stride");
217
218 public:
220 static constexpr int n = n_;
222 static constexpr int s = s_;
223
225 typedef T_ ValueType;
227 typedef typename Intern::DataTypeExtractor<ValueType>::MyDataType DataType;
228
230 T_ v[s_];
231
233 CUDA_HOST_DEVICE Vector()
234 {
235 }
236
238 CUDA_HOST_DEVICE explicit Vector(DataType value)
239 {
240 for(int i(0); i < n_; ++i)
241 {
242 v[i] = value;
243 }
244 }
245
247 template<int sx_>
248 CUDA_HOST_DEVICE Vector(const Vector<T_, n_, sx_>& x)
249 {
250 for(int i(0); i < n_; ++i)
251 {
252 v[i] = x.v[i];
253 }
254 }
255
257 template<typename Tx_, int sx_>
258 CUDA_HOST_DEVICE explicit Vector(const Vector<Tx_, n_, sx_>& x)
259 {
260 for(int i(0); i < n_; ++i)
261 {
262 v[i] = ValueType(x.v[i]);
263 }
264 }
265
267 template<typename Tx_>
268 CUDA_HOST_DEVICE explicit Vector(const std::array<Tx_, n_>& x)
269 {
270 for(int i(0); i < n_; ++i)
271 {
272 v[i] = ValueType(x[i]);
273 }
274 }
275
277 template<typename Tx_, int sx_>
278 CUDA_HOST_DEVICE static Vector convert_new(const Vector<Tx_, n_, sx_>& x)
279 {
280 Vector v;
281 for(int i(0); i < n_; ++i)
282 {
283 if constexpr(std::is_same<T_, DataType>::value)
284 v[i] = T_(x.v[i]);
285 else
286 v[i] = T_::convert(x.v[i]);
287 }
288 return v;
289 }
290
292 CUDA_HOST_DEVICE static Vector convert_new(Vector&& x)
293 {
294 return std::move(x);
295 }
296
309 template<typename Tx_>
310 CUDA_HOST_DEVICE Vector(const std::initializer_list<Tx_>& x)
311 {
312 XASSERTM(std::size_t(n_) == x.size(), "invalid initializer list size");
313 auto it(x.begin());
314 for(int i(0); i < n_; ++i, ++it)
315 v[i] = T_(*it);
316 }
317
319 CUDA_HOST_DEVICE Vector& operator=(DataType value)
320 {
321 for(int i(0); i < n_; ++i)
322 {
323 v[i] = value;
324 }
325 return *this;
326 }
327
329 template<int sx_>
330 CUDA_HOST_DEVICE Vector& operator=(const Vector<T_, n_, sx_>& x)
331 {
332 for(int i(0); i < n_; ++i)
333 {
334 v[i] = x.v[i];
335 }
336 return *this;
337 }
338
340 template<typename Tx_, int sx_>
341 CUDA_HOST_DEVICE Vector& operator=(const Vector<Tx_, n_, sx_>& x)
342 {
343 for(int i(0); i < n_; ++i)
344 {
345 v[i] = ValueType(x.v[i]);
346 }
347 return *this;
348 }
349
351 template<typename Tx_>
352 CUDA_HOST_DEVICE Vector& operator=(const std::array<Tx_, n_>& x)
353 {
354 for(int i(0); i < n_; ++i)
355 {
356 v[i] = ValueType(x[std::size_t(i)]);
357 }
358 return *this;
359 }
360
375 template<typename Tx_>
376 CUDA_HOST_DEVICE Vector& operator=(const std::initializer_list<Tx_>& x)
377 {
378 XASSERTM(std::size_t(n_) == x.size(), "invalid initializer list size");
379 auto it(x.begin());
380 for(int i(0); i < n_; ++i, ++it)
381 v[i] = T_(*it);
382 return *this;
383 }
384
386 template<typename Tx_, int sx_>
387 CUDA_HOST_DEVICE void convert(const Vector<Tx_, n_, sx_>& x)
388 {
389 for(int i(0); i < n_; ++i)
390 v[i] = T_(x.v[i]);
391 }
392
401 CUDA_HOST_DEVICE T_& operator()(int i)
402 {
403 ASSERTM((i >= 0) && (i < n_), "index i out-of-bounds");
404 return v[i];
405 }
406
408 CUDA_HOST_DEVICE const T_& operator()(int i) const
409 {
410 ASSERTM((i >= 0) && (i < n_), "index i out-of-bounds");
411 return v[i];
412 }
413
415 CUDA_HOST_DEVICE T_& operator[](int i)
416 {
417 ASSERTM((i >= 0) && (i < n_), "index i out-of-bounds");
418 return v[i];
419 }
420
422 CUDA_HOST_DEVICE const T_& operator[](int i) const
423 {
424 ASSERTM((i >= 0) && (i < n_), "index i out-of-bounds");
425 return v[i];
426 }
427
434 template<int snx_>
435 CUDA_HOST_DEVICE void copy(const Vector<T_, n_, snx_>& x)
436 {
437 for(int i(0); i < n_; ++i)
438 {
439 v[i] = x.v[i];
440 }
441 }
442
452 template<int nn_, int nx_, int snx_>
453 CUDA_HOST_DEVICE void copy_n(const Vector<T_, nx_, snx_>& x)
454 {
455 static_assert(nn_ <= n_, "invalid copy_n size");
456 static_assert(nn_ <= nx_, "invalid copy_n size");
457 for(int i(0); i < nn_; ++i)
458 {
459 v[i] = x.v[i];
460 }
461 }
462
464 CUDA_HOST_DEVICE Vector& operator*=(DataType alpha)
465 {
466 for(int i(0); i < n_; ++i)
467 {
468 v[i] *= alpha;
469 }
470 return *this;
471 }
472
474 template <int sx_>
475 CUDA_HOST_DEVICE Vector& operator*=(const Vector<T_, n_, sx_>& x)
476 {
477 for(int i(0); i < n_; ++i)
478 {
479 v[i] *= x.v[i];
480 }
481 return *this;
482 }
483
485 template<int sx_>
486 CUDA_HOST_DEVICE Vector& operator+=(const Vector<T_, n_, sx_>& x)
487 {
488 for(int i(0); i < n_; ++i)
489 {
490 v[i] += x.v[i];
491 }
492 return *this;
493 }
494
496 template<int sx_>
497 CUDA_HOST_DEVICE Vector& operator-=(const Vector<T_, n_, sx_>& x)
498 {
499 for(int i(0); i < n_; ++i)
500 {
501 v[i] -= x.v[i];
502 }
503 return *this;
504 }
505
512 CUDA_HOST_DEVICE void format(DataType alpha = DataType(0))
513 {
514 for(int i(0); i < n_; ++i)
515 {
516 v[i] = alpha;
517 }
518 }
519
529 template<int nn_>
530 CUDA_HOST_DEVICE void format_n(DataType alpha = DataType(0))
531 {
532 static_assert(nn_ <= n_, "invalid format_n size");
533 for(int i(0); i < nn_; ++i)
534 {
535 v[i] = alpha;
536 }
537 }
538
545 CUDA_HOST_DEVICE void scale(DataType alpha)
546 {
547 for(int i(0); i < n_; ++i)
548 {
549 v[i] *= alpha;
550 }
551 }
552
562 template<int nn_>
563 CUDA_HOST_DEVICE void scale_n(DataType alpha)
564 {
565 static_assert(nn_ <= n_, "invalid scale_n size");
566 for(int i(0); i < nn_; ++i)
567 {
568 v[i] *= alpha;
569 }
570 }
571
577 CUDA_HOST_DEVICE Vector& normalize()
578 {
579 #ifndef __CUDACC__
580 const DataType norm2(this->norm_euclid());
581 ASSERTM(norm2 > Math::eps<DataType>(), "Trying to normalize a null vector!");
582 return ((*this) *= (DataType(1)/norm2));
583 #else
584 const DataType norm2_sqr(this->norm_euclid_sqr());
585 ASSERTM(norm2_sqr > CudaMath::cuda_get_eps<DataType>(), "Trying to normalize a null vector!");
586 return ((*this) *= CudaMath::cuda_rsqrt(norm2_sqr));
587 #endif
588 }
589
598 template<int nn_>
599 CUDA_HOST_DEVICE Vector& normalize_n()
600 {
601 static_assert(nn_ <= n_, "invalid normalize_n size");
602#ifndef __CUDACC__
603 const DataType norm2(this->template norm_euclid_n<nn_>());
604 ASSERTM(norm2 > Math::eps<DataType>(), "Trying to normalize a null vector!");
605 this->template scale_n<nn_>(DataType(1)/norm2);
606 return *this;
607#else
608 const DataType norm2_sqr(this->template norm_euclid_sqr_n<nn_>());
609 ASSERTM(norm2_sqr > CudaMath::cuda_get_eps<DataType>(), "Trying to normalize a null vector!");
610 this->template scale_n<nn_>(CudaMath::cuda_rsqrt(norm2_sqr));
611 return *this;
612#endif
613 }
614
620 CUDA_HOST_DEVICE Vector& negate()
621 {
622 for(int i(0); i < n_; ++i)
623 v[i] = -v[i];
624 return *this;
625 }
626
635 template<int nn_>
636 CUDA_HOST_DEVICE Vector& negate_n()
637 {
638 static_assert(nn_ <= n_, "invalid negate_n size");
639 for(int i(0); i < nn_; ++i)
640 v[i] = -v[i];
641 return *this;
642 }
643
655 template<int snx_>
656 CUDA_HOST_DEVICE Vector& axpy(DataType alpha, const Vector<T_, n_, snx_>& x)
657 {
658 for(int i(0); i < n_; ++i)
659 v[i] += alpha * x.v[i];
660 return *this;
661 }
662
677 template<int nn_, int nx_, int snx_>
678 CUDA_HOST_DEVICE Vector& axpy_n(DataType alpha, const Vector<T_, nx_, snx_>& x)
679 {
680 static_assert(nn_ <= n_, "invalid negate_n size");
681 static_assert(nn_ <= nx_, "invalid negate_n size");
682 for(int i(0); i < nn_; ++i)
683 v[i] += alpha * x.v[i];
684 return *this;
685 }
686
704 template<int sna_, int snb_>
705 CUDA_HOST_DEVICE Vector& set_convex(DataType alpha, const Vector<T_, n_, sna_>& a, const Vector<T_, n_, snb_>& b)
706 {
707 for(int i(0); i < n_; ++i)
708 v[i] = (T_(1) - alpha) * a.v[i] + alpha * b.v[i];
709 return *this;
710 }
711
732 template<int nn_, int na_, int nb_, int sna_, int snb_>
733 CUDA_HOST_DEVICE Vector& set_convex_n(DataType alpha, const Vector<T_, na_, sna_>& a, const Vector<T_, nb_, snb_>& b)
734 {
735 static_assert(nn_ <= n_, "invalid set_convex_n size");
736 static_assert(nn_ <= na_, "invalid set_convex_n size");
737 static_assert(nn_ <= nb_, "invalid set_convex_n size");
738 for(int i(0); i < nn_; ++i)
739 v[i] = (T_(1) - alpha) * a.v[i] + alpha * b.v[i];
740 return *this;
741 }
742
758 template<int m_, int sma_, int sna_, int sx_>
760 {
761 // we have to compare void* addresses here, because we might get a type mismatch error otherwise
762 ASSERTM((const void*)this != (const void*)&x, "result vector and multiplicand vector 'x' must be different objects");
763
764 for(int i(0); i < n_; ++i)
765 {
766 v[i] = T_(0);
767 for(int j(0); j < m_; ++j)
768 {
769 v[i] += a.v[i][j] * x.v[j];
770 }
771 }
772 return *this;
773 }
774
794 template<int mm_, int nn_, int ma_, int na_, int sna_, int sma_, int nx_, int sx_>
796 {
797 static_assert(mm_ <= n_, "invalid set_mat_vec_mult_n size");
798 static_assert(mm_ <= ma_, "invalid set_mat_vec_mult_n size");
799 static_assert(nn_ <= nx_, "invalid set_mat_vec_mult_n size");
800 static_assert(nn_ <= na_, "invalid set_mat_vec_mult_n size");
801
802 // we have to compare void* addresses here, because we might get a type mismatch error otherwise
803 ASSERTM((const void*)this != (const void*)&x, "result vector and multiplicand vector 'x' must be different objects");
804
805 for(int i(0); i < mm_; ++i)
806 {
807 v[i] = T_(0);
808 for(int j(0); j < nn_; ++j)
809 {
810 v[i] += a.v[i][j] * x.v[j];
811 }
812 }
813 return *this;
814 }
815
831 template<int m_, int sma_, int sna_, int sx_>
833 {
834 // we have to compare void* addresses here, because we might get a type mismatch error otherwise
835 ASSERTM((const void*)this != (const void*)&x, "result vector and multiplicand vector 'x' must be different objects");
836
837 for(int j(0); j < n_; ++j)
838 {
839 v[j] = T_(0);
840 for(int i(0); i < m_; ++i)
841 {
842 v[j] += a.v[i][j] * x.v[i];
843 }
844 }
845 return *this;
846 }
847
867 template<int nn_, int mm_, int mx_, int smx_, int ma_, int na_, int sma_, int sna_>
869 {
870 static_assert(mm_ <= mx_, "invalid set_mat_vec_mult_n size");
871 static_assert(mm_ <= ma_, "invalid set_mat_vec_mult_n size");
872 static_assert(nn_ <= n_, "invalid set_mat_vec_mult_n size");
873 static_assert(nn_ <= na_, "invalid set_mat_vec_mult_n size");
874
875 // we have to compare void* addresses here, because we might get a type mismatch error otherwise
876 ASSERTM((const void*)this != (const void*)&x, "result vector and multiplicand vector 'x' must be different objects");
877
878 for(int j(0); j < nn_; ++j)
879 {
880 v[j] = T_(0);
881 for(int i(0); i < mm_; ++i)
882 {
883 v[j] += a.v[i][j] * x.v[i];
884 }
885 }
886 return *this;
887 }
888
907 template<int m_, int sma_, int sna_, int sx_>
909 {
910 // we have to compare void* addresses here, because we might get a type mismatch error otherwise
911 ASSERTM((const void*)this != (const void*)&x, "result vector and multiplicand vector 'x' must be different objects");
912
913 for(int i(0); i < n_; ++i)
914 {
915 for(int j(0); j < m_; ++j)
916 {
917 v[i] += alpha * a.v[i][j] * x.v[j];
918 }
919 }
920 return *this;
921 }
922
947 template<int mm_, int nn_, int ma_, int na_, int sna_, int sma_, int nx_, int sx_>
949 {
950 static_assert(mm_ <= n_, "invalid add_mat_vec_mult_n size");
951 static_assert(mm_ <= ma_, "invalid add_mat_vec_mult_n size");
952 static_assert(nn_ <= nx_, "invalid add_mat_vec_mult_n size");
953 static_assert(nn_ <= na_, "invalid add_mat_vec_mult_n size");
954
955 // we have to compare void* addresses here, because we might get a type mismatch error otherwise
956 ASSERTM((const void*)this != (const void*)&x, "result vector and multiplicand vector 'x' must be different objects");
957
958 for(int i(0); i < mm_; ++i)
959 {
960 for(int j(0); j < nn_; ++j)
961 {
962 v[i] += alpha * a.v[i][j] * x.v[j];
963 }
964 }
965 return *this;
966 }
967
986 template<int m_, int sma_, int sna_, int sx_>
988 {
989 // we have to compare void* addresses here, because we might get a type mismatch error otherwise
990 ASSERTM((const void*)this != (const void*)&x, "result vector and multiplicand vector 'x' must be different objects");
991
992 for(int j(0); j < n_; ++j)
993 {
994 for(int i(0); i < m_; ++i)
995 {
996 v[j] += alpha * a.v[i][j] * x.v[i];
997 }
998 }
999 return *this;
1000 }
1001
1026 template<int nn_, int mm_, int mx_, int smx_, int ma_, int na_, int sma_, int sna_>
1028 {
1029 static_assert(mm_ <= mx_, "invalid add_vec_mat_mult_n size");
1030 static_assert(mm_ <= ma_, "invalid add_vec_mat_mult_n size");
1031 static_assert(nn_ <= n_, "invalid add_vec_mat_mult_n size");
1032 static_assert(nn_ <= na_, "invalid add_vec_mat_mult_n size");
1033
1034 // we have to compare void* addresses here, because we might get a type mismatch error otherwise
1035 ASSERTM((const void*)this != (const void*)&x, "result vector and multiplicand vector 'x' must be different objects");
1036
1037 for(int j(0); j < nn_; ++j)
1038 {
1039 for(int i(0); i < mm_; ++i)
1040 {
1041 v[j] += alpha * a.v[i][j] * x.v[i];
1042 }
1043 }
1044 return *this;
1045 }
1046
1053 CUDA_HOST_DEVICE DataType norm_euclid_sqr() const
1054 {
1055 DataType r(DataType(0));
1056 for(int i(0); i < n_; ++i)
1057 {
1058 #ifndef __CUDACC__
1059 r += Math::sqr(v[i]);
1060 #else
1061 r += CudaMath::cuda_sqr(v[i]);
1062 #endif
1063 }
1064 return r;
1065 }
1066
1073 template<int nn_>
1074 CUDA_HOST_DEVICE DataType norm_euclid_sqr_n() const
1075 {
1076 static_assert(nn_ <= n_, "invalid norm_euclid_sqr_n size");
1077 DataType r(DataType(0));
1078 for(int i(0); i < nn_; ++i)
1079 {
1080 #ifndef __CUDACC__
1081 r += Math::sqr(v[i]);
1082 #else
1083 r += CudaMath::cuda_sqr(v[i]);
1084 #endif
1085 }
1086 return r;
1087 }
1088
1095 CUDA_HOST_DEVICE DataType norm_euclid() const
1096 {
1097 #ifndef __CUDACC__
1098 return Math::sqrt(norm_euclid_sqr());
1099 #else
1100 return CudaMath::cuda_sqrt(norm_euclid_sqr());
1101 #endif
1102 }
1103
1110 template<int nn_>
1111 CUDA_HOST_DEVICE DataType norm_euclid_n() const
1112 {
1113 static_assert(nn_ <= n_, "invalid norm_euclid_n size");
1114 #ifndef __CUDACC__
1115 return Math::sqrt(this->template norm_euclid_sqr_n<nn_>());
1116 #else
1117 return CudaMath::cuda_sqrt(this->template norm_euclid_sqr_n<nn_>());
1118 #endif
1119 }
1120
1127 CUDA_HOST_DEVICE DataType norm_l1() const
1128 {
1129 DataType r(DataType(0));
1130 for(int i(0); i < n_; ++i)
1131 {
1132 #ifndef __CUDACC__
1133 r += Math::abs(v[i]);
1134 #else
1135 r += CudaMath::cuda_abs(v[i]);
1136 #endif
1137 }
1138 return r;
1139 }
1140
1147 template<int nn_>
1148 CUDA_HOST_DEVICE DataType norm_l1_n() const
1149 {
1150 static_assert(nn_ <= n_, "invalid norm_l1_n size");
1151 DataType r(DataType(0));
1152 for(int i(0); i < nn_; ++i)
1153 {
1154 #ifndef __CUDACC__
1155 r += Math::abs(v[i]);
1156 #else
1157 r += CudaMath::cuda_abs(v[i]);
1158 #endif
1159 }
1160 return r;
1161 }
1162
1169 CUDA_HOST_DEVICE DataType norm_max() const
1170 {
1171 DataType r(DataType(0));
1172 for(int i(0); i < n_; ++i)
1173 {
1174 #ifndef __CUDACC__
1175 r = Math::max(r, Math::abs(v[i]));
1176 #else
1177 r = CudaMath::cuda_max(r, CudaMath::cuda_abs(v[i]));
1178 #endif
1179 }
1180 return r;
1181 }
1182
1189 template<int nn_>
1190 CUDA_HOST_DEVICE DataType norm_max_n() const
1191 {
1192 static_assert(nn_ <= n_, "invalid norm_l1_n size");
1193 DataType r(DataType(0));
1194 for(int i(0); i < nn_; ++i)
1195 {
1196 #ifndef __CUDACC__
1197 r = Math::max(r, Math::abs(v[i]));
1198 #else
1199 r = CudaMath::cuda_max(r, CudaMath::cuda_abs(v[i]));
1200 #endif
1201 }
1202 return r;
1203 }
1204
1208 CUDA_HOST_DEVICE static Vector null()
1209 {
1210 return Vector(DataType(0));
1211 }
1212
1216 CUDA_HOST_DEVICE bool normalized() const
1217 {
1219 }
1220
1225 {
1226 return Dist::Typemap{}.add_entry<T_>(0, s_);
1227 }
1228
1235 CUDA_HOST friend std::ostream & operator<< (std::ostream & lhs, const Vector & b)
1236 {
1237 lhs << "[";
1238 for (int i(0) ; i < b.n ; ++i)
1239 {
1240 lhs << " " << stringify(b(i));
1241 }
1242 lhs << " ]";
1243
1244 return lhs;
1245 }
1246
1247 CUDA_HOST friend std::istream& operator>>(std::istream& in, Vector& vector)
1248 {
1249 // Ignore all input until opening bracket
1250 in.ignore(std::numeric_limits<std::streamsize>::max(), '[');
1251
1252 for(int i(0); i < Vector::n; ++i)
1253 {
1254 in >> vector(i);
1255 }
1256
1257 // Ignore all input until closing bracket is consumed
1258 in.ignore(std::numeric_limits<std::streamsize>::max(), ']');
1259
1260 return in;
1261 }
1262 }; // class Vector
1263
1264 template<typename T_, int sx_, int sa_>
1265 inline void cross(Vector<T_, 2, sx_>& x, const Vector<T_, 2, sa_>& a)
1266 {
1267 x.v[0] = a.v[1];
1268 x.v[1] = -a.v[0];
1269 }
1270
1271 template<typename T_, int sx_, int sa_, int sb_>
1272 inline void cross(Vector<T_, 3, sx_>& x, const Vector<T_, 3, sa_>& a, const Vector<T_, 3, sb_>& b)
1273 {
1274 x.v[0] = a.v[1]*b.v[2] - a.v[2]*b.v[1];
1275 x.v[1] = a.v[2]*b.v[0] - a.v[0]*b.v[2];
1276 x.v[2] = a.v[0]*b.v[1] - a.v[1]*b.v[0];
1277 }
1278
1280 template<typename T_, int n_, int s_>
1281 CUDA_HOST_DEVICE inline Vector<T_, n_> operator*(typename Vector<T_, n_>::DataType alpha, const Vector<T_, n_, s_>& x)
1282 {
1283 return Vector<T_, n_>(x) *= alpha;
1284 }
1285
1287 template<typename T_, int n_, int s_>
1288 CUDA_HOST_DEVICE inline Vector<T_, n_> operator*(const Vector<T_, n_, s_>& x, typename Vector<T_, n_>::DataType alpha)
1289 {
1290 return Vector<T_, n_>(x) *= alpha;
1291 }
1292
1294 template<typename T_, int n_, int sa_, int sb_>
1296 {
1297 return Vector<T_, n_>(a) *= b;
1298 }
1299
1301 template<typename T_, int n_, int sa_, int sb_>
1302 CUDA_HOST_DEVICE inline Vector<T_, n_> operator+(const Vector<T_, n_, sa_>& a, const Vector<T_, n_, sb_>& b)
1303 {
1304 return Vector<T_, n_>(a) += b;
1305 }
1306
1308 template<typename T_, int n_, int sa_, int sb_>
1309 CUDA_HOST_DEVICE inline Vector<T_, n_> operator-(const Vector<T_, n_, sa_>& a, const Vector<T_, n_, sb_>& b)
1310 {
1311 return Vector<T_, n_>(a) -= b;
1312 }
1313
1320 template<typename T_>
1321 CUDA_HOST inline T_ calculate_opening_angle(const Vector<T_,2>& x, const Vector<T_, 2>& y)
1322 {
1323 #ifdef __CUDACC__
1324 XABORTM("calculate_opening_angle not implemented for CUDA");
1325 return T_(0);
1326 #else
1327 return Math::calc_opening_angle(x[0], x[1], y[0], y[1]);
1328 #endif
1329 }
1330
1339 template<typename T_, int dim_>
1341 {
1342 T_ norm2(y.template norm_euclid_n<dim_>());
1343 if(norm2 < Math::eps<T_>())
1344 norm2 = T_(1);
1345 const auto tmp_normalized = (T_(1)/norm2) * y;
1346 return dot(x, tmp_normalized) * tmp_normalized;
1347 }
1348
1349
1350 /* ************************************************************************************************************* */
1351 /* ************************************************************************************************************* */
1352 // Tiny Matrix implementation
1353 /* ************************************************************************************************************* */
1354 /* ************************************************************************************************************* */
1355
1356 template<typename T_, int m_, int n_, int sm_, int sn_>
1357 class Matrix
1358 {
1359 static_assert(m_ > 0, "invalid row count");
1360 static_assert(n_ > 0, "invalid column count");
1361 static_assert(sm_ >= m_, "invalid row stride");
1362 static_assert(sn_ >= n_, "invalid column stride");
1363
1364 public:
1366 static constexpr int m = m_;
1368 static constexpr int n = n_;
1370 static constexpr int sm = sm_;
1372 static constexpr int sn = sn_;
1373
1375 typedef T_ ValueType;
1377 typedef typename Intern::DataTypeExtractor<ValueType>::MyDataType DataType;
1378
1382 RowType v[sm_];
1383
1385 CUDA_HOST_DEVICE Matrix()
1386 {
1387 }
1388
1390 CUDA_HOST_DEVICE explicit Matrix(DataType value)
1391 {
1392 for(int i(0); i < m_; ++i)
1393 {
1394 v[i] = value;
1395 }
1396 }
1397
1399 template<typename T2_, int sma_, int sna_>
1400 CUDA_HOST_DEVICE Matrix(const Matrix<T2_, m_, n_, sma_, sna_>& a)
1401 {
1402 for(int i(0); i < m_; ++i)
1403 {
1404 for(int j(0); j < n_; ++j)
1405 {
1406 v[i][j] = T_(a.v[i][j]);
1407 }
1408 }
1409 }
1410
1422 template<typename Tx_>
1423 CUDA_HOST_DEVICE explicit Matrix(const std::initializer_list<Tx_>& x)
1424 {
1425 XASSERTM(std::size_t(m_) == x.size(), "invalid initializer list size");
1426 auto it(x.begin());
1427 for(int i(0); i < m_; ++i, ++it)
1428 v[i] = *it;
1429 }
1430
1444 template<typename Tx_>
1445 CUDA_HOST_DEVICE explicit Matrix(const std::initializer_list<std::initializer_list<Tx_>>& x)
1446 {
1447 XASSERTM(std::size_t(m_) == x.size(), "invalid initializer list size");
1448 auto it(x.begin());
1449 for(int i(0); i < m_; ++i, ++it)
1450 v[i] = *it;
1451 }
1452
1454 CUDA_HOST_DEVICE Matrix& operator=(DataType value)
1455 {
1456 for(int i(0); i < m_; ++i)
1457 {
1458 v[i] = value;
1459 }
1460 return *this;
1461 }
1462
1464 template<int sma_, int sna_>
1466 {
1467 for(int i(0); i < m_; ++i)
1468 {
1469 v[i] = a.v[i];
1470 }
1471 return *this;
1472 }
1473
1486 template<typename Tx_>
1487 CUDA_HOST_DEVICE Matrix& operator=(const std::initializer_list<Tx_>& x)
1488 {
1489 XASSERTM(std::size_t(m_) == x.size(), "invalid initializer list size");
1490 auto it(x.begin());
1491 for(int i(0); i < m_; ++i, ++it)
1492 v[i] = *it;
1493 return *this;
1494 }
1495
1507 template<typename Tx_>
1508 CUDA_HOST_DEVICE Matrix& operator=(const std::initializer_list<std::initializer_list<Tx_>>& x)
1509 {
1510 XASSERTM(std::size_t(m_) == x.size(), "invalid initializer list size");
1511 auto it(x.begin());
1512 for(int i(0); i < m_; ++i, ++it)
1513 v[i] = *it;
1514 return *this;
1515 }
1516
1518 template<typename Tx_, int sma_, int sna_>
1519 CUDA_HOST_DEVICE void convert(const Matrix<Tx_, m_, n_, sma_, sna_>& a)
1520 {
1521 for(int i(0); i < m_; ++i)
1522 v[i].convert(a.v[i]);
1523 }
1524
1534 CUDA_HOST_DEVICE T_& operator()(int i, int j)
1535 {
1536 ASSERTM( (i >= 0) && (i < m_), "index i out-of-bounds");
1537 ASSERTM( (j >= 0) && (j < n_), "index j out-of-bounds");
1538 return v[i][j];
1539 }
1540
1542 CUDA_HOST_DEVICE const T_& operator()(int i, int j) const
1543 {
1544 ASSERTM( (i >= 0) && (i < m_), "index i out-of-bounds");
1545 ASSERTM( (j >= 0) && (j < n_), "index j out-of-bounds");
1546 return v[i][j];
1547 }
1548
1558 CUDA_HOST_DEVICE RowType& operator[](int i)
1559 {
1560 ASSERTM( (i >= 0) && (i <m_), "index i out-of-bounds");
1561 return v[i];
1562 }
1563
1565 CUDA_HOST_DEVICE const RowType& operator[](int i) const
1566 {
1567 ASSERTM( (i >= 0) && (i <m_), "index i out-of-bounds");
1568 return v[i];
1569 }
1570
1572 CUDA_HOST_DEVICE Matrix& operator*=(DataType alpha)
1573 {
1574 for(int i(0); i < m_; ++i)
1575 {
1576 v[i] *= alpha;
1577 }
1578 return *this;
1579 }
1580
1582 template<int sma_, int sna_>
1584 {
1585 for(int i(0); i < m_; ++i)
1586 {
1587 v[i] += a.v[i];
1588 }
1589 return *this;
1590 }
1591
1593 template<int sma_, int sna_>
1595 {
1596 for(int i(0); i < m_; ++i)
1597 {
1598 v[i] -= a.v[i];
1599 }
1600 return *this;
1601 }
1602
1609 template<int sma_, int sna_>
1610 CUDA_HOST_DEVICE void copy(const Matrix<T_, m_, n_, sma_, sna_>& a)
1611 {
1612 for(int i(0); i < m_; ++i)
1613 for(int j(0); j < n_; ++j)
1614 v[i][j] = a.v[i][j];
1615 }
1616
1629 template<int mm_, int nn_, int ma_, int na_, int sma_, int sna_>
1630 CUDA_HOST_DEVICE void copy_n(const Matrix<T_, ma_, na_, sma_, sna_>& a)
1631 {
1632 static_assert(mm_ <= m_, "invalid copy_n size");
1633 static_assert(mm_ <= ma_, "invalid copy_n size");
1634 static_assert(nn_ <= n_, "invalid copy_n size");
1635 static_assert(nn_ <= na_, "invalid copy_n size");
1636 for(int i(0); i < mm_; ++i)
1637 for(int j(0); j < nn_; ++j)
1638 v[i][j] = a.v[i][j];
1639 }
1640
1647 CUDA_HOST_DEVICE void format(DataType alpha = DataType(0))
1648 {
1649 for(int i(0); i < m_; ++i)
1650 {
1651 v[i].format(alpha);
1652 }
1653 }
1654
1666 CUDA_HOST_DEVICE DataType norm_hessian_sqr() const
1667 {
1668 DataType r(0);
1669 for(int i(0); i < m_; ++i)
1670 {
1671 #ifndef __CUDACC__
1672 r += Math::sqr(v[i][i]);
1673 #else
1674 r += CudaMath::cuda_sqr(v[i][i]);
1675 #endif
1676 for(int j(0); j < n_; ++j)
1677 {
1678 #ifndef __CUDACC__
1679 r += Math::sqr(v[i][j]);
1680 #else
1681 r += CudaMath::cuda_sqr(v[i][j]);
1682 #endif
1683 }
1684 }
1685 return r / DataType(2);
1686 }
1687
1696 CUDA_HOST_DEVICE DataType norm_frobenius() const
1697 {
1698 #ifndef __CUDACC__
1699 return Math::sqrt(norm_frobenius_sqr());
1700 #else
1701 return CudaMath::cuda_sqrt(norm_frobenius_sqr());
1702 #endif
1703 }
1704
1713 CUDA_HOST_DEVICE DataType norm_frobenius_sqr() const
1714 {
1715 DataType r(0);
1716 for(int i(0); i < m_; ++i)
1717 {
1718 for(int j(0); j < n_; ++j)
1719 {
1720 #ifndef __CUDACC__
1721 r += Math::sqr(v[i][j]);
1722 #else
1723 r += CudaMath::cuda_sqr(v[i][j]);
1724 #endif
1725 }
1726 }
1727 return r;
1728 }
1729
1735 CUDA_HOST_DEVICE DataType norm_sub_id_frobenius() const
1736 {
1737 DataType r(0);
1738 for(int i(0); i < m_; ++i)
1739 {
1740 for(int j(0); j < n_; ++j)
1741 {
1742 #ifndef __CUDACC__
1743 r += Math::sqr(v[i][j] - DataType(i == j ? 1 : 0));
1744 #else
1745 r += CudaMath::cuda_sqr(v[i][j] - DataType(i == j ? 1 : 0));
1746 #endif
1747 }
1748 }
1749 #ifndef __CUDACC__
1750 return Math::sqrt(r);
1751 #else
1752 return CudaMath::cuda_sqrt(r);
1753 #endif
1754 }
1755
1765 CUDA_HOST_DEVICE DataType trace() const
1766 {
1767 #ifndef __CUDACC__
1768 int k = Math::min(m_, n_);
1769 #else
1770 int k = CudaMath::cuda_min(m_, n_);
1771 #endif
1772 DataType r(0);
1773 for(int i(0); i < k; ++i)
1774 {
1775 r += v[i][i];
1776 }
1777 return r;
1778 }
1779
1787 CUDA_HOST_DEVICE DataType det() const
1788 {
1789 return Intern::DetHelper<m_, n_>::compute(*this);
1790 }
1791
1803 CUDA_HOST_DEVICE DataType vol() const
1804 {
1805 return Intern::VolHelper<m_, n_>::compute(*this);
1806 }
1807
1818 template<int sma_, int sna_>
1820 {
1821 // we have to compare void* addresses here, because we might get a type mismatch error otherwise
1822 ASSERTM((const void*)this != (const void*)&a, "result matrix and input matrix 'a' must be different objects");
1823 Intern::InverseHelper<m_, n_>::compute(*this, a);
1824 return *this;
1825 }
1826
1827 #ifdef __CUDACC__
1843 template<typename ThreadGroup_, int sma_, int sna_>
1844 CUDA_HOST_DEVICE __forceinline__ Matrix& grouped_set_inverse(const ThreadGroup_& tg, const Matrix<T_, m_, n_, sma_, sna_>& a, const T_& det)
1845 {
1846 // we have to compare void* addresses here, because we might get a type mismatch error otherwise
1847 ASSERTM((const void*)this != (const void*)&a, "result matrix and input matrix 'a' must be different objects");
1848 Intern::CudaGroupedInverseHelper<m_, n_>::compute(tg, *this, a, det);
1849 return *this;
1850 }
1851 #endif
1852
1870 template<int sma_, int sna_>
1872 {
1873 // we have to compare void* addresses here, because we might get a type mismatch error otherwise
1874 ASSERTM((const void*)this != (const void*)&a, "result matrix and input matrix 'a' must be different objects");
1875 Intern::CofactorHelper<m_, n_>::compute(*this, a);
1876 return *this;
1877 }
1878
1887 template<int sma_, int sna_>
1889 {
1890 // we have to compare void* addresses here, because we might get a type mismatch error otherwise
1891 ASSERTM((const void*)this != (const void*)&a, "result matrix and input matrix 'a' must be different objects");
1892
1893 for(int i(0); i < m_; ++i)
1894 {
1895 for(int j(0); j < n_; ++j)
1896 {
1897 v[i][j] = a.v[j][i];
1898 }
1899 }
1900 return *this;
1901 }
1902
1915 template<int l_, int sla_, int sna_>
1917 {
1918 static_assert(m_ == n_, "Gram matrices must be square");
1919
1920 format();
1921
1922 for(int k(0); k < l_; ++k)
1923 {
1924 for(int i(0); i < n_; ++i)
1925 {
1926 for(int j(0); j < n_; ++j)
1927 {
1928 v[i][j] += a.v[k][i] * a.v[k][j];
1929 }
1930 }
1931 }
1932
1933 return *this;
1934 }
1935
1951 template<int snx_, int sny_>
1952 CUDA_HOST_DEVICE DataType scalar_product(const Vector<T_, m_, snx_>& x, const Vector<T_, n_, sny_>& y) const
1953 {
1954 DataType r(DataType(0));
1955 for(int i(0); i < m_; ++i)
1956 {
1957 r += x[i] * dot(v[i], y);
1958 }
1959 return r;
1960 }
1961
1979 template<int snx_, int sny_>
1980 CUDA_HOST_DEVICE Matrix& add_outer_product(
1981 const Vector<T_, m_, snx_>& x,
1982 const Vector<T_, n_, sny_>& y,
1983 const DataType alpha = DataType(1))
1984 {
1985 for(int i(0); i < m_; ++i)
1986 {
1987 for(int j(0); j < n_; ++j)
1988 {
1989 v[i][j] += alpha * x[i] * y[j];
1990 }
1991 }
1992 return *this;
1993 }
1994
2009 template<int snx_, int sny_>
2010 CUDA_HOST_DEVICE Matrix& set_outer_product(
2011 const Vector<T_, m_, snx_>& x,
2012 const Vector<T_, n_, sny_>& y)
2013 {
2014 for(int i(0); i < m_; ++i)
2015 {
2016 for(int j(0); j < n_; ++j)
2017 {
2018 v[i][j] = x[i] * y[j];
2019 }
2020 }
2021 return *this;
2022 }
2023
2035 template<int sma_, int sna_>
2036 CUDA_HOST_DEVICE Matrix& axpy(DataType alpha, const Matrix<T_, m_, n_, sma_, sna_>& a)
2037 {
2038 for(int i(0); i < m_; ++i)
2039 {
2040 for(int j(0); j < n_; ++j)
2041 {
2042 v[i][j] += alpha * a.v[i][j];
2043 }
2044 }
2045 return *this;
2046 }
2047
2054 CUDA_HOST_DEVICE Matrix& add_scalar_main_diag(DataType alpha)
2055 {
2056 for(int i(0); (i < m_) && (i < n_); ++i)
2057 v[i][i] += alpha;
2058 return *this;
2059 }
2060
2079 template<int la_, int lb_, int sma_, int sna_, int smb_, int snb_>
2080 CUDA_HOST_DEVICE Matrix& add_mat_mat_mult(
2083 DataType alpha = DataType(1))
2084 {
2085 // we have to compare void* addresses here, because we might get a type mismatch error otherwise
2086 ASSERTM((const void*)this != (const void*)&a, "result matrix and multiplicand matrix 'a' must be different objects");
2087 ASSERTM((const void*)this != (const void*)&b, "result matrix and multiplicand matrix 'b' must be different objects");
2088 ASSERTM(la_ == lb_, "second dimension of a must be equal to first dimension of b");
2089
2090 for(int i(0); i < m_; ++i)
2091 {
2092 for(int j(0); j < n_; ++j)
2093 {
2094 DataType r(0);
2095 for(int k(0); k < la_; ++k)
2096 {
2097 r += a.v[i][k] * b.v[k][j];
2098 }
2099 v[i][j] += alpha * r;
2100 }
2101 }
2102 return *this;
2103 }
2104
2120 template<int la_, int lb_, int sma_, int sna_, int smb_, int snb_>
2122 {
2123 // we have to compare void* addresses here, because we might get a type mismatch error otherwise
2124 ASSERTM((const void*)this != (const void*)&a, "result matrix and multiplicand matrix 'a' must be different objects");
2125 ASSERTM((const void*)this != (const void*)&b, "result matrix and multiplicand matrix 'b' must be different objects");
2126 ASSERTM(la_ == lb_, "second dimension of a must be equal to first dimension of b");
2127
2128 format();
2129 return add_mat_mat_mult(a, b);
2130 }
2131
2153 template<int k_, int l_, int sma_, int sna_, int smb_, int snb_, int smd_, int snd_>
2154 CUDA_HOST_DEVICE Matrix& add_double_mat_mult(
2158 DataType alpha = DataType(1))
2159 {
2160 // we have to compare void* addresses here, because we might get a type mismatch error otherwise
2161 ASSERTM((const void*)this != (const void*)&a, "result matrix and multiplicand matrix 'a' must be different objects");
2162 ASSERTM((const void*)this != (const void*)&b, "result matrix and multiplicand matrix 'b' must be different objects");
2163 ASSERTM((const void*)this != (const void*)&d, "result matrix and multiplicand matrix 'd' must be different objects");
2164
2165 for(int i(0); i < m_; ++i)
2166 {
2167 for(int j(0); j < n_; ++j)
2168 {
2169 DataType r(0);
2170 for(int p(0); p < k_; ++p)
2171 {
2172 DataType t(0);
2173 for(int q(0); q < l_; ++q)
2174 {
2175 t += a(p,q) * d(q,j);
2176 }
2177 r += b(p,i)*t;
2178 }
2179 v[i][j] += alpha * r;
2180 }
2181 }
2182 return *this;
2183 }
2184
2206 template<int k_, int l_, int sma_, int sna_, int smb_, int snb_, int smd_, int snd_>
2207 CUDA_HOST_DEVICE Matrix& set_double_mat_mult(
2211 T_ alpha = T_(1))
2212 {
2213 // we have to compare void* addresses here, because we might get a type mismatch error otherwise
2214 ASSERTM((const void*)this != (const void*)&a, "result matrix and multiplicand matrix 'a' must be different objects");
2215 ASSERTM((const void*)this != (const void*)&b, "result matrix and multiplicand matrix 'b' must be different objects");
2216 ASSERTM((const void*)this != (const void*)&d, "result matrix and multiplicand matrix 'd' must be different objects");
2217
2218 format();
2219 return add_double_mat_mult(a, b, d, alpha);
2220 }
2221
2242 template<int l_, int snv_, int slt_, int smt_, int snt_>
2243 CUDA_HOST_DEVICE Matrix& add_vec_tensor_mult(
2244 const Vector<T_, l_, snv_>& x,
2246 DataType alpha = DataType(1))
2247 {
2248 for(int i(0); i < m_; ++i)
2249 {
2250 for(int j(0); j < n_; ++j)
2251 {
2252 DataType r(0);
2253 for(int k(0); k < l_; ++k)
2254 {
2255 r += x(k) * t(k,i,j);
2256 }
2257 v[i][j] += alpha * r;
2258 }
2259 }
2260 return *this;
2261 }
2262
2283 template<int l_, int snv_, int slt_, int smt_, int snt_>
2284 CUDA_HOST_DEVICE Matrix& set_vec_tensor_mult(
2285 const Vector<T_, l_, snv_>& x,
2287 DataType alpha = DataType(1))
2288 {
2289 format();
2290 return add_vec_tensor_mult(x, t, alpha);
2291 }
2292
2298 CUDA_HOST_DEVICE Matrix& set_identity()
2299 {
2300 for(int i(0); i < m_; ++i)
2301 for(int j(0); j < n_; ++j)
2302 v[i][j] = (i == j ? T_(1) : T_(0));
2303 return *this;
2304 }
2305
2314 CUDA_HOST_DEVICE Matrix& set_rotation_2d(T_ angle)
2315 {
2316 static_assert((m_ == 2) && (n_ == 2), "this function works only for 2x2 matrices");
2317 #ifndef __CUDACC__
2318 v[0][0] = (v[1][1] = Math::cos(angle));
2319 v[0][1] = -(v[1][0] = Math::sin(angle));
2320 #else
2321 v[0][0] = (v[1][1] = CudaMath::cuda_cos(angle));
2322 v[0][1] = -(v[1][0] = CudaMath::cuda_sin(angle));
2323 #endif
2324 return *this;
2325 }
2326
2341 CUDA_HOST_DEVICE Matrix& set_rotation_3d(T_ yaw, T_ pitch, T_ roll)
2342 {
2343 static_assert((m_ == 3) && (n_ == 3), "this function works only for 3x3 matrices");
2344 #ifndef __CUDACC__
2345 const T_ cy = Math::cos(yaw);
2346 const T_ sy = Math::sin(yaw);
2347 const T_ cp = Math::cos(pitch);
2348 const T_ sp = Math::sin(pitch);
2349 const T_ cr = Math::cos(roll);
2350 const T_ sr = Math::sin(roll);
2351 #else
2352 const T_ cy = CudaMath::cuda_cos(yaw);
2353 const T_ sy = CudaMath::cuda_sin(yaw);
2354 const T_ cp = CudaMath::cuda_cos(pitch);
2355 const T_ sp = CudaMath::cuda_sin(pitch);
2356 const T_ cr = CudaMath::cuda_cos(roll);
2357 const T_ sr = CudaMath::cuda_sin(roll);
2358 #endif
2359 v[0][0] = cy*cp;
2360 v[0][1] = cy*sp*sr - sy*cr;
2361 v[0][2] = cy*sp*cr + sy*sr;
2362 v[1][0] = sy*cp;
2363 v[1][1] = sy*sp*sr + cy*cr;
2364 v[1][2] = sy*sp*cr - cy*sr;
2365 v[2][0] = -sp;
2366 v[2][1] = cp*sr;
2367 v[2][2] = cp*cr;
2368 return *this;
2369 }
2370
2374 CUDA_HOST_DEVICE static Matrix null()
2375 {
2376 return Matrix(DataType(0));
2377 }
2378
2383 {
2384 return Dist::Typemap{}.add_entry<RowType>(0, sm_);
2385 }
2386
2393 CUDA_HOST friend std::ostream & operator<< (std::ostream & lhs, const Matrix& A)
2394 {
2395 for (int i(0) ; i < m-1 ; ++i)
2396 {
2397 lhs << A[i] << "\n";
2398 }
2399 lhs << A[m-1];
2400
2401 return lhs;
2402 }
2403
2410 CUDA_HOST friend std::istream& operator>>(std::istream & in, Matrix& A)
2411 {
2412 for (int i(0) ; i < m; ++i)
2413 {
2414 in >> A[i];
2415 }
2416
2417 return in;
2418 }
2419 }; // class Matrix
2420
2430 template<typename T_, int mx_, int smx_, int ma_, int na_, int sm_, int sn_>
2432 {
2433 static_assert(mx_ >= 2, "invalid nu vector size for orthogonal_2x1");
2434 static_assert(ma_ >= 2, "invalid matrix row size for orthogonal_2x1");
2435 static_assert(na_ >= 1, "invalid matrix column size for orthogonal_2x1");
2436
2437 // 2d "cross" product. The sign has to be on the second component so the input is rotated in negative direction
2438 nu[0] = tau[1][0];
2439 nu[1] = -tau[0][0];
2440 }
2441
2451 template<typename T_, int mx_, int smx_, int ma_, int na_, int sm_, int sn_>
2453 {
2454 static_assert(mx_ >= 3, "invalid nu vector size for orthogonal_3x2");
2455 static_assert(ma_ >= 3, "invalid matrix row size for orthogonal_3x2");
2456 static_assert(na_ >= 2, "invalid matrix column size for orthogonal_3x2");
2457
2458 // 3d cross product
2459 nu[0] = tau[1][0]*tau[2][1] - tau[2][0]*tau[1][1];
2460 nu[1] = tau[2][0]*tau[0][1] - tau[0][0]*tau[2][1];
2461 nu[2] = tau[0][0]*tau[1][1] - tau[1][0]*tau[0][1];
2462 }
2463
2464#ifdef DOXYGEN
2488 template<typename T_, int m_, int sm_, int sn_>
2490#endif
2491
2493 template<typename T_, int sm_, int sn_>
2494 CUDA_HOST_DEVICE Vector<T_, 2> orthogonal(const Matrix<T_, 2, 1, sm_, sn_>& tau)
2495 {
2496 Vector<T_, 2, sm_> nu(T_(0));
2497 orthogonal_2x1(nu, tau);
2498 return nu;
2499 }
2500
2501 template<typename T_, int sm_, int sn_>
2502 CUDA_HOST_DEVICE Vector<T_, 3> orthogonal(const Matrix<T_, 3, 2, sm_, sn_>& tau)
2503 {
2504 Vector<T_, 3, sm_> nu(T_(0));
2505 orthogonal_3x2(nu, tau);
2506 return nu;
2507 }
2509
2511 template<typename T_, int m_, int n_, int sm_, int sn_, int sx_>
2513 {
2514 return Vector<T_, m_>().set_mat_vec_mult(a, x);
2515 }
2516
2518 template<typename T_, int m_, int n_, int sm_, int sn_, int sx_>
2520 {
2521 return Vector<T_, n_>().set_vec_mat_mult(x, a);
2522 }
2523
2525 template<typename T_, int m_, int n_, int sm_, int sn_>
2527 {
2528 return Matrix<T_, m_, n_>(a) *= alpha;
2529 }
2530
2532 template<typename T_, int m_, int n_, int sm_, int sn_>
2534 {
2535 return Matrix<T_, m_, n_>(a) *= alpha;
2536 }
2537
2539 template<typename T_, int m_, int n_, int l_, int sma_, int sna_, int smb_, int snb_>
2541 {
2542 return Matrix<T_, m_, n_>().set_mat_mat_mult(a, b);
2543 }
2544
2546 template<typename T_, int m_, int n_,int sma_, int sna_, int smb_, int snb_>
2548 {
2549 return Matrix<T_, m_, n_>(a) += b;
2550 }
2551
2553 template<typename T_, int m_, int n_,int sma_, int sna_, int smb_, int snb_>
2555 {
2556 return Matrix<T_, m_, n_>(a) -= b;
2557 }
2558
2559 /* ************************************************************************************************************* */
2560 /* ************************************************************************************************************* */
2561 // Tiny Tensor3 implementation
2562 /* ************************************************************************************************************* */
2563 /* ************************************************************************************************************* */
2564
2565 template<typename T_, int l_, int m_, int n_, int sl_, int sm_, int sn_>
2566 class Tensor3
2567 {
2568 static_assert(l_ > 0, "invalid tube count");
2569 static_assert(m_ > 0, "invalid row count");
2570 static_assert(n_ > 0, "invalid column count");
2571 static_assert(sl_ >= l_, "invalid tube stride");
2572 static_assert(sm_ >= m_, "invalid row stride");
2573 static_assert(sn_ >= n_, "invalid column stride");
2574
2575 public:
2577 static constexpr int l = l_;
2579 static constexpr int m = m_;
2581 static constexpr int n = n_;
2583 static constexpr int sl = sl_;
2585 static constexpr int sm = sm_;
2587 static constexpr int sn = sn_;
2588
2590 typedef T_ ValueType;
2592 typedef typename Intern::DataTypeExtractor<ValueType>::MyDataType DataType;
2593
2598
2600 CUDA_HOST_DEVICE Tensor3()
2601 {
2602 }
2603
2605 CUDA_HOST_DEVICE explicit Tensor3(DataType value)
2606 {
2607 for(int i(0); i < l_; ++i)
2608 v[i] = value;
2609 }
2610
2617 template<typename Tx_>
2618 CUDA_HOST_DEVICE explicit Tensor3(const std::initializer_list<Tx_>& x)
2619 {
2620 XASSERTM(std::size_t(l_) == x.size(), "invalid initializer list size");
2621 auto it(x.begin());
2622 for(int i(0); i < l_; ++i, ++it)
2623 v[i] = *it;
2624 }
2625
2632 template<typename Tx_>
2633 CUDA_HOST_DEVICE explicit Tensor3(const std::initializer_list<std::initializer_list<std::initializer_list<Tx_>>>& x)
2634 {
2635 XASSERTM(std::size_t(l_) == x.size(), "invalid initializer list size");
2636 auto it(x.begin());
2637 for(int i(0); i < l_; ++i, ++it)
2638 v[i] = *it;
2639 }
2640
2642 template<int sla_, int sma_, int sna_>
2644 {
2645 for(int i(0); i < l_; ++i)
2646 v[i] = a.v[i];
2647 }
2648
2650 CUDA_HOST_DEVICE Tensor3& operator=(DataType value)
2651 {
2652 for(int i(0); i < l_; ++i)
2653 v[i] = value;
2654 return *this;
2655 }
2656
2658 template<int sla_, int sma_, int sna_>
2660 {
2661 for(int i(0); i < l_; ++i)
2662 v[i] = a.v[i];
2663 return *this;
2664 }
2665
2674 template<typename Tx_>
2675 CUDA_HOST_DEVICE Tensor3& operator=(const std::initializer_list<Tx_>& x)
2676 {
2677 XASSERTM(std::size_t(l_) == x.size(), "invalid initializer list size");
2678 auto it(x.begin());
2679 for(int i(0); i < l_; ++i, ++it)
2680 v[i] = *it;
2681 return *this;
2682 }
2683
2692 template<typename Tx_>
2693 CUDA_HOST_DEVICE Tensor3& operator=(const std::initializer_list<std::initializer_list<std::initializer_list<Tx_>>>& x)
2694 {
2695 XASSERTM(std::size_t(l_) == x.size(), "invalid initializer list size");
2696 auto it(x.begin());
2697 for(int i(0); i < l_; ++i, ++it)
2698 v[i] = *it;
2699 return *this;
2700 }
2701
2703 template<typename Tx_, int sla_, int sma_, int sna_>
2705 {
2706 for(int i(0); i < l_; ++i)
2707 v[i].convert(a.v[i]);
2708 }
2709
2719 CUDA_HOST_DEVICE T_& operator()(int h, int i, int j)
2720 {
2721 ASSERTM( (h >= 0) && (h < l_), "index h out-of-bounds");
2722 ASSERTM( (i >= 0) && (i < m_), "index i out-of-bounds");
2723 ASSERTM( (j >= 0) && (j < n_), "index j out-of-bounds");
2724 return v[h](i,j);
2725 }
2726
2728 CUDA_HOST_DEVICE const T_& operator()(int h, int i, int j) const
2729 {
2730 ASSERTM( (h >= 0) && (h < l_), "index h out-of-bounds");
2731 ASSERTM( (i >= 0) && (i < m_), "index i out-of-bounds");
2732 ASSERTM( (j >= 0) && (j < n_), "index j out-of-bounds");
2733 return v[h](i,j);
2734 }
2735
2745 CUDA_HOST_DEVICE PlaneType& operator[](int h)
2746 {
2747 ASSERTM( (h >= 0) && (h < l_), "index h out-of-bounds");
2748 return v[h];
2749 }
2750
2752 CUDA_HOST_DEVICE const PlaneType& operator[](int h) const
2753 {
2754 ASSERTM( (h >= 0) && (h < l_), "index h out-of-bounds");
2755 return v[h];
2756 }
2757
2759 CUDA_HOST_DEVICE Tensor3& operator*=(DataType alpha)
2760 {
2761 for(int i(0); i < l_; ++i)
2762 v[i] *= alpha;
2763 return *this;
2764 }
2765
2767 template<int sla_, int sma_, int sna_>
2769 {
2770 for(int i(0); i < l_; ++i)
2771 v[i] += a.v[i];
2772 return *this;
2773 }
2774
2776 template<int sla_, int sma_, int sna_>
2778 {
2779 for(int i(0); i < l_; ++i)
2780 v[i] -= a.v[i];
2781 return *this;
2782 }
2783
2790 template<int sla_, int sma_, int sna_>
2791 CUDA_HOST_DEVICE void copy(const Tensor3<T_, l_, m_, n_, sla_, sma_, sna_>& a)
2792 {
2793 for(int i(0); i < l_; ++i)
2794 for(int j(0); j < m_; ++j)
2795 for(int k(0); k < n_; ++k)
2796 v[i][j][k] = a.v[i][j][k];
2797 }
2798
2811 template<int ll_,int mm_, int nn_, int la_, int ma_, int na_, int sla_, int sma_, int sna_>
2813 {
2814 static_assert(ll_ <= l_, "invalid copy_n size");
2815 static_assert(ll_ <= la_, "invalid copy_n size");
2816 static_assert(mm_ <= m_, "invalid copy_n size");
2817 static_assert(mm_ <= ma_, "invalid copy_n size");
2818 static_assert(nn_ <= n_, "invalid copy_n size");
2819 static_assert(nn_ <= na_, "invalid copy_n size");
2820 for(int i(0); i < ll_; ++i)
2821 for(int j(0); j < mm_; ++j)
2822 for(int k(0); k < nn_; ++k)
2823 v[i][j][k] = a.v[i][j][k];
2824 }
2825
2827 CUDA_HOST_DEVICE void format(DataType alpha = DataType(0))
2828 {
2829 (*this) = alpha;
2830 }
2831
2852 template<int k_, int sma_, int sna_, int slt_, int smt_, int snt_>
2853 CUDA_HOST_DEVICE Tensor3& add_mat_tensor_mult(
2856 DataType alpha = DataType(1))
2857 {
2858 // we have to compare void* addresses here, because we might get a type mismatch error otherwise
2859 ASSERTM((const void*)this != (const void*)&t, "result tensor and multiplicand tensor 't' must be different objects");
2860
2861 for(int h(0); h < l_; ++h)
2862 {
2863 for(int i(0); i < m_; ++i)
2864 {
2865 for(int j(0); j < n_; ++j)
2866 {
2867 DataType r(0);
2868 for(int p(0); p < k_; ++p)
2869 {
2870 r += a(h,p) * t(p,i,j);
2871 }
2872 operator()(h,i,j) += alpha * r;
2873 }
2874 }
2875 }
2876 return *this;
2877 }
2878
2903 template<
2904 int lt_, int mt_, int nt_, // input tensor dimensions
2905 int slt_, int smt_, int snt_, // input tensor strides
2906 int smb_, int snb_, int smd_, int snd_> // input matrix strides
2907 CUDA_HOST_DEVICE Tensor3& add_double_mat_mult(
2911 DataType alpha = DataType(1))
2912 {
2913 // we have to compare void* addresses here, because we might get a type mismatch error otherwise
2914 ASSERTM((const void*)this != (const void*)&t, "result tensor and multiplicand tensor 't' must be different objects");
2915
2916 for(int h(0); h < l_; ++h)
2917 {
2918 for(int i(0); i < m_; ++i)
2919 {
2920 for(int j(0); j < n_; ++j)
2921 {
2922 DataType r(0);
2923 for(int p(0); p < mt_; ++p)
2924 {
2925 for(int q(0); q < nt_; ++q)
2926 {
2927 r += t(h,p,q) * b(p,i) * d(q,j);
2928 }
2929 }
2930 operator()(h,i,j) += alpha * r;
2931 }
2932 }
2933 }
2934 return *this;
2935 }
2936
2957 template<int slx_, int sma_, int sna_>
2959 const Vector<T_, l_, slx_>& x,
2961 DataType alpha = DataType(1))
2962 {
2963 for(int h(0); h < l_; ++h)
2964 {
2965 for(int i(0); i < m_; ++i)
2966 {
2967 for(int j(0); j < n_; ++j)
2968 {
2969 operator()(h,i,j) += alpha * x(h) * a(i, j);
2970 }
2971 }
2972 }
2973 return *this;
2974 }
2975
2979 CUDA_HOST_DEVICE static Tensor3 null()
2980 {
2981 return Tensor3(DataType(0));
2982 }
2983
2988 {
2989 return Dist::Typemap{}.add_entry<PlaneType>(0, sl_);
2990 }
2991 }; // class Tensor3<...>
2992
2994 template<typename T_, int l_, int m_, int n_, int sl_, int sm_, int sn_>
2997 {
2998 return Tensor3<T_, l_, m_, n_, sl_, sm_, sn_>(a) *= alpha;
2999 }
3000
3002 template<typename T_, int l_, int m_, int n_, int sl_, int sm_, int sn_>
3005 {
3006 return Tensor3<T_, l_, m_, n_, sl_, sm_, sn_>(a) *= alpha;
3007 }
3008
3009 /* ************************************************************************************************************* */
3010 /* ************************************************************************************************************* */
3011 // Various helper functions
3012 /* ************************************************************************************************************* */
3013 /* ************************************************************************************************************* */
3025 template<typename T_, typename std::enable_if<Intern::DataTypeExtractor<T_>::level == 0, bool>::type = true>
3026 CUDA_HOST_DEVICE inline T_ dot(const T_& a, const T_& b)
3027 {
3028 return a*b;
3029 }
3030
3042 template<typename T_, int n_, int sa_, int sb_>
3043 CUDA_HOST_DEVICE inline typename Vector<T_, n_>::DataType dot(const Vector<T_, n_, sa_>& a, const Vector<T_, n_, sb_>& b)
3044 {
3045 typename Vector<T_, n_>::DataType r(0);
3046
3047 for(int i(0); i < n_; ++i)
3048 {
3049 r += Tiny::dot(a.v[i], b.v[i]);
3050 }
3051 return r;
3052 }
3053
3065 template<typename T_, int m_, int n_, int sma_, int sna_, int smb_, int snb_>
3067 {
3068 typename Matrix<T_, m_, n_>::DataType r(0);
3069 for(int i(0); i < m_; ++i)
3070 {
3071 r += Tiny::dot(a.v[i], b.v[i]);
3072 }
3073 return r;
3074 }
3075
3084 template<typename T_, int l_, int m_, int n_, int sla_ ,int sma_, int sna_, int slb_, int smb_, int snb_>
3085 CUDA_HOST_DEVICE inline typename Tensor3<T_, l_, m_, n_>::DataType dot(
3088 {
3090 for(int i(0); i < l_; ++i)
3091 {
3092 r += Tiny::dot(a.v[i], b.v[i]);
3093 }
3094 return r;
3095 }
3096
3106 template<typename T_>
3107 CUDA_HOST_DEVICE inline void add_id(T_& x, const T_& alpha)
3108 {
3109 x += alpha;
3110 }
3111
3121 template<typename T_, int n_, int sn_>
3122 CUDA_HOST_DEVICE inline void add_id(Vector<T_, n_, sn_>& x, const typename Vector<T_, n_, sn_>::DataType& alpha)
3123 {
3124 for(int i(0); i < n_; ++i)
3125 add_id(x(i), alpha);
3126 }
3127
3137 template<typename T_, int n_, int sm_, int sn_>
3138 CUDA_HOST_DEVICE inline void add_id(Matrix<T_, n_, n_, sm_, sn_>& x, const typename Matrix<T_, n_, n_, sm_, sn_>::DataType& alpha)
3139 {
3140 for(int i(0); i < n_; ++i)
3141 add_id(x(i,i), alpha);
3142 }
3143
3153 template<typename T_, int n_, int sl_, int sm_, int sn_>
3155 {
3156 for(int i(0); i < n_; ++i)
3157 add_id(x(i,i,i), alpha);
3158 }
3159
3174 template<typename T_>
3175 CUDA_HOST_DEVICE inline void axpy(T_& y, const T_& x, const T_& alpha)
3176 {
3177 y += alpha*x;
3178 }
3179
3194 template<typename T_, int n_, int sn_>
3195 CUDA_HOST_DEVICE inline void axpy(
3197 const Vector<T_, n_, sn_>& x,
3198 const typename Vector<T_, n_, sn_>::DataType& alpha)
3199 {
3200 for(int i(0); i < n_; ++i)
3201 axpy(y.v[i], x.v[i], alpha);
3202 }
3203
3218 template<typename T_, int m_, int n_, int sm_, int sn_>
3219 CUDA_HOST_DEVICE inline void axpy(
3222 const typename Matrix<T_, m_, n_, sm_, sn_>::DataType& alpha)
3223 {
3224 for(int i(0); i < m_; ++i)
3225 axpy(y.v[i], x.v[i], alpha);
3226 }
3227
3242 template<typename T_, int l_, int m_, int n_, int sl_, int sm_, int sn_>
3243 CUDA_HOST_DEVICE inline void axpy(
3247 {
3248 for(int i(0); i < l_; ++i)
3249 axpy(y.v[i], x.v[i], alpha);
3250 }
3251
3252 /* ************************************************************************************************************* */
3253 /* ************************************************************************************************************* */
3254 // Internal helpers implementation
3255 /* ************************************************************************************************************* */
3256 /* ************************************************************************************************************* */
3257
3259 namespace Intern
3260 {
3261 // generic square matrix inversion:
3262 template<int n_>
3263 struct DetHelper<n_,n_>
3264 {
3265 template<typename T_, int sma_, int sna_>
3266 CUDA_HOST_DEVICE static T_ compute(const Tiny::Matrix<T_, n_, n_, sma_, sna_>& a)
3267 {
3268 // perform matrix inversion which returns the determinant
3270 const T_ det = Intern::InverseHelper<n_, n_>::compute(b, a);
3271
3272 // if the returned value is not normal, we can assume that the matrix is singular
3273 #ifndef __CUDACC__
3274 return Math::isnormal(det) ? det : T_(0);
3275 #else
3276 return CudaMath::cuda_isnormal(det) ? det : T_(0);
3277 #endif
3278 }
3279 };
3280
3281 template<>
3282 struct DetHelper<1,1>
3283 {
3284 template<typename T_, int sma_, int sna_>
3285 CUDA_HOST_DEVICE static T_ compute(const Tiny::Matrix<T_, 1, 1, sma_, sna_>& a)
3286 {
3287 return a(0,0);
3288 }
3289 };
3290
3291
3292 template<>
3293 struct DetHelper<2,2>
3294 {
3295 template<typename T_, int sma_, int sna_>
3296 CUDA_HOST_DEVICE static T_ compute(const Tiny::Matrix<T_, 2, 2, sma_, sna_>& a)
3297 {
3298 return a(0,0)*a(1,1) - a(0,1)*a(1,0);
3299 }
3300 };
3301
3302 template<>
3303 struct DetHelper<3,3>
3304 {
3305 template<typename T_, int sma_, int sna_>
3306 CUDA_HOST_DEVICE static T_ compute(const Tiny::Matrix<T_, 3, 3, sma_, sna_>& a)
3307 {
3308 return a(0,0)*(a(1,1)*a(2,2) - a(1,2)*a(2,1))
3309 + a(0,1)*(a(1,2)*a(2,0) - a(1,0)*a(2,2))
3310 + a(0,2)*(a(1,0)*a(2,1) - a(1,1)*a(2,0));
3311 }
3312 };
3313
3314 template<>
3315 struct DetHelper<4,4>
3316 {
3317 template<typename T_, int sma_, int sna_>
3318 CUDA_HOST_DEVICE static T_ compute(const Tiny::Matrix<T_, 4, 4, sma_, sna_>& a)
3319 {
3320 // 2x2 determinants of rows 3-4
3321 T_ w[6] =
3322 {
3323 a(2,0)*a(3,1) - a(2,1)*a(3,0),
3324 a(2,0)*a(3,2) - a(2,2)*a(3,0),
3325 a(2,0)*a(3,3) - a(2,3)*a(3,0),
3326 a(2,1)*a(3,2) - a(2,2)*a(3,1),
3327 a(2,1)*a(3,3) - a(2,3)*a(3,1),
3328 a(2,2)*a(3,3) - a(2,3)*a(3,2)
3329 };
3330
3331 return
3332 + a(0,0) * (a(1,1)*w[5] - a(1,2)*w[4] + a(1,3)*w[3])
3333 - a(0,1) * (a(1,0)*w[5] - a(1,2)*w[2] + a(1,3)*w[1])
3334 + a(0,2) * (a(1,0)*w[4] - a(1,1)*w[2] + a(1,3)*w[0])
3335 - a(0,3) * (a(1,0)*w[3] - a(1,1)*w[1] + a(1,2)*w[0]);
3336 }
3337 };
3338
3339 template<>
3340 struct DetHelper<5,5>
3341 {
3342 template<typename T_, int sma_, int sna_>
3343 CUDA_HOST_DEVICE static T_ compute(const Tiny::Matrix<T_, 5, 5, sma_, sna_>& a)
3344 {
3345 // 2x2 determinants of rows 4-5
3346 T_ v[10] =
3347 {
3348 a(3,0)*a(4,1) - a(3,1)*a(4,0),
3349 a(3,0)*a(4,2) - a(3,2)*a(4,0),
3350 a(3,0)*a(4,3) - a(3,3)*a(4,0),
3351 a(3,0)*a(4,4) - a(3,4)*a(4,0),
3352 a(3,1)*a(4,2) - a(3,2)*a(4,1),
3353 a(3,1)*a(4,3) - a(3,3)*a(4,1),
3354 a(3,1)*a(4,4) - a(3,4)*a(4,1),
3355 a(3,2)*a(4,3) - a(3,3)*a(4,2),
3356 a(3,2)*a(4,4) - a(3,4)*a(4,2),
3357 a(3,3)*a(4,4) - a(3,4)*a(4,3)
3358 };
3359 // 3x3 determinants of rows 3-4-5
3360 T_ w[10] =
3361 {
3362 a(2,0)*v[4] - a(2,1)*v[1] + a(2,2)*v[0],
3363 a(2,0)*v[5] - a(2,1)*v[2] + a(2,3)*v[0],
3364 a(2,0)*v[6] - a(2,1)*v[3] + a(2,4)*v[0],
3365 a(2,0)*v[7] - a(2,2)*v[2] + a(2,3)*v[1],
3366 a(2,0)*v[8] - a(2,2)*v[3] + a(2,4)*v[1],
3367 a(2,0)*v[9] - a(2,3)*v[3] + a(2,4)*v[2],
3368 a(2,1)*v[7] - a(2,2)*v[5] + a(2,3)*v[4],
3369 a(2,1)*v[8] - a(2,2)*v[6] + a(2,4)*v[4],
3370 a(2,1)*v[9] - a(2,3)*v[6] + a(2,4)*v[5],
3371 a(2,2)*v[9] - a(2,3)*v[8] + a(2,4)*v[7]
3372 };
3373
3374 return
3375 + a(0,0)*(a(1,1)*w[9] - a(1,2)*w[8] + a(1,3)*w[7] - a(1,4)*w[6])
3376 - a(0,1)*(a(1,0)*w[9] - a(1,2)*w[5] + a(1,3)*w[4] - a(1,4)*w[3])
3377 + a(0,2)*(a(1,0)*w[8] - a(1,1)*w[5] + a(1,3)*w[2] - a(1,4)*w[1])
3378 - a(0,3)*(a(1,0)*w[7] - a(1,1)*w[4] + a(1,2)*w[2] - a(1,4)*w[0])
3379 + a(0,4)*(a(1,0)*w[6] - a(1,1)*w[3] + a(1,2)*w[1] - a(1,3)*w[0]);
3380 }
3381 };
3382
3383 template<>
3384 struct DetHelper<6,6>
3385 {
3386 template<typename T_, int sma_, int sna_>
3387 CUDA_HOST_DEVICE static T_ compute(const Tiny::Matrix<T_, 6, 6, sma_, sna_>& a)
3388 {
3389 // 2x2 determinants of rows 5-6
3390 T_ v[15] =
3391 {
3392 a(4,0)*a(5,1) - a(4,1)*a(5,0),
3393 a(4,0)*a(5,2) - a(4,2)*a(5,0),
3394 a(4,0)*a(5,3) - a(4,3)*a(5,0),
3395 a(4,0)*a(5,4) - a(4,4)*a(5,0),
3396 a(4,0)*a(5,5) - a(4,5)*a(5,0),
3397 a(4,1)*a(5,2) - a(4,2)*a(5,1),
3398 a(4,1)*a(5,3) - a(4,3)*a(5,1),
3399 a(4,1)*a(5,4) - a(4,4)*a(5,1),
3400 a(4,1)*a(5,5) - a(4,5)*a(5,1),
3401 a(4,2)*a(5,3) - a(4,3)*a(5,2),
3402 a(4,2)*a(5,4) - a(4,4)*a(5,2),
3403 a(4,2)*a(5,5) - a(4,5)*a(5,2),
3404 a(4,3)*a(5,4) - a(4,4)*a(5,3),
3405 a(4,3)*a(5,5) - a(4,5)*a(5,3),
3406 a(4,4)*a(5,5) - a(4,5)*a(5,4)
3407 };
3408 // 3x3 determinants of rows 4-5-6
3409 T_ w[20] =
3410 {
3411 a(3,0)*v[ 5] - a(3,1)*v[ 1] + a(3,2)*v[ 0],
3412 a(3,0)*v[ 6] - a(3,1)*v[ 2] + a(3,3)*v[ 0],
3413 a(3,0)*v[ 7] - a(3,1)*v[ 3] + a(3,4)*v[ 0],
3414 a(3,0)*v[ 8] - a(3,1)*v[ 4] + a(3,5)*v[ 0],
3415 a(3,0)*v[ 9] - a(3,2)*v[ 2] + a(3,3)*v[ 1],
3416 a(3,0)*v[10] - a(3,2)*v[ 3] + a(3,4)*v[ 1],
3417 a(3,0)*v[11] - a(3,2)*v[ 4] + a(3,5)*v[ 1],
3418 a(3,0)*v[12] - a(3,3)*v[ 3] + a(3,4)*v[ 2],
3419 a(3,0)*v[13] - a(3,3)*v[ 4] + a(3,5)*v[ 2],
3420 a(3,0)*v[14] - a(3,4)*v[ 4] + a(3,5)*v[ 3],
3421 a(3,1)*v[ 9] - a(3,2)*v[ 6] + a(3,3)*v[ 5],
3422 a(3,1)*v[10] - a(3,2)*v[ 7] + a(3,4)*v[ 5],
3423 a(3,1)*v[11] - a(3,2)*v[ 8] + a(3,5)*v[ 5],
3424 a(3,1)*v[12] - a(3,3)*v[ 7] + a(3,4)*v[ 6],
3425 a(3,1)*v[13] - a(3,3)*v[ 8] + a(3,5)*v[ 6],
3426 a(3,1)*v[14] - a(3,4)*v[ 8] + a(3,5)*v[ 7],
3427 a(3,2)*v[12] - a(3,3)*v[10] + a(3,4)*v[ 9],
3428 a(3,2)*v[13] - a(3,3)*v[11] + a(3,5)*v[ 9],
3429 a(3,2)*v[14] - a(3,4)*v[11] + a(3,5)*v[10],
3430 a(3,3)*v[14] - a(3,4)*v[13] + a(3,5)*v[12]
3431 };
3432 // 4x4 determinants of rows 3-4-5-6
3433 v[ 0] = a(2,0)*w[10] - a(2,1)*w[ 4] + a(2,2)*w[ 1] - a(2,3)*w[ 0];
3434 v[ 1] = a(2,0)*w[11] - a(2,1)*w[ 5] + a(2,2)*w[ 2] - a(2,4)*w[ 0];
3435 v[ 2] = a(2,0)*w[12] - a(2,1)*w[ 6] + a(2,2)*w[ 3] - a(2,5)*w[ 0];
3436 v[ 3] = a(2,0)*w[13] - a(2,1)*w[ 7] + a(2,3)*w[ 2] - a(2,4)*w[ 1];
3437 v[ 4] = a(2,0)*w[14] - a(2,1)*w[ 8] + a(2,3)*w[ 3] - a(2,5)*w[ 1];
3438 v[ 5] = a(2,0)*w[15] - a(2,1)*w[ 9] + a(2,4)*w[ 3] - a(2,5)*w[ 2];
3439 v[ 6] = a(2,0)*w[16] - a(2,2)*w[ 7] + a(2,3)*w[ 5] - a(2,4)*w[ 4];
3440 v[ 7] = a(2,0)*w[17] - a(2,2)*w[ 8] + a(2,3)*w[ 6] - a(2,5)*w[ 4];
3441 v[ 8] = a(2,0)*w[18] - a(2,2)*w[ 9] + a(2,4)*w[ 6] - a(2,5)*w[ 5];
3442 v[ 9] = a(2,0)*w[19] - a(2,3)*w[ 9] + a(2,4)*w[ 8] - a(2,5)*w[ 7];
3443 v[10] = a(2,1)*w[16] - a(2,2)*w[13] + a(2,3)*w[11] - a(2,4)*w[10];
3444 v[11] = a(2,1)*w[17] - a(2,2)*w[14] + a(2,3)*w[12] - a(2,5)*w[10];
3445 v[12] = a(2,1)*w[18] - a(2,2)*w[15] + a(2,4)*w[12] - a(2,5)*w[11];
3446 v[13] = a(2,1)*w[19] - a(2,3)*w[15] + a(2,4)*w[14] - a(2,5)*w[13];
3447 v[14] = a(2,2)*w[19] - a(2,3)*w[18] + a(2,4)*w[17] - a(2,5)*w[16];
3448
3449 return
3450 + a(0,0)*(a(1,1)*v[14] - a(1,2)*v[13] + a(1,3)*v[12] - a(1,4)*v[11] + a(1,5)*v[10])
3451 - a(0,1)*(a(1,0)*v[14] - a(1,2)*v[ 9] + a(1,3)*v[ 8] - a(1,4)*v[ 7] + a(1,5)*v[ 6])
3452 + a(0,2)*(a(1,0)*v[13] - a(1,1)*v[ 9] + a(1,3)*v[ 5] - a(1,4)*v[ 4] + a(1,5)*v[ 3])
3453 - a(0,3)*(a(1,0)*v[12] - a(1,1)*v[ 8] + a(1,2)*v[ 5] - a(1,4)*v[ 2] + a(1,5)*v[ 1])
3454 + a(0,4)*(a(1,0)*v[11] - a(1,1)*v[ 7] + a(1,2)*v[ 4] - a(1,3)*v[ 2] + a(1,5)*v[ 0])
3455 - a(0,5)*(a(1,0)*v[10] - a(1,1)*v[ 6] + a(1,2)*v[ 3] - a(1,3)*v[ 1] + a(1,4)*v[ 0]);
3456 }
3457 };
3458
3464
3465 template<int m_, int n_>
3466 struct VolHelper
3467 {
3468 template<typename T_, int sma_, int sna_>
3469 CUDA_HOST_DEVICE static T_ compute(const Tiny::Matrix<T_, n_, n_, sma_, sna_>& a)
3470 {
3471 // generic fallback implementation: compute b := a^T * a and return sqrt(det(b))
3472 Tiny::Matrix<T_, n_, n_> b;
3473 for(int i(0); i < n_; ++i)
3474 {
3475 for(int j(0); j < n_; ++j)
3476 {
3477 b(i,j) = T_(0);
3478 for(int k(0); k < m_; ++k)
3479 {
3480 b(i,j) += a(k,i)*a(k,j);
3481 }
3482 }
3483 }
3484 #ifndef __CUDACC__
3485 return Math::sqrt(DetHelper<n_,n_>::compute(b));
3486 #else
3487 return CudaMath::cuda_sqrt(DetHelper<n_,n_>::compute(b));
3488 #endif
3489 }
3490 };
3491
3492 template<int n_>
3493 struct VolHelper<n_,n_>
3494 {
3495 template<typename T_, int sma_, int sna_>
3496 CUDA_HOST_DEVICE static T_ compute(const Tiny::Matrix<T_, n_, n_, sma_, sna_>& a)
3497 {
3498 // square matrix special case: vol(a) = abs(det(a))
3499 #ifndef __CUDACC__
3500 return Math::abs(DetHelper<n_,n_>::compute(a));
3501 #else
3502 return CudaMath::cuda_abs(DetHelper<n_,n_>::compute(a));
3503 #endif
3504 }
3505 };
3506
3507 template<>
3508 struct VolHelper<2,1>
3509 {
3510 template<typename T_, int sma_, int sna_>
3511 CUDA_HOST_DEVICE static T_ compute(const Tiny::Matrix<T_, 2, 1, sma_, sna_>& a)
3512 {
3513 // This is the euclid norm of the only matrix column.
3514 #ifndef __CUDACC__
3515 return Math::sqrt(Math::sqr(a(0,0)) + Math::sqr(a(1,0)));
3516 #else
3517 return CudaMath::cuda_sqrt(CudaMath::cuda_sqr(a(0,0)) + CudaMath::cuda_sqr(a(1,0)));
3518 #endif
3519 }
3520 };
3521
3522 template<>
3523 struct VolHelper<3,1>
3524 {
3525 template<typename T_, int sma_, int sna_>
3526 CUDA_HOST_DEVICE static T_ compute(const Tiny::Matrix<T_, 3, 1, sma_, sna_>& a)
3527 {
3528 // This is the euclid norm of the only matrix column.
3529 #ifndef __CUDACC__
3530 return Math::sqrt(Math::sqr(a(0,0)) + Math::sqr(a(1,0)) + Math::sqr(a(2,0)));
3531 #else
3532 return CudaMath::cuda_sqrt(CudaMath::cuda_sqr(a(0,0)) + CudaMath::cuda_sqr(a(1,0)) + CudaMath::cuda_sqr(a(2,0)));
3533 #endif
3534 }
3535 };
3536
3537 template<>
3538 struct VolHelper<3,2>
3539 {
3540 template<typename T_, int sma_, int sna_>
3541 CUDA_HOST_DEVICE static T_ compute(const Tiny::Matrix<T_, 3, 2, sma_, sna_>& a)
3542 {
3543 // This is the euclid norm of the 3D cross product of the two matrix columns.
3544 #ifndef __CUDACC__
3545 return Math::sqrt(
3546 Math::sqr(a(1,0)*a(2,1) - a(2,0)*a(1,1)) +
3547 Math::sqr(a(2,0)*a(0,1) - a(0,0)*a(2,1)) +
3548 Math::sqr(a(0,0)*a(1,1) - a(1,0)*a(0,1)));
3549 #else
3550 return CudaMath::cuda_sqrt(
3551 CudaMath::cuda_sqr(a(1,0)*a(2,1) - a(2,0)*a(1,1)) +
3552 CudaMath::cuda_sqr(a(2,0)*a(0,1) - a(0,0)*a(2,1)) +
3553 CudaMath::cuda_sqr(a(0,0)*a(1,1) - a(1,0)*a(0,1)));
3554 #endif
3555 }
3556 };
3557
3563
3564 template<>
3565 struct InverseHelper<1,1>
3566 {
3567 template<typename T_, int smb_, int snb_, int sma_, int sna_>
3568 CUDA_HOST_DEVICE static T_ compute(Tiny::Matrix<T_, 1, 1, smb_, snb_>& b, const Tiny::Matrix<T_, 1, 1, sma_, sna_>& a)
3569 {
3570 b(0,0) = T_(1) / a(0,0);
3571 return a(0,0);
3572 }
3573 };
3574
3575 template<>
3576 struct InverseHelper<2,2>
3577 {
3578 template<typename T_, int smb_, int snb_, int sma_, int sna_>
3579 CUDA_HOST_DEVICE static T_ compute(Tiny::Matrix<T_, 2, 2, smb_, snb_>& b, const Tiny::Matrix<T_, 2, 2, sma_, sna_>& a)
3580 {
3581 T_ det = a(0,0)*a(1,1) - a(0,1)*a(1,0);
3582 T_ d = T_(1) / det;
3583 b(0,0) = d*a(1,1);
3584 b(0,1) = -d*a(0,1);
3585 b(1,0) = -d*a(1,0);
3586 b(1,1) = d*a(0,0);
3587 return det;
3588 }
3589 };
3590
3591 template<>
3592 struct InverseHelper<3,3>
3593 {
3594 template<typename T_, int smb_, int snb_, int sma_, int sna_>
3595 CUDA_HOST_DEVICE static T_ compute(Tiny::Matrix<T_, 3, 3, smb_, snb_>& b, const Tiny::Matrix<T_, 3, 3, sma_, sna_>& a)
3596 {
3597 b(0,0) = a(1,1)*a(2,2) - a(1,2)*a(2,1);
3598 b(1,0) = a(1,2)*a(2,0) - a(1,0)*a(2,2);
3599 b(2,0) = a(1,0)*a(2,1) - a(1,1)*a(2,0);
3600 T_ det = a(0,0)*b(0,0) + a(0,1)*b(1,0) + a(0,2)*b(2,0);
3601 T_ d = T_(1) / det;
3602 b(0,0) *= d;
3603 b(1,0) *= d;
3604 b(2,0) *= d;
3605 b(0,1) = d*(a(0,2)*a(2,1) - a(0,1)*a(2,2));
3606 b(1,1) = d*(a(0,0)*a(2,2) - a(0,2)*a(2,0));
3607 b(2,1) = d*(a(0,1)*a(2,0) - a(0,0)*a(2,1));
3608 b(0,2) = d*(a(0,1)*a(1,2) - a(0,2)*a(1,1));
3609 b(1,2) = d*(a(0,2)*a(1,0) - a(0,0)*a(1,2));
3610 b(2,2) = d*(a(0,0)*a(1,1) - a(0,1)*a(1,0));
3611 return det;
3612 }
3613 };
3614
3615 template<>
3616 struct InverseHelper<4,4>
3617 {
3618 template<typename T_, int smb_, int snb_, int sma_, int sna_>
3619 CUDA_HOST_DEVICE static T_ compute(Tiny::Matrix<T_, 4, 4, smb_, snb_>& b, const Tiny::Matrix<T_, 4, 4, sma_, sna_>& a)
3620 {
3621 T_ w[6];
3622 w[0] = a(2,0)*a(3,1)-a(2,1)*a(3,0);
3623 w[1] = a(2,0)*a(3,2)-a(2,2)*a(3,0);
3624 w[2] = a(2,0)*a(3,3)-a(2,3)*a(3,0);
3625 w[3] = a(2,1)*a(3,2)-a(2,2)*a(3,1);
3626 w[4] = a(2,1)*a(3,3)-a(2,3)*a(3,1);
3627 w[5] = a(2,2)*a(3,3)-a(2,3)*a(3,2);
3628 b(0,0) = a(1,1)*w[5]-a(1,2)*w[4]+a(1,3)*w[3];
3629 b(1,0) =-a(1,0)*w[5]+a(1,2)*w[2]-a(1,3)*w[1];
3630 b(2,0) = a(1,0)*w[4]-a(1,1)*w[2]+a(1,3)*w[0];
3631 b(3,0) =-a(1,0)*w[3]+a(1,1)*w[1]-a(1,2)*w[0];
3632 T_ det = a(0,0)*b(0,0)+a(0,1)*b(1,0)+a(0,2)*b(2,0)+a(0,3)*b(3,0);
3633 T_ d = T_(1) / det;
3634 b(0,0) *= d;
3635 b(1,0) *= d;
3636 b(2,0) *= d;
3637 b(3,0) *= d;
3638 b(0,1) = d*(-a(0,1)*w[5]+a(0,2)*w[4]-a(0,3)*w[3]);
3639 b(1,1) = d*( a(0,0)*w[5]-a(0,2)*w[2]+a(0,3)*w[1]);
3640 b(2,1) = d*(-a(0,0)*w[4]+a(0,1)*w[2]-a(0,3)*w[0]);
3641 b(3,1) = d*( a(0,0)*w[3]-a(0,1)*w[1]+a(0,2)*w[0]);
3642 w[0] = a(0,0)*a(1,1)-a(0,1)*a(1,0);
3643 w[1] = a(0,0)*a(1,2)-a(0,2)*a(1,0);
3644 w[2] = a(0,0)*a(1,3)-a(0,3)*a(1,0);
3645 w[3] = a(0,1)*a(1,2)-a(0,2)*a(1,1);
3646 w[4] = a(0,1)*a(1,3)-a(0,3)*a(1,1);
3647 w[5] = a(0,2)*a(1,3)-a(0,3)*a(1,2);
3648 b(0,2) = d*( a(3,1)*w[5]-a(3,2)*w[4]+a(3,3)*w[3]);
3649 b(1,2) = d*(-a(3,0)*w[5]+a(3,2)*w[2]-a(3,3)*w[1]);
3650 b(2,2) = d*( a(3,0)*w[4]-a(3,1)*w[2]+a(3,3)*w[0]);
3651 b(3,2) = d*(-a(3,0)*w[3]+a(3,1)*w[1]-a(3,2)*w[0]);
3652 b(0,3) = d*(-a(2,1)*w[5]+a(2,2)*w[4]-a(2,3)*w[3]);
3653 b(1,3) = d*( a(2,0)*w[5]-a(2,2)*w[2]+a(2,3)*w[1]);
3654 b(2,3) = d*(-a(2,0)*w[4]+a(2,1)*w[2]-a(2,3)*w[0]);
3655 b(3,3) = d*( a(2,0)*w[3]-a(2,1)*w[1]+a(2,2)*w[0]);
3656 return det;
3657 }
3658 };
3659
3660 template<>
3661 struct InverseHelper<5,5>
3662 {
3663 template<typename T_, int smb_, int snb_, int sma_, int sna_>
3664 CUDA_HOST_DEVICE static T_ compute(Tiny::Matrix<T_, 5, 5, smb_, snb_>& b, const Tiny::Matrix<T_, 5, 5, sma_, sna_>& a)
3665 {
3666 T_ w[20];
3667 w[ 0] = a(3,0)*a(4,1)-a(3,1)*a(4,0);
3668 w[ 1] = a(3,0)*a(4,2)-a(3,2)*a(4,0);
3669 w[ 2] = a(3,0)*a(4,3)-a(3,3)*a(4,0);
3670 w[ 3] = a(3,0)*a(4,4)-a(3,4)*a(4,0);
3671 w[ 4] = a(3,1)*a(4,2)-a(3,2)*a(4,1);
3672 w[ 5] = a(3,1)*a(4,3)-a(3,3)*a(4,1);
3673 w[ 6] = a(3,1)*a(4,4)-a(3,4)*a(4,1);
3674 w[ 7] = a(3,2)*a(4,3)-a(3,3)*a(4,2);
3675 w[ 8] = a(3,2)*a(4,4)-a(3,4)*a(4,2);
3676 w[ 9] = a(3,3)*a(4,4)-a(3,4)*a(4,3);
3677 w[10] = a(2,0)*w[4]-a(2,1)*w[1]+a(2,2)*w[0];
3678 w[11] = a(2,0)*w[5]-a(2,1)*w[2]+a(2,3)*w[0];
3679 w[12] = a(2,0)*w[6]-a(2,1)*w[3]+a(2,4)*w[0];
3680 w[13] = a(2,0)*w[7]-a(2,2)*w[2]+a(2,3)*w[1];
3681 w[14] = a(2,0)*w[8]-a(2,2)*w[3]+a(2,4)*w[1];
3682 w[15] = a(2,0)*w[9]-a(2,3)*w[3]+a(2,4)*w[2];
3683 w[16] = a(2,1)*w[7]-a(2,2)*w[5]+a(2,3)*w[4];
3684 w[17] = a(2,1)*w[8]-a(2,2)*w[6]+a(2,4)*w[4];
3685 w[18] = a(2,1)*w[9]-a(2,3)*w[6]+a(2,4)*w[5];
3686 w[19] = a(2,2)*w[9]-a(2,3)*w[8]+a(2,4)*w[7];
3687 b(0,0) = a(1,1)*w[19]-a(1,2)*w[18]+a(1,3)*w[17]-a(1,4)*w[16];
3688 b(1,0) =-a(1,0)*w[19]+a(1,2)*w[15]-a(1,3)*w[14]+a(1,4)*w[13];
3689 b(2,0) = a(1,0)*w[18]-a(1,1)*w[15]+a(1,3)*w[12]-a(1,4)*w[11];
3690 b(3,0) =-a(1,0)*w[17]+a(1,1)*w[14]-a(1,2)*w[12]+a(1,4)*w[10];
3691 b(4,0) = a(1,0)*w[16]-a(1,1)*w[13]+a(1,2)*w[11]-a(1,3)*w[10];
3692 T_ det = a(0,0)*b(0,0)+a(0,1)*b(1,0)+a(0,2)*b(2,0)+a(0,3)*b(3,0)+a(0,4)*b(4,0);
3693 T_ d = T_(1) / det;
3694 b(0,0) *= d;
3695 b(1,0) *= d;
3696 b(2,0) *= d;
3697 b(3,0) *= d;
3698 b(4,0) *= d;
3699 b(0,1) = d*(-a(0,1)*w[19]+a(0,2)*w[18]-a(0,3)*w[17]+a(0,4)*w[16]);
3700 b(1,1) = d*( a(0,0)*w[19]-a(0,2)*w[15]+a(0,3)*w[14]-a(0,4)*w[13]);
3701 b(2,1) = d*(-a(0,0)*w[18]+a(0,1)*w[15]-a(0,3)*w[12]+a(0,4)*w[11]);
3702 b(3,1) = d*( a(0,0)*w[17]-a(0,1)*w[14]+a(0,2)*w[12]-a(0,4)*w[10]);
3703 b(4,1) = d*(-a(0,0)*w[16]+a(0,1)*w[13]-a(0,2)*w[11]+a(0,3)*w[10]);
3704 w[10] = a(1,0)*w[4]-a(1,1)*w[1]+a(1,2)*w[0];
3705 w[11] = a(1,0)*w[5]-a(1,1)*w[2]+a(1,3)*w[0];
3706 w[12] = a(1,0)*w[6]-a(1,1)*w[3]+a(1,4)*w[0];
3707 w[13] = a(1,0)*w[7]-a(1,2)*w[2]+a(1,3)*w[1];
3708 w[14] = a(1,0)*w[8]-a(1,2)*w[3]+a(1,4)*w[1];
3709 w[15] = a(1,0)*w[9]-a(1,3)*w[3]+a(1,4)*w[2];
3710 w[16] = a(1,1)*w[7]-a(1,2)*w[5]+a(1,3)*w[4];
3711 w[17] = a(1,1)*w[8]-a(1,2)*w[6]+a(1,4)*w[4];
3712 w[18] = a(1,1)*w[9]-a(1,3)*w[6]+a(1,4)*w[5];
3713 w[19] = a(1,2)*w[9]-a(1,3)*w[8]+a(1,4)*w[7];
3714 b(0,2) = d*( a(0,1)*w[19]-a(0,2)*w[18]+a(0,3)*w[17]-a(0,4)*w[16]);
3715 b(1,2) = d*(-a(0,0)*w[19]+a(0,2)*w[15]-a(0,3)*w[14]+a(0,4)*w[13]);
3716 b(2,2) = d*( a(0,0)*w[18]-a(0,1)*w[15]+a(0,3)*w[12]-a(0,4)*w[11]);
3717 b(3,2) = d*(-a(0,0)*w[17]+a(0,1)*w[14]-a(0,2)*w[12]+a(0,4)*w[10]);
3718 b(4,2) = d*( a(0,0)*w[16]-a(0,1)*w[13]+a(0,2)*w[11]-a(0,3)*w[10]);
3719 w[ 0] = a(0,0)*a(1,1)-a(0,1)*a(1,0);
3720 w[ 1] = a(0,0)*a(1,2)-a(0,2)*a(1,0);
3721 w[ 2] = a(0,0)*a(1,3)-a(0,3)*a(1,0);
3722 w[ 3] = a(0,0)*a(1,4)-a(0,4)*a(1,0);
3723 w[ 4] = a(0,1)*a(1,2)-a(0,2)*a(1,1);
3724 w[ 5] = a(0,1)*a(1,3)-a(0,3)*a(1,1);
3725 w[ 6] = a(0,1)*a(1,4)-a(0,4)*a(1,1);
3726 w[ 7] = a(0,2)*a(1,3)-a(0,3)*a(1,2);
3727 w[ 8] = a(0,2)*a(1,4)-a(0,4)*a(1,2);
3728 w[ 9] = a(0,3)*a(1,4)-a(0,4)*a(1,3);
3729 w[10] = a(2,0)*w[4]-a(2,1)*w[1]+a(2,2)*w[0];
3730 w[11] = a(2,0)*w[5]-a(2,1)*w[2]+a(2,3)*w[0];
3731 w[12] = a(2,0)*w[6]-a(2,1)*w[3]+a(2,4)*w[0];
3732 w[13] = a(2,0)*w[7]-a(2,2)*w[2]+a(2,3)*w[1];
3733 w[14] = a(2,0)*w[8]-a(2,2)*w[3]+a(2,4)*w[1];
3734 w[15] = a(2,0)*w[9]-a(2,3)*w[3]+a(2,4)*w[2];
3735 w[16] = a(2,1)*w[7]-a(2,2)*w[5]+a(2,3)*w[4];
3736 w[17] = a(2,1)*w[8]-a(2,2)*w[6]+a(2,4)*w[4];
3737 w[18] = a(2,1)*w[9]-a(2,3)*w[6]+a(2,4)*w[5];
3738 w[19] = a(2,2)*w[9]-a(2,3)*w[8]+a(2,4)*w[7];
3739 b(0,3) = d*( a(4,1)*w[19]-a(4,2)*w[18]+a(4,3)*w[17]-a(4,4)*w[16]);
3740 b(1,3) = d*(-a(4,0)*w[19]+a(4,2)*w[15]-a(4,3)*w[14]+a(4,4)*w[13]);
3741 b(2,3) = d*( a(4,0)*w[18]-a(4,1)*w[15]+a(4,3)*w[12]-a(4,4)*w[11]);
3742 b(3,3) = d*(-a(4,0)*w[17]+a(4,1)*w[14]-a(4,2)*w[12]+a(4,4)*w[10]);
3743 b(4,3) = d*( a(4,0)*w[16]-a(4,1)*w[13]+a(4,2)*w[11]-a(4,3)*w[10]);
3744 b(0,4) = d*(-a(3,1)*w[19]+a(3,2)*w[18]-a(3,3)*w[17]+a(3,4)*w[16]);
3745 b(1,4) = d*( a(3,0)*w[19]-a(3,2)*w[15]+a(3,3)*w[14]-a(3,4)*w[13]);
3746 b(2,4) = d*(-a(3,0)*w[18]+a(3,1)*w[15]-a(3,3)*w[12]+a(3,4)*w[11]);
3747 b(3,4) = d*( a(3,0)*w[17]-a(3,1)*w[14]+a(3,2)*w[12]-a(3,4)*w[10]);
3748 b(4,4) = d*(-a(3,0)*w[16]+a(3,1)*w[13]-a(3,2)*w[11]+a(3,3)*w[10]);
3749 return det;
3750 }
3751 };
3752
3753 template<>
3754 struct InverseHelper<6,6>
3755 {
3756 template<typename T_, int smb_, int snb_, int sma_, int sna_>
3757 CUDA_HOST_DEVICE static T_ compute(Tiny::Matrix<T_, 6, 6, smb_, snb_>& b, const Tiny::Matrix<T_, 6, 6, sma_, sna_>& a)
3758 {
3759 T_ w[35];
3760 w[ 0] = a(4,0)*a(5,1)-a(4,1)*a(5,0);
3761 w[ 1] = a(4,0)*a(5,2)-a(4,2)*a(5,0);
3762 w[ 2] = a(4,0)*a(5,3)-a(4,3)*a(5,0);
3763 w[ 3] = a(4,0)*a(5,4)-a(4,4)*a(5,0);
3764 w[ 4] = a(4,0)*a(5,5)-a(4,5)*a(5,0);
3765 w[ 5] = a(4,1)*a(5,2)-a(4,2)*a(5,1);
3766 w[ 6] = a(4,1)*a(5,3)-a(4,3)*a(5,1);
3767 w[ 7] = a(4,1)*a(5,4)-a(4,4)*a(5,1);
3768 w[ 8] = a(4,1)*a(5,5)-a(4,5)*a(5,1);
3769 w[ 9] = a(4,2)*a(5,3)-a(4,3)*a(5,2);
3770 w[10] = a(4,2)*a(5,4)-a(4,4)*a(5,2);
3771 w[11] = a(4,2)*a(5,5)-a(4,5)*a(5,2);
3772 w[12] = a(4,3)*a(5,4)-a(4,4)*a(5,3);
3773 w[13] = a(4,3)*a(5,5)-a(4,5)*a(5,3);
3774 w[14] = a(4,4)*a(5,5)-a(4,5)*a(5,4);
3775 w[15] = a(3,0)*w[5]-a(3,1)*w[1]+a(3,2)*w[0];
3776 w[16] = a(3,0)*w[6]-a(3,1)*w[2]+a(3,3)*w[0];
3777 w[17] = a(3,0)*w[7]-a(3,1)*w[3]+a(3,4)*w[0];
3778 w[18] = a(3,0)*w[8]-a(3,1)*w[4]+a(3,5)*w[0];
3779 w[19] = a(3,0)*w[9]-a(3,2)*w[2]+a(3,3)*w[1];
3780 w[20] = a(3,0)*w[10]-a(3,2)*w[3]+a(3,4)*w[1];
3781 w[21] = a(3,0)*w[11]-a(3,2)*w[4]+a(3,5)*w[1];
3782 w[22] = a(3,0)*w[12]-a(3,3)*w[3]+a(3,4)*w[2];
3783 w[23] = a(3,0)*w[13]-a(3,3)*w[4]+a(3,5)*w[2];
3784 w[24] = a(3,0)*w[14]-a(3,4)*w[4]+a(3,5)*w[3];
3785 w[25] = a(3,1)*w[9]-a(3,2)*w[6]+a(3,3)*w[5];
3786 w[26] = a(3,1)*w[10]-a(3,2)*w[7]+a(3,4)*w[5];
3787 w[27] = a(3,1)*w[11]-a(3,2)*w[8]+a(3,5)*w[5];
3788 w[28] = a(3,1)*w[12]-a(3,3)*w[7]+a(3,4)*w[6];
3789 w[29] = a(3,1)*w[13]-a(3,3)*w[8]+a(3,5)*w[6];
3790 w[30] = a(3,1)*w[14]-a(3,4)*w[8]+a(3,5)*w[7];
3791 w[31] = a(3,2)*w[12]-a(3,3)*w[10]+a(3,4)*w[9];
3792 w[32] = a(3,2)*w[13]-a(3,3)*w[11]+a(3,5)*w[9];
3793 w[33] = a(3,2)*w[14]-a(3,4)*w[11]+a(3,5)*w[10];
3794 w[34] = a(3,3)*w[14]-a(3,4)*w[13]+a(3,5)*w[12];
3795 w[ 0] = a(2,0)*w[25]-a(2,1)*w[19]+a(2,2)*w[16]-a(2,3)*w[15];
3796 w[ 1] = a(2,0)*w[26]-a(2,1)*w[20]+a(2,2)*w[17]-a(2,4)*w[15];
3797 w[ 2] = a(2,0)*w[27]-a(2,1)*w[21]+a(2,2)*w[18]-a(2,5)*w[15];
3798 w[ 3] = a(2,0)*w[28]-a(2,1)*w[22]+a(2,3)*w[17]-a(2,4)*w[16];
3799 w[ 4] = a(2,0)*w[29]-a(2,1)*w[23]+a(2,3)*w[18]-a(2,5)*w[16];
3800 w[ 5] = a(2,0)*w[30]-a(2,1)*w[24]+a(2,4)*w[18]-a(2,5)*w[17];
3801 w[ 6] = a(2,0)*w[31]-a(2,2)*w[22]+a(2,3)*w[20]-a(2,4)*w[19];
3802 w[ 7] = a(2,0)*w[32]-a(2,2)*w[23]+a(2,3)*w[21]-a(2,5)*w[19];
3803 w[ 8] = a(2,0)*w[33]-a(2,2)*w[24]+a(2,4)*w[21]-a(2,5)*w[20];
3804 w[ 9] = a(2,0)*w[34]-a(2,3)*w[24]+a(2,4)*w[23]-a(2,5)*w[22];
3805 w[10] = a(2,1)*w[31]-a(2,2)*w[28]+a(2,3)*w[26]-a(2,4)*w[25];
3806 w[11] = a(2,1)*w[32]-a(2,2)*w[29]+a(2,3)*w[27]-a(2,5)*w[25];
3807 w[12] = a(2,1)*w[33]-a(2,2)*w[30]+a(2,4)*w[27]-a(2,5)*w[26];
3808 w[13] = a(2,1)*w[34]-a(2,3)*w[30]+a(2,4)*w[29]-a(2,5)*w[28];
3809 w[14] = a(2,2)*w[34]-a(2,3)*w[33]+a(2,4)*w[32]-a(2,5)*w[31];
3810 b(0,0) = a(1,1)*w[14]-a(1,2)*w[13]+a(1,3)*w[12]-a(1,4)*w[11]+a(1,5)*w[10];
3811 b(1,0) = -a(1,0)*w[14]+a(1,2)*w[9]-a(1,3)*w[8]+a(1,4)*w[7]-a(1,5)*w[6];
3812 b(2,0) = a(1,0)*w[13]-a(1,1)*w[9]+a(1,3)*w[5]-a(1,4)*w[4]+a(1,5)*w[3];
3813 b(3,0) = -a(1,0)*w[12]+a(1,1)*w[8]-a(1,2)*w[5]+a(1,4)*w[2]-a(1,5)*w[1];
3814 b(4,0) = a(1,0)*w[11]-a(1,1)*w[7]+a(1,2)*w[4]-a(1,3)*w[2]+a(1,5)*w[0];
3815 b(5,0) = -a(1,0)*w[10]+a(1,1)*w[6]-a(1,2)*w[3]+a(1,3)*w[1]-a(1,4)*w[0];
3816 T_ det = a(0,0)*b(0,0) + a(0,1)*b(1,0) + a(0,2)*b(2,0)
3817 + a(0,3)*b(3,0) + a(0,4)*b(4,0) + a(0,5)*b(5,0);
3818 T_ d = T_(1) / det;
3819 b(0,0) *= d;
3820 b(1,0) *= d;
3821 b(2,0) *= d;
3822 b(3,0) *= d;
3823 b(4,0) *= d;
3824 b(5,0) *= d;
3825 b(0,1) = d*(-a(0,1)*w[14]+a(0,2)*w[13]-a(0,3)*w[12]+a(0,4)*w[11]-a(0,5)*w[10]);
3826 b(1,1) = d*( a(0,0)*w[14]-a(0,2)*w[9]+a(0,3)*w[8]-a(0,4)*w[7]+a(0,5)*w[6]);
3827 b(2,1) = d*(-a(0,0)*w[13]+a(0,1)*w[9]-a(0,3)*w[5]+a(0,4)*w[4]-a(0,5)*w[3]);
3828 b(3,1) = d*( a(0,0)*w[12]-a(0,1)*w[8]+a(0,2)*w[5]-a(0,4)*w[2]+a(0,5)*w[1]);
3829 b(4,1) = d*(-a(0,0)*w[11]+a(0,1)*w[7]-a(0,2)*w[4]+a(0,3)*w[2]-a(0,5)*w[0]);
3830 b(5,1) = d*( a(0,0)*w[10]-a(0,1)*w[6]+a(0,2)*w[3]-a(0,3)*w[1]+a(0,4)*w[0]);
3831 w[ 0] = a(4,0)*a(5,1)-a(4,1)*a(5,0);
3832 w[ 1] = a(4,0)*a(5,2)-a(4,2)*a(5,0);
3833 w[ 2] = a(4,0)*a(5,3)-a(4,3)*a(5,0);
3834 w[ 3] = a(4,0)*a(5,4)-a(4,4)*a(5,0);
3835 w[ 4] = a(4,0)*a(5,5)-a(4,5)*a(5,0);
3836 w[ 5] = a(4,1)*a(5,2)-a(4,2)*a(5,1);
3837 w[ 6] = a(4,1)*a(5,3)-a(4,3)*a(5,1);
3838 w[ 7] = a(4,1)*a(5,4)-a(4,4)*a(5,1);
3839 w[ 8] = a(4,1)*a(5,5)-a(4,5)*a(5,1);
3840 w[ 9] = a(4,2)*a(5,3)-a(4,3)*a(5,2);
3841 w[10] = a(4,2)*a(5,4)-a(4,4)*a(5,2);
3842 w[11] = a(4,2)*a(5,5)-a(4,5)*a(5,2);
3843 w[12] = a(4,3)*a(5,4)-a(4,4)*a(5,3);
3844 w[13] = a(4,3)*a(5,5)-a(4,5)*a(5,3);
3845 w[14] = a(4,4)*a(5,5)-a(4,5)*a(5,4);
3846 w[15] = a(1,0)*w[5]-a(1,1)*w[1]+a(1,2)*w[0];
3847 w[16] = a(1,0)*w[6]-a(1,1)*w[2]+a(1,3)*w[0];
3848 w[17] = a(1,0)*w[7]-a(1,1)*w[3]+a(1,4)*w[0];
3849 w[18] = a(1,0)*w[8]-a(1,1)*w[4]+a(1,5)*w[0];
3850 w[19] = a(1,0)*w[9]-a(1,2)*w[2]+a(1,3)*w[1];
3851 w[20] = a(1,0)*w[10]-a(1,2)*w[3]+a(1,4)*w[1];
3852 w[21] = a(1,0)*w[11]-a(1,2)*w[4]+a(1,5)*w[1];
3853 w[22] = a(1,0)*w[12]-a(1,3)*w[3]+a(1,4)*w[2];
3854 w[23] = a(1,0)*w[13]-a(1,3)*w[4]+a(1,5)*w[2];
3855 w[24] = a(1,0)*w[14]-a(1,4)*w[4]+a(1,5)*w[3];
3856 w[25] = a(1,1)*w[9]-a(1,2)*w[6]+a(1,3)*w[5];
3857 w[26] = a(1,1)*w[10]-a(1,2)*w[7]+a(1,4)*w[5];
3858 w[27] = a(1,1)*w[11]-a(1,2)*w[8]+a(1,5)*w[5];
3859 w[28] = a(1,1)*w[12]-a(1,3)*w[7]+a(1,4)*w[6];
3860 w[29] = a(1,1)*w[13]-a(1,3)*w[8]+a(1,5)*w[6];
3861 w[30] = a(1,1)*w[14]-a(1,4)*w[8]+a(1,5)*w[7];
3862 w[31] = a(1,2)*w[12]-a(1,3)*w[10]+a(1,4)*w[9];
3863 w[32] = a(1,2)*w[13]-a(1,3)*w[11]+a(1,5)*w[9];
3864 w[33] = a(1,2)*w[14]-a(1,4)*w[11]+a(1,5)*w[10];
3865 w[34] = a(1,3)*w[14]-a(1,4)*w[13]+a(1,5)*w[12];
3866 w[ 0] = a(0,0)*w[25]-a(0,1)*w[19]+a(0,2)*w[16]-a(0,3)*w[15];
3867 w[ 1] = a(0,0)*w[26]-a(0,1)*w[20]+a(0,2)*w[17]-a(0,4)*w[15];
3868 w[ 2] = a(0,0)*w[27]-a(0,1)*w[21]+a(0,2)*w[18]-a(0,5)*w[15];
3869 w[ 3] = a(0,0)*w[28]-a(0,1)*w[22]+a(0,3)*w[17]-a(0,4)*w[16];
3870 w[ 4] = a(0,0)*w[29]-a(0,1)*w[23]+a(0,3)*w[18]-a(0,5)*w[16];
3871 w[ 5] = a(0,0)*w[30]-a(0,1)*w[24]+a(0,4)*w[18]-a(0,5)*w[17];
3872 w[ 6] = a(0,0)*w[31]-a(0,2)*w[22]+a(0,3)*w[20]-a(0,4)*w[19];
3873 w[ 7] = a(0,0)*w[32]-a(0,2)*w[23]+a(0,3)*w[21]-a(0,5)*w[19];
3874 w[ 8] = a(0,0)*w[33]-a(0,2)*w[24]+a(0,4)*w[21]-a(0,5)*w[20];
3875 w[ 9] = a(0,0)*w[34]-a(0,3)*w[24]+a(0,4)*w[23]-a(0,5)*w[22];
3876 w[10] = a(0,1)*w[31]-a(0,2)*w[28]+a(0,3)*w[26]-a(0,4)*w[25];
3877 w[11] = a(0,1)*w[32]-a(0,2)*w[29]+a(0,3)*w[27]-a(0,5)*w[25];
3878 w[12] = a(0,1)*w[33]-a(0,2)*w[30]+a(0,4)*w[27]-a(0,5)*w[26];
3879 w[13] = a(0,1)*w[34]-a(0,3)*w[30]+a(0,4)*w[29]-a(0,5)*w[28];
3880 w[14] = a(0,2)*w[34]-a(0,3)*w[33]+a(0,4)*w[32]-a(0,5)*w[31];
3881 b(0,2) = d*( a(3,1)*w[14]-a(3,2)*w[13]+a(3,3)*w[12]-a(3,4)*w[11]+a(3,5)*w[10]);
3882 b(1,2) = d*(-a(3,0)*w[14]+a(3,2)*w[9]-a(3,3)*w[8]+a(3,4)*w[7]-a(3,5)*w[6]);
3883 b(2,2) = d*( a(3,0)*w[13]-a(3,1)*w[9]+a(3,3)*w[5]-a(3,4)*w[4]+a(3,5)*w[3]);
3884 b(3,2) = d*(-a(3,0)*w[12]+a(3,1)*w[8]-a(3,2)*w[5]+a(3,4)*w[2]-a(3,5)*w[1]);
3885 b(4,2) = d*( a(3,0)*w[11]-a(3,1)*w[7]+a(3,2)*w[4]-a(3,3)*w[2]+a(3,5)*w[0]);
3886 b(5,2) = d*(-a(3,0)*w[10]+a(3,1)*w[6]-a(3,2)*w[3]+a(3,3)*w[1]-a(3,4)*w[0]);
3887 b(0,3) = d*(-a(2,1)*w[14]+a(2,2)*w[13]-a(2,3)*w[12]+a(2,4)*w[11]-a(2,5)*w[10]);
3888 b(1,3) = d*( a(2,0)*w[14]-a(2,2)*w[9]+a(2,3)*w[8]-a(2,4)*w[7]+a(2,5)*w[6]);
3889 b(2,3) = d*(-a(2,0)*w[13]+a(2,1)*w[9]-a(2,3)*w[5]+a(2,4)*w[4]-a(2,5)*w[3]);
3890 b(3,3) = d*( a(2,0)*w[12]-a(2,1)*w[8]+a(2,2)*w[5]-a(2,4)*w[2]+a(2,5)*w[1]);
3891 b(4,3) = d*(-a(2,0)*w[11]+a(2,1)*w[7]-a(2,2)*w[4]+a(2,3)*w[2]-a(2,5)*w[0]);
3892 b(5,3) = d*( a(2,0)*w[10]-a(2,1)*w[6]+a(2,2)*w[3]-a(2,3)*w[1]+a(2,4)*w[0]);
3893 w[ 0] = a(2,0)*a(3,1)-a(2,1)*a(3,0);
3894 w[ 1] = a(2,0)*a(3,2)-a(2,2)*a(3,0);
3895 w[ 2] = a(2,0)*a(3,3)-a(2,3)*a(3,0);
3896 w[ 3] = a(2,0)*a(3,4)-a(2,4)*a(3,0);
3897 w[ 4] = a(2,0)*a(3,5)-a(2,5)*a(3,0);
3898 w[ 5] = a(2,1)*a(3,2)-a(2,2)*a(3,1);
3899 w[ 6] = a(2,1)*a(3,3)-a(2,3)*a(3,1);
3900 w[ 7] = a(2,1)*a(3,4)-a(2,4)*a(3,1);
3901 w[ 8] = a(2,1)*a(3,5)-a(2,5)*a(3,1);
3902 w[ 9] = a(2,2)*a(3,3)-a(2,3)*a(3,2);
3903 w[10] = a(2,2)*a(3,4)-a(2,4)*a(3,2);
3904 w[11] = a(2,2)*a(3,5)-a(2,5)*a(3,2);
3905 w[12] = a(2,3)*a(3,4)-a(2,4)*a(3,3);
3906 w[13] = a(2,3)*a(3,5)-a(2,5)*a(3,3);
3907 w[14] = a(2,4)*a(3,5)-a(2,5)*a(3,4);
3908 w[15] = a(1,0)*w[5]-a(1,1)*w[1]+a(1,2)*w[0];
3909 w[16] = a(1,0)*w[6]-a(1,1)*w[2]+a(1,3)*w[0];
3910 w[17] = a(1,0)*w[7]-a(1,1)*w[3]+a(1,4)*w[0];
3911 w[18] = a(1,0)*w[8]-a(1,1)*w[4]+a(1,5)*w[0];
3912 w[19] = a(1,0)*w[9]-a(1,2)*w[2]+a(1,3)*w[1];
3913 w[20] = a(1,0)*w[10]-a(1,2)*w[3]+a(1,4)*w[1];
3914 w[21] = a(1,0)*w[11]-a(1,2)*w[4]+a(1,5)*w[1];
3915 w[22] = a(1,0)*w[12]-a(1,3)*w[3]+a(1,4)*w[2];
3916 w[23] = a(1,0)*w[13]-a(1,3)*w[4]+a(1,5)*w[2];
3917 w[24] = a(1,0)*w[14]-a(1,4)*w[4]+a(1,5)*w[3];
3918 w[25] = a(1,1)*w[9]-a(1,2)*w[6]+a(1,3)*w[5];
3919 w[26] = a(1,1)*w[10]-a(1,2)*w[7]+a(1,4)*w[5];
3920 w[27] = a(1,1)*w[11]-a(1,2)*w[8]+a(1,5)*w[5];
3921 w[28] = a(1,1)*w[12]-a(1,3)*w[7]+a(1,4)*w[6];
3922 w[29] = a(1,1)*w[13]-a(1,3)*w[8]+a(1,5)*w[6];
3923 w[30] = a(1,1)*w[14]-a(1,4)*w[8]+a(1,5)*w[7];
3924 w[31] = a(1,2)*w[12]-a(1,3)*w[10]+a(1,4)*w[9];
3925 w[32] = a(1,2)*w[13]-a(1,3)*w[11]+a(1,5)*w[9];
3926 w[33] = a(1,2)*w[14]-a(1,4)*w[11]+a(1,5)*w[10];
3927 w[34] = a(1,3)*w[14]-a(1,4)*w[13]+a(1,5)*w[12];
3928 w[ 0] = a(0,0)*w[25]-a(0,1)*w[19]+a(0,2)*w[16]-a(0,3)*w[15];
3929 w[ 1] = a(0,0)*w[26]-a(0,1)*w[20]+a(0,2)*w[17]-a(0,4)*w[15];
3930 w[ 2] = a(0,0)*w[27]-a(0,1)*w[21]+a(0,2)*w[18]-a(0,5)*w[15];
3931 w[ 3] = a(0,0)*w[28]-a(0,1)*w[22]+a(0,3)*w[17]-a(0,4)*w[16];
3932 w[ 4] = a(0,0)*w[29]-a(0,1)*w[23]+a(0,3)*w[18]-a(0,5)*w[16];
3933 w[ 5] = a(0,0)*w[30]-a(0,1)*w[24]+a(0,4)*w[18]-a(0,5)*w[17];
3934 w[ 6] = a(0,0)*w[31]-a(0,2)*w[22]+a(0,3)*w[20]-a(0,4)*w[19];
3935 w[ 7] = a(0,0)*w[32]-a(0,2)*w[23]+a(0,3)*w[21]-a(0,5)*w[19];
3936 w[ 8] = a(0,0)*w[33]-a(0,2)*w[24]+a(0,4)*w[21]-a(0,5)*w[20];
3937 w[ 9] = a(0,0)*w[34]-a(0,3)*w[24]+a(0,4)*w[23]-a(0,5)*w[22];
3938 w[10] = a(0,1)*w[31]-a(0,2)*w[28]+a(0,3)*w[26]-a(0,4)*w[25];
3939 w[11] = a(0,1)*w[32]-a(0,2)*w[29]+a(0,3)*w[27]-a(0,5)*w[25];
3940 w[12] = a(0,1)*w[33]-a(0,2)*w[30]+a(0,4)*w[27]-a(0,5)*w[26];
3941 w[13] = a(0,1)*w[34]-a(0,3)*w[30]+a(0,4)*w[29]-a(0,5)*w[28];
3942 w[14] = a(0,2)*w[34]-a(0,3)*w[33]+a(0,4)*w[32]-a(0,5)*w[31];
3943 b(0,4) = d*( a(5,1)*w[14]-a(5,2)*w[13]+a(5,3)*w[12]-a(5,4)*w[11]+a(5,5)*w[10]);
3944 b(1,4) = d*(-a(5,0)*w[14]+a(5,2)*w[9]-a(5,3)*w[8]+a(5,4)*w[7]-a(5,5)*w[6]);
3945 b(2,4) = d*( a(5,0)*w[13]-a(5,1)*w[9]+a(5,3)*w[5]-a(5,4)*w[4]+a(5,5)*w[3]);
3946 b(3,4) = d*(-a(5,0)*w[12]+a(5,1)*w[8]-a(5,2)*w[5]+a(5,4)*w[2]-a(5,5)*w[1]);
3947 b(4,4) = d*( a(5,0)*w[11]-a(5,1)*w[7]+a(5,2)*w[4]-a(5,3)*w[2]+a(5,5)*w[0]);
3948 b(5,4) = d*(-a(5,0)*w[10]+a(5,1)*w[6]-a(5,2)*w[3]+a(5,3)*w[1]-a(5,4)*w[0]);
3949 b(0,5) = d*(-a(4,1)*w[14]+a(4,2)*w[13]-a(4,3)*w[12]+a(4,4)*w[11]-a(4,5)*w[10]);
3950 b(1,5) = d*( a(4,0)*w[14]-a(4,2)*w[9]+a(4,3)*w[8]-a(4,4)*w[7]+a(4,5)*w[6]);
3951 b(2,5) = d*(-a(4,0)*w[13]+a(4,1)*w[9]-a(4,3)*w[5]+a(4,4)*w[4]-a(4,5)*w[3]);
3952 b(3,5) = d*( a(4,0)*w[12]-a(4,1)*w[8]+a(4,2)*w[5]-a(4,4)*w[2]+a(4,5)*w[1]);
3953 b(4,5) = d*(-a(4,0)*w[11]+a(4,1)*w[7]-a(4,2)*w[4]+a(4,3)*w[2]-a(4,5)*w[0]);
3954 b(5,5) = d*( a(4,0)*w[10]-a(4,1)*w[6]+a(4,2)*w[3]-a(4,3)*w[1]+a(4,4)*w[0]);
3955 return det;
3956 }
3957 };
3958
3959 // generic square matrix inversion:
3960 template<int n_>
3961 struct InverseHelper<n_, n_>
3962 {
3963 template<typename T_, int smb_, int snb_, int sma_, int sna_>
3964 CUDA_HOST_DEVICE static T_ compute(Tiny::Matrix<T_, n_, n_, smb_, snb_>& b, const Tiny::Matrix<T_, n_, n_, sma_, sna_>& a)
3965 {
3966 // copy matrix a to b
3967 for (int i(0); i < n_; ++i)
3968 {
3969 for (int j(0); j < n_; ++j)
3970 {
3971 b(i,j) = a(i,j);
3972 }
3973 }
3974
3975 // create pivot array
3976 int p[n_];
3977
3978 // initialize identity permutation
3979 for(int i(0); i < n_; ++i)
3980 {
3981 p[i] = i;
3982 }
3983
3984 // initialize determinant to 1
3985 T_ det = T_(1);
3986
3987 // primary column elimination loop
3988 for(int k(0); k < n_; ++k)
3989 {
3990 // step 1: find a pivot for the elimination of column k
3991 {
3992 // for this, we only check the rows p[j] with j >= k, as all
3993 // rows p[j] with j < k have already been eliminated and are
3994 // therefore not candidates for pivoting
3995 #ifdef __CUDACC__
3996 T_ pivot = CudaMath::cuda_abs(b(p[k], p[k]));
3997 #else
3998 T_ pivot = Math::abs(b(p[k], p[k]));
3999 #endif
4000 int i = k;
4001
4002 // loop over all unprocessed rows
4003 for(int j(k+1); j < n_; ++j)
4004 {
4005 // get our matrix value and check whether it can be a pivot
4006 #ifdef __CUDACC__
4007 T_ abs_val = CudaMath::cuda_abs(b(p[j], p[j]));
4008 #else
4009 T_ abs_val = Math::abs(b(p[j], p[j]));
4010 #endif
4011 if(abs_val > pivot)
4012 {
4013 pivot = abs_val;
4014 i = j;
4015 }
4016 }
4017
4018 // do we have to swap rows i and k?
4019 if(i > k)
4020 {
4021 // swap rows "virtually" by exchanging their permutation positions
4022 int t = p[k];
4023 p[k] = p[i];
4024 p[i] = t;
4025 }
4026 }
4027
4028 // step 2: process pivot row
4029 {
4030 // update determinant by multiplying with the pivot element
4031 det *= b(p[k], p[k]);
4032
4033 // get our inverted pivot element
4034 const T_ pivot = T_(1) / b(p[k], p[k]);
4035
4036 // replace column entry by unit column entry
4037 b(p[k], p[k]) = T_(1);
4038
4039 // divide the whole row by the inverted pivot
4040 for(int j(0); j < n_; ++j)
4041 {
4042 b(p[k], j) *= pivot;
4043 }
4044 }
4045
4046 // step 3: eliminate pivot column
4047
4048 // loop over all rows of the matrix
4049 for(int i(0); i < n_; ++i)
4050 {
4051 // skip the pivot row
4052 if(i == p[k])
4053 continue;
4054
4055 // fetch elimination factor
4056 const T_ factor = b(i, p[k]);
4057
4058 // replace by unit column entry
4059 b(i, p[k]) = T_(0);
4060
4061 // process the row
4062 for(int j(0); j < n_; ++j)
4063 {
4064 b(i, j) -= b(p[k], j) * factor;
4065 }
4066 }
4067 }
4068
4069 // return determinant
4070 return det;
4071 }
4072 };
4073
4079
4080 #ifdef __CUDACC__
4081 template<>
4082 struct CudaGroupedInverseHelper<1,1>
4083 {
4084 template<typename T_, typename ThreadGroup_, int smb_, int snb_, int sma_, int sna_>
4085 CUDA_DEVICE static __forceinline__ void compute(const ThreadGroup_& tg, Tiny::Matrix<T_, 1, 1, smb_, snb_>& b, const Tiny::Matrix<T_, 1, 1, sma_, sna_>&, const T_& det)
4086 {
4087 if(tg.thread_rank() == 0)
4088 b(0,0) = T_(1) / det;
4089 tg.sync();
4090 }
4091 };
4092
4093 template<>
4094 struct CudaGroupedInverseHelper<2,2>
4095 {
4096 template<typename T_, typename ThreadGroup_, int smb_, int snb_, int sma_, int sna_>
4097 CUDA_DEVICE static __forceinline__ void compute(const ThreadGroup_& tg, Tiny::Matrix<T_, 2, 2, smb_, snb_>& b, const Tiny::Matrix<T_, 2, 2, sma_, sna_>& a, const T_& det)
4098 {
4099 //i think an if else cascade could do better than heavy modulo operations...
4100 T_ d = T_(1) / det;
4101
4102 for(int idx = tg.thread_rank(); idx < 4; idx += tg.num_threads())
4103 {
4104 const int i = idx /2;
4105 const int j = idx %2;
4106 b(i,j) = ((i+j)==1 ? T_(-1) : T_(1)) * d * a(1-j,1-i);
4107
4108 }
4109 }
4110 };
4111
4112 template<>
4113 struct CudaGroupedInverseHelper<3,3>
4114 {
4115 template<typename T_, typename ThreadGroup_, int smb_, int snb_, int sma_, int sna_>
4116 CUDA_DEVICE static __forceinline__ void compute(const ThreadGroup_& tg, Tiny::Matrix<T_, 3, 3, smb_, snb_>& b, const Tiny::Matrix<T_, 3, 3, sma_, sna_>& a, const T_& det)
4117 {
4118 // for(int i = tg.thread_rank(); i < 3; i += tg.num_threads())
4119 // {
4120 // b[i*snb_+0] = a[1*sna_+1+i-3*(i/2)]*a[2*sna_+(2*(1-i+i/2))] - a[1*sna_+(2*(1-i+i/2))]*a[2*sna_+1+i-3*(i/2)];
4121 // }
4122 // tg.sync();
4123 // T_ det = a[0*sna_+0]*b[0*snb_+0] + a[0*sna_+1]*b[1*snb_+0] + a[0*sna_+2]*b[2*snb_+0];
4124 // T_ d = T_(1) / det;
4125 // for(int idx = tg.thread_rank(); idx < 3; idx += tg.num_threads())
4126 // {
4127 // const int i = idx/3;
4128 // const int j = idx%3;
4129 // b[i*snb_+j] = d*(a[()*sna_+1]*a[2*sna_+2] - a[1*sna_+2]*a[2*sna_+1])
4130
4131 // }
4132 //i think an if else cascade could do better than heavy modulo operations...
4133 T_ d = T_(1) / det;
4134
4135 for(int idx = tg.thread_rank(); idx < 9; idx += tg.num_threads())
4136 {
4137 if(idx == 0)
4138 b(0,0) = d*(a(1,1)*a(2,2) - a(1,2)*a(2,1));
4139 else if(idx == 3)
4140 b(1,0) = d*(a(1,2)*a(2,0) - a(1,0)*a(2,2));
4141 else if(idx == 6)
4142 b(2,0) = d*(a(1,0)*a(2,1) - a(1,1)*a(2,0));
4143 else if(idx == 1)
4144 b(0,1) = d*(a(0,2)*a(2,1) - a(0,1)*a(2,2));
4145 else if(idx == 4)
4146 b(1,1) = d*(a(0,0)*a(2,2) - a(0,2)*a(2,0));
4147 else if(idx == 7)
4148 b(2,1) = d*(a(0,1)*a(2,0) - a(0,0)*a(2,1));
4149 else if(idx == 2)
4150 b(0,2) = d*(a(0,1)*a(1,2) - a(0,2)*a(1,1));
4151 else if(idx == 5)
4152 b(1,2) = d*(a(0,2)*a(1,0) - a(0,0)*a(1,2));
4153 else if(idx == 8)
4154 b(2,2) = d*(a(0,0)*a(1,1) - a(0,1)*a(1,0));
4155 }
4156 }
4157 };
4158
4159 // generic square matrix inversion:
4160 template<int n_>
4161 struct CudaGroupedInverseHelper<n_, n_>
4162 {
4163 template<typename T_, typename ThreadGroup_, int smb_, int snb_, int sma_, int sna_>
4164 CUDA_DEVICE static __forceinline__ void compute(const ThreadGroup_& tg, Tiny::Matrix<T_, n_, n_, smb_, snb_>& b, const Tiny::Matrix<T_, n_, n_, sma_, sna_>& a, const T_&)
4165 {
4166 // copy matrix a to b
4167 for (int i = tg.thread_rank(); i < n_*n_; i += tg.num_threads())
4168 {
4169 const int row = i/n_;
4170 const int col = i%n_;
4171 b.v[row][col] = a.v[row][col];
4172 }
4173
4174 // create shared pivot array
4175 __shared__ int p[n_];
4176 for (int i = tg.thread_rank(); i < n_; i += tg.num_threads())
4177 {
4178 p[i] = 0;
4179 }
4180 tg.sync();
4181 // call grouped invert from first warp subgroup
4182 if(tg.thread_rank() < 32)
4183 {
4184 auto a_g = cg::coalesced_threads();
4185
4186 CudaMath::cuda_grouped_invert_matrix(a_g, n_, snb_, &b.v[0][0], p);
4187 }
4188 tg.sync();
4189 }
4190 };
4191 #endif // __CUDACC__
4192
4198
4199 template<int m_, int n_>
4200 struct CofactorHelper
4201 {
4202 template<typename T_, int smb_, int snb_, int sma_, int sna_>
4203 CUDA_HOST_DEVICE static void compute(Tiny::Matrix<T_, m_, n_, smb_, snb_>& b, const Tiny::Matrix<T_, m_, n_, sma_, sna_>& a)
4204 {
4205 static_assert(m_ == n_, "cofactor computation is only available for square matrices!");
4206
4207 // compute inverse
4208 const T_ det = Intern::InverseHelper<m_,n_>::compute(b, a);
4209
4210 for(int i(0); i < m_; ++i)
4211 {
4212 for(int j(0); j <= i; ++j)
4213 {
4214 b(i,j) *= det;
4215 }
4216 for(int j(i+1); j < n_; ++j)
4217 {
4218 std::swap(b(i,j), b(j,i));
4219 b(i,j) *= det;
4220 }
4221 }
4222 }
4223 };
4224
4225 template<>
4226 struct CofactorHelper<1,1>
4227 {
4228 template<typename T_, int smb_, int snb_, int sma_, int sna_>
4229 CUDA_HOST_DEVICE static void compute(Tiny::Matrix<T_, 1, 1, smb_, snb_>& b, const Tiny::Matrix<T_, 1, 1, sma_, sna_>&)
4230 {
4231 b[0] = T_(1);
4232 }
4233 };
4234
4235 template<>
4236 struct CofactorHelper<2,2>
4237 {
4238 template<typename T_, int smb_, int snb_, int sma_, int sna_>
4239 CUDA_HOST_DEVICE static void compute(Tiny::Matrix<T_, 2, 2, smb_, snb_>& b, const Tiny::Matrix<T_, 2, 2, sma_, sna_>& a)
4240 {
4241 b(0,0) = a(1,1);
4242 b(0,1) = -a(0,1);
4243 b(1,0) = -a(1,0);
4244 b(1,1) = a(0,0);
4245 }
4246 };
4247
4248 template<>
4249 struct CofactorHelper<3,3>
4250 {
4251 template<typename T_, int smb_, int snb_, int sma_, int sna_>
4252 CUDA_HOST_DEVICE static void compute(Tiny::Matrix<T_, 3, 3, smb_, snb_>& b, const Tiny::Matrix<T_, 3, 3, sma_, sna_>& a)
4253 {
4254 b(0,0) = a(1,1)*a(2,2) - a(1,2)*a(2,1);
4255 b(1,0) = a(1,2)*a(2,0) - a(1,0)*a(2,2);
4256 b(2,0) = a(1,0)*a(2,1) - a(1,1)*a(2,0);
4257 b(0,1) = a(0,2)*a(2,1) - a(0,1)*a(2,2);
4258 b(1,1) = a(0,0)*a(2,2) - a(0,2)*a(2,0);
4259 b(2,1) = a(0,1)*a(2,0) - a(0,0)*a(2,1);
4260 b(0,2) = a(0,1)*a(1,2) - a(0,2)*a(1,1);
4261 b(1,2) = a(0,2)*a(1,0) - a(0,0)*a(1,2);
4262 b(2,2) = a(0,0)*a(1,1) - a(0,1)*a(1,0);
4263 }
4264 };
4265
4266 template<>
4267 struct CofactorHelper<4,4>
4268 {
4269 template<typename T_, int smb_, int snb_, int sma_, int sna_>
4270 CUDA_HOST_DEVICE static void compute(Tiny::Matrix<T_, 4, 4, smb_, snb_>& b, const Tiny::Matrix<T_, 4, 4, sma_, sna_>& a)
4271 {
4272 T_ w[6];
4273 w[0] = a(2,0)*a(3,1)-a(2,1)*a(3,0);
4274 w[1] = a(2,0)*a(3,2)-a(2,2)*a(3,0);
4275 w[2] = a(2,0)*a(3,3)-a(2,3)*a(3,0);
4276 w[3] = a(2,1)*a(3,2)-a(2,2)*a(3,1);
4277 w[4] = a(2,1)*a(3,3)-a(2,3)*a(3,1);
4278 w[5] = a(2,2)*a(3,3)-a(2,3)*a(3,2);
4279
4280 b(0,0) = a(1,1)*w[5]-a(1,2)*w[4]+a(1,3)*w[3];
4281 b(1,0) =-a(1,0)*w[5]+a(1,2)*w[2]-a(1,3)*w[1];
4282 b(2,0) = a(1,0)*w[4]-a(1,1)*w[2]+a(1,3)*w[0];
4283 b(3,0) =-a(1,0)*w[3]+a(1,1)*w[1]-a(1,2)*w[0];
4284
4285 b(0,1) =-a(0,1)*w[5]+a(0,2)*w[4]-a(0,3)*w[3];
4286 b(1,1) = a(0,0)*w[5]-a(0,2)*w[2]+a(0,3)*w[1];
4287 b(2,1) =-a(0,0)*w[4]+a(0,1)*w[2]-a(0,3)*w[0];
4288 b(3,1) = a(0,0)*w[3]-a(0,1)*w[1]+a(0,2)*w[0];
4289
4290 w[0] = a(0,0)*a(1,1)-a(0,1)*a(1,0);
4291 w[1] = a(0,0)*a(1,2)-a(0,2)*a(1,0);
4292 w[2] = a(0,0)*a(1,3)-a(0,3)*a(1,0);
4293 w[3] = a(0,1)*a(1,2)-a(0,2)*a(1,1);
4294 w[4] = a(0,1)*a(1,3)-a(0,3)*a(1,1);
4295 w[5] = a(0,2)*a(1,3)-a(0,3)*a(1,2);
4296
4297 b(0,2) = a(3,1)*w[5]-a(3,2)*w[4]+a(3,3)*w[3];
4298 b(1,2) =-a(3,0)*w[5]+a(3,2)*w[2]-a(3,3)*w[1];
4299 b(2,2) = a(3,0)*w[4]-a(3,1)*w[2]+a(3,3)*w[0];
4300 b(3,2) =-a(3,0)*w[3]+a(3,1)*w[1]-a(3,2)*w[0];
4301
4302 b(0,3) =-a(2,1)*w[5]+a(2,2)*w[4]-a(2,3)*w[3];
4303 b(1,3) = a(2,0)*w[5]-a(2,2)*w[2]+a(2,3)*w[1];
4304 b(2,3) =-a(2,0)*w[4]+a(2,1)*w[2]-a(2,3)*w[0];
4305 b(3,3) = a(2,0)*w[3]-a(2,1)*w[1]+a(2,2)*w[0];
4306 }
4307 };
4308
4309 template<>
4310 struct CofactorHelper<5,5>
4311 {
4312 template<typename T_, int smb_, int snb_, int sma_, int sna_>
4313 CUDA_HOST_DEVICE static void compute(Tiny::Matrix<T_, 5, 5, smb_, snb_>& b, const Tiny::Matrix<T_, 5, 5, sma_, sna_>& a)
4314 {
4315 T_ w[20];
4316 w[ 0] = a(3,0)*a(4,1)-a(3,1)*a(4,0);
4317 w[ 1] = a(3,0)*a(4,2)-a(3,2)*a(4,0);
4318 w[ 2] = a(3,0)*a(4,3)-a(3,3)*a(4,0);
4319 w[ 3] = a(3,0)*a(4,4)-a(3,4)*a(4,0);
4320 w[ 4] = a(3,1)*a(4,2)-a(3,2)*a(4,1);
4321 w[ 5] = a(3,1)*a(4,3)-a(3,3)*a(4,1);
4322 w[ 6] = a(3,1)*a(4,4)-a(3,4)*a(4,1);
4323 w[ 7] = a(3,2)*a(4,3)-a(3,3)*a(4,2);
4324 w[ 8] = a(3,2)*a(4,4)-a(3,4)*a(4,2);
4325 w[ 9] = a(3,3)*a(4,4)-a(3,4)*a(4,3);
4326 w[10] = a(2,0)*w[4]-a(2,1)*w[1]+a(2,2)*w[0];
4327 w[11] = a(2,0)*w[5]-a(2,1)*w[2]+a(2,3)*w[0];
4328 w[12] = a(2,0)*w[6]-a(2,1)*w[3]+a(2,4)*w[0];
4329 w[13] = a(2,0)*w[7]-a(2,2)*w[2]+a(2,3)*w[1];
4330 w[14] = a(2,0)*w[8]-a(2,2)*w[3]+a(2,4)*w[1];
4331 w[15] = a(2,0)*w[9]-a(2,3)*w[3]+a(2,4)*w[2];
4332 w[16] = a(2,1)*w[7]-a(2,2)*w[5]+a(2,3)*w[4];
4333 w[17] = a(2,1)*w[8]-a(2,2)*w[6]+a(2,4)*w[4];
4334 w[18] = a(2,1)*w[9]-a(2,3)*w[6]+a(2,4)*w[5];
4335 w[19] = a(2,2)*w[9]-a(2,3)*w[8]+a(2,4)*w[7];
4336
4337 b(0,0) = a(1,1)*w[19]-a(1,2)*w[18]+a(1,3)*w[17]-a(1,4)*w[16];
4338 b(1,0) =-a(1,0)*w[19]+a(1,2)*w[15]-a(1,3)*w[14]+a(1,4)*w[13];
4339 b(2,0) = a(1,0)*w[18]-a(1,1)*w[15]+a(1,3)*w[12]-a(1,4)*w[11];
4340 b(3,0) =-a(1,0)*w[17]+a(1,1)*w[14]-a(1,2)*w[12]+a(1,4)*w[10];
4341 b(4,0) = a(1,0)*w[16]-a(1,1)*w[13]+a(1,2)*w[11]-a(1,3)*w[10];
4342
4343 b(0,1) =-a(0,1)*w[19]+a(0,2)*w[18]-a(0,3)*w[17]+a(0,4)*w[16];
4344 b(1,1) = a(0,0)*w[19]-a(0,2)*w[15]+a(0,3)*w[14]-a(0,4)*w[13];
4345 b(2,1) =-a(0,0)*w[18]+a(0,1)*w[15]-a(0,3)*w[12]+a(0,4)*w[11];
4346 b(3,1) = a(0,0)*w[17]-a(0,1)*w[14]+a(0,2)*w[12]-a(0,4)*w[10];
4347 b(4,1) =-a(0,0)*w[16]+a(0,1)*w[13]-a(0,2)*w[11]+a(0,3)*w[10];
4348
4349 w[10] = a(1,0)*w[4]-a(1,1)*w[1]+a(1,2)*w[0];
4350 w[11] = a(1,0)*w[5]-a(1,1)*w[2]+a(1,3)*w[0];
4351 w[12] = a(1,0)*w[6]-a(1,1)*w[3]+a(1,4)*w[0];
4352 w[13] = a(1,0)*w[7]-a(1,2)*w[2]+a(1,3)*w[1];
4353 w[14] = a(1,0)*w[8]-a(1,2)*w[3]+a(1,4)*w[1];
4354 w[15] = a(1,0)*w[9]-a(1,3)*w[3]+a(1,4)*w[2];
4355 w[16] = a(1,1)*w[7]-a(1,2)*w[5]+a(1,3)*w[4];
4356 w[17] = a(1,1)*w[8]-a(1,2)*w[6]+a(1,4)*w[4];
4357 w[18] = a(1,1)*w[9]-a(1,3)*w[6]+a(1,4)*w[5];
4358 w[19] = a(1,2)*w[9]-a(1,3)*w[8]+a(1,4)*w[7];
4359
4360 b(0,2) = a(0,1)*w[19]-a(0,2)*w[18]+a(0,3)*w[17]-a(0,4)*w[16];
4361 b(1,2) =-a(0,0)*w[19]+a(0,2)*w[15]-a(0,3)*w[14]+a(0,4)*w[13];
4362 b(2,2) = a(0,0)*w[18]-a(0,1)*w[15]+a(0,3)*w[12]-a(0,4)*w[11];
4363 b(3,2) =-a(0,0)*w[17]+a(0,1)*w[14]-a(0,2)*w[12]+a(0,4)*w[10];
4364 b(4,2) = a(0,0)*w[16]-a(0,1)*w[13]+a(0,2)*w[11]-a(0,3)*w[10];
4365
4366 w[ 0] = a(0,0)*a(1,1)-a(0,1)*a(1,0);
4367 w[ 1] = a(0,0)*a(1,2)-a(0,2)*a(1,0);
4368 w[ 2] = a(0,0)*a(1,3)-a(0,3)*a(1,0);
4369 w[ 3] = a(0,0)*a(1,4)-a(0,4)*a(1,0);
4370 w[ 4] = a(0,1)*a(1,2)-a(0,2)*a(1,1);
4371 w[ 5] = a(0,1)*a(1,3)-a(0,3)*a(1,1);
4372 w[ 6] = a(0,1)*a(1,4)-a(0,4)*a(1,1);
4373 w[ 7] = a(0,2)*a(1,3)-a(0,3)*a(1,2);
4374 w[ 8] = a(0,2)*a(1,4)-a(0,4)*a(1,2);
4375 w[ 9] = a(0,3)*a(1,4)-a(0,4)*a(1,3);
4376 w[10] = a(2,0)*w[4]-a(2,1)*w[1]+a(2,2)*w[0];
4377 w[11] = a(2,0)*w[5]-a(2,1)*w[2]+a(2,3)*w[0];
4378 w[12] = a(2,0)*w[6]-a(2,1)*w[3]+a(2,4)*w[0];
4379 w[13] = a(2,0)*w[7]-a(2,2)*w[2]+a(2,3)*w[1];
4380 w[14] = a(2,0)*w[8]-a(2,2)*w[3]+a(2,4)*w[1];
4381 w[15] = a(2,0)*w[9]-a(2,3)*w[3]+a(2,4)*w[2];
4382 w[16] = a(2,1)*w[7]-a(2,2)*w[5]+a(2,3)*w[4];
4383 w[17] = a(2,1)*w[8]-a(2,2)*w[6]+a(2,4)*w[4];
4384 w[18] = a(2,1)*w[9]-a(2,3)*w[6]+a(2,4)*w[5];
4385 w[19] = a(2,2)*w[9]-a(2,3)*w[8]+a(2,4)*w[7];
4386
4387 b(0,3) = a(4,1)*w[19]-a(4,2)*w[18]+a(4,3)*w[17]-a(4,4)*w[16];
4388 b(1,3) = -a(4,0)*w[19]+a(4,2)*w[15]-a(4,3)*w[14]+a(4,4)*w[13];
4389 b(2,3) = a(4,0)*w[18]-a(4,1)*w[15]+a(4,3)*w[12]-a(4,4)*w[11];
4390 b(3,3) = -a(4,0)*w[17]+a(4,1)*w[14]-a(4,2)*w[12]+a(4,4)*w[10];
4391 b(4,3) = a(4,0)*w[16]-a(4,1)*w[13]+a(4,2)*w[11]-a(4,3)*w[10];
4392
4393 b(0,4) = -a(3,1)*w[19]+a(3,2)*w[18]-a(3,3)*w[17]+a(3,4)*w[16];
4394 b(1,4) = a(3,0)*w[19]-a(3,2)*w[15]+a(3,3)*w[14]-a(3,4)*w[13];
4395 b(2,4) = -a(3,0)*w[18]+a(3,1)*w[15]-a(3,3)*w[12]+a(3,4)*w[11];
4396 b(3,4) = a(3,0)*w[17]-a(3,1)*w[14]+a(3,2)*w[12]-a(3,4)*w[10];
4397 b(4,4) = -a(3,0)*w[16]+a(3,1)*w[13]-a(3,2)*w[11]+a(3,3)*w[10];
4398 }
4399 };
4400
4401 template<>
4402 struct CofactorHelper<6,6>
4403 {
4404 template<typename T_, int smb_, int snb_, int sma_, int sna_>
4405 CUDA_HOST_DEVICE static void compute(Tiny::Matrix<T_, 6, 6, smb_, snb_>& b, const Tiny::Matrix<T_, 6, 6, sma_, sna_>& a)
4406 {
4407 T_ w[35];
4408 w[ 0] = a(4,0)*a(5,1)-a(4,1)*a(5,0);
4409 w[ 1] = a(4,0)*a(5,2)-a(4,2)*a(5,0);
4410 w[ 2] = a(4,0)*a(5,3)-a(4,3)*a(5,0);
4411 w[ 3] = a(4,0)*a(5,4)-a(4,4)*a(5,0);
4412 w[ 4] = a(4,0)*a(5,5)-a(4,5)*a(5,0);
4413 w[ 5] = a(4,1)*a(5,2)-a(4,2)*a(5,1);
4414 w[ 6] = a(4,1)*a(5,3)-a(4,3)*a(5,1);
4415 w[ 7] = a(4,1)*a(5,4)-a(4,4)*a(5,1);
4416 w[ 8] = a(4,1)*a(5,5)-a(4,5)*a(5,1);
4417 w[ 9] = a(4,2)*a(5,3)-a(4,3)*a(5,2);
4418 w[10] = a(4,2)*a(5,4)-a(4,4)*a(5,2);
4419 w[11] = a(4,2)*a(5,5)-a(4,5)*a(5,2);
4420 w[12] = a(4,3)*a(5,4)-a(4,4)*a(5,3);
4421 w[13] = a(4,3)*a(5,5)-a(4,5)*a(5,3);
4422 w[14] = a(4,4)*a(5,5)-a(4,5)*a(5,4);
4423 w[15] = a(3,0)*w[5]-a(3,1)*w[1]+a(3,2)*w[0];
4424 w[16] = a(3,0)*w[6]-a(3,1)*w[2]+a(3,3)*w[0];
4425 w[17] = a(3,0)*w[7]-a(3,1)*w[3]+a(3,4)*w[0];
4426 w[18] = a(3,0)*w[8]-a(3,1)*w[4]+a(3,5)*w[0];
4427 w[19] = a(3,0)*w[9]-a(3,2)*w[2]+a(3,3)*w[1];
4428 w[20] = a(3,0)*w[10]-a(3,2)*w[3]+a(3,4)*w[1];
4429 w[21] = a(3,0)*w[11]-a(3,2)*w[4]+a(3,5)*w[1];
4430 w[22] = a(3,0)*w[12]-a(3,3)*w[3]+a(3,4)*w[2];
4431 w[23] = a(3,0)*w[13]-a(3,3)*w[4]+a(3,5)*w[2];
4432 w[24] = a(3,0)*w[14]-a(3,4)*w[4]+a(3,5)*w[3];
4433 w[25] = a(3,1)*w[9]-a(3,2)*w[6]+a(3,3)*w[5];
4434 w[26] = a(3,1)*w[10]-a(3,2)*w[7]+a(3,4)*w[5];
4435 w[27] = a(3,1)*w[11]-a(3,2)*w[8]+a(3,5)*w[5];
4436 w[28] = a(3,1)*w[12]-a(3,3)*w[7]+a(3,4)*w[6];
4437 w[29] = a(3,1)*w[13]-a(3,3)*w[8]+a(3,5)*w[6];
4438 w[30] = a(3,1)*w[14]-a(3,4)*w[8]+a(3,5)*w[7];
4439 w[31] = a(3,2)*w[12]-a(3,3)*w[10]+a(3,4)*w[9];
4440 w[32] = a(3,2)*w[13]-a(3,3)*w[11]+a(3,5)*w[9];
4441 w[33] = a(3,2)*w[14]-a(3,4)*w[11]+a(3,5)*w[10];
4442 w[34] = a(3,3)*w[14]-a(3,4)*w[13]+a(3,5)*w[12];
4443
4444 w[ 0] = a(2,0)*w[25]-a(2,1)*w[19]+a(2,2)*w[16]-a(2,3)*w[15];
4445 w[ 1] = a(2,0)*w[26]-a(2,1)*w[20]+a(2,2)*w[17]-a(2,4)*w[15];
4446 w[ 2] = a(2,0)*w[27]-a(2,1)*w[21]+a(2,2)*w[18]-a(2,5)*w[15];
4447 w[ 3] = a(2,0)*w[28]-a(2,1)*w[22]+a(2,3)*w[17]-a(2,4)*w[16];
4448 w[ 4] = a(2,0)*w[29]-a(2,1)*w[23]+a(2,3)*w[18]-a(2,5)*w[16];
4449 w[ 5] = a(2,0)*w[30]-a(2,1)*w[24]+a(2,4)*w[18]-a(2,5)*w[17];
4450 w[ 6] = a(2,0)*w[31]-a(2,2)*w[22]+a(2,3)*w[20]-a(2,4)*w[19];
4451 w[ 7] = a(2,0)*w[32]-a(2,2)*w[23]+a(2,3)*w[21]-a(2,5)*w[19];
4452 w[ 8] = a(2,0)*w[33]-a(2,2)*w[24]+a(2,4)*w[21]-a(2,5)*w[20];
4453 w[ 9] = a(2,0)*w[34]-a(2,3)*w[24]+a(2,4)*w[23]-a(2,5)*w[22];
4454 w[10] = a(2,1)*w[31]-a(2,2)*w[28]+a(2,3)*w[26]-a(2,4)*w[25];
4455 w[11] = a(2,1)*w[32]-a(2,2)*w[29]+a(2,3)*w[27]-a(2,5)*w[25];
4456 w[12] = a(2,1)*w[33]-a(2,2)*w[30]+a(2,4)*w[27]-a(2,5)*w[26];
4457 w[13] = a(2,1)*w[34]-a(2,3)*w[30]+a(2,4)*w[29]-a(2,5)*w[28];
4458 w[14] = a(2,2)*w[34]-a(2,3)*w[33]+a(2,4)*w[32]-a(2,5)*w[31];
4459
4460 b(0,0) = a(1,1)*w[14]-a(1,2)*w[13]+a(1,3)*w[12]-a(1,4)*w[11]+a(1,5)*w[10];
4461 b(1,0) = -a(1,0)*w[14]+a(1,2)*w[9]-a(1,3)*w[8]+a(1,4)*w[7]-a(1,5)*w[6];
4462 b(2,0) = a(1,0)*w[13]-a(1,1)*w[9]+a(1,3)*w[5]-a(1,4)*w[4]+a(1,5)*w[3];
4463 b(3,0) = -a(1,0)*w[12]+a(1,1)*w[8]-a(1,2)*w[5]+a(1,4)*w[2]-a(1,5)*w[1];
4464 b(4,0) = a(1,0)*w[11]-a(1,1)*w[7]+a(1,2)*w[4]-a(1,3)*w[2]+a(1,5)*w[0];
4465 b(5,0) = -a(1,0)*w[10]+a(1,1)*w[6]-a(1,2)*w[3]+a(1,3)*w[1]-a(1,4)*w[0];
4466
4467 b(0,1) =-a(0,1)*w[14]+a(0,2)*w[13]-a(0,3)*w[12]+a(0,4)*w[11]-a(0,5)*w[10];
4468 b(1,1) = a(0,0)*w[14]-a(0,2)*w[9]+a(0,3)*w[8]-a(0,4)*w[7]+a(0,5)*w[6];
4469 b(2,1) =-a(0,0)*w[13]+a(0,1)*w[9]-a(0,3)*w[5]+a(0,4)*w[4]-a(0,5)*w[3];
4470 b(3,1) = a(0,0)*w[12]-a(0,1)*w[8]+a(0,2)*w[5]-a(0,4)*w[2]+a(0,5)*w[1];
4471 b(4,1) =-a(0,0)*w[11]+a(0,1)*w[7]-a(0,2)*w[4]+a(0,3)*w[2]-a(0,5)*w[0];
4472 b(5,1) = a(0,0)*w[10]-a(0,1)*w[6]+a(0,2)*w[3]-a(0,3)*w[1]+a(0,4)*w[0];
4473
4474 w[ 0] = a(4,0)*a(5,1)-a(4,1)*a(5,0);
4475 w[ 1] = a(4,0)*a(5,2)-a(4,2)*a(5,0);
4476 w[ 2] = a(4,0)*a(5,3)-a(4,3)*a(5,0);
4477 w[ 3] = a(4,0)*a(5,4)-a(4,4)*a(5,0);
4478 w[ 4] = a(4,0)*a(5,5)-a(4,5)*a(5,0);
4479 w[ 5] = a(4,1)*a(5,2)-a(4,2)*a(5,1);
4480 w[ 6] = a(4,1)*a(5,3)-a(4,3)*a(5,1);
4481 w[ 7] = a(4,1)*a(5,4)-a(4,4)*a(5,1);
4482 w[ 8] = a(4,1)*a(5,5)-a(4,5)*a(5,1);
4483 w[ 9] = a(4,2)*a(5,3)-a(4,3)*a(5,2);
4484 w[10] = a(4,2)*a(5,4)-a(4,4)*a(5,2);
4485 w[11] = a(4,2)*a(5,5)-a(4,5)*a(5,2);
4486 w[12] = a(4,3)*a(5,4)-a(4,4)*a(5,3);
4487 w[13] = a(4,3)*a(5,5)-a(4,5)*a(5,3);
4488 w[14] = a(4,4)*a(5,5)-a(4,5)*a(5,4);
4489 w[15] = a(1,0)*w[5]-a(1,1)*w[1]+a(1,2)*w[0];
4490 w[16] = a(1,0)*w[6]-a(1,1)*w[2]+a(1,3)*w[0];
4491 w[17] = a(1,0)*w[7]-a(1,1)*w[3]+a(1,4)*w[0];
4492 w[18] = a(1,0)*w[8]-a(1,1)*w[4]+a(1,5)*w[0];
4493 w[19] = a(1,0)*w[9]-a(1,2)*w[2]+a(1,3)*w[1];
4494 w[20] = a(1,0)*w[10]-a(1,2)*w[3]+a(1,4)*w[1];
4495 w[21] = a(1,0)*w[11]-a(1,2)*w[4]+a(1,5)*w[1];
4496 w[22] = a(1,0)*w[12]-a(1,3)*w[3]+a(1,4)*w[2];
4497 w[23] = a(1,0)*w[13]-a(1,3)*w[4]+a(1,5)*w[2];
4498 w[24] = a(1,0)*w[14]-a(1,4)*w[4]+a(1,5)*w[3];
4499 w[25] = a(1,1)*w[9]-a(1,2)*w[6]+a(1,3)*w[5];
4500 w[26] = a(1,1)*w[10]-a(1,2)*w[7]+a(1,4)*w[5];
4501 w[27] = a(1,1)*w[11]-a(1,2)*w[8]+a(1,5)*w[5];
4502 w[28] = a(1,1)*w[12]-a(1,3)*w[7]+a(1,4)*w[6];
4503 w[29] = a(1,1)*w[13]-a(1,3)*w[8]+a(1,5)*w[6];
4504 w[30] = a(1,1)*w[14]-a(1,4)*w[8]+a(1,5)*w[7];
4505 w[31] = a(1,2)*w[12]-a(1,3)*w[10]+a(1,4)*w[9];
4506 w[32] = a(1,2)*w[13]-a(1,3)*w[11]+a(1,5)*w[9];
4507 w[33] = a(1,2)*w[14]-a(1,4)*w[11]+a(1,5)*w[10];
4508 w[34] = a(1,3)*w[14]-a(1,4)*w[13]+a(1,5)*w[12];
4509 w[ 0] = a(0,0)*w[25]-a(0,1)*w[19]+a(0,2)*w[16]-a(0,3)*w[15];
4510 w[ 1] = a(0,0)*w[26]-a(0,1)*w[20]+a(0,2)*w[17]-a(0,4)*w[15];
4511 w[ 2] = a(0,0)*w[27]-a(0,1)*w[21]+a(0,2)*w[18]-a(0,5)*w[15];
4512 w[ 3] = a(0,0)*w[28]-a(0,1)*w[22]+a(0,3)*w[17]-a(0,4)*w[16];
4513 w[ 4] = a(0,0)*w[29]-a(0,1)*w[23]+a(0,3)*w[18]-a(0,5)*w[16];
4514 w[ 5] = a(0,0)*w[30]-a(0,1)*w[24]+a(0,4)*w[18]-a(0,5)*w[17];
4515 w[ 6] = a(0,0)*w[31]-a(0,2)*w[22]+a(0,3)*w[20]-a(0,4)*w[19];
4516 w[ 7] = a(0,0)*w[32]-a(0,2)*w[23]+a(0,3)*w[21]-a(0,5)*w[19];
4517 w[ 8] = a(0,0)*w[33]-a(0,2)*w[24]+a(0,4)*w[21]-a(0,5)*w[20];
4518 w[ 9] = a(0,0)*w[34]-a(0,3)*w[24]+a(0,4)*w[23]-a(0,5)*w[22];
4519 w[10] = a(0,1)*w[31]-a(0,2)*w[28]+a(0,3)*w[26]-a(0,4)*w[25];
4520 w[11] = a(0,1)*w[32]-a(0,2)*w[29]+a(0,3)*w[27]-a(0,5)*w[25];
4521 w[12] = a(0,1)*w[33]-a(0,2)*w[30]+a(0,4)*w[27]-a(0,5)*w[26];
4522 w[13] = a(0,1)*w[34]-a(0,3)*w[30]+a(0,4)*w[29]-a(0,5)*w[28];
4523 w[14] = a(0,2)*w[34]-a(0,3)*w[33]+a(0,4)*w[32]-a(0,5)*w[31];
4524
4525 b(0,2) = a(3,1)*w[14]-a(3,2)*w[13]+a(3,3)*w[12]-a(3,4)*w[11]+a(3,5)*w[10];
4526 b(1,2) =-a(3,0)*w[14]+a(3,2)*w[9]-a(3,3)*w[8]+a(3,4)*w[7]-a(3,5)*w[6];
4527 b(2,2) = a(3,0)*w[13]-a(3,1)*w[9]+a(3,3)*w[5]-a(3,4)*w[4]+a(3,5)*w[3];
4528 b(3,2) =-a(3,0)*w[12]+a(3,1)*w[8]-a(3,2)*w[5]+a(3,4)*w[2]-a(3,5)*w[1];
4529 b(4,2) = a(3,0)*w[11]-a(3,1)*w[7]+a(3,2)*w[4]-a(3,3)*w[2]+a(3,5)*w[0];
4530 b(5,2) =-a(3,0)*w[10]+a(3,1)*w[6]-a(3,2)*w[3]+a(3,3)*w[1]-a(3,4)*w[0];
4531
4532 b(0,3) =-a(2,1)*w[14]+a(2,2)*w[13]-a(2,3)*w[12]+a(2,4)*w[11]-a(2,5)*w[10];
4533 b(1,3) = a(2,0)*w[14]-a(2,2)*w[9]+a(2,3)*w[8]-a(2,4)*w[7]+a(2,5)*w[6];
4534 b(2,3) =-a(2,0)*w[13]+a(2,1)*w[9]-a(2,3)*w[5]+a(2,4)*w[4]-a(2,5)*w[3];
4535 b(3,3) = a(2,0)*w[12]-a(2,1)*w[8]+a(2,2)*w[5]-a(2,4)*w[2]+a(2,5)*w[1];
4536 b(4,3) =-a(2,0)*w[11]+a(2,1)*w[7]-a(2,2)*w[4]+a(2,3)*w[2]-a(2,5)*w[0];
4537 b(5,3) = a(2,0)*w[10]-a(2,1)*w[6]+a(2,2)*w[3]-a(2,3)*w[1]+a(2,4)*w[0];
4538
4539 w[ 0] = a(2,0)*a(3,1)-a(2,1)*a(3,0);
4540 w[ 1] = a(2,0)*a(3,2)-a(2,2)*a(3,0);
4541 w[ 2] = a(2,0)*a(3,3)-a(2,3)*a(3,0);
4542 w[ 3] = a(2,0)*a(3,4)-a(2,4)*a(3,0);
4543 w[ 4] = a(2,0)*a(3,5)-a(2,5)*a(3,0);
4544 w[ 5] = a(2,1)*a(3,2)-a(2,2)*a(3,1);
4545 w[ 6] = a(2,1)*a(3,3)-a(2,3)*a(3,1);
4546 w[ 7] = a(2,1)*a(3,4)-a(2,4)*a(3,1);
4547 w[ 8] = a(2,1)*a(3,5)-a(2,5)*a(3,1);
4548 w[ 9] = a(2,2)*a(3,3)-a(2,3)*a(3,2);
4549 w[10] = a(2,2)*a(3,4)-a(2,4)*a(3,2);
4550 w[11] = a(2,2)*a(3,5)-a(2,5)*a(3,2);
4551 w[12] = a(2,3)*a(3,4)-a(2,4)*a(3,3);
4552 w[13] = a(2,3)*a(3,5)-a(2,5)*a(3,3);
4553 w[14] = a(2,4)*a(3,5)-a(2,5)*a(3,4);
4554 w[15] = a(1,0)*w[5]-a(1,1)*w[1]+a(1,2)*w[0];
4555 w[16] = a(1,0)*w[6]-a(1,1)*w[2]+a(1,3)*w[0];
4556 w[17] = a(1,0)*w[7]-a(1,1)*w[3]+a(1,4)*w[0];
4557 w[18] = a(1,0)*w[8]-a(1,1)*w[4]+a(1,5)*w[0];
4558 w[19] = a(1,0)*w[9]-a(1,2)*w[2]+a(1,3)*w[1];
4559 w[20] = a(1,0)*w[10]-a(1,2)*w[3]+a(1,4)*w[1];
4560 w[21] = a(1,0)*w[11]-a(1,2)*w[4]+a(1,5)*w[1];
4561 w[22] = a(1,0)*w[12]-a(1,3)*w[3]+a(1,4)*w[2];
4562 w[23] = a(1,0)*w[13]-a(1,3)*w[4]+a(1,5)*w[2];
4563 w[24] = a(1,0)*w[14]-a(1,4)*w[4]+a(1,5)*w[3];
4564 w[25] = a(1,1)*w[9]-a(1,2)*w[6]+a(1,3)*w[5];
4565 w[26] = a(1,1)*w[10]-a(1,2)*w[7]+a(1,4)*w[5];
4566 w[27] = a(1,1)*w[11]-a(1,2)*w[8]+a(1,5)*w[5];
4567 w[28] = a(1,1)*w[12]-a(1,3)*w[7]+a(1,4)*w[6];
4568 w[29] = a(1,1)*w[13]-a(1,3)*w[8]+a(1,5)*w[6];
4569 w[30] = a(1,1)*w[14]-a(1,4)*w[8]+a(1,5)*w[7];
4570 w[31] = a(1,2)*w[12]-a(1,3)*w[10]+a(1,4)*w[9];
4571 w[32] = a(1,2)*w[13]-a(1,3)*w[11]+a(1,5)*w[9];
4572 w[33] = a(1,2)*w[14]-a(1,4)*w[11]+a(1,5)*w[10];
4573 w[34] = a(1,3)*w[14]-a(1,4)*w[13]+a(1,5)*w[12];
4574
4575 w[ 0] = a(0,0)*w[25]-a(0,1)*w[19]+a(0,2)*w[16]-a(0,3)*w[15];
4576 w[ 1] = a(0,0)*w[26]-a(0,1)*w[20]+a(0,2)*w[17]-a(0,4)*w[15];
4577 w[ 2] = a(0,0)*w[27]-a(0,1)*w[21]+a(0,2)*w[18]-a(0,5)*w[15];
4578 w[ 3] = a(0,0)*w[28]-a(0,1)*w[22]+a(0,3)*w[17]-a(0,4)*w[16];
4579 w[ 4] = a(0,0)*w[29]-a(0,1)*w[23]+a(0,3)*w[18]-a(0,5)*w[16];
4580 w[ 5] = a(0,0)*w[30]-a(0,1)*w[24]+a(0,4)*w[18]-a(0,5)*w[17];
4581 w[ 6] = a(0,0)*w[31]-a(0,2)*w[22]+a(0,3)*w[20]-a(0,4)*w[19];
4582 w[ 7] = a(0,0)*w[32]-a(0,2)*w[23]+a(0,3)*w[21]-a(0,5)*w[19];
4583 w[ 8] = a(0,0)*w[33]-a(0,2)*w[24]+a(0,4)*w[21]-a(0,5)*w[20];
4584 w[ 9] = a(0,0)*w[34]-a(0,3)*w[24]+a(0,4)*w[23]-a(0,5)*w[22];
4585 w[10] = a(0,1)*w[31]-a(0,2)*w[28]+a(0,3)*w[26]-a(0,4)*w[25];
4586 w[11] = a(0,1)*w[32]-a(0,2)*w[29]+a(0,3)*w[27]-a(0,5)*w[25];
4587 w[12] = a(0,1)*w[33]-a(0,2)*w[30]+a(0,4)*w[27]-a(0,5)*w[26];
4588 w[13] = a(0,1)*w[34]-a(0,3)*w[30]+a(0,4)*w[29]-a(0,5)*w[28];
4589 w[14] = a(0,2)*w[34]-a(0,3)*w[33]+a(0,4)*w[32]-a(0,5)*w[31];
4590
4591 b(0,4) = a(5,1)*w[14]-a(5,2)*w[13]+a(5,3)*w[12]-a(5,4)*w[11]+a(5,5)*w[10];
4592 b(1,4) =-a(5,0)*w[14]+a(5,2)*w[9]-a(5,3)*w[8]+a(5,4)*w[7]-a(5,5)*w[6];
4593 b(2,4) = a(5,0)*w[13]-a(5,1)*w[9]+a(5,3)*w[5]-a(5,4)*w[4]+a(5,5)*w[3];
4594 b(3,4) =-a(5,0)*w[12]+a(5,1)*w[8]-a(5,2)*w[5]+a(5,4)*w[2]-a(5,5)*w[1];
4595 b(4,4) = a(5,0)*w[11]-a(5,1)*w[7]+a(5,2)*w[4]-a(5,3)*w[2]+a(5,5)*w[0];
4596 b(5,4) =-a(5,0)*w[10]+a(5,1)*w[6]-a(5,2)*w[3]+a(5,3)*w[1]-a(5,4)*w[0];
4597
4598 b(0,5) =-a(4,1)*w[14]+a(4,2)*w[13]-a(4,3)*w[12]+a(4,4)*w[11]-a(4,5)*w[10];
4599 b(1,5) = a(4,0)*w[14]-a(4,2)*w[9]+a(4,3)*w[8]-a(4,4)*w[7]+a(4,5)*w[6];
4600 b(2,5) =-a(4,0)*w[13]+a(4,1)*w[9]-a(4,3)*w[5]+a(4,4)*w[4]-a(4,5)*w[3];
4601 b(3,5) = a(4,0)*w[12]-a(4,1)*w[8]+a(4,2)*w[5]-a(4,4)*w[2]+a(4,5)*w[1];
4602 b(4,5) =-a(4,0)*w[11]+a(4,1)*w[7]-a(4,2)*w[4]+a(4,3)*w[2]-a(4,5)*w[0];
4603 b(5,5) = a(4,0)*w[10]-a(4,1)*w[6]+a(4,2)*w[3]-a(4,3)*w[1]+a(4,4)*w[0];
4604 }
4605 };
4606 } // namespace Intern
4608 } // namespace Tiny
4609} // namespace FEAT
#define XABORTM(msg)
Abortion macro definition with custom message.
Definition: assertion.hpp:192
#define ASSERTM(expr, msg)
Debug-Assertion macro definition with custom message.
Definition: assertion.hpp:230
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
Definition: assertion.hpp:263
FEAT Kernel base header.
MPI typemap description.
Definition: dist.hpp:228
Typemap & add_entry(std::size_t displacement, int blocklength=1)
Add a member to this typemap.
Definition: dist.hpp:259
Math Limits class template.
Definition: math.hpp:1475
Tiny Matrix class template.
CUDA_HOST_DEVICE DataType det() const
Returns the determinant of the matrix.
CUDA_HOST_DEVICE Matrix(DataType value)
value-assignment constructor
static Dist::Typemap typemap()
Returns a Typemap for sending matrices via MPI.
CUDA_HOST_DEVICE Matrix & set_cofactor(const Matrix< T_, m_, n_, sma_, sna_ > &a)
Sets this matrix to the cofactor matrix of another matrix.
CUDA_HOST_DEVICE Matrix & add_scalar_main_diag(DataType alpha)
Adds a value onto the matrix's main diagonal.
CUDA_HOST_DEVICE Matrix & add_outer_product(const Vector< T_, m_, snx_ > &x, const Vector< T_, n_, sny_ > &y, const DataType alpha=DataType(1))
Adds the outer product of two vectors onto the matrix.
CUDA_HOST_DEVICE Matrix & add_mat_mat_mult(const Matrix< T_, m_, la_, sma_, sna_ > &a, const Matrix< T_, lb_, n_, smb_, snb_ > &b, DataType alpha=DataType(1))
Adds the algebraic matrix-product of two other matrices onto this matrix.
CUDA_HOST_DEVICE Matrix & set_mat_mat_mult(const Matrix< T_, m_, la_, sma_, sna_ > &a, const Matrix< T_, lb_, n_, smb_, snb_ > &b)
Sets this matrix to the algebraic matrix-product of two other matrices.
CUDA_HOST_DEVICE DataType norm_frobenius_sqr() const
Returns the Frobenius norm squared of the matrix.
T_ ValueType
the data type of the matrix
CUDA_HOST_DEVICE Matrix & operator=(const Matrix< T_, m_, n_, sma_, sna_ > &a)
assignment operator
CUDA_HOST_DEVICE Matrix(const std::initializer_list< std::initializer_list< Tx_ > > &x)
Initializer list constructor.
CUDA_HOST_DEVICE Matrix & add_vec_tensor_mult(const Vector< T_, l_, snv_ > &x, const Tensor3< T_, l_, m_, n_, slt_, smt_, snt_ > &t, DataType alpha=DataType(1))
Adds the result of a vector-tensor left-product onto this matrix.
CUDA_HOST_DEVICE Matrix & set_transpose(const Matrix< T_, n_, m_, sma_, sna_ > &a)
Sets this matrix to the transpose of another matrix.
CUDA_HOST friend std::istream & operator>>(std::istream &in, Matrix &A)
Tiny::Matrix streaming operator.
CUDA_HOST_DEVICE Matrix & axpy(DataType alpha, const Matrix< T_, m_, n_, sma_, sna_ > &a)
Adds another scaled matrix onto this matrix.
CUDA_HOST_DEVICE Matrix & operator-=(const Matrix< T_, m_, n_, sma_, sna_ > &a)
matrix component-wise subtraction operator
CUDA_HOST_DEVICE Matrix(const Matrix< T2_, m_, n_, sma_, sna_ > &a)
copy constructor
static CUDA_HOST_DEVICE Matrix null()
Returns a null-matrix.
CUDA_HOST_DEVICE DataType norm_sub_id_frobenius() const
Returns the Frobenius norm of the difference of this matrix and the identity matrix.
CUDA_HOST_DEVICE Matrix & set_inverse(const Matrix< T_, m_, n_, sma_, sna_ > &a)
Sets this matrix to the inverse of another matrix.
CUDA_HOST_DEVICE const RowType & operator[](int i) const
Row-Access operator.
CUDA_HOST_DEVICE Matrix & operator*=(DataType alpha)
scalar-right-multiply-by operator
CUDA_HOST_DEVICE DataType scalar_product(const Vector< T_, m_, snx_ > &x, const Vector< T_, n_, sny_ > &y) const
Computes the scalar product of two vectors with this matrix.
Intern::DataTypeExtractor< ValueType >::MyDataType DataType
The basic data type buried in the lowest level of the vector.
Vector< T_, n_, sn_ > RowType
the type of a single matrix row
CUDA_HOST_DEVICE const T_ & operator()(int i, int j) const
Access operator.
CUDA_HOST_DEVICE Matrix & operator=(DataType value)
value-assignment operator
CUDA_HOST_DEVICE Matrix & set_rotation_3d(T_ yaw, T_ pitch, T_ roll)
Sets this matrix to a 3D yaw-pitch-roll rotation matrix.
CUDA_HOST_DEVICE DataType trace() const
Returns the trace of the matrix.
CUDA_HOST_DEVICE DataType norm_hessian_sqr() const
Returns the Hessian norm square.
CUDA_HOST_DEVICE Matrix(const std::initializer_list< Tx_ > &x)
Initializer list of Tiny::Vector constructor.
CUDA_HOST_DEVICE Matrix & set_gram(const Matrix< T_, l_, n_, sla_, sna_ > &a)
Sets this matrix to the Gram matrix of another matrix.
CUDA_HOST_DEVICE Matrix & set_rotation_2d(T_ angle)
Sets this matrix to a 2D rotation matrix.
CUDA_HOST_DEVICE Matrix & set_double_mat_mult(const Matrix< T_, k_, l_, sma_, sna_ > &a, const Matrix< T_, k_, m_, smb_, snb_ > &b, const Matrix< T_, l_, n_, smd_, snd_ > &d, T_ alpha=T_(1))
Sets this matrix to the algebraic matrix double-product of three other matrices.
CUDA_HOST_DEVICE T_ & operator()(int i, int j)
Access operator.
CUDA_HOST_DEVICE Matrix & add_double_mat_mult(const Matrix< T_, k_, l_, sma_, sna_ > &a, const Matrix< T_, k_, m_, smb_, snb_ > &b, const Matrix< T_, l_, n_, smd_, snd_ > &d, DataType alpha=DataType(1))
Adds the algebraic matrix double-product of three other matrices onto this matrix.
CUDA_HOST_DEVICE void convert(const Matrix< Tx_, m_, n_, sma_, sna_ > &a)
conversion operator
CUDA_HOST_DEVICE Matrix & set_identity()
Sets this matrix to the identity matrix.
CUDA_HOST_DEVICE Matrix & operator+=(const Matrix< T_, m_, n_, sma_, sna_ > &a)
matrix component-wise addition operator
CUDA_HOST_DEVICE RowType & operator[](int i)
Row-Access operator.
CUDA_HOST_DEVICE Matrix & operator=(const std::initializer_list< std::initializer_list< Tx_ > > &x)
Initializer list assignment operator.
CUDA_HOST_DEVICE Matrix()
default constructor
CUDA_HOST_DEVICE void copy_n(const Matrix< T_, ma_, na_, sma_, sna_ > &a)
Copies the upper left mm_ x nn_ entries of a matrix.
CUDA_HOST_DEVICE DataType vol() const
Returns the volume of the matrix.
RowType v[sm_]
actual matrix data; that's an array of vectors
CUDA_HOST_DEVICE Matrix & operator=(const std::initializer_list< Tx_ > &x)
Initializer list assignment operator.
CUDA_HOST_DEVICE Matrix & set_vec_tensor_mult(const Vector< T_, l_, snv_ > &x, const Tensor3< T_, l_, m_, n_, slt_, smt_, snt_ > &t, DataType alpha=DataType(1))
Sets this matrix to the result of a vector-tensor left-product.
CUDA_HOST_DEVICE Matrix & set_outer_product(const Vector< T_, m_, snx_ > &x, const Vector< T_, n_, sny_ > &y)
Sets this matrix to the outer product of two vectors.
CUDA_HOST_DEVICE void format(DataType alpha=DataType(0))
Formats the matrix.
CUDA_HOST_DEVICE void copy(const Matrix< T_, m_, n_, sma_, sna_ > &a)
Copies a matrix.
CUDA_HOST_DEVICE DataType norm_frobenius() const
Returns the Frobenius norm of the matrix.
Tiny Tensor3 class template.
CUDA_HOST_DEVICE Tensor3 & add_double_mat_mult(const Tensor3< T_, lt_, mt_, nt_, slt_, smt_, snt_ > &t, const Matrix< T_, nt_, n_, smb_, snb_ > &b, const Matrix< T_, mt_, m_, smd_, snd_ > &d, DataType alpha=DataType(1))
Adds the result of a matrix-tensor-matrix double-product onto this tensor.
static Dist::Typemap typemap()
Returns a Typemap for sending Tensor3 via MPI.
Intern::DataTypeExtractor< ValueType >::MyDataType DataType
The basic data type buried in the lowest level of the vector.
CUDA_HOST_DEVICE void copy_n(const Tensor3< T_, la_, ma_, na_, sla_, sma_, sna_ > &a)
Copies the upper left mm_ x nn_ entries of a matrix.
static constexpr int l
the tube count of the tensor
CUDA_HOST_DEVICE Tensor3 & operator=(const std::initializer_list< Tx_ > &x)
Initializer list assignment operator.
static constexpr int sn
the column stride of the tensor
CUDA_HOST_DEVICE Tensor3 & operator=(const Tensor3< T_, l_, m_, n_, sla_, sma_, sna_ > &a)
copy-assignment operator
CUDA_HOST_DEVICE void format(DataType alpha=DataType(0))
formats the tensor
CUDA_HOST_DEVICE const T_ & operator()(int h, int i, int j) const
Access operator.
CUDA_HOST_DEVICE Tensor3 & operator=(DataType value)
value-assignment operator
CUDA_HOST_DEVICE Tensor3 & operator=(const std::initializer_list< std::initializer_list< std::initializer_list< Tx_ > > > &x)
Initializer list assignment operator.
CUDA_HOST_DEVICE Tensor3 & add_vec_mat_outer_product(const Vector< T_, l_, slx_ > &x, const Matrix< T_, m_, n_, sma_, sna_ > &a, DataType alpha=DataType(1))
Adds the result of a vector-matrix outer product onto this tensor.
CUDA_HOST_DEVICE Tensor3 & operator+=(const Tensor3< T_, l_, m_, n_, sla_, sma_, sna_ > &a)
tensor component-wise addition operator
CUDA_HOST_DEVICE Tensor3(const std::initializer_list< std::initializer_list< std::initializer_list< Tx_ > > > &x)
Initializer list constructor.
CUDA_HOST_DEVICE const PlaneType & operator[](int h) const
Plane-Access operator.
CUDA_HOST_DEVICE Tensor3(const Tensor3< T_, l_, m_, n_, sla_, sma_, sna_ > &a)
copy-constructor
Matrix< T_, m_, n_, sm_, sn_ > PlaneType
Type of tensor data; that's an array of matrices.
static constexpr int sl
the tube stride of the tensor
CUDA_HOST_DEVICE Tensor3(const std::initializer_list< Tx_ > &x)
Initializer list constructor.
CUDA_HOST_DEVICE PlaneType & operator[](int h)
Plane-Access operator.
CUDA_HOST_DEVICE Tensor3(DataType value)
value-assignment constructor
static CUDA_HOST_DEVICE Tensor3 null()
Returns a null-tensor.
CUDA_HOST_DEVICE Tensor3 & add_mat_tensor_mult(const Matrix< T_, l_, k_, sma_, sna_ > &a, const Tensor3< T_, k_, m_, n_, slt_, smt_, snt_ > &t, DataType alpha=DataType(1))
Adds the result of a matrix-tensor product onto this tensor.
PlaneType v[sl_]
Actual tensor data.
static constexpr int m
the row count of the tensor
CUDA_HOST_DEVICE void convert(const Tensor3< Tx_, l_, m_, n_, sla_, sma_, sna_ > &a)
conversion operator
CUDA_HOST_DEVICE Tensor3()
default constructor
CUDA_HOST_DEVICE Tensor3 & operator-=(const Tensor3< T_, l_, m_, n_, sla_, sma_, sna_ > &a)
tensor component-wise subtraction operator
CUDA_HOST_DEVICE T_ & operator()(int h, int i, int j)
Access operator.
static constexpr int n
the column count of the tensor
static constexpr int sm
the row stride of the tensor
T_ ValueType
the data type of the tensor
CUDA_HOST_DEVICE Tensor3 & operator*=(DataType alpha)
scalar right-multiply-by operator
CUDA_HOST_DEVICE void copy(const Tensor3< T_, l_, m_, n_, sla_, sma_, sna_ > &a)
Copies a tensor3.
Tiny Vector class template.
static CUDA_HOST_DEVICE Vector null()
Returns a null-vector.
CUDA_HOST_DEVICE DataType norm_euclid_n() const
Computes the euclid norm of first nn_ entries of this vector.
CUDA_HOST_DEVICE Vector & add_mat_vec_mult(const Matrix< T_, n_, m_, sma_, sna_ > &a, const Vector< T_, m_, sx_ > &x, DataType alpha=DataType(1))
Adds the result of a matrix-vector product onto this vector.
CUDA_HOST_DEVICE DataType norm_euclid_sqr_n() const
Computes the squared euclid norm of first nn_ entries of this vector.
CUDA_HOST_DEVICE Vector & normalize_n()
Normalizes the first nn_ entries of this vector.
CUDA_HOST_DEVICE Vector & operator=(const Vector< T_, n_, sx_ > &x)
copy-assignment operator
T_ v[s_]
actual vector data
CUDA_HOST_DEVICE bool normalized() const
Return if the vector is normalized.
CUDA_HOST_DEVICE void format_n(DataType alpha=DataType(0))
Formats the first nn_ entries of the vector.
static CUDA_HOST_DEVICE Vector convert_new(Vector &&x)
overload for moveable rvalue type
CUDA_HOST_DEVICE void format(DataType alpha=DataType(0))
Formats the vector.
CUDA_HOST_DEVICE Vector(const Vector< T_, n_, sx_ > &x)
copy constructor
CUDA_HOST_DEVICE Vector & operator=(const std::initializer_list< Tx_ > &x)
Initializer list assignment operator.
CUDA_HOST_DEVICE DataType norm_max() const
Computes the max-norm of this vector.
CUDA_HOST_DEVICE T_ & operator()(int i)
Access operator.
CUDA_HOST_DEVICE const T_ & operator[](int i) const
Access operator.
CUDA_HOST_DEVICE Vector & operator-=(const Vector< T_, n_, sx_ > &x)
vector-subtract operator
CUDA_HOST_DEVICE Vector & set_vec_mat_mult(const Vector< T_, m_, sx_ > &x, const Matrix< T_, m_, n_, sma_, sna_ > &a)
Sets this vector to the result of a vector-matrix product.
static CUDA_HOST_DEVICE Vector convert_new(const Vector< Tx_, n_, sx_ > &x)
convert function, not callable with non convertable inner type
CUDA_HOST_DEVICE Vector & add_vec_mat_mult_n(const Vector< T_, mx_, smx_ > &x, const Matrix< T_, ma_, na_, sma_, sna_ > &a, DataType alpha=DataType(1))
Adds the result of a vector-matrix product onto this vector.
CUDA_HOST_DEVICE Vector(const Vector< Tx_, n_, sx_ > &x)
copy constructor
CUDA_HOST_DEVICE void scale_n(DataType alpha)
Scales the first nn_ entries of the vector.
CUDA_HOST_DEVICE Vector & set_mat_vec_mult_n(const Matrix< T_, ma_, na_, sma_, sna_ > &a, const Vector< T_, nx_, sx_ > &x)
Sets the first nn_ entries of this vector to the result of a matrix-vector product with the first mm_...
static constexpr int n
the length of the vector
CUDA_HOST_DEVICE Vector & operator*=(DataType alpha)
scalar-multiply operator
CUDA_HOST_DEVICE Vector()
default constructor
CUDA_HOST_DEVICE DataType norm_l1() const
Computes the l1-norm of this vector.
CUDA_HOST_DEVICE Vector & set_convex(DataType alpha, const Vector< T_, n_, sna_ > &a, const Vector< T_, n_, snb_ > &b)
Sets this vector to the convex combination of two other vectors.
CUDA_HOST_DEVICE Vector & axpy_n(DataType alpha, const Vector< T_, nx_, snx_ > &x)
Adds the first nn_ entries of another scaled vector onto this vector.
CUDA_HOST_DEVICE Vector & add_mat_vec_mult_n(const Matrix< T_, ma_, na_, sma_, sna_ > &a, const Vector< T_, nx_, sx_ > &x, DataType alpha=DataType(1))
Adds the result of a matrix-vector product onto this vector.
CUDA_HOST friend std::ostream & operator<<(std::ostream &lhs, const Vector &b)
Tiny::Vector streaming operator.
CUDA_HOST_DEVICE Vector & operator+=(const Vector< T_, n_, sx_ > &x)
vector-add operator
CUDA_HOST_DEVICE Vector & operator=(DataType value)
value-assignment operator
CUDA_HOST_DEVICE Vector & operator=(const Vector< Tx_, n_, sx_ > &x)
copy-assignment operator
CUDA_HOST_DEVICE const T_ & operator()(int i) const
Access operator.
CUDA_HOST_DEVICE Vector & axpy(DataType alpha, const Vector< T_, n_, snx_ > &x)
Adds another scaled vector onto this vector.
CUDA_HOST_DEVICE void copy(const Vector< T_, n_, snx_ > &x)
Copies a vector.
CUDA_HOST_DEVICE Vector(const std::array< Tx_, n_ > &x)
copy constructor
CUDA_HOST_DEVICE Vector & normalize()
Normalizes this vector.
CUDA_HOST_DEVICE Vector & operator=(const std::array< Tx_, n_ > &x)
copy-assignment operator
CUDA_HOST_DEVICE Vector & negate()
Negates the vector, i.e. effectively multiplies all components by -1.
Intern::DataTypeExtractor< ValueType >::MyDataType DataType
The basic data type buried in the lowest level of the vector.
CUDA_HOST_DEVICE Vector(DataType value)
value-assignment constructor
CUDA_HOST_DEVICE DataType norm_max_n() const
Computes the max-norm of the vector.
CUDA_HOST_DEVICE Vector & negate_n()
Negates the first nn_ entries of this vector.
static Dist::Typemap typemap()
Returns a Typemap for sending vectors via MPI.
CUDA_HOST_DEVICE DataType norm_l1_n() const
Computes the l1-norm of the first nn_ entries of this vector.
CUDA_HOST_DEVICE Vector & set_convex_n(DataType alpha, const Vector< T_, na_, sna_ > &a, const Vector< T_, nb_, snb_ > &b)
Sets the first nn_ entries of this vector to the convex combination of two other vectors.
CUDA_HOST_DEVICE DataType norm_euclid_sqr() const
Computes the squared euclid norm of this vector.
CUDA_HOST_DEVICE void copy_n(const Vector< T_, nx_, snx_ > &x)
Copies the first nn_ entries of a vector.
CUDA_HOST_DEVICE void scale(DataType alpha)
Scales the vector.
CUDA_HOST_DEVICE Vector(const std::initializer_list< Tx_ > &x)
Initializer list constructor.
static constexpr int s
the stride of the vector
CUDA_HOST_DEVICE Vector & set_mat_vec_mult(const Matrix< T_, n_, m_, sma_, sna_ > &a, const Vector< T_, m_, sx_ > &x)
Sets this vector to the result of a matrix-vector product.
CUDA_HOST_DEVICE Vector & add_vec_mat_mult(const Vector< T_, m_, sx_ > &x, const Matrix< T_, m_, n_, sma_, sna_ > &a, DataType alpha=DataType(1))
Adds the result of a vector-matrix product onto this vector.
CUDA_HOST_DEVICE T_ & operator[](int i)
Access operator.
T_ ValueType
the value type of the vector
CUDA_HOST_DEVICE DataType norm_euclid() const
Computes the euclid norm of this vector.
CUDA_HOST_DEVICE Vector & set_vec_mat_mult_n(const Vector< T_, mx_, smx_ > &x, const Matrix< T_, ma_, na_, sma_, sna_ > &a)
Sets the first mm_ entries of this vector to the result of a vector-matrix product with the first mm_...
CUDA_HOST_DEVICE void convert(const Vector< Tx_, n_, sx_ > &x)
conversion operator
CUDA_HOST_DEVICE Vector & operator*=(const Vector< T_, n_, sx_ > &x)
element-wise-multiply operator
T_ sqrt(T_ x)
Returns the square-root of a value.
Definition: math.hpp:300
T_ abs(T_ x)
Returns the absolute value.
Definition: math.hpp:275
DT_ calc_opening_angle(DT_ x1, DT_ x2, DT_ y1, DT_ y2)
Calculates the opening angle of two 2D vectors.
Definition: math.hpp:1450
T_ pow(T_ x, T_ y)
Returns x raised to the power of y.
Definition: math.hpp:643
T_ sin(T_ x)
Returns the sine of a value.
Definition: math.hpp:344
T_ sqr(T_ x)
Returns the square of a value.
Definition: math.hpp:95
T_ min(T_ a, T_ b)
Returns the minimum of two values.
Definition: math.hpp:123
T_ cos(T_ x)
Returns the cosine of a value.
Definition: math.hpp:386
T_ max(T_ a, T_ b)
Returns the maximum of two values.
Definition: math.hpp:137
CUDA_HOST_DEVICE Vector< T_, n_ > operator+(const Vector< T_, n_, sa_ > &a, const Vector< T_, n_, sb_ > &b)
vector addition operator
CUDA_HOST Vector< T_, dim_ > project_onto(const Vector< T_, dim_ > &x, const Vector< T_, dim_ > &y)
Calculates the projected vector.
CUDA_HOST_DEVICE T_ dot(const T_ &a, const T_ &b)
Computes the dot-product of two scalars.
CUDA_HOST_DEVICE void orthogonal_3x2(Vector< T_, mx_, smx_ > &nu, const Matrix< T_, ma_, na_, sm_, sn_ > &tau)
Computes the positively oriented orthogonal vector to the columns of a 3x2 matrix.
CUDA_HOST_DEVICE void axpy(T_ &y, const T_ &x, const T_ &alpha)
Performs an AXPY of two scalars.
CUDA_HOST_DEVICE void orthogonal_2x1(Vector< T_, mx_, smx_ > &nu, const Matrix< T_, ma_, na_, sm_, sn_ > &tau)
Computes the positively oriented orthogonal vector to the columns of a 2x1 matrix.
CUDA_HOST_DEVICE void add_id(T_ &x, const T_ &alpha)
Adds a scaled identity onto a scalar.
Vector< T_, m_ > orthogonal(const Matrix< T_, m_, m_-1, sm_, sn_ > &tau)
Computes the positively oriented orthogonal vector to the columns of a m_ x (m_-1) Matrix.
CUDA_HOST_DEVICE Vector< T_, n_ > operator-(const Vector< T_, n_, sa_ > &a, const Vector< T_, n_, sb_ > &b)
vector subtraction operator
CUDA_HOST_DEVICE Vector< T_, n_ > component_product(const Vector< T_, n_, sa_ > &a, const Vector< T_, n_, sb_ > &b)
vector element-wise-product operator
CUDA_HOST_DEVICE Vector< T_, n_ > operator*(typename Vector< T_, n_ >::DataType alpha, const Vector< T_, n_, s_ > &x)
scalar-left-multiplication operator
CUDA_HOST T_ calculate_opening_angle(const Vector< T_, 2 > &x, const Vector< T_, 2 > &y)
Calculates the counter-clockwise opening angle between two 2D vectors.
FEAT namespace.
Definition: adjactor.hpp:12
String stringify(const T_ &item)
Converts an item into a String.
Definition: string.hpp:993
@ value
specifies whether the space should supply basis function values