Ginger 1.1.5; VERSION ${PROJECT_VERSION}
Loading...
Searching...
No Matches
matrix2.hpp
Go to the documentation of this file.
1
6#pragma once
7
8#include <utility> // import std::move
9
10#include "vector2.hpp"
11
12#if __cpp_constexpr >= 201304
13# define CONSTEXPR14 constexpr
14#else
15# define CONSTEXPR14 inline
16#endif
17
18namespace ginger {
28 template <typename T1, typename T2 = T1> class Matrix2 {
29 private:
30 T1 _x;
31 T2 _y;
32
33 public:
40 constexpr Matrix2(T1 x, T2 y) noexcept : _x{std::move(x)}, _y{std::move(y)} {}
41
47 constexpr auto x() const -> const T1& { return this->_x; }
48
54 constexpr auto y() const -> const T2& { return this->_y; }
55
56 // /**
57 // * @brief Construct a new Matrix2 object
58 // *
59 // * @param[in] x
60 // * @param[in] y
61 // */
62 // constexpr Matrix2(const T1& x, const T2& y) : Vector2<T1, T2>{x, y} {}
63
68
74 constexpr auto operator-() const -> Matrix2<T1, T2> { return {-this->x(), -this->y()}; }
75
84 template <typename U1, typename U2>
86 this->_x += other.x();
87 this->_y += other.y();
88 return *this;
89 }
90
99 template <typename U1, typename U2> //
101 this->_x -= other.x();
102 this->_y -= other.y();
103 return *this;
104 }
105
113 template <typename R> CONSTEXPR14 auto operator*=(const R& alpha) -> Matrix2<T1, T2>& {
114 this->_x *= alpha;
115 this->_y *= alpha;
116 return *this;
117 }
118
126 template <typename R> CONSTEXPR14 auto operator/=(const R& alpha) -> Matrix2<T1, T2>& {
127 this->_x /= alpha;
128 this->_y /= alpha;
129 return *this;
130 }
131
141 template <typename U1, typename U2> //
142 friend constexpr auto operator+(Matrix2<T1, T2> x, const Matrix2<U1, U2>& y)
143 -> Matrix2<T1, T2> {
144 return std::move(x) += y;
145 }
146
156 template <typename U1, typename U2> //
157 friend constexpr auto operator-(Matrix2<T1, T2> x, const Matrix2<U1, U2>& y)
158 -> Matrix2<T1, T2> {
159 return std::move(x) -= y;
160 }
161
170 template <typename R> friend constexpr auto operator*(Matrix2<T1, T2> x, const R& alpha)
171 -> Matrix2<T1, T2> {
172 return x *= alpha;
173 }
174
183 template <typename R> friend constexpr auto operator*(const R& alpha, Matrix2<T1, T2> x)
184 -> Matrix2<T1, T2> {
185 return x *= alpha;
186 }
187
196 template <typename R> friend constexpr auto operator/(Matrix2<T1, T2> x, const R& alpha)
197 -> Matrix2<T1, T2> {
198 return x /= alpha;
199 }
200
216 template <typename U1, typename U2> //
217 constexpr auto mdot(const Vector2<U1, U2>& other) const -> T1 {
218 return T1{this->_x.dot(other), this->_y.dot(other)};
219 }
220
231 constexpr auto det() const -> double {
232 return this->x().x() * this->y().y() - this->x().y() * this->y().x();
233 }
234
236 };
237} // namespace ginger
Matrix2.
Definition matrix2.hpp:28
constexpr auto x() const -> const T1 &
Get the first column vector.
Definition matrix2.hpp:47
CONSTEXPR14 auto operator-=(const Matrix2< U1, U2 > &other) -> Matrix2< T1, T2 > &
Subtract another matrix from this one.
Definition matrix2.hpp:100
CONSTEXPR14 auto operator+=(const Matrix2< U1, U2 > &other) -> Matrix2< T1, T2 > &
Add another matrix to this one.
Definition matrix2.hpp:85
friend constexpr auto operator/(Matrix2< T1, T2 > x, const R &alpha) -> Matrix2< T1, T2 >
Divide matrix by a scalar.
Definition matrix2.hpp:196
friend constexpr auto operator+(Matrix2< T1, T2 > x, const Matrix2< U1, U2 > &y) -> Matrix2< T1, T2 >
Add two matrices.
Definition matrix2.hpp:142
friend constexpr auto operator*(Matrix2< T1, T2 > x, const R &alpha) -> Matrix2< T1, T2 >
Multiply matrix by a scalar.
Definition matrix2.hpp:170
constexpr Matrix2(T1 x, T2 y) noexcept
Construct a new Matrix2 object.
Definition matrix2.hpp:40
constexpr auto y() const -> const T2 &
Get the second column vector.
Definition matrix2.hpp:54
friend constexpr auto operator*(const R &alpha, Matrix2< T1, T2 > x) -> Matrix2< T1, T2 >
Multiply a scalar by a matrix.
Definition matrix2.hpp:183
constexpr auto mdot(const Vector2< U1, U2 > &other) const -> T1
Multiply matrix with vector.
Definition matrix2.hpp:217
CONSTEXPR14 auto operator*=(const R &alpha) -> Matrix2< T1, T2 > &
Multiply.
Definition matrix2.hpp:113
constexpr auto operator-() const -> Matrix2< T1, T2 >
Negate the matrix.
Definition matrix2.hpp:74
friend constexpr auto operator-(Matrix2< T1, T2 > x, const Matrix2< U1, U2 > &y) -> Matrix2< T1, T2 >
Subtract two matrices.
Definition matrix2.hpp:157
CONSTEXPR14 auto operator/=(const R &alpha) -> Matrix2< T1, T2 > &
Divide matrix by a scalar.
Definition matrix2.hpp:126
constexpr auto det() const -> double
Calculate the determinant.
Definition matrix2.hpp:231
Vector2.
Definition vector2.hpp:19
#define CONSTEXPR14
Definition matrix2.hpp:15
Definition logger.hpp:10