Alexandria  2.16
Please provide a description of the project.
Polynomial.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 <utility>
26 #include <cmath>
27 #include <memory>
29 
30 namespace Euclid {
31 namespace MathUtils {
32 
33 Polynomial::Polynomial(std::vector<double> coefficients) : m_coef{std::move(coefficients)} {
34 }
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 
52 }
53 
55  if (!m_derivative) {
56  std::vector<double> derivCoef {};
57  for (size_t i = 1; i < m_coef.size(); i++) {
58  derivCoef.push_back(i * this->m_coef[i]);
59  }
60  m_derivative.reset(new Polynomial{std::move(derivCoef)});
61  }
62  return m_derivative;
63 }
64 
66  if (!m_indefIntegral) {
67  std::vector<double> indefIntegCoef {};
68  indefIntegCoef.push_back(0.);
69  for (size_t i = 0; i < m_coef.size(); i++) {
70  indefIntegCoef.push_back(m_coef[i] / (i+1));
71  }
72  m_indefIntegral.reset(new Polynomial{std::move(indefIntegCoef)});
73  }
74  return m_indefIntegral;
75 }
76 
77 } // End of MathUtils
78 } // end of namespace Euclid
std::shared_ptr< Function > m_indefIntegral
The function representing the indefinite integral (uses lazy initialization)
Definition: Polynomial.h:81
STL class.
std::vector< double > m_coef
The vector where the polynomial coefficients are stored.
Definition: Polynomial.h:77
Polynomial(std::vector< double > coefficients)
Definition: Polynomial.cpp:33
std::shared_ptr< Function > derivative() const override
Returns the derivative of the polynomial.
Definition: Polynomial.cpp:54
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:79
T push_back(T... args)
const std::vector< double > & getCoefficients() const
Returns the coefficients of the polynomial.
Definition: Polynomial.cpp:36
double operator()(const double) const override
Calculates the value of the polynomial for the given value.
Definition: Polynomial.cpp:40
T move(T... args)
std::unique_ptr< Function > clone() const override
Creates a new polynomial with the same coefficients.
Definition: Polynomial.cpp:50
T size(T... args)
std::shared_ptr< Function > indefiniteIntegral() const override
Returns the indefinite integral of the polynomial.
Definition: Polynomial.cpp:65
STL class.