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