P-E-R-R-Y
Utilitaires, contrats et vendors du moteur, en une seule doc
Loading...
Searching...
No Matches
Matrix.hpp
Go to the documentation of this file.
1
10
11#pragma once
12
13#include <cmath>
14
15#include <type_traits>
16#include <cstddef>
17#include <cmath>
18
19template <
20 typename T, std::size_t ROWS, std::size_t COLS,
21 typename = typename std::enable_if<ROWS >= 1 && COLS >= 1>::type
22>
23struct Matrix {
24 T data[ROWS][COLS]{};
25
26 // Statics
27 template <typename = typename std::enable_if<std::is_arithmetic<T>::value && ROWS == COLS>::type>
28 static Matrix identity() {
29 Matrix mat{};
30 for (std::size_t i = 0; i < ROWS; ++i) {
31 mat.data[i][i] = T(1);
32 }
33 return mat;
34 }
35
36 static Matrix zero() {
37 return Matrix{};
38 }
39
40 template<typename = typename std::enable_if<ROWS == COLS>::type>
41 static Matrix diagonal(const T (&values)[ROWS]) {
42 Matrix mat{};
43 for (std::size_t i = 0; i < ROWS; ++i)
44 mat.data[i][i] = values[i];
45 return mat;
46 }
47
48 // Access operator
49 T& operator()(std::size_t i, std::size_t j) { return data[i][j]; }
50 const T& operator()(std::size_t i, std::size_t j) const { return data[i][j]; }
51
52 // Unary operators
53 template <typename = typename std::enable_if<std::is_arithmetic<T>::value && ROWS == COLS>::type>
54 Matrix operator-() const {
55 Matrix result{};
56 for (std::size_t i = 0; i < ROWS; ++i)
57 for (std::size_t j = 0; j < COLS; ++j)
58 result.data[i][j] = -data[i][j];
59 return result;
60 }
61
62 // Arythmetic operator
63 template <typename = typename std::enable_if<std::is_arithmetic<T>::value && ROWS == COLS>::type>
64 Matrix operator+(const Matrix& other) const {
65 Matrix result{};
66 for (std::size_t i = 0; i < ROWS; ++i)
67 for (std::size_t j = 0; j < COLS; ++j)
68 result.data[i][j] = data[i][j] + other.data[i][j];
69 return result;
70 }
71
72 template <typename = typename std::enable_if<std::is_arithmetic<T>::value && ROWS == COLS>::type>
73 Matrix operator-(const Matrix& other) const {
74 Matrix result{};
75 for (std::size_t i = 0; i < ROWS; ++i)
76 for (std::size_t j = 0; j < COLS; ++j)
77 result.data[i][j] = data[i][j] - other.data[i][j];
78 return result;
79 }
80
81 //
82 template <std::size_t OTHER_COLS, typename = typename std::enable_if<std::is_arithmetic<T>::value && ROWS == COLS>::type>
85 for (std::size_t i = 0; i < ROWS; ++i)
86 for (std::size_t j = 0; j < OTHER_COLS; ++j)
87 for (std::size_t k = 0; k < COLS; ++k)
88 result.data[i][j] += data[i][k] * other.data[k][j];
89 return result;
90 }
91
92
93 template <typename = typename std::enable_if<std::is_arithmetic<T>::value && ROWS == COLS>::type>
94 Matrix& operator+=(const Matrix& other) {
95 for (std::size_t i = 0; i < ROWS; ++i)
96 for (std::size_t j = 0; j < COLS; ++j)
97 data[i][j] += other.data[i][j];
98 return *this;
99 }
100
101 template <typename = typename std::enable_if<std::is_arithmetic<T>::value && ROWS == COLS>::type>
102 Matrix operator/(T scalar) const {
103 Matrix result{};
104 for (std::size_t i = 0; i < ROWS; ++i)
105 for (std::size_t j = 0; j < COLS; ++j)
106 result.data[i][j] = data[i][j] / scalar;
107 return result;
108 }
109
110 template <typename = typename std::enable_if<std::is_arithmetic<T>::value && ROWS == COLS>::type>
111 Matrix& operator*=(T scalar) {
112 for (std::size_t i = 0; i < ROWS; ++i)
113 for (std::size_t j = 0; j < COLS; ++j)
114 data[i][j] *= scalar;
115 return *this;
116 }
117
118 template <typename = typename std::enable_if<std::is_arithmetic<T>::value && ROWS == COLS>::type>
119 Matrix& operator/=(T scalar) {
120 for (std::size_t i = 0; i < ROWS; ++i)
121 for (std::size_t j = 0; j < COLS; ++j)
122 data[i][j] /= scalar;
123 return *this;
124 }
125
126 template <typename = typename std::enable_if<std::is_arithmetic<T>::value && ROWS == COLS>::type>
127 Matrix& operator-=(const Matrix& other) {
128 for (std::size_t i = 0; i < ROWS; ++i)
129 for (std::size_t j = 0; j < COLS; ++j)
130 data[i][j] -= other.data[i][j];
131 return *this;
132 }
133
134 template <typename = typename std::enable_if<std::is_arithmetic<T>::value && ROWS == COLS>::type>
135 Matrix operator*(T scalar) const {
136 Matrix result{};
137 for (std::size_t i = 0; i < ROWS; ++i)
138 for (std::size_t j = 0; j < COLS; ++j)
139 result.data[i][j] = data[i][j] * scalar;
140 return result;
141 }
142
144 Matrix result{};
145 for (std::size_t i = 0; i < ROWS; ++i)
146 for (std::size_t j = 0; j < COLS; ++j)
147 result.data[j][i] = data[i][j];
148 return result;
149 }
150
151};
152
153// ---------- Aliases ----------
156
Matrix< double, 3, 3 > Matrix3x3
Definition Matrix.hpp:154
Matrix< double, 4, 4 > Matrix4x4
Definition Matrix.hpp:155
Definition Matrix.hpp:23
static Matrix diagonal(const T(&values)[ROWS])
Definition Matrix.hpp:41
Matrix & operator+=(const Matrix &other)
Definition Matrix.hpp:94
Matrix operator-() const
Definition Matrix.hpp:54
Matrix transpose() const
Definition Matrix.hpp:143
static Matrix identity()
Definition Matrix.hpp:28
Matrix operator-(const Matrix &other) const
Definition Matrix.hpp:73
Matrix operator/(T scalar) const
Definition Matrix.hpp:102
Matrix & operator/=(T scalar)
Definition Matrix.hpp:119
double data[ROWS][COLS]
Definition Matrix.hpp:24
const T & operator()(std::size_t i, std::size_t j) const
Definition Matrix.hpp:50
Matrix operator*(T scalar) const
Definition Matrix.hpp:135
Matrix operator+(const Matrix &other) const
Definition Matrix.hpp:64
Matrix & operator-=(const Matrix &other)
Definition Matrix.hpp:127
Matrix & operator*=(T scalar)
Definition Matrix.hpp:111
static Matrix zero()
Definition Matrix.hpp:36
T & operator()(std::size_t i, std::size_t j)
Definition Matrix.hpp:49
Matrix< T, ROWS, OTHER_COLS > operator*(const Matrix< T, COLS, OTHER_COLS > &other) const
Definition Matrix.hpp:83