diff --git a/src/Math/RectangularMatrix.h b/src/Math/RectangularMatrix.h index 7a0f45101..56c65e66d 100644 --- a/src/Math/RectangularMatrix.h +++ b/src/Math/RectangularMatrix.h @@ -344,6 +344,66 @@ template class RectangularMatrix { return out; } + /** @brief Sum of values in the matrix */ + T sum() const { + T out(_data[0].sum()); + + for(std::size_t i = 1; i != cols; ++i) + out += _data[i].sum(); + + return out; + } + + /** @brief Product of values in the matrix */ + T product() const { + T out(_data[0].product()); + + for(std::size_t i = 1; i != cols; ++i) + out *= _data[i].product(); + + return out; + } + + /** @brief Minimal value in the matrix */ + T min() const { + T out(_data[0].min()); + + for(std::size_t i = 1; i != cols; ++i) + out = std::min(out, _data[i].min()); + + return out; + } + + /** @brief Minimal absolute value in the matrix */ + T minAbs() const { + T out(_data[0].minAbs()); + + for(std::size_t i = 1; i != cols; ++i) + out = std::min(out, _data[i].minAbs()); + + return out; + } + + /** @brief Maximal value in the matrix */ + T max() const { + T out(_data[0].max()); + + for(std::size_t i = 1; i != cols; ++i) + out = std::max(out, _data[i].max()); + + return out; + } + + /** @brief Maximal absolute value in the matrix */ + T maxAbs() const { + T out(_data[0].maxAbs()); + + for(std::size_t i = 1; i != cols; ++i) + out = std::max(out, _data[i].maxAbs()); + + return out; + } + private: /* Implementation for RectangularMatrix::RectangularMatrix(const RectangularMatrix&) */ template inline constexpr explicit RectangularMatrix(Implementation::Sequence, const RectangularMatrix& matrix): _data{Vector(matrix[sequence])...} {} diff --git a/src/Math/Test/RectangularMatrixTest.cpp b/src/Math/Test/RectangularMatrixTest.cpp index 53d74418c..d8b9adf19 100644 --- a/src/Math/Test/RectangularMatrixTest.cpp +++ b/src/Math/Test/RectangularMatrixTest.cpp @@ -42,6 +42,13 @@ class RectangularMatrixTest: public Corrade::TestSuite::Tester { void transposed(); void diagonal(); + void sum(); + void product(); + void min(); + void minAbs(); + void max(); + void maxAbs(); + void debug(); void configuration(); }; @@ -73,6 +80,13 @@ RectangularMatrixTest::RectangularMatrixTest() { &RectangularMatrixTest::transposed, &RectangularMatrixTest::diagonal, + &RectangularMatrixTest::sum, + &RectangularMatrixTest::product, + &RectangularMatrixTest::min, + &RectangularMatrixTest::minAbs, + &RectangularMatrixTest::max, + &RectangularMatrixTest::maxAbs, + &RectangularMatrixTest::debug, &RectangularMatrixTest::configuration); } @@ -284,6 +298,52 @@ void RectangularMatrixTest::diagonal() { CORRADE_COMPARE(b.diagonal(), diagonal); } +void RectangularMatrixTest::sum() { + Matrix2 matrix(Vector2(1.0f, 2.0f), + Vector2(3.0f, 4.0f)); + CORRADE_COMPARE(matrix.sum(), 10.0f); +} + +void RectangularMatrixTest::product() { + Matrix2 matrix(Vector2(1.0f, 2.0f), + Vector2(3.0f, 4.0f)); + CORRADE_COMPARE(matrix.product(), 24.0f); +} + +void RectangularMatrixTest::min() { + /* Check also that initial value isn't initialized to 0 */ + Matrix2 matrix(Vector2(-2.0f, 1.0f), + Vector2(3.0f, 4.0f)); + CORRADE_COMPARE(matrix.min(), -2.0f); +} + +void RectangularMatrixTest::minAbs() { + /* Check that initial value is absolute and also all others */ + Matrix2 a(Vector2(-2.0f, 1.0f), + Vector2(3.0f, 4.0f)); + Matrix2 b(Vector2(3.0f, 4.0f), + Vector2(-2.0f, 1.0f)); + CORRADE_COMPARE(a.minAbs(), 1.0f); + CORRADE_COMPARE(a.minAbs(), 1.0f); +} + +void RectangularMatrixTest::max() { + /* Check also that initial value isn't initialized to 0 */ + Matrix2 matrix(Vector2(-2.0f, -1.0f), + Vector2(-3.0f, -4.0f)); + CORRADE_COMPARE(matrix.max(), -1.0f); +} + +void RectangularMatrixTest::maxAbs() { + /* Check that initial value is absolute and also all others */ + Matrix2 a(Vector2(2.0f, 1.0f), + Vector2(3.0f, -4.0f)); + Matrix2 b(Vector2(3.0f, -4.0f), + Vector2(2.0f, 1.0f)); + CORRADE_COMPARE(a.maxAbs(), 4.0f); + CORRADE_COMPARE(b.maxAbs(), 4.0f); +} + void RectangularMatrixTest::debug() { Matrix3x4 m(Vector4(3.0f, 5.0f, 8.0f, 4.0f), Vector4(4.0f, 4.0f, 7.0f, 3.0f),