Alexandria  2.19
Please provide a description of the project.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Sqrt.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2021 Euclid Science Ground Segment
3  *
4  * This library is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License as published by the Free
6  * Software Foundation; either version 3.0 of the License, or (at your option)
7  * any later version.
8  *
9  * This library is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
25 #ifndef ALEXANDRIA_HISTOGRAM_BINNING_SQRT_H
26 #define ALEXANDRIA_HISTOGRAM_BINNING_SQRT_H
27 
28 #include <algorithm>
29 #include <cmath>
30 #include <numeric>
31 #include <vector>
32 
33 #include "Histogram/Histogram.h"
34 
35 namespace Euclid {
36 namespace Histogram {
37 namespace Binning {
38 
42 template <typename VarType>
43 class Sqrt : public BinStrategy<VarType> {
44 public:
45  template <typename Iterator>
46  void computeBins(Iterator begin, Iterator end) {
47  m_nbins = std::ceil(std::sqrt(end - begin));
48  auto minmax = std::minmax_element(begin, end);
49  m_step = (*minmax.second - *minmax.first) / m_nbins;
50  m_start = *minmax.first;
51  m_end = *minmax.second;
52  }
53 
54  ssize_t getBinIndex(VarType value) const final {
55  if (value == m_end)
56  return m_nbins - 1;
57  return (value - m_start) / m_step;
58  }
59 
60  std::pair<VarType, VarType> getBinEdges(size_t i) const final {
61  return std::make_pair(i * m_step + m_start, (i + 1) * m_step + m_start);
62  }
63 
64  VarType getEdge(size_t i) const final {
65  return i * m_step + m_start;
66  }
67 
68 private:
70  VarType m_start, m_step, m_end;
71 };
72 
73 } // end of namespace Binning
74 } // end of namespace Histogram
75 } // end of namespace Euclid
76 
77 #endif // ALEXANDRIA_HISTOGRAM_BINNING_SQRT_H
ssize_t getBinIndex(VarType value) const final
Definition: Sqrt.h:54
T ceil(T...args)
std::pair< VarType, VarType > getBinEdges(size_t i) const final
Definition: Sqrt.h:60
VarType getEdge(size_t i) const final
Definition: Sqrt.h:64
T minmax_element(T...args)
T make_pair(T...args)
T sqrt(T...args)
void computeBins(Iterator begin, Iterator end)
Definition: Sqrt.h:46