system
The vocabulary types everything else shares
Loading...
Searching...
No Matches
Matrix.hpp
Go to the documentation of this file.
1
11
#pragma once
12
13
#include <cmath>
14
15
#include <type_traits>
16
#include <cstddef>
17
#include <cmath>
18
19
template
<
20
typename
T
, std::size_t
ROWS
, std::size_t
COLS
,
21
typename
=
typename
std::enable_if<ROWS >= 1 &&
COLS
>= 1>::type
22
>
23
struct
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>
83
Matrix<T, ROWS, OTHER_COLS>
operator*(
const
Matrix<T, COLS, OTHER_COLS>
&
other
)
const
{
84
Matrix<T, ROWS, OTHER_COLS>
result
{};
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
143
Matrix
transpose
()
const
{
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 ----------
154
using
Matrix3x3
=
Matrix<double, 3, 3>
;
155
using
Matrix4x4
=
Matrix<double, 4, 4>
;
epsilon_v
constexpr T epsilon_v
Definition
Constants.hpp:32
Matrix3x3
Matrix< double, 3, 3 > Matrix3x3
Definition
Matrix.hpp:154
Matrix4x4
Matrix< double, 4, 4 > Matrix4x4
Definition
Matrix.hpp:155
includes
Matrix.hpp
Generated by
1.9.8