Alexandria  2.18
Please provide a description of the project.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 <cmath>
27 #include <memory>
28 #include <utility>
29 
30 namespace Euclid {
31 namespace MathUtils {
32 
33 Polynomial::Polynomial(std::vector<double> coefficients) : m_coef{std::move(coefficients)} {}
34 
36  return m_coef;
37 }
38 
39 double Polynomial::operator()(const double x) const {
40  double result{};
41  double xPow{1};
42  for (double coef : m_coef) {
43  result += coef * xPow;
44  xPow *= x;
45  }
46  return result;
47 }
48 
51 }
52 
54  if (!m_derivative) {
55  std::vector<double> derivCoef{};
56  for (size_t i = 1; i < m_coef.size(); i++) {
57  derivCoef.push_back(i * this->m_coef[i]);
58  }
59  m_derivative.reset(new Polynomial{std::move(derivCoef)});
60  }
61  return m_derivative;
62 }
63 
65  if (!m_indefIntegral) {
66  std::vector<double> indefIntegCoef{};
67  indefIntegCoef.push_back(0.);
68  for (size_t i = 0; i < m_coef.size(); i++) {
69  indefIntegCoef.push_back(m_coef[i] / (i + 1));
70  }
71  m_indefIntegral.reset(new Polynomial{std::move(indefIntegCoef)});
72  }
73  return m_indefIntegral;
74 }
75 
76 } // namespace MathUtils
77 } // end of namespace Euclid
std::shared_ptr< Function > m_indefIntegral
The function representing the indefinite integral (uses lazy initialization)
Definition: Polynomial.h:79
std::vector< double > m_coef
The vector where the polynomial coefficients are stored.
Definition: Polynomial.h:75
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:53
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:77
T push_back(T...args)
double operator()(const double) const override
Calculates the value of the polynomial for the given value.
Definition: Polynomial.cpp:39
T move(T...args)
std::unique_ptr< Function > clone() const override
Creates a new polynomial with the same coefficients.
Definition: Polynomial.cpp:49
T size(T...args)
std::shared_ptr< Function > indefiniteIntegral() const override
Returns the indefinite integral of the polynomial.
Definition: Polynomial.cpp:64
STL class.
const std::vector< double > & getCoefficients() const
Returns the coefficients of the polynomial.
Definition: Polynomial.cpp:35