matrix: add free elementwise add/sub
This commit is contained in:
parent
f6ceb5cdee
commit
5ef6f936a5
13
matrix.hpp
13
matrix.hpp
@ -93,7 +93,18 @@ namespace util {
|
||||
|
||||
|
||||
template <size_t S, typename T>
|
||||
T determinant (const matrix<S,T>&);
|
||||
constexpr
|
||||
matrix<S,T>
|
||||
operator+ (const matrix<S,T>&, const matrix<S,T>&);
|
||||
|
||||
template <size_t S, typename T>
|
||||
constexpr
|
||||
matrix<S,T>
|
||||
operator- (const matrix<S,T>&, const matrix<S,T>&);
|
||||
|
||||
template <size_t S, typename T>
|
||||
T
|
||||
determinant (const matrix<S,T>&);
|
||||
|
||||
template <size_t S, typename T>
|
||||
matrix<S,T>
|
||||
|
27
matrix.ipp
27
matrix.ipp
@ -106,6 +106,33 @@ util::matrix<S,T>::cast (void) const
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
#define MATRIX_ELEMENT_OP(OP) \
|
||||
template <size_t S, typename T> \
|
||||
constexpr \
|
||||
util::matrix<S,T> \
|
||||
util::operator OP ( \
|
||||
const util::matrix<S,T> &a, \
|
||||
const util::matrix<S,T> &b) \
|
||||
{ \
|
||||
static_assert ( \
|
||||
a.rows == b.rows && a.cols == b.cols, \
|
||||
"matrix dimensions must match for elementwise operations" \
|
||||
); \
|
||||
\
|
||||
util::matrix<S,T> res {}; \
|
||||
\
|
||||
for (size_t i = 0; i < a.rows; ++i) \
|
||||
for (size_t j = 0; j < a.cols; ++j) \
|
||||
res[i][j] = a[i][j] OP b[i][j]; \
|
||||
\
|
||||
return res; \
|
||||
}
|
||||
|
||||
MATRIX_ELEMENT_OP(-)
|
||||
MATRIX_ELEMENT_OP(+)
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
template <size_t S, typename T>
|
||||
util::matrix<S,T>
|
||||
|
Loading…
Reference in New Issue
Block a user