Browse Source

slice: add min and max functions

master
bresch 3 years ago committed by Daniel Agar
parent
commit
d03bf42f60
  1. 34
      matrix/Slice.hpp
  2. 5
      test/slice.cpp

34
matrix/Slice.hpp

@ -262,6 +262,40 @@ public:
return norm_squared() > testVal*testVal; 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: private:
size_t _x0, _y0; size_t _x0, _y0;
Matrix<Type,M,N>* _data; Matrix<Type,M,N>* _data;

5
test/slice.cpp

@ -125,6 +125,11 @@ int main()
TEST(!v5.xy().longerThan(5.f)); TEST(!v5.xy().longerThan(5.f));
TEST(isEqualF(5.f, v5.xy().norm())); 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 // assign scalar value to slice
Matrix<float, 3, 1> L; Matrix<float, 3, 1> L;
L(0,0) = -1; L(0,0) = -1;

Loading…
Cancel
Save