Alexandria  2.16
Please provide a description of the project.
Sqrt.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2020 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 <cmath>
29 #include <algorithm>
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 
46  template<typename Iterator>
47  void computeBins(Iterator begin, Iterator end) {
48  m_nbins = std::ceil(std::sqrt(end - begin));
49  auto minmax = std::minmax_element(begin, end);
50  m_step = (*minmax.second - *minmax.first) / m_nbins;
51  m_start = *minmax.first;
52  m_end = *minmax.second;
53  }
54 
55  ssize_t getBinIndex(VarType value) const final {
56  if (value == m_end)
57  return m_nbins - 1;
58  return (value - m_start) / m_step;
59  }
60 
61  std::pair<VarType, VarType> getBinEdges(size_t i) const final {
62  return std::make_pair(i * m_step + m_start, (i + 1) * m_step + m_start);
63  }
64 
65  VarType getEdge(size_t i) const final {
66  return i * m_step + m_start;
67  }
68 
69 private:
71  VarType m_start, m_step, m_end;
72 };
73 
74 } // end of namespace Binning
75 } // end of namespace Histogram
76 } // end of namespace Euclid
77 
78 #endif // ALEXANDRIA_HISTOGRAM_BINNING_SQRT_H
Euclid::Histogram::Binning::Sqrt::getEdge
VarType getEdge(size_t i) const final
Definition: Sqrt.h:65
std::pair
Euclid::Histogram::Binning::Sqrt::computeBins
void computeBins(Iterator begin, Iterator end)
Definition: Sqrt.h:47
Euclid::Histogram::Binning::Sqrt::getBinIndex
ssize_t getBinIndex(VarType value) const final
Definition: Sqrt.h:55
Euclid::Histogram::Binning::Sqrt::m_step
VarType m_step
Definition: Sqrt.h:71
std::sqrt
T sqrt(T... args)
Euclid::Histogram::Binning::Sqrt::m_start
VarType m_start
Definition: Sqrt.h:71
Euclid::Histogram::Binning::Sqrt::getBinEdges
std::pair< VarType, VarType > getBinEdges(size_t i) const final
Definition: Sqrt.h:61
Euclid::Histogram::BinStrategy::m_nbins
size_t m_nbins
Definition: Histogram.h:127
Euclid::Histogram::Binning::Sqrt::m_end
VarType m_end
Definition: Sqrt.h:71
std::ceil
T ceil(T... args)
std::minmax_element
T minmax_element(T... args)
Histogram.h
Euclid::Histogram::Binning::Sqrt
Definition: Sqrt.h:43
std::make_pair
T make_pair(T... args)
Euclid
Definition: InstOrRefHolder.h:29
Euclid::Histogram::BinStrategy
Definition: Histogram.h:49