33 Piecewise::Piecewise(std::vector<double> knots, std::vector<std::shared_ptr<Function> > functions)
34 : m_knots{std::move(knots)} {
35 if (m_knots.size() - functions.size() != 1) {
37 <<
")-functions(" << m_functions.size() <<
")";
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(); });
44 auto knotsIter = m_knots.begin();
45 while (++knotsIter != m_knots.end()) {
46 if (*knotsIter <= *(knotsIter-1)) {
52 Piecewise::Piecewise(std::vector<double> knots, std::vector<std::unique_ptr<Function>>&& functions)
53 : m_knots{std::move(knots)}, m_functions{std::move(functions)} {
54 if (m_knots.size() - m_functions.size() != 1) {
56 <<
")-functions(" << m_functions.size() <<
")";
58 auto knotsIter = m_knots.begin();
59 while (++knotsIter != m_knots.end()) {
60 if (*knotsIter <= *(knotsIter-1)) {
75 auto knotsBegin =
m_knots.begin();
76 if (x < *knotsBegin) {
79 if (x == *knotsBegin) {
83 auto findX = std::lower_bound(knotsBegin, knotsEnd, x);
84 if (findX == knotsEnd) {
91 std::vector<std::unique_ptr<Function>> cloned_functions;
94 cloned_functions.emplace_back(f->clone());
96 return std::unique_ptr<Function> {
new Piecewise(
m_knots, std::move(cloned_functions))};
112 auto knotIter = std::upper_bound(
m_knots.begin(),
m_knots.end(), min);
113 if (knotIter !=
m_knots.begin()) {
117 while (++knotIter !=
m_knots.end()) {
118 auto prevKnotIter = knotIter - 1;
119 if (max <= *prevKnotIter) {
122 if (min < *knotIter) {
123 double down = (min > *prevKnotIter) ? min : *prevKnotIter;
124 double up = (max < *knotIter) ? max : *knotIter;
129 return direction * result;
const std::vector< std::unique_ptr< Function > > & getFunctions() const
Returns the functions in the ranges between the knots.
std::unique_ptr< Function > clone() const override
double operator()(const double) const override
std::vector< double > m_knots
A vector where the knots are kept.
Piecewise(std::vector< double > knots, std::vector< std::shared_ptr< Function >> functions)
std::vector< std::unique_ptr< Function > > m_functions
A vector where the sub-functions are kept.
ELEMENTS_API double integrate(const Function &function, const double min, const double max, std::unique_ptr< NumericalIntegrationScheme > numericalIntegrationScheme=nullptr)
const std::vector< double > & getKnots() const
Returns the knots of the piecewise function.
double integrate(const double x1, const double x2) const override