Jet  v1.3.3
matrix3x3.h
Go to the documentation of this file.
1 // Copyright (c) 2018 Doyub Kim
2 //
3 // I am making my contributions/submissions to this project solely in my
4 // personal capacity and am not conveying any rights to any intellectual
5 // property of any third parties.
6 
7 #ifndef INCLUDE_JET_MATRIX3X3_H_
8 #define INCLUDE_JET_MATRIX3X3_H_
9 
10 #include <jet/matrix.h>
11 #include <jet/vector3.h>
12 
13 #include <array>
14 #include <limits>
15 
16 namespace jet {
17 
27 template <typename T>
28 class Matrix<T, 3, 3> {
29  public:
30  static_assert(std::is_floating_point<T>::value,
31  "Matrix only can be instantiated with floating point types");
32 
33  // MARK: Constructors
34 
36  Matrix();
37 
39  explicit Matrix(T s);
40 
43  Matrix(T m00, T m01, T m02, T m10, T m11, T m12, T m20, T m21, T m22);
44 
63  template <typename U>
64  Matrix(const std::initializer_list<std::initializer_list<U>>& lst);
65 
67  Matrix(const Matrix& m);
68 
71  explicit Matrix(const T* arr);
72 
73  // MARK: Basic setters
74 
76  void set(T s);
77 
80  void set(T m00, T m01, T m02, T m10, T m11, T m12, T m20, T m21, T m22);
81 
101  template <typename U>
102  void set(const std::initializer_list<std::initializer_list<U>>& lst);
103 
105  void set(const Matrix& m);
106 
109  void set(const T* arr);
110 
112  void setDiagonal(T s);
113 
115  void setOffDiagonal(T s);
116 
118  void setRow(size_t i, const Vector3<T>& row);
119 
121  void setColumn(size_t i, const Vector3<T>& col);
122 
123  // MARK: Basic getters
124 
127  bool isSimilar(const Matrix& m,
128  double tol = std::numeric_limits<double>::epsilon()) const;
129 
131  bool isSquare() const;
132 
134  size_t rows() const;
135 
137  size_t cols() const;
138 
140  T* data();
141 
143  const T* data() const;
144 
145  // MARK: Binary operator methods - new instance = this instance (+) input
147  Matrix add(T s) const;
148 
150  Matrix add(const Matrix& m) const;
151 
153  Matrix sub(T s) const;
154 
156  Matrix sub(const Matrix& m) const;
157 
159  Matrix mul(T s) const;
160 
162  Vector3<T> mul(const Vector3<T>& v) const;
163 
165  Matrix mul(const Matrix& m) const;
166 
168  Matrix div(T s) const;
169 
170  // MARK: Binary operator methods - new instance = input (+) this instance
172  Matrix radd(T s) const;
173 
175  Matrix radd(const Matrix& m) const;
176 
178  Matrix rsub(T s) const;
179 
181  Matrix rsub(const Matrix& m) const;
182 
184  Matrix rmul(T s) const;
185 
187  Matrix rmul(const Matrix& m) const;
188 
190  Matrix rdiv(T s) const;
191 
192  // MARK: Augmented operator methods - this instance (+)= input
194  void iadd(T s);
195 
197  void iadd(const Matrix& m);
198 
200  void isub(T s);
201 
203  void isub(const Matrix& m);
204 
206  void imul(T s);
207 
209  void imul(const Matrix& m);
210 
212  void idiv(T s);
213 
214  // MARK: Modifiers
216  void transpose();
217 
219  void invert();
220 
221  // MARK: Complex getters
223  T sum() const;
224 
226  T avg() const;
227 
229  T min() const;
230 
232  T max() const;
233 
235  T absmin() const;
236 
238  T absmax() const;
239 
241  T trace() const;
242 
244  T determinant() const;
245 
247  Matrix diagonal() const;
248 
251 
254 
257 
259  Matrix lowerTri() const;
260 
262  Matrix upperTri() const;
263 
266 
268  Matrix inverse() const;
269 
271  T frobeniusNorm() const;
272 
273  template <typename U>
275 
276  // MARK: Setter operators
278  Matrix& operator=(const Matrix& m);
279 
282 
285 
288 
291 
294 
297 
300 
301  // MARK: Getter operators
303  T& operator[](size_t i);
304 
306  const T& operator[](size_t i) const;
307 
309  T& operator()(size_t i, size_t j);
310 
312  const T& operator()(size_t i, size_t j) const;
313 
315  bool operator==(const Matrix& m) const;
316 
318  bool operator!=(const Matrix& m) const;
319 
320  // MARK: Helpers
322  static Matrix makeZero();
323 
326 
328  static Matrix makeScaleMatrix(T sx, T sy, T sz);
329 
332 
335  static Matrix makeRotationMatrix(const Vector3<T>& axis, T rad);
336 
337  private:
338  std::array<T, 9> _elements;
339 };
340 
342 template <typename T>
344 
345 // Operator overloadings
347 template <typename T>
349 
351 template <typename T>
353 
355 template <typename T>
357 
359 template <typename T>
361 
363 template <typename T>
365 
367 template <typename T>
369 
371 template <typename T>
373 
375 template <typename T>
377 
379 template <typename T>
381 
383 template <typename T>
385 
387 template <typename T>
389 
391 template <typename T>
393 
395 template <typename T>
397 
400 
403 
404 } // namespace jet
405 
406 #include "detail/matrix3x3-inl.h"
407 
408 #endif // INCLUDE_JET_MATRIX3X3_H_
jet::Matrix< T, 3, 3 >::data
const T * data() const
Returns constant pointer of this matrix.
jet::Matrix< T, 3, 3 >::frobeniusNorm
T frobeniusNorm() const
Returns Frobenius norm.
jet::Matrix< T, 3, 3 >::radd
Matrix radd(T s) const
Returns input scalar + this matrix.
jet::Matrix< T, 3, 3 >::diagonal
Matrix diagonal() const
Returns diagonal part of this matrix.
jet::Matrix< T, 3, 3 >::operator-=
Matrix & operator-=(T s)
Subtraction assignment with input scalar.
jet::Matrix< T, 3, 3 >::isSimilar
bool isSimilar(const Matrix &m, double tol=std::numeric_limits< double >::epsilon()) const
jet::Matrix< T, 3, 3 >::operator+=
Matrix & operator+=(T s)
Addition assignment with input scalar.
jet::Matrix< T, 3, 3 >::transpose
void transpose()
Transposes this matrix.
jet::Matrix< T, 3, 3 >::makeRotationMatrix
static Matrix makeRotationMatrix(const Vector3< T > &axis, T rad)
jet::Matrix< T, 3, 3 >::max
T max() const
Returns maximum among all elements.
jet::Matrix< T, 3, 3 >::makeScaleMatrix
static Matrix makeScaleMatrix(const Vector3< T > &s)
Makes scale matrix.
jet::Matrix< T, 3, 3 >::transposed
Matrix transposed() const
Returns transposed matrix.
jet::Matrix< T, 3, 3 >::lowerTri
Matrix lowerTri() const
Returns lower triangle part of this matrix (including the diagonal).
jet::Matrix< T, 3, 3 >::add
Matrix add(T s) const
Returns this matrix + input scalar.
jet::Matrix< T, 3, 3 >::rows
size_t rows() const
Returns number of rows of this matrix.
jet::Matrix< T, 3, 3 >::offDiagonal
Matrix offDiagonal() const
Returns off-diagonal part of this matrix.
jet::Matrix< T, 3, 3 >::invert
void invert()
Inverts this matrix.
jet::Matrix< T, 3, 3 >::min
T min() const
Returns minimum among all elements.
jet::Matrix< T, 3, 3 >::isub
void isub(const Matrix &m)
Subtracts input matrix from this matrix (element-wise).
jet::Matrix< T, 3, 3 >::Matrix
Matrix(const std::initializer_list< std::initializer_list< U >> &lst)
Constructs a matrix with given initializer list lst.
jet::Matrix< T, 3, 3 >::cols
size_t cols() const
Returns number of columns of this matrix.
jet::Matrix
Static-sized M x N matrix class.
Definition: matrix.h:29
jet::Matrix< T, 3, 3 >::sum
T sum() const
Returns sum of all elements.
jet::Matrix< T, 3, 3 >::sub
Matrix sub(T s) const
Returns this matrix - input scalar.
jet::Matrix< T, 3, 3 >::set
void set(const Matrix &m)
Copies from input matrix.
jet::Matrix< T, 3, 3 >::setRow
void setRow(size_t i, const Vector3< T > &row)
Sets i-th row with input vector.
jet::Matrix< T, 3, 3 >::Matrix
Matrix(T m00, T m01, T m02, T m10, T m11, T m12, T m20, T m21, T m22)
jet::Matrix< T, 3, 3 >::iadd
void iadd(const Matrix &m)
Adds input matrix to this matrix (element-wise).
jet::Matrix< T, 3, 3 >::set
void set(T s)
Sets whole matrix with input scalar.
jet::Matrix< T, 3, 3 >::operator-=
Matrix & operator-=(const Matrix &m)
Subtraction assignment with input matrix (element-wise).
jet::Matrix< T, 3, 3 >::isub
void isub(T s)
Subtracts input scalar from this matrix.
jet::Matrix< T, 3, 3 >::absmax
T absmax() const
Returns absolute maximum among all elements.
jet::Matrix< T, 3, 3 >::setDiagonal
void setDiagonal(T s)
Sets diagonal elements with input scalar.
jet::Matrix< T, 3, 3 >::mul
Vector3< T > mul(const Vector3< T > &v) const
Returns this matrix * input vector.
jet::Matrix< T, 3, 3 >::isSquare
bool isSquare() const
Returns true if this matrix is a square matrix.
jet::Matrix< T, 3, 3 >::Matrix
Matrix()
Constructs identity matrix.
jet::Matrix< T, 3, 3 >
3-D matrix class.
Definition: matrix3x3.h:28
jet::Matrix< T, 3, 3 >::setColumn
void setColumn(size_t i, const Vector3< T > &col)
Sets i-th column with input vector.
jet::Matrix< T, 3, 3 >::radd
Matrix radd(const Matrix &m) const
Returns input matrix + this matrix (element-wise).
jet::Matrix< T, 3, 3 >::data
T * data()
Returns data pointer of this matrix.
jet::Matrix< T, 3, 3 >::operator=
Matrix & operator=(const Matrix &m)
Assigns input matrix.
jet::Matrix< T, 3, 3 >::mul
Matrix mul(const Matrix &m) const
Returns this matrix * input matrix.
matrix.h
jet::Matrix< T, 3, 3 >::rsub
Matrix rsub(const Matrix &m) const
Returns input matrix - this matrix (element-wise).
jet
Definition: advection_solver2.h:18
jet::Matrix< T, 3, 3 >::strictLowerTri
Matrix strictLowerTri() const
Returns strictly lower triangle part of this matrix.
jet::Matrix< T, 3, 3 >::imul
void imul(const Matrix &m)
Multiplies input matrix to this matrix.
jet::Matrix< T, 3, 3 >::iadd
void iadd(T s)
Adds input scalar to this matrix.
jet::Matrix< T, 3, 3 >::operator()
const T & operator()(size_t i, size_t j) const
Returns constant reference of (i,j) element.
jet::Matrix< T, 3, 3 >::trace
T trace() const
Returns sum of all diagonal elements.
jet::Matrix< T, 3, 3 >::operator+=
Matrix & operator+=(const Matrix &m)
Addition assignment with input matrix (element-wise).
jet::Matrix< T, 3, 3 >::operator*=
Matrix & operator*=(const Matrix &m)
Multiplication assignment with input matrix.
jet::Matrix< T, 3, 3 >::setOffDiagonal
void setOffDiagonal(T s)
Sets off-diagonal elements with input scalar.
jet::Matrix< T, 3, 3 >::operator!=
bool operator!=(const Matrix &m) const
Returns true if is not equal to m.
jet::Matrix< T, 3, 3 >::add
Matrix add(const Matrix &m) const
Returns this matrix + input matrix (element-wise).
jet::operator+
Matrix2x2< T > operator+(const Matrix2x2< T > &a, const Matrix2x2< T > &b)
Returns a + b (element-size).
jet::Matrix< T, 3, 3 >::operator/=
Matrix & operator/=(T s)
Division assignment with input scalar.
jet::Matrix< T, 3, 3 >::sub
Matrix sub(const Matrix &m) const
Returns this matrix - input matrix (element-wise).
jet::Matrix< T, 3, 3 >::Matrix
Matrix(const Matrix &m)
Constructs a matrix with input matrix.
jet::Matrix< T, 3, 3 >::mul
Matrix mul(T s) const
Returns this matrix * input scalar.
jet::Matrix< T, 3, 3 >::Matrix
Matrix(T s)
Constructs constant value matrix.
jet::Matrix< T, 3, 3 >::operator==
bool operator==(const Matrix &m) const
Returns true if is equal to m.
jet::Matrix3x3F
Matrix3x3< float > Matrix3x3F
Float-type 3x3 matrix.
Definition: matrix3x3.h:399
jet::Matrix3x3D
Matrix3x3< double > Matrix3x3D
Double-type 3x3 matrix.
Definition: matrix3x3.h:402
jet::Matrix< T, 3, 3 >::determinant
T determinant() const
Returns determinant of this matrix.
jet::Matrix< T, 3, 3 >::set
void set(const std::initializer_list< std::initializer_list< U >> &lst)
Sets a matrix with given initializer list lst.
jet::Matrix< T, 3, 3 >::makeIdentity
static Matrix makeIdentity()
Makes all diagonal elements to 1, and other elements to 0.
jet::Matrix< T, 3, 3 >::rdiv
Matrix rdiv(T s) const
Returns input scalar / this matrix.
jet::Matrix< T, 3, 3 >::imul
void imul(T s)
Multiplies input scalar to this matrix.
vector3.h
jet::Matrix< T, 3, 3 >::set
void set(T m00, T m01, T m02, T m10, T m11, T m12, T m20, T m21, T m22)
jet::Matrix< T, 3, 3 >::operator()
T & operator()(size_t i, size_t j)
Returns reference of (i,j) element.
jet::Matrix< T, 3, 3 >::absmin
T absmin() const
Returns absolute minimum among all elements.
jet::Matrix< T, 3, 3 >::Matrix
Matrix(const T *arr)
jet::Matrix< T, 3, 3 >::div
Matrix div(T s) const
Returns this matrix / input scalar.
jet::Matrix< T, 3, 3 >::rmul
Matrix rmul(const Matrix &m) const
Returns input matrix * this matrix.
jet::Matrix< T, 3, 3 >::idiv
void idiv(T s)
Divides this matrix with input scalar.
jet::Matrix< T, 3, 3 >::rsub
Matrix rsub(T s) const
Returns input scalar - this matrix.
jet::Vector< T, 3 >
3-D vector class.
Definition: vector3.h:25
jet::Matrix< T, 3, 3 >::strictUpperTri
Matrix strictUpperTri() const
Returns strictly upper triangle part of this matrix.
jet::Matrix< T, 3, 3 >::inverse
Matrix inverse() const
Returns inverse matrix.
jet::operator/
Matrix2x2< T > operator/(const Matrix2x2< T > &a, T b)
Returns a' / b, where every element of matrix a' is a.
jet::Matrix< T, 3, 3 >::operator[]
const T & operator[](size_t i) const
Returns constant reference of i-th element.
jet::Matrix< T, 3, 3 >::upperTri
Matrix upperTri() const
Returns upper triangle part of this matrix (including the diagonal).
jet::operator-
Matrix2x2< T > operator-(const Matrix2x2< T > &a)
Returns a matrix with opposite sign.
jet::Matrix< T, 3, 3 >::rmul
Matrix rmul(T s) const
Returns input scalar * this matrix.
jet::Matrix< T, 3, 3 >::operator*=
Matrix & operator*=(T s)
Multiplication assignment with input scalar.
jet::Matrix< T, 3, 3 >::avg
T avg() const
Returns average of all elements.
jet::operator*
Matrix2x2< T > operator*(const Matrix2x2< T > &a, T b)
Returns a * b', where every element of matrix b' is b.
jet::Matrix< T, 3, 3 >::makeScaleMatrix
static Matrix makeScaleMatrix(T sx, T sy, T sz)
Makes scale matrix.
jet::Matrix< T, 3, 3 >::operator[]
T & operator[](size_t i)
Returns reference of i-th element.
jet::Matrix< T, 3, 3 >::castTo
Matrix< U, 3, 3 > castTo() const
jet::Matrix< T, 3, 3 >::makeZero
static Matrix makeZero()
Sets all matrix entries to zero.
jet::Matrix< T, 3, 3 >::set
void set(const T *arr)