Itasca C++ Interface
Loading...
Searching...
No Matches
matrix.h
Go to the documentation of this file.
1#pragma once
8#ifdef __LINUX
9#include <string.h>
10#endif
11
12#include "symtensor.h"
13#include <cassert>
14
21template <class T,unsigned SX,unsigned SY=SX>
22class Matrix {
23private:
25 class Column {
26 public:
28 constexpr Column(unsigned x,Matrix<T,SX,SY> &m) : x_(x), m_(m) { }
30 constexpr Column(const Column &r) : x_(r.x_), m_(r.m_) { }
32 constexpr const T &operator[](unsigned y) const { return m_.get(x_,y); }
34 T &operator[](unsigned y) { return m_.get(x_,y); }
35 private:
36 void operator=(const Column &r)=delete;
37 unsigned x_;
39 };
40 // Using template-meta programming to unroll innermost loop of matrix multiply
41 template <unsigned SZ,unsigned I,unsigned J,unsigned K>
42 class innerLoop {
43 public:
44 static constexpr double execute(const Matrix<T,SX,SY> &l,const Matrix<T,SY,SZ> &m) {
45 return l.t_[SX-I][SY-K]*m.t_[SY-K][SZ-J] + innerLoop<SZ,I,J,K-1>::execute(l,m);
46 }
47 };
48 template <unsigned SZ,unsigned I,unsigned J> // Termination specialization
49 class innerLoop<SZ,I,J,1> {
50 public:
51 static constexpr double execute(const Matrix<T,SX,SY> &l,const Matrix<T,SY,SZ> &m) {
52 return l.t_[SX-I][SY-1]*m.t_[SY-1][SZ-J];
53 }
54 };
55 template <unsigned I,unsigned K> // Specialization for column matrix (SZ=1)
56 class innerLoop<1,I,1,K> {
57 public:
58 static constexpr double execute(const Matrix<T,SX,SY> &l,const Matrix<T,SY,1> &m) {
59 return l.t_[SX-I][SY-K]*m.t_[SY-K] + innerLoop<1,I,1,K-1>::execute(l,m);
60 }
61 };
62 template <unsigned I> // termination specialization for column matrix specialization
63 class innerLoop<1,I,1,1> {
64 public:
65 static constexpr double execute(const Matrix<T,SX,SY> &l,const Matrix<T,SY,1> &m) {
66 return l.t_[SX-I][SY-1]*m.t_[SY-1];
67 }
68 };
69 // Using template meta-programming to unroll second loop of matrix multiply.
70 template <unsigned SZ,unsigned I,unsigned J>
71 class loop2Multiply {
72 public:
73 static constexpr void execute(const Matrix<T,SX,SY> &l,const Matrix<T,SY,SZ> &m,Matrix<T,SX,SZ> &ret) {
74 ret.t_[SX-I][SZ-J] = innerLoop<SZ,I,J,SY>::execute(l,m);
75 loop2Multiply<SZ,I,J-1>::execute(l,m,ret);
76 }
77 };
78 template <unsigned SZ,unsigned I> // Termination specialization
79 class loop2Multiply<SZ,I,1> {
80 public:
81 static constexpr void execute(const Matrix<T,SX,SY> &l,const Matrix<T,SY,SZ> &m,Matrix<T,SX,SZ> &ret) {
82 ret.t_[SX-I][SZ-1] = innerLoop<SZ,I,1,SY>::execute(l,m);
83 }
84 };
85 template <unsigned I> // Column matrix specialization (SZ=1)
86 class loop2Multiply<1,I,1> {
87 public:
88 static constexpr void execute(const Matrix<T,SX,SY> &l,const Matrix<T,SY,1> &m,Matrix<T,SX,1> &ret) {
89 ret.t_[SX-I] = innerLoop<1,I,1,SY>::execute(l,m);
90 }
91 };
92 // Using template meta-programming to unroll outermost loop of matrix multiply.
93 template <unsigned SZ,unsigned I>
94 class loopMultiply {
95 public:
96 static constexpr void execute(const Matrix<T,SX,SY> &l,const Matrix<T,SY,SZ> &m,Matrix<T,SX,SZ> &ret) {
97 loop2Multiply<SZ,I,SZ>::execute(l,m,ret);
98 loopMultiply<SZ,I-1>::execute(l,m,ret);
99 }
100 };
101 template <unsigned SZ> // Termination specialization
102 class loopMultiply<SZ,1> {
103 public:
104 static constexpr void execute(const Matrix<T,SX,SY> &l,const Matrix<T,SY,SZ> &m,Matrix<T,SX,SZ> &ret) {
105 loop2Multiply<SZ,1,SZ>::execute(l,m,ret);
106 }
107 };
108
109public:
111 constexpr Matrix() { set(T{}); }
112 constexpr Matrix(const Matrix<T,SX,SY> &m) = default;
113 constexpr Matrix<T,SX,SY> &operator=(const Matrix<T,SX,SY> &)=default;
114
116 explicit constexpr Matrix(const T &t) { set(t); }
117 explicit constexpr Matrix(const std::array<std::array<T,SY>,SX> &a) { for (unsigned i=0;i<SX;++i) for (unsigned j=0;j<SY;++j) t_[i][j] = a[i][j]; }
118 constexpr Matrix(const std::initializer_list<std::initializer_list<T>> l);
119
121 constexpr const T & get(unsigned x,unsigned y) const { assert(x<SX); assert(y<SY); return t_[x][y]; }
123 constexpr T & get(unsigned x,unsigned y) { assert(x<SX); assert(y<SY); return t_[x][y]; }
125 constexpr const Column operator[](unsigned y) const { return Column(y,const_cast<Matrix<T,SX,SY> &>(*this)); }
127 constexpr Column operator[](unsigned y) { return Column(y,*this); }
129 constexpr const T & operator()(unsigned x,unsigned y) const { return get(x,y); }
131 constexpr T & operator()(unsigned x,unsigned y) { return get(x,y); }
132 constexpr UVect2 size() const { return {SX,SY}; }
133
135 constexpr const Matrix<T,SX,SY> &operator+=(const Matrix<T,SX,SY> &m) { for (unsigned i=0;i<SX;++i) for (unsigned j=0;j<SY;++j) t_[i][j] += m.t_[i][j]; return *this; }
137 constexpr const Matrix<T,SX,SY> &operator-=(const Matrix<T,SX,SY> &m) { for (unsigned i=0;i<SX;++i) for (unsigned j=0;j<SY;++j) t_[i][j] -= m.t_[i][j]; return *this; }
139 constexpr const Matrix<T,SX,SY> &operator*=(const T &t) { for (unsigned i=0;i<SX;++i) for (unsigned j=0;j<SY;++j) t_[i][j] *= t; return *this; }
141 constexpr const Matrix<T,SX,SY> &operator/=(const T &t) { for (unsigned i=0;i<SX;++i) for (unsigned j=0;j<SY;++j) t_[i][j] /= t; return *this; }
142
144 Matrix<T,SX,SY> operator+(const Matrix<T,SX,SY> &m) const { Matrix<T,SX,SY> ret; for (unsigned i=0;i<SX;++i) for (unsigned j=0;j<SY;++j) ret.t_[i][j] = t_[i][j] + m.t_[i][j]; return ret; }
146 Matrix<T,SX,SY> operator-(const Matrix<T,SX,SY> &m) const { Matrix<T,SX,SY> ret; for (unsigned i=0;i<SX;++i) for (unsigned j=0;j<SY;++j) ret.t_[i][j] = t_[i][j] - m.t_[i][j]; return ret; }
148 Matrix<T,SX,SY> operator*(const T &t) const { Matrix<T,SX,SY> ret; for (unsigned i=0;i<SX;++i) for (unsigned j=0;j<SY;++j) ret.t_[i][j] = t_[i][j] * t; return ret; }
150 Matrix<T,SX,SY> operator/(const T &t) const { Matrix<T,SX,SY> ret; for (unsigned i=0;i<SX;++i) for (unsigned j=0;j<SY;++j) ret.t_[i][j] = t_[i][j] / t; return ret; }
152 template <unsigned SZ>
153 constexpr Matrix<T,SX,SZ> operator*(const Matrix<T,SY,SZ> &m) const {
154 Matrix<T,SX,SZ> ret;
155 // OK - we are using template expansion to do loop unrolling at compile time. Fun!
156 loopMultiply<SZ,SX>::execute(*this,m,ret);
157 return ret;
158 }
159
160 // Block operations
162 template <unsigned BX,unsigned BY,unsigned SX2,unsigned SY2>
163 void addBlock(const Matrix<T,SX2,SY2> &m,unsigned iSrc,unsigned jSrc,unsigned iDst,unsigned jDst) {
164 addGenBlock<BX,BY,SX2,SY2>(m,iSrc*BX,jSrc*BY,iDst*BX,jDst*BY);
165 }
167 template <unsigned BX,unsigned BY,unsigned SX2,unsigned SY2>
168 void addGenBlock(const Matrix<T,SX2,SY2> &m,unsigned iSrc,unsigned jSrc,unsigned iDst,unsigned jDst) {
169 for (unsigned i=iSrc,id=iDst;i<(iSrc+BX);i++,id++)
170 for (unsigned j=jSrc,jd=jDst;j<(jSrc+BY);j++,jd++)
171 get(id,jd) += m(i,j);
172 }
173
175 T maxNorm() const { T ret(0); for (unsigned i=0;i<SX;++i) for (unsigned j=0;j<SY;++j) ret = std::max(ret,std::abs(t_[i][j])); return ret; }
177 Matrix<T,SY,SX> transpose() const { Matrix<T,SY,SX> ret; for (unsigned i=0;i<SX;++i) for (unsigned j=0;j<SY;++j) ret(j,i) = t_[i][j]; return ret; }
179 T trace() const { T tt = t_[0][0]; unsigned len = std::min(SX,SY); for (unsigned i=1; i<len; ++i) tt += t_[i][i]; return tt; }
181 constexpr void set(const T &t=T{}) { std::fill(&t_[0][0], &t_[0][0]+(SX*SY),t); }
182 constexpr T minEntry() const { double ret=limits<T>::max(); for (unsigned i=0;i<SX;++i) for (unsigned j=0;j<SY;++j) ret=std::min(ret,t_[i][j]); return ret; }
183 constexpr T maxEntry() const { double ret=limits<T>::lowest(); for (unsigned i=0;i<SX;++i) for (unsigned j=0;j<SY;++j) ret=std::max(ret,t_[i][j]); return ret; }
184
186 static constexpr Matrix<T,SX,SY> identity() { Matrix<T,SX,SY> ret(0.0); for (unsigned i=0;i<std::min(SX,SY);++i) ret(i,i) = 1.0; return ret; }
187
188 T t_[SX][SY];
189};
190
191template <class T,unsigned SX,unsigned SY>
192constexpr Matrix<T,SX,SY>::Matrix(const std::initializer_list<std::initializer_list<T>> l) {
193 uint32 i=0;
194 for (auto &y : l) {
195 uint32 j = 0;
196 for (auto &x : y) {
197 t_[i][j] = x;
198 ++j;
199 }
200 ++i;
201 }
202}
203
204
206template <class T,unsigned SX>
207class Matrix<T,SX,1> {
208public:
210 constexpr Matrix() { set(T{}); }
211 constexpr Matrix(const Matrix<T,SX,1> &)=default;
212 constexpr Matrix<T,SX,1> &operator=(const Matrix<T,SX,1> &)=default;
213
215 constexpr explicit Matrix(const T &t) { set(t); }
216 constexpr Matrix(const std::array<T,SX> &a) { for (uint32 i=0;i<SX;++i) t_[i] = a[i]; }
217 constexpr Matrix(const std::initializer_list<T> l) { uint32 i=0; for (auto &x : l) { t_[i] = x; ++i; } }
218
220 constexpr const T & get(unsigned x,[[maybe_unused]] unsigned y) const { assert(x<SX); assert(!y); return t_[x]; }
222 constexpr const T & get(unsigned x) const { assert(x<SX); return t_[x]; }
224 constexpr T & get(unsigned x,[[maybe_unused]] unsigned y) { assert(x<SX); assert(!y); return t_[x]; }
226 constexpr T & get(unsigned x) { assert(x<SX); return t_[x]; }
228 constexpr const T & operator()(unsigned x,unsigned y) const { return get(x,y); }
230 constexpr const T & operator()(unsigned x) const { return get(x); }
231 constexpr const T & operator[](unsigned x) const { return get(x); }
233 constexpr T & operator()(unsigned x,unsigned y) { return get(x,y); }
235 constexpr T & operator()(unsigned x) { return get(x); }
236 constexpr T & operator[](unsigned x) { return get(x); }
237 constexpr UVect2 size() const { return {SX,1}; }
238
240 constexpr const Matrix<T,SX,1> &operator+=(const Matrix<T,SX,1> &m) { for (unsigned i=0;i<SX;++i) t_[i] += m.t_[i]; return *this; }
242 constexpr const Matrix<T,SX,1> &operator-=(const Matrix<T,SX,1> &m) { for (unsigned i=0;i<SX;++i) t_[i] -= m.t_[i]; return *this; }
244 constexpr const Matrix<T,SX,1> &operator*=(const T &t) { for (unsigned i=0;i<SX;++i) t_[i] *= t; return *this; }
246 constexpr const Matrix<T,SX,1> &operator/=(const T &t) { for (unsigned i=0;i<SX;++i) t_[i] /= t; return *this; }
247
249 Matrix<T,SX,1> operator+(const Matrix<T,SX,1> &m) const { Matrix<T,SX,1> ret; for (unsigned i=0;i<SX;++i) ret.t_[i] = t_[i] + m.t_[i]; return ret; }
251 Matrix<T,SX,1> operator-(const Matrix<T,SX,1> &m) const { Matrix<T,SX,1> ret; for (unsigned i=0;i<SX;++i) ret.t_[i] = t_[i] - m.t_[i]; return ret; }
253 Matrix<T,SX,1> operator*(const T &t) const { Matrix<T,SX,1> ret; for (unsigned i=0;i<SX;++i) ret.t_[i] = t_[i] * t; return ret; }
255 Matrix<T,SX,1> operator/(const T &t) const { Matrix<T,SX,1> ret; for (unsigned i=0;i<SX;++i) ret.t_[i] = t_[i] / t; return ret; }
257 template <unsigned SZ> Matrix<T,SX,SZ> operator*(const Matrix<T,1,SZ> &m) const {
258 Matrix<T,SX,SZ> ret;
259 for (unsigned i=0;i<SX;++i) // ROWS OF FIRST
260 for (unsigned j=0;j<SZ;++j) // COLUMNS OF SECOND
261 ret.get(i,j) = get(i,0)*m.get(0,j);
262 return ret;
263 }
264
265 // Block operations
267 template <unsigned BX,unsigned BY,unsigned SX2,unsigned SY2> void addBlock(const Matrix<T,SX2,SY2> &m,unsigned iSrc,unsigned jSrc,unsigned iDst,unsigned jDst) {
268 addGenBlock<BX,BY,SX2,SY2>(m,iSrc*BX,jSrc*BY,iDst*BX,jDst*BY);
269 }
271 template <unsigned BX,unsigned SX2> void addBlock(const Matrix<T,SX2,1> &m,unsigned iSrc,unsigned iDst) {
272 addGenBlock<BX,SX2>(m,iSrc*BX,iDst*BX);
273 }
275 template <unsigned BX,unsigned BY,unsigned SX2,unsigned SY2> void addGenBlock(const Matrix<T,SX2,SY2> &m,unsigned iSrc,unsigned jSrc,unsigned iDst,unsigned jDst) {
276 for (unsigned i=iSrc,id=iDst;i<(iSrc+BX);i++,id++)
277 for (unsigned j=jSrc,jd=jDst;j<(jSrc+BY);j++,jd++)
278 get(id,jd) += m(i,j);
279 }
281 template <unsigned BX,unsigned SX2> void addGenBlock(const Matrix<T,SX2,1> &m,unsigned iSrc,unsigned iDst) {
282 for (unsigned i=iSrc,id=iDst;i<(iSrc+BX);i++,id++)
283 get(id) += m(i);
284 }
285
287 T maxNorm() const { T ret(0); for (unsigned i=0;i<SX;++i) ret = std::max(ret,std::abs(t_[i])); return ret; }
289 Matrix<T,1,SX> transpose() const { Matrix<T,1,SX> ret; for (unsigned i=0;i<SX;++i) ret(0,i) = t_[i]; return ret; }
291 constexpr void set(const T &t=T{}) { std::fill(&t_[0],&t_[0]+SX,t); }
292 constexpr T minEntry() const { double ret=limits<T>::max(); for (unsigned i=0;i<SX;++i) ret=std::min(ret,t_[i]); return ret; }
293 constexpr T maxEntry() const { double ret=limits<T>::lowest(); for (unsigned i=0;i<SX;++i) ret=std::max(ret,t_[i]); return ret; }
294
296 static Matrix<T,SX,1> identity() { Matrix<T,SX,1> ret(0.0); ret.t_[0] = 1.0; return ret; }
297 T t_[SX];
298};
299
304template <class T,unsigned SX>
306private:
308 class Column {
309 public:
311 Column(unsigned x,SymMatrix<T,SX> &m) : x_(x), m_(m) { }
313 Column(const Column &r) : x_(r.x_), m_(r.m_) { }
315 const T &operator[](unsigned y) const { return m_.get(x_,y); }
317 T &operator[](unsigned y) { return m_.get(x_,y); }
318 private:
319 void operator=(const Column &r); // DISALLOWED
320 unsigned x_;
321 SymMatrix<T,SX> &m_;
322 };
323 // Using template-meta programming to unroll innermost loop of matrix multiply
324 template <unsigned SZ,unsigned I,unsigned J,unsigned K>
325 class innerLoop {
326 public:
327 static constexpr double execute(const SymMatrix<T,SX> &l,const Matrix<T,SX,SZ> &m) {
328 return l.t_[index(SX-I,SX-K)]*m.t_[SX-K][SZ-J] + innerLoop<SZ,I,J,K-1>::execute(l,m);
329 }
330 };
331 template <unsigned SZ,unsigned I,unsigned J> // Termination specialization
332 class innerLoop<SZ,I,J,1> {
333 public:
334 static constexpr double execute(const SymMatrix<T,SX> &l,const Matrix<T,SX,SZ> &m) {
335 return l.t_[index(SX-I,SX-1)]*m.t_[SX-1][SZ-J];
336 }
337 };
338 template <unsigned I,unsigned K> // Specialization for column matrix (SZ=1)
339 class innerLoop<1,I,1,K> {
340 public:
341 static constexpr double execute(const SymMatrix<T,SX> &l,const Matrix<T,SX,1> &m) {
342 return l.t_[index(SX-I,SX-K)]*m.t_[SX-K] + innerLoop<1,I,1,K-1>::execute(l,m);
343 }
344 };
345 template <unsigned I> // termination specialization for column matrix specialization
346 class innerLoop<1,I,1,1> {
347 public:
348 static constexpr double execute(const SymMatrix<T,SX> &l,const Matrix<T,SX,1> &m) {
349 return l.t_[index(SX-I,SX-1)]*m.t_[SX-1];
350 }
351 };
352 // Using template meta-programming to unroll second loop of matrix multiply.
353 template <unsigned SZ,unsigned I,unsigned J>
354 class loop2Multiply {
355 public:
356 static constexpr void execute(const SymMatrix<T,SX> &l,const Matrix<T,SX,SZ> &m,Matrix<T,SX,SZ> &ret) {
357 ret.t_[SX-I][SZ-J] = innerLoop<SZ,I,J,SX>::execute(l,m);
358 loop2Multiply<SZ,I,J-1>::execute(l,m,ret);
359 }
360 };
361 template <unsigned SZ,unsigned I> // Termination specialization
362 class loop2Multiply<SZ,I,1> {
363 public:
364 static constexpr void execute(const SymMatrix<T,SX> &l,const Matrix<T,SX,SZ> &m,Matrix<T,SX,SZ> &ret) {
365 ret.t_[SX-I][SZ-1] = innerLoop<SZ,I,1,SX>::execute(l,m);
366 }
367 };
368 template <unsigned I> // Column matrix specialization (SZ=1)
369 class loop2Multiply<1,I,1> {
370 public:
371 static constexpr void execute(const SymMatrix<T,SX> &l,const Matrix<T,SX,1> &m,Matrix<T,SX,1> &ret) {
372 ret.t_[SX-I] = innerLoop<1,I,1,SX>::execute(l,m);
373 }
374 };
375 // Using template meta-programming to unroll outermost loop of matrix multiply.
376 template <unsigned SZ,unsigned I>
377 class loopMultiply {
378 public:
379 static constexpr void execute(const SymMatrix<T,SX> &l,const Matrix<T,SX,SZ> &m,Matrix<T,SX,SZ> &ret) {
380 loop2Multiply<SZ,I,SZ>::execute(l,m,ret);
381 loopMultiply<SZ,I-1>::execute(l,m,ret);
382 }
383 };
384 template <unsigned SZ> // Termination specialization
385 class loopMultiply<SZ,1> {
386 public:
387 static constexpr void execute(const SymMatrix<T,SX> &l,const Matrix<T,SX,SZ> &m,Matrix<T,SX,SZ> &ret) {
388 loop2Multiply<SZ,1,SZ>::execute(l,m,ret);
389 }
390 };
391
392
393 // Using template-meta programming to unroll innermost loop of matrix multiply
394 template <unsigned I,unsigned J,unsigned K>
395 class innerLoopS {
396 public:
397 static constexpr double execute(const SymMatrix<T,SX> &l,const SymMatrix<T,SX> &m) {
398 return l.t_[index(SX-I,SX-K)]*m.t_[index(SX-K,SX-J)] + innerLoopS<I,J,K-1>::execute(l,m);
399 }
400 };
401
402 template <unsigned I,unsigned J> // Termination specialization
403 class innerLoopS<I,J,1> {
404 public:
405 static constexpr double execute(const SymMatrix<T,SX> &l,const SymMatrix<T,SX> &m) {
406 return l.t_[index(SX-I,SX-1)]*m.t_[index(SX-1,SX-J)];
407 // what is X here? seems undefined??
408 }
409 };
410
411 // Using template meta-programming to unroll second loop of matrix multiply.
412 template <unsigned I,unsigned J>
413 class loop2MultiplyS {
414 public:
415 static constexpr void execute(const SymMatrix<T,SX> &l,const SymMatrix<T,SX> &m,Matrix<T,SX,SX> &ret) {
416 ret.t_[SX-I][SX-J] = innerLoopS<I,J,SX>::execute(l,m);
417 loop2MultiplyS<I,J-1>::execute(l,m,ret);
418 }
419 };
420 template <unsigned I> // Termination specialization
421 class loop2MultiplyS<I,1> {
422 public:
423 static constexpr void execute(const SymMatrix<T,SX> &l,const SymMatrix<T,SX> &m,Matrix<T,SX,SX> &ret) {
424 ret.t_[SX-I][SX-1] = innerLoopS<I,1,SX>::execute(l,m);
425 }
426 };
427 // Using template meta-programming to unroll outermost loop of matrix multiply.
428 template <unsigned I>
429 class loopMultiplyS {
430 public:
431 static constexpr void execute(const SymMatrix<T,SX> &l,const SymMatrix<T,SX> &m,Matrix<T,SX,SX> &ret) {
432 loop2MultiplyS<I,SX>::execute(l,m,ret);
433 loopMultiplyS<I-1>::execute(l,m,ret);
434 }
435 };
436#ifndef __LINUX
437 template <> // Termination specialization
438 class loopMultiplyS<1> {
439 public:
440 static constexpr void execute(const SymMatrix<T,SX> &l,const SymMatrix<T,SX> &m,Matrix<T,SX,SX> &ret) {
441 loop2MultiplyS<1,SX>::execute(l,m,ret);
442 }
443 };
444#endif
445
446public:
447 constexpr SymMatrix() { set(T{}); }
448 constexpr SymMatrix(const SymMatrix<T,SX> &) = default;
449 constexpr SymMatrix<T,SX> &operator=(const SymMatrix<T,SX> &) = default;
450
452 explicit SymMatrix(const T &t) { set(t); }
453
455 const T & get(unsigned x,unsigned y) const { return t_[index(x,y)]; }
457 T & get(unsigned x,unsigned y) { return t_[index(x,y)]; }
459 const Column operator[](unsigned y) const { return Column(y,*this); }
461 Column operator[](unsigned y) { return Column(y,*this); }
463 const T & operator()(unsigned x,unsigned y) const { return get(x,y); }
465 T & operator()(unsigned x,unsigned y) { return get(x,y); }
466 constexpr UVect2 size() const { return {SX,SX}; }
467
469 const SymMatrix<T,SX> &operator+=(const SymMatrix<T,SX> &m) { for (unsigned i=0;i<len_;++i) t_[i] += m.t_[i]; return *this; }
471 const SymMatrix<T,SX> &operator-=(const SymMatrix<T,SX> &m) { for (unsigned i=0;i<len_;++i) t_[i] -= m.t_[i]; return *this; }
473 const SymMatrix<T,SX> &operator*=(const T &t) { for (unsigned i=0;i<len_;++i) t_[i] *= t; return *this; }
475 const SymMatrix<T,SX> &operator/=(const T &t) { for (unsigned i=0;i<len_;++i) t_[i] /= t; return *this; }
476
478 SymMatrix<T,SX> operator+(const SymMatrix<T,SX> &m) const { SymMatrix<T,SX> ret; for (unsigned i=0;i<len_;++i) ret.t_[i] = t_[i] + m.t_[i]; return ret; }
480 Matrix<T,SX,SX> operator+(const Matrix<T,SX,SX> &m) const { Matrix<T,SX,SX> ret; for (unsigned i=0;i<SX;++i) for (unsigned j=0;j<SX;++j) ret(i,j) = get(i,j) + m.get(i,j); return ret; }
482 SymMatrix<T,SX> operator-(const SymMatrix<T,SX> &m) const { SymMatrix<T,SX> ret; for (unsigned i=0;i<len_;++i) ret.t_[i] = t_[i] - m.t_[i]; return ret; }
484 Matrix<T, SX, SX> operator-(const Matrix<T, SX, SX>& m) const { Matrix<T, SX, SX> ret; for (unsigned i = 0; i < SX; ++i) for (unsigned j = 0; j < SX; ++j) ret(i, j) = get(i, j) - m.get(i, j); return ret; }
485
487 SymMatrix<T,SX> operator*(const T &t) const { SymMatrix<T,SX> ret; for (unsigned i=0;i<len_;++i) ret.t_[i] = t_[i] * t; return ret; }
489 SymMatrix<T,SX> operator/(const T &t) const { SymMatrix<T,SX> ret; for (unsigned i=0;i<len_;++i) ret.t_[i] = t_[i] / t; return ret; }
491 constexpr Matrix<T,SX,SX> operator*(const SymMatrix<T,SX> &m) const {
492 Matrix<T,SX,SX> ret;
493 loopMultiplyS<SX>::execute(*this,m,ret);
494 return ret;
495 }
497 template <unsigned SZ> constexpr Matrix<T,SX,SZ> operator*(const Matrix<T,SX,SZ> &m) const {
498 Matrix<T,SX,SZ> ret;
499 loopMultiply<SZ,SX>::execute(*this,m,ret);
500 return ret;
501 }
502
503 // Block operations
505 template <unsigned BX,unsigned BY,unsigned SX2,unsigned SY2> void addBlock(const Matrix<T,SX2,SY2> &m,unsigned iSrc,unsigned jSrc,unsigned iDst,unsigned jDst) {
506 addGenBlock<BX,BY,SX2,SY2>(m,iSrc*BX,jSrc*BY,iDst*BX,jDst*BY);
507 }
509 template <unsigned BX,unsigned BY,unsigned SX2> void addBlock(const SymMatrix<T,SX2> &m,unsigned iSrc,unsigned jSrc,unsigned iDst,unsigned jDst) {
510 addGenBlock<BX,BY,SX2>(m,iSrc*BX,jSrc*BY,iDst*BX,jDst*BY);
511 }
513 template <unsigned BX,unsigned BY,unsigned SX2,unsigned SY2> void addGenBlock(const Matrix<T,SX2,SY2> &m,unsigned iSrc,unsigned jSrc,unsigned iDst,unsigned jDst) {
514 for (unsigned i=iSrc,id=iDst;i<(iSrc+BX);i++,id++) {
515 for (unsigned j=jSrc,jd=jDst;j<(jSrc+BY);j++,jd++) {
516 if (id<=jd)
517 get(id,jd) += m(i,j);
518 }
519 }
520 }
522 template <unsigned BX,unsigned BY,unsigned SX2> void addGenBlock(const SymMatrix<T,SX2> &m,unsigned iSrc,unsigned jSrc,unsigned iDst,unsigned jDst) {
523 for (unsigned i=iSrc,id=iDst;i<(iSrc+BX);i++,id++) {
524 for (unsigned j=jSrc,jd=jDst;j<(jSrc+BY);j++,jd++) {
525 if (id<=jd)
526 get(id,jd) += m(i,j);
527 }
528 }
529 }
530
532 T maxNorm() const { T ret(0); for (unsigned i=0;i<len_;++i) ret = std::max(std::abs(t_[i]),ret); return ret; }
534 SymMatrix<T,SX> transpose() const { return *this; }
536 T trace() const { T tt = t_[0]; for (unsigned i=1; i<SX; ++i) tt += get(i,i); return tt; }
538 constexpr void set(const T &t=T{}) { std::fill(&t_[0],&t_[0]+len_,t); }
539 constexpr T minEntry() const { double ret=limits<T>::max(); for (unsigned i=0;i<len_;++i) ret=std::min(ret,t_[i]); return ret; }
540 constexpr T maxEntry() const { double ret=limits<T>::lowest(); for (unsigned i=0;i<len_;++i) ret=std::max(ret,t_[i]); return ret; }
542 Matrix<T,SX,SX> toMatrix() const { Matrix<T,SX,SX> ret; for (unsigned i=0;i<SX;++i) for (unsigned j=0;j<SX;++j) ret(i,j)=get(i,j); return ret; }
543
545 static SymMatrix<T,SX> identity() { SymMatrix<T,SX> ret(0.0); for (unsigned i=0;i<SX;++i) ret(i,i) = 1.0; return ret; }
548 SymMatrix<T,SX> ret;
549#ifdef _DEBUG
550 double maxdif = 0.0;
551#endif
552 for (unsigned i=0;i<SX;++i) {
553 ret(i,i) = m(i,i);
554 for (unsigned j=i+1;j<SX;++j) {
555 ret(i,j) = (m(i,j) + m(j,i)) * 0.5;
556#ifdef _DEBUG
557 maxdif = std::max(std::abs(ret(i,j) - m(i,j)),maxdif);
558#endif
559 }
560 }
561#ifdef _DEBUG
562 assert(maxdif <= ret.maxNorm()*limits<float>::epsilon());
563#endif
564 return ret;
565 }
566
567 static constexpr unsigned len_ = (SX*SX + SX) / 2;
568 static constexpr unsigned indexConst_ = 1 + 2*SX;
569 static constexpr unsigned index(unsigned x,unsigned y) {
570 unsigned x2 = x > y ? y : x;
571 unsigned y2 = x > y ? x : y;
572 unsigned i = (((indexConst_-x2)*x2)/2) + (y2-x2);
573 return i;
574 }
575
576 T t_[len_];
577};
578
579// MOO NOTE!:: There are optimizations that can be made here!
582// Using template-meta programming to unroll innermost loop of matrix multiply
583template <class T,unsigned SX,unsigned SY,unsigned I,unsigned J,unsigned K>
585public:
586 static double execute(const Matrix<T,SX,SY> &l,const SymMatrix<T,SY> &m) {
587 return l.t_[SX-I][SY-K]*m.t_[m.index(SY-K,SY-J)] + innerLoopMS<T,SX,SY,I,J,K-1>::execute(l,m);
588 }
589};
590template <class T,unsigned SX,unsigned SY,unsigned I,unsigned J> // Termination specialization
591class innerLoopMS<T,SX,SY,I,J,1> {
592public:
593 static double execute(const Matrix<T,SX,SY> &l,const SymMatrix<T,SY> &m) {
594 return l.t_[SX-I][SY-1]*m.t_[m.index(SY-1,SY-J)];
595 }
596};
597// Using template meta-programming to unroll second loop of matrix multiply.
598template <class T,unsigned SX,unsigned SY,unsigned I,unsigned J>
600public:
601 static void execute(const Matrix<T,SX,SY> &l,const SymMatrix<T,SY> &m,Matrix<T,SX,SY> &ret) {
602 ret.t_[SX-I][SY-J] = innerLoopMS<T,SX,SY,I,J,SY>::execute(l,m);
604 }
605};
606template <class T,unsigned SX,unsigned SY,unsigned I> // Termination specialization
607class loop2MultiplyMS<T,SX,SY,I,1> {
608public:
609 static void execute(const Matrix<T,SX,SY> &l,const SymMatrix<T,SY> &m,Matrix<T,SX,SY> &ret) {
610 ret.t_[SX-I][SY-1] = innerLoopMS<T,SX,SY,I,1,SY>::execute(l,m);
611 }
612};
613// Using template meta-programming to unroll outermost loop of matrix multiply.
614template <class T,unsigned SX,unsigned SY,unsigned I>
616public:
617 static void execute(const Matrix<T,SX,SY> &l,const SymMatrix<T,SY> &m,Matrix<T,SX,SY> &ret) {
620 }
621};
622template <class T,unsigned SX,unsigned SY> // Termination specialization
623class loopMultiplyMS<T,SX,SY,1> {
624public:
625 static void execute(const Matrix<T,SX,SY> &l,const SymMatrix<T,SY> &m,Matrix<T,SX,SY> &ret) {
627 }
628};
629
630template <class T,unsigned SX,unsigned SY>
631Matrix<T,SX,SY> operator*(const Matrix<T,SX,SY> &m,const SymMatrix<T,SY> &s) {
632 Matrix<T,SX,SY> ret;
634 return ret;
635}
636
642template <class T,unsigned S>
643class VMatrix : public Matrix<T,S,1> {
644public:
645 using Matrix<T,S,1>::Matrix;
646 using Matrix<T,S,1>::operator=;
647 using iterator = T *;
648 using const_iterator = const T *;
649
650 //constexpr VMatrix()=default;
651 constexpr VMatrix(const VMatrix<T,S> &)=default;
652 constexpr VMatrix(const Matrix<T,S,1> &m) : Matrix<T,S,1>(m) { }
653 constexpr VMatrix<T,S> &operator=(const VMatrix<T,S> &m)=default;
654
655 iterator begin() { return this->t_; }
656 iterator end() { return this->t_+S; }
657 const_iterator begin() const { return this->t_; }
658 const_iterator end() const { return this->t_+S; }
659
661 constexpr const T &operator[](unsigned x) const { return this->get(x); }
663 constexpr T & operator[](unsigned x) { return this->get(x); }
664};
665
667template <unsigned SX,unsigned SY=SX>
669
671template <unsigned SX>
673
675template <unsigned S>
677
678template <class T,unsigned S>
679double innerProduct(const VMatrix<T,S> &v1,const VMatrix<T,S> &v2) {
680 double d=0.0;
681 for (uint32 i=0;i<S;++i)
682 d += v1(i)*v2(i);
683 return d;
684}
685
686template <class T>
687double innerProduct(const VMatrix<T,3> &v1,const DVect3 &v2) {
688 double d=0.0;
689 for (unsigned i=0;i<3;++i)
690 d += v1(i)*v2[i];
691 return d;
692}
693
694template <class T>
695double innerProduct(const VMatrix<T,2> &v1,const DVect2 &v2) {
696 double d=0.0;
697 for (unsigned i=0;i<2;++i)
698 d += v1(i)*v2[i];
699 return d;
700}
701
702template <class T,unsigned S>
703Matrix<T,S,S> outerProduct(const VMatrix<T,S> &v1,const VMatrix<T,S> &v2) {
704 Matrix<T,S,S> ret;
705 for (uint32 i=0;i<S;++i) {
706 for (uint32 j=0;j<S;++j)
707 ret(i,j) = v1(i) * v2(j);
708 }
709 return ret;
710}
711
715template <class T>
717 Matrix<T,3,3> ret;
718 ret.get(0,0) = v1.x() * v2.x();
719 ret.get(0,1) = v1.x() * v2.y();
720 ret.get(0,2) = v1.x() * v2.z();
721 ret.get(1,0) = v1.y() * v2.x();
722 ret.get(1,1) = v1.y() * v2.y();
723 ret.get(1,2) = v1.y() * v2.z();
724 ret.get(2,0) = v1.z() * v2.x();
725 ret.get(2,1) = v1.z() * v2.y();
726 ret.get(2,2) = v1.z() * v2.z();
727 return ret;
728}
729
733template <class T>
735 Matrix<T,2,2> ret;
736 ret.get(0,0) = v1.x() * v2.x();
737 ret.get(0,1) = v1.x() * v2.y();
738 ret.get(1,0) = v1.y() * v2.x();
739 ret.get(1,1) = v1.y() * v2.y();
740 return ret;
741}
742
747template <class T>
748constexpr T determinant(const Matrix<T,3,3> &mat) {
749 T t = mat.get(0,0) * (mat.get(1,1) * mat.get(2,2) - mat.get(2,1) * mat.get(1,2));
750 t -= mat.get(1,0) * (mat.get(0,1) * mat.get(2,2) - mat.get(2,1) * mat.get(0,2));
751 t += mat.get(2,0) * (mat.get(0,1) * mat.get(1,2) - mat.get(1,1) * mat.get(0,2));
752 return t;
753}
754
758template <class T>
759constexpr T determinant(const Matrix<T,2,2> &mat) {
760 return mat.get(0,0) * mat.get(1,1) - mat.get(0,1) * mat.get(1,0);
761}
762
767template <class T>
768constexpr Matrix<T,3,3> inverse(const Matrix<T,3,3> &mat) {
769 // First calculate the adjoint
770 const T a11 = mat(0,0), a21 = mat(1,0), a31 = mat(2,0);
771 const T a12 = mat(0,1), a22 = mat(1,1), a32 = mat(2,1);
772 const T a13 = mat(0,2), a23 = mat(1,2), a33 = mat(2,2);
773
774 Matrix<T,3,3> rm = {{ a22*a33 - a32*a23,-a12*a33 + a32*a13, a12*a23 - a22*a13},
775 {-a21*a33 + a31*a23, a11*a33 - a31*a13,-a11*a23 + a21*a13},
776 { a21*a32 - a31*a22,-a11*a32 + a31*a12, a11*a22 - a21*a12}};
777 return rm / determinant(mat);
778}
779
784template <class T>
785constexpr Matrix<T,2,2> inverse(const Matrix<T,2,2> &mat) {
786 Matrix<T,2,2> rm;
787 rm.get(0,0) = mat.get(1,1);
788 rm.get(1,1) = mat.get(0,0);
789 rm.get(0,1) = mat.get(0,1)*(-1);
790 rm.get(1,0) = mat.get(1,0)*(-1);
791 //Divide by the determinant
792 return rm / determinant(mat);
793}
794
795// Returns both inverse and determinant (Before inverse).
796template <class T>
797constexpr std::tuple<Matrix<T,3,3>,double> inverseDet(const Matrix<T,3,3> &mat) {
798 // First calculate the adjoint
799 const T &a11 = mat(0,0), a21 = mat(1,0), a31 = mat(2,0);
800 const T &a12 = mat(0,1), a22 = mat(1,1), a32 = mat(2,1);
801 const T &a13 = mat(0,2), a23 = mat(1,2), a33 = mat(2,2);
802
803 Matrix<T,3,3> rm;
804 rm.get(0,0) = a22*a33 - a32*a23;
805 rm.get(1,0) = -a21*a33 + a31*a23;
806 rm.get(2,0) = a21*a32 - a31*a22;
807 rm.get(0,1) = -a12*a33 + a32*a13;
808 rm.get(1,1) = a11*a33 - a31*a13;
809 rm.get(2,1) = -a11*a32 + a31*a12;
810 rm.get(0,2) = a12*a23 - a22*a13;
811 rm.get(1,2) = -a11*a23 + a21*a13;
812 rm.get(2,2) = a11*a22 - a21*a12;
813 // Divide by the determinant
814 double det = determinant(mat);
815 return {rm / det,det};
816}
817
820template <class T>
821constexpr VMatrix<T,2> toMatrix(const Vector2<T> &v) {
822 VMatrix<T,2> ret;
823 ret.get(0) = v.x();
824 ret.get(1) = v.y();
825 return ret;
826}
827
830template <class T>
831constexpr VMatrix<T,3> toMatrix(const Vector3<T> &v) {
832 VMatrix<T,3> ret;
833 ret.get(0) = v.x();
834 ret.get(1) = v.y();
835 ret.get(2) = v.z();
836 return ret;
837}
838
840template <class T,unsigned SX>
841constexpr VMatrix<T,SX> toVMatrix(const Vector2<T> &v,unsigned start=0) {
842 VMatrix<T,SX> ret(0.0);
843 ret.get(start) = v.x();
844 ret.get(start+1) = v.y();
845 return ret;
846}
847
850template <unsigned SX> constexpr DVMatrix<SX> toDVMatrix(const DVect2 &v,unsigned start=0) { return toVMatrix<double,SX>(v,start); }
851
853template <class T,unsigned SX>
854constexpr VMatrix<T,SX> toVMatrix(const Vector3<T> &v,unsigned start=0) {
855 VMatrix<T,SX> ret(0.0);
856 ret.get(start) = v.x();
857 ret.get(start+1) = v.y();
858 ret.get(start+2) = v.z();
859 return ret;
860}
861
864template <unsigned SX>
865DVMatrix<SX> constexpr toDVMatrix(const DVect3 &v,unsigned start=0) { return toVMatrix<double,SX>(v,start); }
866
869template <class T>
870constexpr Vector2<T> operator*(const Matrix<T,2,2> &m,const Vector2<T> &v) {
871 Vector2<T> ret(v.x()*m.get(0,0) + v.y()*m.get(0,1),
872 v.x()*m.get(1,0) + v.y()*m.get(1,1));
873 return ret;
874}
875
878template <class T>
879constexpr Vector2<T> operator*(const SymMatrix<T,2> &m,const Vector2<T> &v) {
880 Vector2<T> ret(v.x()*m.get(0,0) + v.y()*m.get(0,1),
881 v.x()*m.get(1,0) + v.y()*m.get(1,1));
882 return ret;
883}
884
887template <class T>
888constexpr Vector3<T> operator*(const Matrix<T,3,3> &m,const Vector3<T> &v) {
889 Vector3<T> ret(v.x()*m.get(0,0) + v.y()*m.get(0,1) + v.z()*m.get(0,2),
890 v.x()*m.get(1,0) + v.y()*m.get(1,1) + v.z()*m.get(1,2),
891 v.x()*m.get(2,0) + v.y()*m.get(2,1) + v.z()*m.get(2,2));
892 return ret;
893}
894
897template <class T>
899 Vector3<T> ret(v.x()*m.get(0,0) + v.y()*m.get(0,1) + v.z()*m.get(0,2),
900 v.x()*m.get(1,0) + v.y()*m.get(1,1) + v.z()*m.get(1,2),
901 v.x()*m.get(2,0) + v.y()*m.get(2,1) + v.z()*m.get(2,2));
902 return ret;
903}
904
907BASE_EXPORT DMatrix<3,3> toMatrix3(const SymTensor &s);
908BASE_EXPORT DMatrix<2,2> toMatrix2(const SymTensor &s);
909
912BASE_EXPORT DMatrix<2,2> toMatrix2(const SymTensor &s);
913
916BASE_EXPORT DSymMatrix<3> toSymMatrix3(const SymTensor &s);
917BASE_EXPORT DSymMatrix<2> toSymMatrix2(const SymTensor &s);
918
922inline SymTensor toSymTensor(const DMatrix<3,3> &m) {
923 SymTensor ret(m.get(0,0),
924 m.get(1,1),
925 m.get(2,2),
926 (m.get(0,1)+m.get(1,0))*0.5,
927 (m.get(0,2)+m.get(2,0))*0.5,
928 (m.get(1,2)+m.get(2,1))*0.5);
929 return ret;
930}
931
932inline SymTensor toSymTensor(const DMatrix<2,2> &m) {
933 SymTensor ret(m.get(0,0),
934 m.get(1,1),
935 0.0,
936 (m.get(0,1)+m.get(1,0))*0.5,
937 0.0,
938 0.0);
939 return ret;
940}
941
944inline SymTensor toSymTensor(const DSymMatrix<3> &m) {
945 SymTensor ret(m.get(0,0),m.get(1,1),m.get(1,2),
946 m.get(0,1),m.get(0,2),m.get(1,2));
947 return ret;
948}
949
951template <class T,unsigned SX>
952Vector2<T> toVector2(const VMatrix<T,SX> &m,unsigned start=0) { Vector2<T> ret(m(start),m(start+1)); return ret; }
953
955template <class T,unsigned SX>
956Vector3<T> toVector3(const VMatrix<T,SX> &m,unsigned start=0) { Vector3<T> ret(m(start),m(start+1),m(start+2)); return ret; }
957
959template <class T>
960Vector2<T> toVector(const VMatrix<T,2> &m) { Vector2<T> ret(m(0),m(1)); return ret; }
961
963template <class T>
964Vector3<T> toVector(const VMatrix<T,3> &m) { Vector3<T> ret(m(0),m(1),m(2)); return ret; }
965
967template <class T,unsigned SY>
968Vector2<T> columnToVector(const Matrix<T,2,SY> &m,unsigned col) { Vector2<T> ret(m(0,col),m(1,col)); return ret; }
970template <class T,unsigned SY>
971Vector3<T> columnToVector(const Matrix<T,3,SY> &m,unsigned col) { Vector3<T> ret(m(0,col),m(1,col),m(2,col)); return ret; }
973template <class T,unsigned SX>
974Vector2<T> rowToVector(const Matrix<T,SX,2> &m,unsigned row) { Vector2<T> ret(m(row,0),m(row,1)); return ret; }
976template <class T,unsigned SX>
977Vector3<T> rowToVector(const Matrix<T,SX,3> &m,unsigned row) { Vector3<T> ret(m(row,0),m(row,1),m(row,2)); return ret; }
978
979template <class T,unsigned SY>
980constexpr void vectorToColumn(Matrix<T,2,SY> &m,const DVect2 &v,unsigned col) { m(0,col) = v.x(); m(1,col) = v.y(); }
981
982template <class T,unsigned SY>
983constexpr void vectorToColumn(Matrix<T,3,SY> &m,const DVect3 &v,unsigned col) { m(0,col) = v.x(); m(1,col) = v.y(); m(2,col) = v.z(); }
984
985template <class T,unsigned SX,unsigned SY>
986constexpr void vectorToColumn(Matrix<T,SX,SY> &m,const VMatrix<T,SX> &v,unsigned col) { for (uint32 i=0;i<SX;++i) m(i,col) = v(i); }
987
988template <class T,unsigned SX>
989void vectorToRow(Matrix<T,SX,2> &m,const DVect2 &v,unsigned row) { m(row,0) = v.x(); m(row,1) = v.y(); }
990
991template <class T,unsigned SX>
992void vectorToRow(Matrix<T,SX,3> &m,const DVect3 &v,unsigned row) { m(row,0) = v.x(); m(row,1) = v.y(); m(row,2) = v.z(); }
993
994template <class T,unsigned SX,unsigned SY>
995constexpr void vectorToRow(Matrix<T,SX,SY> &m,const VMatrix<T,SY> &v,unsigned row) { for (uint32 j=0;j<SY;++j) m(row,j) = v(j); }
996
997template <class T,unsigned SX,unsigned SY>
998VMatrix<T,SX> column(const Matrix<T,SX,SY> &m,unsigned c) {
999 VMatrix<T,SX> ret;
1000 for (unsigned i=0;i<SX;++i)
1001 ret(i) = m(i,c);
1002 return ret;
1003}
1004
1005inline DVMatrix<3> operator *(const SymTensor &s,const DVMatrix<3> &v) {
1006 DVMatrix<3> ret;
1007 ret[0] = s.s11()*v[0] + s.s12()*v[1] + s.s13()*v[2];
1008 ret[1] = s.s12()*v[0] + s.s22()*v[1] + s.s23()*v[2];
1009 ret[2] = s.s13()*v[0] + s.s23()*v[1] + s.s33()*v[2];
1010 return ret;
1011}
1012
1013inline DVMatrix<2> operator *(const SymTensor &s,const DVMatrix<2> &v) {
1014 DVMatrix<2> ret;
1015 ret[0] = s.s11()*v[0] + s.s12()*v[1];
1016 ret[1] = s.s12()*v[0] + s.s22()*v[1];
1017 return ret;
1018}
1019
1021// EoF
constexpr const T & operator()(unsigned x) const
() operator access to const get(x)
Definition matrix.h:230
constexpr const Matrix< T, SX, 1 > & operator*=(const T &t)
In-place multiplication by a scalar.
Definition matrix.h:244
Matrix< T, SX, 1 > operator/(const T &t) const
Binary scalar division operator.
Definition matrix.h:255
constexpr const T & get(unsigned x, unsigned y) const
Retrieve constant value at row x column y. Bounds checking is done in a debug compile.
Definition matrix.h:220
constexpr const Matrix< T, SX, 1 > & operator/=(const T &t)
In-place division by a scalar.
Definition matrix.h:246
constexpr T & get(unsigned x)
Retrieve value at row x. Bounds checking is done in a debug compile.
Definition matrix.h:226
Matrix< T, SX, 1 > operator-(const Matrix< T, SX, 1 > &m) const
Binary subtraction operator.
Definition matrix.h:251
Matrix< T, SX, SZ > operator*(const Matrix< T, 1, SZ > &m) const
Binary matrix multiplication operator – simplest naive approach.
Definition matrix.h:257
Matrix< T, 1, SX > transpose() const
returns the transposed matrix of this matrix
Definition matrix.h:289
void addBlock(const Matrix< T, SX2, 1 > &m, unsigned iSrc, unsigned iDst)
Adds a block to this matrix from matrix m using the block size BX from block indice iSrc to indice iD...
Definition matrix.h:271
constexpr const T & operator()(unsigned x, unsigned y) const
() operator access to const get(x,y)
Definition matrix.h:228
Matrix< T, SX, 1 > operator*(const T &t) const
Binary scalar multiplication operator.
Definition matrix.h:253
void addGenBlock(const Matrix< T, SX2, SY2 > &m, unsigned iSrc, unsigned jSrc, unsigned iDst, unsigned jDst)
Adds a block to this matrix from matrix m using the block size (BX,BY), from normal not block indices...
Definition matrix.h:275
static Matrix< T, SX, 1 > identity()
Returns an identity matrix (or as close as you can get if not diagonal).
Definition matrix.h:296
constexpr T & operator()(unsigned x)
() operator access to get(x)
Definition matrix.h:235
T maxNorm() const
Returns the infinity norm of the matrix, or the maximum absolute magnitude of any element.
Definition matrix.h:287
constexpr Matrix()
Default constructor, does nothing and no initialization.
Definition matrix.h:210
Matrix< T, SX, 1 > operator+(const Matrix< T, SX, 1 > &m) const
Binary addition operator.
Definition matrix.h:249
constexpr T & get(unsigned x, unsigned y)
Retrieve value at row x column y. Bounds checking is done in a debug compile.
Definition matrix.h:224
constexpr Matrix(const T &t)
Explicit constructor, initializes all elements to value t.
Definition matrix.h:215
constexpr void set(const T &t=T{})
Sets all matrix elemnts to T.
Definition matrix.h:291
constexpr const Matrix< T, SX, 1 > & operator-=(const Matrix< T, SX, 1 > &m)
In-place matrix subtraction.
Definition matrix.h:242
void addGenBlock(const Matrix< T, SX2, 1 > &m, unsigned iSrc, unsigned iDst)
Adds a block to this matrix from matrix m using the block size BX from normal non block indice iSrc t...
Definition matrix.h:281
constexpr const T & get(unsigned x) const
Retrieve constant value at row x. Bounds checking is done in a debug compile.
Definition matrix.h:222
void addBlock(const Matrix< T, SX2, SY2 > &m, unsigned iSrc, unsigned jSrc, unsigned iDst, unsigned jDst)
Adds a block to this matrix from matrix m using the block size (BX,BY), from block indices (iSrc,...
Definition matrix.h:267
constexpr const Matrix< T, SX, 1 > & operator+=(const Matrix< T, SX, 1 > &m)
In-place matrix addition.
Definition matrix.h:240
constexpr T & operator()(unsigned x, unsigned y)
() operator access to get(x,y)
Definition matrix.h:233
A template-based matrix class, size fixed at compile time. Defaults to symmetric sized matrix.
Definition matrix.h:22
Matrix< T, SX, SY > operator*(const T &t) const
Binary scalar multiplication operator.
Definition matrix.h:148
void addGenBlock(const Matrix< T, SX2, SY2 > &m, unsigned iSrc, unsigned jSrc, unsigned iDst, unsigned jDst)
Adds a block to this matrix from matrix m using the block size (BX,BY), from normal not block indices...
Definition matrix.h:168
Matrix< T, SX, SY > operator-(const Matrix< T, SX, SY > &m) const
Binary subtraction operator.
Definition matrix.h:146
constexpr const Matrix< T, SX, SY > & operator-=(const Matrix< T, SX, SY > &m)
In-place matrix subtraction.
Definition matrix.h:137
T maxNorm() const
Returns the infinity norm of the matrix, or the maximum absolute magnitude of any element.
Definition matrix.h:175
constexpr T & operator()(unsigned x, unsigned y)
() operator access to get(x,y)
Definition matrix.h:131
constexpr Matrix()
Default constructor, does nothing and no initialization.
Definition matrix.h:111
constexpr Matrix(const T &t)
Explicit constructor, initializes all elements to value t.
Definition matrix.h:116
Matrix< T, SX, SY > operator/(const T &t) const
Binary scalar division operator.
Definition matrix.h:150
void addBlock(const Matrix< T, SX2, SY2 > &m, unsigned iSrc, unsigned jSrc, unsigned iDst, unsigned jDst)
Adds a block to this matrix from matrix m using the block size (BX,BY), from block indices (iSrc,...
Definition matrix.h:163
static constexpr Matrix< T, SX, SY > identity()
Returns an identity matrix (or as close as you can get if not diagonal).
Definition matrix.h:186
T trace() const
Return the trace of the matrix or the sum of the diagonal components. Works also if the matrix is not...
Definition matrix.h:179
constexpr void set(const T &t=T{})
Set all entries in the matrix to t.
Definition matrix.h:181
constexpr const Matrix< T, SX, SY > & operator+=(const Matrix< T, SX, SY > &m)
In-place matrix addition.
Definition matrix.h:135
constexpr const Column operator[](unsigned y) const
Array access operator returns a Column helper class, which has it's own [] operator to access a colum...
Definition matrix.h:125
constexpr const T & get(unsigned x, unsigned y) const
Retrieve value at row x column y. Bounds checking is done in a debug compile.
Definition matrix.h:121
constexpr Matrix< T, SX, SZ > operator*(const Matrix< T, SY, SZ > &m) const
Binary matrix multiplication operator – simplest naive approach.
Definition matrix.h:153
Matrix< T, SY, SX > transpose() const
Return the transpose of the matrix.
Definition matrix.h:177
constexpr T & get(unsigned x, unsigned y)
Retrieve value at row x column y. Bounds checking is done in a debug compile.
Definition matrix.h:123
constexpr const T & operator()(unsigned x, unsigned y) const
() operator access to get(x,y)
Definition matrix.h:129
constexpr Column operator[](unsigned y)
Array access operator returns a Column helper class, which has it's own [] operator to access a colum...
Definition matrix.h:127
constexpr const Matrix< T, SX, SY > & operator*=(const T &t)
In-place multiplication by a scalar.
Definition matrix.h:139
Matrix< T, SX, SY > operator+(const Matrix< T, SX, SY > &m) const
Binary addition operator.
Definition matrix.h:144
constexpr const Matrix< T, SX, SY > & operator/=(const T &t)
In-place division by a scalar.
Definition matrix.h:141
A template-based symmetric matrix class, size fixed at compile time. This primitive template-based ma...
Definition matrix.h:305
constexpr Matrix< T, SX, SX > operator*(const SymMatrix< T, SX > &m) const
Binary matrix multiplication operator – simplest naive approach.
Definition matrix.h:491
SymMatrix< T, SX > operator/(const T &t) const
Binary scalar division operator for a symetric matrix.
Definition matrix.h:489
const T & operator()(unsigned x, unsigned y) const
() operator access to get(x,y)
Definition matrix.h:463
constexpr Matrix< T, SX, SZ > operator*(const Matrix< T, SX, SZ > &m) const
Binary matrix multiplication operator – simplest naive approach.
Definition matrix.h:497
SymMatrix(const T &t)
Explicit constructor, initializes all elements to value t.
Definition matrix.h:452
const SymMatrix< T, SX > & operator/=(const T &t)
In-place division by a scalar.
Definition matrix.h:475
SymMatrix< T, SX > operator-(const SymMatrix< T, SX > &m) const
Binary subtraction operator for a symetric matrix.
Definition matrix.h:482
const SymMatrix< T, SX > & operator*=(const T &t)
In-place multiplication by a scalar.
Definition matrix.h:473
T & get(unsigned x, unsigned y)
Retrieve value at row x column y. Bounds checking is done in a debug compile.
Definition matrix.h:457
const SymMatrix< T, SX > & operator-=(const SymMatrix< T, SX > &m)
In-place matrix subtraction.
Definition matrix.h:471
Matrix< T, SX, SX > operator+(const Matrix< T, SX, SX > &m) const
Binary addition operator.
Definition matrix.h:480
const Column operator[](unsigned y) const
Array access operator returns a Column helper class, which has it's own [] operator to access a colum...
Definition matrix.h:459
void addBlock(const SymMatrix< T, SX2 > &m, unsigned iSrc, unsigned jSrc, unsigned iDst, unsigned jDst)
Adds a block to this matrix from matrix m using the block size (BX,BY), from block indices (iSrc,...
Definition matrix.h:509
void addGenBlock(const SymMatrix< T, SX2 > &m, unsigned iSrc, unsigned jSrc, unsigned iDst, unsigned jDst)
Adds a block to this matrix from matrix m using the block size (BX,BY), from normal not block indices...
Definition matrix.h:522
const T & get(unsigned x, unsigned y) const
Retrieve value at row x column y. Bounds checking is done in a debug compile.
Definition matrix.h:455
static SymMatrix< T, SX > identity()
Returns an identity matrix (or as close as you can get if not diagonal).
Definition matrix.h:545
void addBlock(const Matrix< T, SX2, SY2 > &m, unsigned iSrc, unsigned jSrc, unsigned iDst, unsigned jDst)
Adds a block to this matrix from matrix m using the block size (BX,BY), from block indices (iSrc,...
Definition matrix.h:505
SymMatrix< T, SX > operator*(const T &t) const
Binary scalar multiplication operator for a symetric matrix.
Definition matrix.h:487
T & operator()(unsigned x, unsigned y)
() operator access to get(x,y)
Definition matrix.h:465
Matrix< T, SX, SX > operator-(const Matrix< T, SX, SX > &m) const
Binary subtraction operator.
Definition matrix.h:484
SymMatrix< T, SX > transpose() const
Return the transpose of the matrix.
Definition matrix.h:534
Matrix< T, SX, SX > toMatrix() const
Returns its copy.
Definition matrix.h:542
constexpr void set(const T &t=T{})
Sets all matrix elements to t.
Definition matrix.h:538
const SymMatrix< T, SX > & operator+=(const SymMatrix< T, SX > &m)
In-place matrix addition.
Definition matrix.h:469
static SymMatrix< T, SX > fromMatrix(const Matrix< T, SX, SX > &m)
Assign from full matrix.
Definition matrix.h:547
SymMatrix< T, SX > operator+(const SymMatrix< T, SX > &m) const
Binary addition operator for a symetric matris.
Definition matrix.h:478
T trace() const
Return the trace of the matrix or the sum of the diagonal components. Works also if the matrix is not...
Definition matrix.h:536
void addGenBlock(const Matrix< T, SX2, SY2 > &m, unsigned iSrc, unsigned jSrc, unsigned iDst, unsigned jDst)
Adds a block to this matrix from matrix m using the block size (BX,BY), from normal not block indices...
Definition matrix.h:513
T maxNorm() const
Returns the infinity norm of the matrix, or the maximum absolute magnitude of any element.
Definition matrix.h:532
Column operator[](unsigned y)
Array access operator returns a Column helper class, which has it's own [] operator to access a colum...
Definition matrix.h:461
A symmetric 2nd order tensor.
Definition symtensor.h:22
double s33() const
Component access, note that s12 is equivalent to s21 (for instance).
Definition symtensor.h:42
double s12() const
Component access, note that s12 is equivalent to s21 (for instance).
Definition symtensor.h:43
double s11() const
Component access, note that s12 is equivalent to s21 (for instance).
Definition symtensor.h:40
double s22() const
Component access, note that s12 is equivalent to s21 (for instance).
Definition symtensor.h:41
double s23() const
Component access, note that s12 is equivalent to s21 (for instance).
Definition symtensor.h:45
double s13() const
Component access, note that s12 is equivalent to s21 (for instance).
Definition symtensor.h:44
A 1-Dimensional version of Matrix, to represent a vector.
Definition matrix.h:643
constexpr T & operator[](unsigned x)
1D version of array operator, which currently unfortunately eliminates [x][0] syntax on VMatrix (may ...
Definition matrix.h:663
constexpr const T & operator[](unsigned x) const
1D version of array operator, which currently unfortunately eliminates [x][0] syntax on VMatrix (may ...
Definition matrix.h:661
2D vector utility class.
Definition vect.h:32
constexpr const T & x() const
X component access.
Definition vect.h:53
constexpr const T & y() const
Y component access.
Definition vect.h:55
3D vector utility class.
Definition vect.h:150
constexpr const T & y() const
The y-component of the vector.
Definition vect.h:169
constexpr const T & x() const
The x-component of the vector.
Definition vect.h:167
constexpr const T & z() const
The z-component of the vector.
Definition vect.h:171
Definition matrix.h:584
debug checked shorthand for std::numeric_limits<T>::
Definition limit.h:25
Definition matrix.h:599
Definition matrix.h:615
constexpr T determinant(const Matrix< T, 3, 3 > &mat)
Returns the determinant of a 3X3 Matrix.
Definition matrix.h:748
Vector2< T > columnToVector(const Matrix< T, 2, SY > &m, unsigned col)
returns in a Vector2<t> the column col from matrix Matrix<T,SX,2>
Definition matrix.h:968
constexpr Matrix< T, 3, 3 > inverse(const Matrix< T, 3, 3 > &mat)
Returns the inverse of a 3X3 Matrix.
Definition matrix.h:768
constexpr Vector3< T > operator*(const Matrix< T, 3, 3 > &m, const Vector3< T > &v)
Definition matrix.h:888
constexpr DVMatrix< SX > toDVMatrix(const DVect2 &v, unsigned start=0)
Definition matrix.h:850
Matrix< T, 3, 3 > outerProduct(const Vector3< T > &v1, const Vector3< T > &v2)
Creates a 3X3 Matrix from the outer product of two Vector3 types.
Definition matrix.h:716
constexpr VMatrix< T, 2 > toMatrix(const Vector2< T > &v)
Definition matrix.h:821
constexpr VMatrix< T, 3 > toMatrix(const Vector3< T > &v)
Definition matrix.h:831
Vector3< T > toVector3(const VMatrix< T, SX > &m, unsigned start=0)
Converts a VMatrix to a Vector3, using three elements starting at index start.
Definition matrix.h:956
constexpr VMatrix< T, SX > toVMatrix(const Vector2< T > &v, unsigned start=0)
Converts a Vector2 into a VMatrix of arbitrary size, at an arbitrary starting index.
Definition matrix.h:841
constexpr Vector2< T > operator*(const Matrix< T, 2, 2 > &m, const Vector2< T > &v)
Definition matrix.h:870
constexpr Vector2< T > operator*(const SymMatrix< T, 2 > &m, const Vector2< T > &v)
Definition matrix.h:879
constexpr T determinant(const Matrix< T, 2, 2 > &mat)
Returns the determinant of a 2X2 Matrix.
Definition matrix.h:759
#define BASE_EXPORT
Definition basedef.h:25
Vector3< T > operator*(const SymMatrix< T, 3 > &m, const Vector3< T > &v)
Definition matrix.h:898
Vector2< T > rowToVector(const Matrix< T, SX, 2 > &m, unsigned row)
returns in a Vector2<t> the row row from matrix Matrix<T,SX,2>
Definition matrix.h:974
Matrix< T, 2, 2 > outerProduct(const Vector2< T > &v1, const Vector2< T > &v2)
Creates a 2X2 Matrix from the outer product of two Vector2 types.
Definition matrix.h:734
constexpr Matrix< T, 2, 2 > inverse(const Matrix< T, 2, 2 > &mat)
Returns the inverse of a 2X2 Matrix.
Definition matrix.h:785
Vector2< T > toVector2(const VMatrix< T, SX > &m, unsigned start=0)
Converts a VMatrix to a Vector2, using two elements starting at index start.
Definition matrix.h:952
Vector2< T > toVector(const VMatrix< T, 2 > &m)
Converts a VMatrix to a Vector3, using three elements starting at index start.
Definition matrix.h:960
A Symmetric 2nd order tensor.