slice: add min and max functions

This commit is contained in:
bresch 2021-10-06 13:59:43 +02:00 committed by Daniel Agar
parent 007a7f78ae
commit d03bf42f60
2 changed files with 39 additions and 0 deletions

View File

@ -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<Type,M,N>* _data;

View File

@ -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<float, 3, 1> L;
L(0,0) = -1;