Jet  v1.3.3
matrix2x2.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_MATRIX2X2_H_
8 #define INCLUDE_JET_MATRIX2X2_H_
9 
10 #include <jet/matrix.h>
11 #include <jet/vector2.h>
12 
13 #include <array>
14 #include <limits>
15 
16 namespace jet {
17 
25 template <typename T>
26 class Matrix<T, 2, 2> {
27  public:
28  static_assert(std::is_floating_point<T>::value,
29  "Matrix only can be instantiated with floating point types");
30 
31  // MARK: Constructors
32 
34  Matrix();
35 
37  explicit Matrix(T s);
38 
41  Matrix(T m00, T m01, T m10, T m11);
42 
60  template <typename U>
61  Matrix(const std::initializer_list<std::initializer_list<U>>& lst);
62 
64  Matrix(const Matrix& m);
65 
68  explicit Matrix(const T* arr);
69 
70  // MARK: Basic setters
71 
73  void set(T s);
74 
77  void set(T m00, T m01, T m10, T m11);
78 
97  template <typename U>
98  void set(const std::initializer_list<std::initializer_list<U>>& lst);
99 
101  void set(const Matrix& m);
102 
105  void set(const T* arr);
106 
108  void setDiagonal(T s);
109 
111  void setOffDiagonal(T s);
112 
114  void setRow(size_t i, const Vector2<T>& row);
115 
117  void setColumn(size_t i, const Vector2<T>& col);
118 
119  // MARK: Basic getters
120 
123  bool isSimilar(const Matrix& m,
124  double tol = std::numeric_limits<double>::epsilon()) const;
125 
127  bool isSquare() const;
128 
130  size_t rows() const;
131 
133  size_t cols() const;
134 
136  T* data();
137 
139  const T* data() const;
140 
141  // MARK: Binary operator methods - new instance = this instance (+) input
143  Matrix add(T s) const;
144 
146  Matrix add(const Matrix& m) const;
147 
149  Matrix sub(T s) const;
150 
152  Matrix sub(const Matrix& m) const;
153 
155  Matrix mul(T s) const;
156 
158  Vector2<T> mul(const Vector2<T>& v) const;
159 
161  Matrix mul(const Matrix& m) const;
162 
164  Matrix div(T s) const;
165 
166  // MARK: Binary operator methods - new instance = input (+) this instance
168  Matrix radd(T s) const;
169 
171  Matrix radd(const Matrix& m) const;
172 
174  Matrix rsub(T s) const;
175 
177  Matrix rsub(const Matrix& m) const;
178 
180  Matrix rmul(T s) const;
181 
183  Matrix rmul(const Matrix& m) const;
184 
186  Matrix rdiv(T s) const;
187 
188  // MARK: Augmented operator methods - this instance (+)= input
190  void iadd(T s);
191 
193  void iadd(const Matrix& m);
194 
196  void isub(T s);
197 
199  void isub(const Matrix& m);
200 
202  void imul(T s);
203 
205  void imul(const Matrix& m);
206 
208  void idiv(T s);
209 
210  // MARK: Modifiers
212  void transpose();
213 
215  void invert();
216 
217  // MARK: Complex getters
218 
220  T sum() const;
221 
223  T avg() const;
224 
226  T min() const;
227 
229  T max() const;
230 
232  T absmin() const;
233 
235  T absmax() const;
236 
238  T trace() const;
239 
241  T determinant() const;
242 
244  Matrix diagonal() const;
245 
248 
251 
254 
256  Matrix lowerTri() const;
257 
259  Matrix upperTri() const;
260 
263 
265  Matrix inverse() const;
266 
268  T frobeniusNorm() const;
269 
270  template <typename U>
272 
273  // MARK: Setter operators
275  Matrix& operator=(const Matrix& m);
276 
279 
282 
285 
288 
291 
294 
297 
298  // MARK: Getter operators
300  T& operator[](size_t i);
301 
303  const T& operator[](size_t i) const;
304 
306  T& operator()(size_t i, size_t j);
307 
309  const T& operator()(size_t i, size_t j) const;
310 
312  bool operator==(const Matrix& m) const;
313 
315  bool operator!=(const Matrix& m) const;
316 
317  // MARK: Helpers
319  static Matrix makeZero();
320 
323 
325  static Matrix makeScaleMatrix(T sx, T sy);
326 
329 
332  static Matrix makeRotationMatrix(const T& rad);
333 
334  private:
335  std::array<T, 4> _elements;
336 };
337 
339 template <typename T>
341 
342 // MARK: Operator overloadings
344 template <typename T>
346 
348 template <typename T>
350 
352 template <typename T>
353 Matrix2x2<T> operator+(const Matrix2x2<T>& a, const T& b);
354 
356 template <typename T>
357 Matrix2x2<T> operator+(const T& a, const Matrix2x2<T>& b);
358 
360 template <typename T>
362 
364 template <typename T>
366 
368 template <typename T>
370 
372 template <typename T>
374 
376 template <typename T>
378 
380 template <typename T>
382 
384 template <typename T>
386 
388 template <typename T>
390 
392 template <typename T>
393 Matrix2x2<T> operator/(const T& a, const Matrix2x2<T>& b);
394 
397 
400 
401 } // namespace jet
402 
403 #include "detail/matrix2x2-inl.h"
404 
405 #endif // INCLUDE_JET_MATRIX2X2_H_
jet::Matrix< T, 2, 2 >::operator+=
Matrix & operator+=(const Matrix &m)
Addition assignment with input matrix (element-wise).
jet::Matrix< T, 2, 2 >::operator[]
T & operator[](size_t i)
Returns reference of i-th element.
jet::Matrix< T, 2, 2 >::rmul
Matrix rmul(const Matrix &m) const
Returns input matrix * this matrix.
jet::Matrix< T, 2, 2 >::makeScaleMatrix
static Matrix makeScaleMatrix(T sx, T sy)
Makes scale matrix.
jet::Matrix< T, 2, 2 >::transpose
void transpose()
Transposes this matrix.
jet::Matrix< T, 2, 2 >::isub
void isub(const Matrix &m)
Subtracts input matrix from this matrix (element-wise).
jet::Matrix< T, 2, 2 >::idiv
void idiv(T s)
Divides this matrix with input scalar.
jet::Matrix< T, 2, 2 >::Matrix
Matrix(const Matrix &m)
Constructs a matrix with input matrix.
jet::Matrix< T, 2, 2 >::determinant
T determinant() const
Returns determinant of this matrix.
jet::Matrix< T, 2, 2 >::mul
Vector2< T > mul(const Vector2< T > &v) const
Returns this matrix * input vector.
jet::Matrix< T, 2, 2 >::rsub
Matrix rsub(const Matrix &m) const
Returns input matrix - this matrix (element-wise).
jet::Matrix< T, 2, 2 >::data
T * data()
Returns data pointer of this matrix.
jet::Matrix
Static-sized M x N matrix class.
Definition: matrix.h:29
jet::Matrix< T, 2, 2 >::sub
Matrix sub(T s) const
Returns this matrix - input scalar.
jet::Matrix< T, 2, 2 >::rdiv
Matrix rdiv(T s) const
Returns input scalar / this matrix.
jet::Matrix< T, 2, 2 >::rsub
Matrix rsub(T s) const
Returns input scalar - this matrix.
jet::Matrix< T, 2, 2 >::invert
void invert()
Inverts this matrix.
jet::Matrix< T, 2, 2 >::rmul
Matrix rmul(T s) const
Returns input scalar * this matrix.
jet::Matrix2x2F
Matrix2x2< float > Matrix2x2F
Float-type 2x2 matrix.
Definition: matrix2x2.h:396
jet::Matrix< T, 2, 2 >::setOffDiagonal
void setOffDiagonal(T s)
Sets off-diagonal elements with input scalar.
jet::Matrix2x2D
Matrix2x2< double > Matrix2x2D
Double-type 2x2 matrix.
Definition: matrix2x2.h:399
jet::Matrix< T, 2, 2 >::Matrix
Matrix(const std::initializer_list< std::initializer_list< U >> &lst)
Constructs a matrix with given initializer list lst.
jet::Matrix< T, 2, 2 >::strictUpperTri
Matrix strictUpperTri() const
Returns strictly upper triangle part of this matrix.
jet::Matrix< T, 2, 2 >::mul
Matrix mul(const Matrix &m) const
Returns this matrix * input matrix.
jet::Matrix< T, 2, 2 >::operator[]
const T & operator[](size_t i) const
Returns constant reference of i-th element.
jet::Matrix< T, 2, 2 >::isSimilar
bool isSimilar(const Matrix &m, double tol=std::numeric_limits< double >::epsilon()) const
jet::Matrix< T, 2, 2 >::operator+=
Matrix & operator+=(T s)
Addition assignment with input scalar.
jet::Matrix< T, 2, 2 >::set
void set(const T *arr)
jet::Matrix< T, 2, 2 >::Matrix
Matrix(T m00, T m01, T m10, T m11)
jet::Matrix< T, 2, 2 >::mul
Matrix mul(T s) const
Returns this matrix * input scalar.
jet::Matrix< T, 2, 2 >::frobeniusNorm
T frobeniusNorm() const
Returns Frobenius norm.
jet::Matrix< T, 2, 2 >::imul
void imul(T s)
Multiplies input scalar to this matrix.
jet::Matrix< T, 2, 2 >::operator*=
Matrix & operator*=(const Matrix &m)
Multiplication assignment with input matrix.
jet::Matrix< T, 2, 2 >::iadd
void iadd(const Matrix &m)
Adds input matrix to this matrix (element-wise).
jet::Matrix< T, 2, 2 >::strictLowerTri
Matrix strictLowerTri() const
Returns strictly lower triangle part of this matrix.
jet::Matrix< T, 2, 2 >::setRow
void setRow(size_t i, const Vector2< T > &row)
Sets i-th row with input vector.
matrix.h
jet
Definition: advection_solver2.h:18
jet::Matrix< T, 2, 2 >::trace
T trace() const
Returns sum of all diagonal elements.
jet::Matrix< T, 2, 2 >::castTo
Matrix< U, 2, 2 > castTo() const
vector2.h
jet::Matrix< T, 2, 2 >::makeScaleMatrix
static Matrix makeScaleMatrix(const Vector2< T > &s)
Makes scale matrix.
jet::Matrix< T, 2, 2 >::Matrix
Matrix(T s)
Constructs constant value matrix.
jet::Matrix< T, 2, 2 >::Matrix
Matrix()
Constructs identity matrix.
jet::Matrix< T, 2, 2 >::set
void set(T m00, T m01, T m10, T m11)
jet::Matrix< T, 2, 2 >::add
Matrix add(const Matrix &m) const
Returns this matrix + input matrix (element-wise).
jet::Matrix< T, 2, 2 >::absmax
T absmax() const
Returns absolute maximum among all elements.
jet::Matrix< T, 2, 2 >
2-D matrix class.
Definition: matrix2x2.h:26
jet::Matrix< T, 2, 2 >::operator()
T & operator()(size_t i, size_t j)
Returns reference of (i,j) element.
jet::Vector< T, 2 >
2-D vector class.
Definition: vector2.h:24
jet::Matrix< T, 2, 2 >::operator-=
Matrix & operator-=(T s)
Subtraction assignment with input scalar.
jet::Matrix< T, 2, 2 >::upperTri
Matrix upperTri() const
Returns upper triangle part of this matrix (including the diagonal).
jet::Matrix< T, 2, 2 >::operator==
bool operator==(const Matrix &m) const
Returns true if is equal to m.
jet::operator+
Matrix2x2< T > operator+(const Matrix2x2< T > &a, const Matrix2x2< T > &b)
Returns a + b (element-size).
jet::Matrix< T, 2, 2 >::operator!=
bool operator!=(const Matrix &m) const
Returns true if is not equal to m.
jet::Matrix< T, 2, 2 >::inverse
Matrix inverse() const
Returns inverse matrix.
jet::Matrix< T, 2, 2 >::lowerTri
Matrix lowerTri() const
Returns lower triangle part of this matrix (including the diagonal).
jet::Matrix< T, 2, 2 >::data
const T * data() const
Returns constant pointer of this matrix.
jet::Matrix< T, 2, 2 >::sub
Matrix sub(const Matrix &m) const
Returns this matrix - input matrix (element-wise).
jet::Matrix< T, 2, 2 >::makeIdentity
static Matrix makeIdentity()
Makes all diagonal elements to 1, and other elements to 0.
jet::Matrix< T, 2, 2 >::makeZero
static Matrix makeZero()
Sets all matrix entries to zero.
jet::Matrix< T, 2, 2 >::Matrix
Matrix(const T *arr)
jet::Matrix< T, 2, 2 >::isub
void isub(T s)
Subtracts input scalar from this matrix.
jet::Matrix< T, 2, 2 >::cols
size_t cols() const
Returns number of columns of this matrix.
jet::Matrix< T, 2, 2 >::radd
Matrix radd(const Matrix &m) const
Returns input matrix + this matrix (element-wise).
jet::Matrix< T, 2, 2 >::set
void set(T s)
Sets whole matrix with input scalar.
jet::Matrix< T, 2, 2 >::sum
T sum() const
Returns sum of all elements.
jet::Matrix< T, 2, 2 >::makeRotationMatrix
static Matrix makeRotationMatrix(const T &rad)
jet::Matrix< T, 2, 2 >::transposed
Matrix transposed() const
Returns transposed matrix.
jet::Matrix< T, 2, 2 >::imul
void imul(const Matrix &m)
Multiplies input matrix to this matrix.
jet::Matrix< T, 2, 2 >::set
void set(const Matrix &m)
Copies from input matrix.
jet::Matrix< T, 2, 2 >::isSquare
bool isSquare() const
Returns true if this matrix is a square matrix.
jet::Matrix< T, 2, 2 >::operator=
Matrix & operator=(const Matrix &m)
Assigns input 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, 2, 2 >::div
Matrix div(T s) const
Returns this matrix / input scalar.
jet::Matrix< T, 2, 2 >::set
void set(const std::initializer_list< std::initializer_list< U >> &lst)
Sets this matrix with given initializer list lst.
jet::Matrix< T, 2, 2 >::add
Matrix add(T s) const
Returns this matrix + input scalar.
jet::Matrix< T, 2, 2 >::offDiagonal
Matrix offDiagonal() const
Returns off-diagonal part of this matrix.
jet::Matrix< T, 2, 2 >::operator/=
Matrix & operator/=(T s)
Division assignment with input scalar.
jet::Matrix< T, 2, 2 >::setColumn
void setColumn(size_t i, const Vector2< T > &col)
Sets i-th column with input vector.
jet::operator-
Matrix2x2< T > operator-(const Matrix2x2< T > &a)
Returns a matrix with opposite sign.
jet::Matrix< T, 2, 2 >::min
T min() const
Returns minimum among all elements.
jet::Matrix< T, 2, 2 >::iadd
void iadd(T s)
Adds input scalar to this matrix.
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, 2, 2 >::operator()
const T & operator()(size_t i, size_t j) const
Returns constant reference of (i,j) element.
jet::Matrix< T, 2, 2 >::max
T max() const
Returns maximum among all elements.
jet::Matrix< T, 2, 2 >::diagonal
Matrix diagonal() const
Returns diagonal part of this matrix.
jet::Matrix< T, 2, 2 >::setDiagonal
void setDiagonal(T s)
Sets diagonal elements with input scalar.
jet::Matrix< T, 2, 2 >::absmin
T absmin() const
Returns absolute minimum among all elements.
jet::Matrix< T, 2, 2 >::avg
T avg() const
Returns average of all elements.
jet::Matrix< T, 2, 2 >::operator-=
Matrix & operator-=(const Matrix &m)
Subtraction assignment with input matrix (element-wise).
jet::Matrix< T, 2, 2 >::operator*=
Matrix & operator*=(T s)
Multiplication assignment with input scalar.
jet::Matrix< T, 2, 2 >::rows
size_t rows() const
Returns number of rows of this matrix.
jet::Matrix< T, 2, 2 >::radd
Matrix radd(T s) const
Returns input scalar + this matrix.