Alexandria  2.22.0
Please provide a description of the project.
Piecewise.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 MATHUTILS_PIECEWISE_H
26 #define MATHUTILS_PIECEWISE_H
27 
28 #include <memory>
29 #include <vector>
30 
31 #include "ElementsKernel/Export.h"
32 
34 
35 namespace Euclid {
36 namespace MathUtils {
37 
49 public:
50  virtual ~PiecewiseBase() = default;
51 
53  const std::vector<double>& getKnots() const {
54  return m_knots;
55  }
56 
57 protected:
58  explicit PiecewiseBase(std::vector<double> knots) : m_knots(std::move(knots)) {}
59 
60  ssize_t findKnot(double x) const {
61  auto knotsBegin = m_knots.begin();
62  if (x < *knotsBegin) {
63  return -1;
64  }
65  if (x == *knotsBegin) {
66  return 0;
67  }
68  auto knotsEnd = m_knots.end();
69  auto findX = std::lower_bound(knotsBegin, knotsEnd, x);
70  return findX - knotsBegin;
71  }
72 
75 };
76 
87 class ELEMENTS_API Piecewise final : public PiecewiseBase {
88 
89 public:
104 
106 
108  virtual ~Piecewise() = default;
109 
111  const std::vector<std::unique_ptr<Function>>& getFunctions() const;
112 
115  double operator()(const double) const override;
116 
119  void operator()(const std::vector<double>& xs, std::vector<double>& out) const override;
120 
123  std::unique_ptr<Function> clone() const override;
124 
132  double integrate(const double x1, const double x2) const override;
133 
134 private:
137 };
138 
139 } // namespace MathUtils
140 } // end of namespace Euclid
141 
142 #endif /* MATHUTILS_PIECEWISE_H */
Interface representing an integrable function.
Definition: Integrable.h:44
Represents a piecewise function.
Definition: Piecewise.h:48
ssize_t findKnot(double x) const
Definition: Piecewise.h:60
PiecewiseBase(std::vector< double > knots)
Definition: Piecewise.h:58
virtual ~PiecewiseBase()=default
std::vector< double > m_knots
A vector where the knots are kept.
Definition: Piecewise.h:74
const std::vector< double > & getKnots() const
Returns the knots of the piecewise function.
Definition: Piecewise.h:53
Represents a piecewise function.
Definition: Piecewise.h:87
virtual ~Piecewise()=default
Default destructor.
std::vector< std::unique_ptr< Function > > m_functions
A vector where the sub-functions are kept.
Definition: Piecewise.h:136
#define ELEMENTS_API
T lower_bound(T... args)
ELEMENTS_API double integrate(const Function &function, const double min, const double max, std::unique_ptr< NumericalIntegrationScheme > numericalIntegrationScheme=nullptr)
STL namespace.