From d03bf42f6071f71cbc71bb820a0a4479575e9931 Mon Sep 17 00:00:00 2001 From: bresch Date: Wed, 6 Oct 2021 13:59:43 +0200 Subject: [PATCH] slice: add min and max functions --- matrix/Slice.hpp | 34 ++++++++++++++++++++++++++++++++++ test/slice.cpp | 5 +++++ 2 files changed, 39 insertions(+) diff --git a/matrix/Slice.hpp b/matrix/Slice.hpp index 6ab54c0be3..f98ac483c0 100644 --- a/matrix/Slice.hpp +++ b/matrix/Slice.hpp @@ -262,6 +262,40 @@ public: return norm_squared() > testVal*testVal; } + Type max() const + { + Type max_val = (*this)(0,0); + + for (size_t i = 0; i < P; i++) { + for (size_t j = 0; j < Q; j++) { + Type val = (*this)(i,j); + + if (val > max_val) { + max_val = val; + } + } + } + + return max_val; + } + + Type min() const + { + Type min_val = (*this)(0,0); + + for (size_t i = 0; i < P; i++) { + for (size_t j = 0; j < Q; j++) { + Type val = (*this)(i,j); + + if (val < min_val) { + min_val = val; + } + } + } + + return min_val; + } + private: size_t _x0, _y0; Matrix* _data; diff --git a/test/slice.cpp b/test/slice.cpp index a8db0acd47..6f838f0ccf 100644 --- a/test/slice.cpp +++ b/test/slice.cpp @@ -125,6 +125,11 @@ int main() TEST(!v5.xy().longerThan(5.f)); TEST(isEqualF(5.f, v5.xy().norm())); + // min/max + TEST(m33.row(1).max() == 5); + TEST(m33.col(0).min() == 0); + TEST((m33.slice<2,2>(1,1).max()) == 10); + // assign scalar value to slice Matrix L; L(0,0) = -1;