Alexandria  2.27.0
SDC-CH common library for the Euclid project
Polynomial.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 
26 #include <algorithm>
27 #include <cmath>
28 #include <memory>
29 #include <utility>
30 
31 namespace Euclid {
32 namespace MathUtils {
33 
34 Polynomial::Polynomial(std::vector<double> coefficients) : m_coef{std::move(coefficients)} {}
35 
37  return m_coef;
38 }
39 
40 double Polynomial::operator()(const double x) const {
41  double result{};
42  double xPow{1};
43  for (double coef : m_coef) {
44  result += coef * xPow;
45  xPow *= x;
46  }
47  return result;
48 }
49 
51  out.resize(xs.size());
52  std::transform(xs.begin(), xs.end(), out.begin(), *this);
53 }
54 
57 }
58 
60  if (!m_derivative) {
61  std::vector<double> derivCoef{};
62  for (size_t i = 1; i < m_coef.size(); i++) {
63  derivCoef.push_back(i * this->m_coef[i]);
64  }
65  m_derivative.reset(new Polynomial{std::move(derivCoef)});
66  }
67  return m_derivative;
68 }
69 
71  if (!m_indefIntegral) {
72  std::vector<double> indefIntegCoef{};
73  indefIntegCoef.push_back(0.);
74  for (size_t i = 0; i < m_coef.size(); i++) {
75  indefIntegCoef.push_back(m_coef[i] / (i + 1));
76  }
77  m_indefIntegral.reset(new Polynomial{std::move(indefIntegCoef)});
78  }
79  return m_indefIntegral;
80 }
81 
82 } // namespace MathUtils
83 } // end of namespace Euclid
T begin(T... args)
Represents a polynomial function.
Definition: Polynomial.h:43
std::shared_ptr< Function > m_derivative
The function representing the derivative (uses lazy initialization)
Definition: Polynomial.h:80
std::shared_ptr< Function > indefiniteIntegral() const override
Returns the indefinite integral of the polynomial.
Definition: Polynomial.cpp:70
std::shared_ptr< Function > m_indefIntegral
The function representing the indefinite integral (uses lazy initialization)
Definition: Polynomial.h:82
Polynomial(std::vector< double > coefficients)
Definition: Polynomial.cpp:34
std::vector< double > m_coef
The vector where the polynomial coefficients are stored.
Definition: Polynomial.h:78
double operator()(const double) const override
Calculates the value of the polynomial for the given value.
Definition: Polynomial.cpp:40
std::unique_ptr< Function > clone() const override
Creates a new polynomial with the same coefficients.
Definition: Polynomial.cpp:55
std::shared_ptr< Function > derivative() const override
Returns the derivative of the polynomial.
Definition: Polynomial.cpp:59
const std::vector< double > & getCoefficients() const
Returns the coefficients of the polynomial.
Definition: Polynomial.cpp:36
T end(T... args)
T move(T... args)
STL namespace.
T push_back(T... args)
T resize(T... args)
T size(T... args)
T transform(T... args)