npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
maxAbsValue.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_MAXABSVALUE_HH_
2 #define NPSTAT_MAXABSVALUE_HH_
3 
4 /*!
5 // \file maxAbsValue.hh
6 //
7 // \brief Maximum absolute value in an array
8 //
9 // Author: I. Volobouev
10 //
11 // March 2023
12 */
13 
14 #include <cassert>
15 #include <stdexcept>
16 #include <cmath>
17 #include <cstdlib>
18 
19 namespace npstat {
20  template<typename T>
21  inline auto maxAbsValue(
22  const T* x, const unsigned long len) -> decltype(std::abs(*x))
23  {
24  typedef decltype(std::abs(*x)) return_type;
25 
26  if (!len) throw std::invalid_argument(
27  "In npstat::maxAbsValue: empty input array");
28  assert(x);
29  return_type maxValue = return_type();
30  for (unsigned long i=0; i<len; ++i)
31  {
32  const return_type v = std::abs(*x++);
33  if (maxValue < v)
34  maxValue = v;
35  }
36  return maxValue;
37  }
38 }
39 
40 #endif // NPSTAT_MAXABSVALUE_HH_
Definition: AbsArrayProjector.hh:14