Browse Source

Math: added RectangularMatrix::sum(),product(),{min,max}(),{min,max}Abs().

pull/7/head
Vladimír Vondruš 13 years ago
parent
commit
0f547d7e8e
  1. 60
      src/Math/RectangularMatrix.h
  2. 60
      src/Math/Test/RectangularMatrixTest.cpp

60
src/Math/RectangularMatrix.h

@ -344,6 +344,66 @@ template<std::size_t cols, std::size_t rows, class T> 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<cols, rows, T>::RectangularMatrix(const RectangularMatrix<cols, rows, U>&) */
template<class U, std::size_t ...sequence> inline constexpr explicit RectangularMatrix(Implementation::Sequence<sequence...>, const RectangularMatrix<cols, rows, U>& matrix): _data{Vector<rows, T>(matrix[sequence])...} {}

60
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),

Loading…
Cancel
Save