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