From 1a95d94f3ffd64b13e1a33ac9ea80c4cc1fba09f Mon Sep 17 00:00:00 2001
From: Javier Cervantes Villanueva <javier.cervantes.villanueva@cern.ch>
Date: Wed, 6 Mar 2019 17:29:20 +0100
Subject: [PATCH] [VecOps][ROOT-10012] Add max and min operations

Add tests with double and int types
---
 math/vecops/inc/ROOT/RVec.hxx    | 32 ++++++++++++++++++++++++++++++++
 math/vecops/test/vecops_rvec.cxx | 14 ++++++++++++++
 2 files changed, 46 insertions(+)

diff --git a/math/vecops/inc/ROOT/RVec.hxx b/math/vecops/inc/ROOT/RVec.hxx
index 5b435fa0286..c852a0706fd 100644
--- a/math/vecops/inc/ROOT/RVec.hxx
+++ b/math/vecops/inc/ROOT/RVec.hxx
@@ -715,6 +715,38 @@ double Mean(const RVec<T> &v)
    return double(Sum(v)) / v.size();
 }
 
+/// Get the greatest element of an RVec
+///
+/// Example code, at the ROOT prompt:
+/// ~~~~{.cpp}
+/// using namespace ROOT::VecOps;
+/// RVec<float> v {1.f, 2.f, 4.f};
+/// auto v_max = Max(v)
+/// v_max
+/// (float) 4.f
+/// ~~~~
+template <typename T>
+T Max(const RVec<T> &v)
+{
+   return *std::max_element(v.begin(), v.end());
+}
+
+/// Get the smallest element of an RVec
+///
+/// Example code, at the ROOT prompt:
+/// ~~~~{.cpp}
+/// using namespace ROOT::VecOps;
+/// RVec<float> v {1.f, 2.f, 4.f};
+/// auto v_min = Min(v)
+/// v_min
+/// (float) 1.f
+/// ~~~~
+template <typename T>
+T Min(const RVec<T> &v)
+{
+   return *std::min_element(v.begin(), v.end());
+}
+
 /// Get the variance of the elements of an RVec
 ///
 /// The return type is a double precision floating point number.
diff --git a/math/vecops/test/vecops_rvec.cxx b/math/vecops/test/vecops_rvec.cxx
index c32888c3d91..f0f0e0de1bd 100644
--- a/math/vecops/test/vecops_rvec.cxx
+++ b/math/vecops/test/vecops_rvec.cxx
@@ -592,20 +592,34 @@ TEST(VecOps, SimpleStatOps)
    ROOT::VecOps::RVec<double> v1 {42.};
    ASSERT_DOUBLE_EQ(Sum(v1), 42.);
    ASSERT_DOUBLE_EQ(Mean(v1), 42.);
+   ASSERT_DOUBLE_EQ(Max(v1), 42.);
+   ASSERT_DOUBLE_EQ(Min(v1), 42.);
    ASSERT_DOUBLE_EQ(StdDev(v1), 0.);
    ASSERT_DOUBLE_EQ(Var(v1), 0.);
 
    ROOT::VecOps::RVec<double> v2 {1., 2., 3.};
    ASSERT_DOUBLE_EQ(Sum(v2), 6.);
    ASSERT_DOUBLE_EQ(Mean(v2), 2.);
+   ASSERT_DOUBLE_EQ(Max(v2), 3.);
+   ASSERT_DOUBLE_EQ(Min(v2), 1.);
    ASSERT_DOUBLE_EQ(Var(v2), 1.);
    ASSERT_DOUBLE_EQ(StdDev(v2), 1.);
 
    ROOT::VecOps::RVec<double> v3 {10., 20., 32.};
    ASSERT_DOUBLE_EQ(Sum(v3), 62.);
    ASSERT_DOUBLE_EQ(Mean(v3), 20.666666666666668);
+   ASSERT_DOUBLE_EQ(Max(v3), 32.);
+   ASSERT_DOUBLE_EQ(Min(v3), 10.);
    ASSERT_DOUBLE_EQ(Var(v3), 121.33333333333337);
    ASSERT_DOUBLE_EQ(StdDev(v3), 11.015141094572206);
+
+   ROOT::VecOps::RVec<int> v4 {1, 2, 3};
+   ASSERT_DOUBLE_EQ(Sum(v4), 6.);
+   ASSERT_DOUBLE_EQ(Mean(v4), 2.);
+   ASSERT_DOUBLE_EQ(Max(v4), 3);
+   ASSERT_DOUBLE_EQ(Min(v4), 1);
+   ASSERT_DOUBLE_EQ(Var(v4), 1.);
+   ASSERT_DOUBLE_EQ(StdDev(v4), 1.);
 }
 
 TEST(VecOps, Any)
-- 
GitLab