This repository has been archived by the owner on Sep 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Matrix.hpp
334 lines (263 loc) · 6.63 KB
/
Matrix.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/**
* @author frk, ReactiioN
* @date 08.03.2016
* @visit https://github.com/frk1
* https://thefrk.pw
* https://www.unknowncheats.me/forum/members/1067779.html
*
* https://github.com/ReactiioN1337
* https://reactiion.pw
* https://www.unknowncheats.me/forum/members/264622.html
*/
#pragma once
#include <array>
#include <string>
template < typename T, size_t T_Rows, size_t T_Cols >
class Matrix
{
public:
using array_t = std::array< T, T_Rows * T_Cols >;
using matrix_t = Matrix< T, T_Rows, T_Cols >;
using col_matrix_t = Matrix< T, 1, T_Cols >;
using row_matrix_t = Matrix< T, T_Rows, 1 >;
private:
array_t m_cValues;
public:
Matrix( void )
{
static_assert( std::is_arithmetic< T >::value, "Type T has to be arithmetic!" );
static_assert( T_Rows >= 1 && T_Cols >= 1, "Minimal row and col dimensions are 1." );
m_cValues.fill( static_cast<T>( 0 ) );
}
~Matrix( void )
{
}
explicit Matrix( std::array< T, ( T_Rows* T_Cols ) > cValues )
: m_cValues( cValues )
{
static_assert( std::is_arithmetic< T >::value, "Type T has to be arithmetic!" );
}
template < typename... K >
Matrix( K ... args )
{
static_assert( std::is_arithmetic< T >::value, "Type T has to be arithmetic!" );
m_cValues = std::array< T, T_Rows* T_Cols > { static_cast<T>( args )... };
}
constexpr static size_t rows( void )
{
return T_Rows;
}
constexpr static size_t cols( void )
{
return T_Cols;
}
constexpr static size_t size( void )
{
return T_Rows * T_Cols;
}
constexpr static bool isVector( void )
{
return T_Rows == 1 || T_Cols == 1;
}
bool empty( void ) const
{
return all_of( m_cValues.begin(), m_cValues.end(), []( T i )
{
return i == static_cast< T >( 0 );
} );
}
col_matrix_t row( size_t iRow ) const
{
std::array< T, T_Cols > values;
for( size_t i = 0; i < T_Cols; ++i )
values.at( i ) = at( iRow, i );
return col_matrix_t{ values };
}
row_matrix_t col( size_t iCol ) const
{
std::array< T, T_Rows > values;
for( size_t i = 0; i < T_Rows; ++i )
values.at( i ) = at( i, iCol );
return row_matrix_t{ values };
}
void row( size_t iRow, col_matrix_t mat )
{
for( size_t i = 0; i < T_Cols; ++i )
at( iRow, i ) = mat( i );
}
void col( size_t iCol, row_matrix_t mat )
{
for( size_t i = 0; i < T_Rows; ++i )
at( i, iCol ) = mat( i );
}
T& at( size_t i )
{
return m_cValues.at( i );
}
const T& at( size_t i ) const
{
return m_cValues.at( i );
}
T& at( size_t iRow, size_t iCol )
{
return m_cValues.at( iRow * T_Cols + iCol );
}
const T& at( size_t iRow, size_t iCol ) const
{
return m_cValues.at( iRow * T_Cols + iCol );
}
T& operator () ( size_t i )
{
return at( i );
}
const T& operator () ( size_t i ) const
{
return at( i );
}
T& operator () ( size_t iRow, size_t iCol )
{
return at( iRow, iCol );
}
const T& operator () ( size_t iRow, size_t iCol ) const
{
return at( iRow, iCol );
}
matrix_t operator + ( T other ) const
{
auto buf = *this;
buf += other;
return buf;
}
matrix_t operator - ( T other ) const
{
auto buf = *this;
buf -= other;
return buf;
}
matrix_t operator * ( T other ) const
{
auto buf = *this;
buf *= other;
return buf;
}
matrix_t operator / ( T other ) const
{
auto buf = *this;
buf /= other;
return buf;
}
matrix_t& operator += ( T other )
{
for( auto& val : m_cValues )
val += other;
return *this;
}
matrix_t& operator -= ( T other )
{
for( auto& val : m_cValues )
val -= other;
return *this;
}
matrix_t& operator *= ( T other )
{
for( auto& val : m_cValues )
val *= other;
return *this;
}
matrix_t& operator /= ( T other )
{
for( auto& val : m_cValues )
val /= other;
return *this;
}
matrix_t operator + ( const matrix_t& other ) const
{
auto buf = *this;
for( size_t i = 0; i < size(); ++i )
buf( i ) += other( i );
return buf;
}
matrix_t operator - ( const matrix_t& other ) const
{
auto buf = *this;
for( size_t i = 0; i < size(); ++i )
buf( i ) -= other( i );
return buf;
}
matrix_t& operator += ( const matrix_t& other )
{
for( size_t i = 0; i < size(); ++i )
at( i ) += other( i );
return ( *this );
}
bool operator == ( const matrix_t& other ) const
{
for( size_t i = 0; i < size(); ++i )
if( at( i ) != other.at( i ) )
return false;
return true;
}
bool operator != ( const matrix_t& other ) const
{
return !( ( *this ) == other );
}
matrix_t& operator -= ( const matrix_t& other )
{
for( size_t i = 0; i < size(); ++i )
at( i ) -= other( i );
return ( *this );
}
T norm( void ) const
{
static_assert( matrix_t::isVector(), "Matrix::norm can only be used on vectors!" );
T buf = static_cast<T>( 0 );
for( auto v : m_cValues )
buf += ( v * v );
return sqrt( buf );
}
void normalize( void )
{
static_assert( matrix_t::isVector(), "Matrix::normalize can only be used on vectors!" );
( *this ) /= this->norm();
}
matrix_t normalized( void ) const
{
static_assert( matrix_t::isVector(), "Matrix::normalized can only be used on vectors!" );
auto buf = *this;
buf.normalize();
return buf;
}
friend std::ostream& operator <<( std::ostream& os, const matrix_t& v )
{
for( size_t i = 0; i < T_Rows; ++i )
{
for( size_t j = 0; j < T_Cols; j++ )
os << v.at( i, j ) << "\t";
os << "\n";
}
return os;
}
template < typename K, size_t OROWS, size_t OCOLS >
T dot( const Matrix< K, OROWS, OCOLS >& other ) const
{
static_assert( matrix_t::isVector() && Matrix< K, OROWS, OCOLS >::isVector(), "Matrix::dot can only be used on vectors!" );
T buf = static_cast<T>( 0 );
for( size_t i = 0; i < (std::min)( size(), other.size() ); ++i )
buf += at( i ) * other.at( i );
return buf;
}
matrix_t cross( const matrix_t& other ) const
{
static_assert( matrix_t::size() == 3, "Matrix::cross can only be called for 3 dimensional vectors!" );
return{
at( 1 )* other.at( 2 ) - at( 2 )* other.at( 1 ),
at( 2 )* other.at( 0 ) - at( 0 )* other.at( 2 ),
at( 0 )* other.at( 1 ) - at( 1 )* other.at( 0 ),
};
}
matrix_t ncross( const matrix_t& other ) const
{
static_assert( matrix_t::size() == 3, "Matrix::ncross can only be called for 3 dimensional vectors!" );
return cross( other ).normalized();
}
};