matrix: add free elementwise add/sub

This commit is contained in:
Danny Robson 2016-08-15 18:53:52 +10:00
parent f6ceb5cdee
commit 5ef6f936a5
2 changed files with 39 additions and 1 deletions

View File

@ -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>

View File

@ -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>