Alexandria  2.18
Please provide a description of the project.
Piecewise.cpp
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 
28 #include <algorithm>
29 
30 namespace Euclid {
31 namespace MathUtils {
32 
34  if (m_knots.size() - functions.size() != 1) {
35  throw Elements::Exception() << "Invalid number of knots(" << m_knots.size() << ")-functions(" << m_functions.size() << ")";
36  }
37 
38  m_functions.reserve(functions.size());
39  std::transform(functions.begin(), functions.end(), std::back_inserter(m_functions),
40  [](std::shared_ptr<Function>& f) { return f->clone(); });
41 
42  auto knotsIter = m_knots.begin();
43  while (++knotsIter != m_knots.end()) {
44  if (*knotsIter <= *(knotsIter - 1)) {
45  throw Elements::Exception("knots must be strictly increasing");
46  }
47  }
48 }
49 
51  : m_knots{std::move(knots)}, m_functions{std::move(functions)} {
52  if (m_knots.size() - m_functions.size() != 1) {
53  throw Elements::Exception() << "Invalid number of knots(" << m_knots.size() << ")-functions(" << m_functions.size() << ")";
54  }
55  auto knotsIter = m_knots.begin();
56  while (++knotsIter != m_knots.end()) {
57  if (*knotsIter <= *(knotsIter - 1)) {
58  throw Elements::Exception("knots must be strictly increasing");
59  }
60  }
61 }
62 
64  return m_knots;
65 }
66 
68  return m_functions;
69 }
70 
71 double Piecewise::operator()(const double x) const {
72  auto knotsBegin = m_knots.begin();
73  if (x < *knotsBegin) {
74  return 0;
75  }
76  if (x == *knotsBegin) {
77  return (*m_functions[0])(x);
78  }
79  auto knotsEnd = m_knots.end();
80  auto findX = std::lower_bound(knotsBegin, knotsEnd, x);
81  if (findX == knotsEnd) {
82  return 0;
83  }
84  return (*m_functions[findX - knotsBegin - 1])(x);
85 }
86 
88  std::vector<std::unique_ptr<Function>> cloned_functions;
89  cloned_functions.reserve(m_functions.size());
90  for (auto& f : m_functions) {
91  cloned_functions.emplace_back(f->clone());
92  }
93  return std::unique_ptr<Function>{new Piecewise(m_knots, std::move(cloned_functions))};
94 }
95 
96 double Piecewise::integrate(const double x1, const double x2) const {
97  if (x1 == x2) {
98  return 0;
99  }
100  int direction = 1;
101  double min = x1;
102  double max = x2;
103  if (min > max) {
104  direction = -1;
105  min = x2;
106  max = x1;
107  }
108  double result = 0;
109  auto knotIter = std::upper_bound(m_knots.begin(), m_knots.end(), min);
110  if (knotIter != m_knots.begin()) {
111  --knotIter;
112  }
113  auto functionIter = m_functions.begin() + (knotIter - m_knots.begin());
114  while (++knotIter != m_knots.end()) {
115  auto prevKnotIter = knotIter - 1;
116  if (max <= *prevKnotIter) {
117  break;
118  }
119  if (min < *knotIter) {
120  double down = (min > *prevKnotIter) ? min : *prevKnotIter;
121  double up = (max < *knotIter) ? max : *knotIter;
122  result += Euclid::MathUtils::integrate(**functionIter, down, up);
123  }
124  ++functionIter;
125  }
126  return direction * result;
127 }
128 
129 } // namespace MathUtils
130 } // end of namespace Euclid
T back_inserter(T... args)
T begin(T... args)
std::vector< double > m_knots
A vector where the knots are kept.
Definition: Piecewise.h:96
double integrate(const double x1, const double x2) const override
Definition: Piecewise.cpp:96
const std::vector< double > & getKnots() const
Returns the knots of the piecewise function.
Definition: Piecewise.cpp:63
double operator()(const double) const override
Definition: Piecewise.cpp:71
std::vector< std::unique_ptr< Function > > m_functions
A vector where the sub-functions are kept.
Definition: Piecewise.h:98
Piecewise(std::vector< double > knots, std::vector< std::shared_ptr< Function >> functions)
Definition: Piecewise.cpp:33
const std::vector< std::unique_ptr< Function > > & getFunctions() const
Returns the functions in the ranges between the knots.
Definition: Piecewise.cpp:67
std::unique_ptr< Function > clone() const override
Definition: Piecewise.cpp:87
T emplace_back(T... args)
T end(T... args)
T lower_bound(T... args)
T move(T... args)
ELEMENTS_API double integrate(const Function &function, const double min, const double max, std::unique_ptr< NumericalIntegrationScheme > numericalIntegrationScheme=nullptr)
STL namespace.
T reserve(T... args)
T size(T... args)
T transform(T... args)
T upper_bound(T... args)