diff --git a/matrix.cpp b/matrix.cpp index 32ffeb24..10eb2787 100644 --- a/matrix.cpp +++ b/matrix.cpp @@ -190,14 +190,10 @@ matrix::operator* (const point &rhs) const //----------------------------------------------------------------------------- template matrix -matrix::operator* (T f) const +matrix::operator* (T t) const { matrix out; - - for (size_t i = 0; i < S; ++i) - for (size_t j = 0; j < S; ++j) - out.values[i][j] = values[i][j] * f; - + std::transform (cbegin (), cend (), std::begin (out), [t] (auto x) { return x * t; }); return out; } @@ -205,12 +201,9 @@ matrix::operator* (T f) const //----------------------------------------------------------------------------- template matrix& -matrix::operator*= (T f) +matrix::operator*= (T t) { - for (size_t i = 0; i < S; ++i) - for (size_t j = 0; j < S; ++j) - values[i][j] *= f; - + std::transform (cbegin (), cend (), begin (), [t] (auto x) { return x * t; }); return *this; } @@ -220,12 +213,8 @@ template util::matrix util::matrix::operator/ (T t) const { - auto out = *this; - - for (auto &i: out.values) - for (auto &j: i) - j /= t; - + matrix out; + std::transform (cbegin (), cend (), std::begin (out), [t] (auto x) { return x / t; }); return out; } @@ -233,12 +222,9 @@ util::matrix::operator/ (T t) const //----------------------------------------------------------------------------- template matrix& -matrix::operator/= (T s) +matrix::operator/= (T t) { - for (size_t r = 0; r < rows; ++r) - for (size_t c = 0; c < cols; ++c) - values[r][c] /= s; - + std::transform (cbegin (), cend (), begin (), [t] (auto x) { return x / t; }); return *this; } @@ -248,11 +234,8 @@ template bool matrix::operator== (const matrix &rhs) const { - for (size_t r = 0; r < rows; ++r) - for (size_t c = 0; c < cols; ++c) - if (!almost_equal (rhs.values[r][c], values[r][c])) - return false; - return true; + constexpr bool (*comparator)(const T&,const T&) = util::almost_equal; + return std::equal (cbegin (), cend (), std::cbegin (rhs), comparator); } diff --git a/matrix.ipp b/matrix.ipp index 6eb6a154..fff3819b 100644 --- a/matrix.ipp +++ b/matrix.ipp @@ -101,15 +101,14 @@ util::matrix util::matrix::cast (void) const { util::matrix out; - - for (size_t i = 0; i < rows; ++i) - for (size_t j = 0; j < cols; ++j) - out.values[i][j] = values[i][j]; - + std::copy (cbegin (), cend (), std::begin (out)); return out; } +} + + ///////////////////////////////////////////////////////////////////////////////// //template //T