Alexandria 2.31.0
SDC-CH common library for the Euclid project
Loading...
Searching...
No Matches
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
31namespace Euclid {
32namespace MathUtils {
33
35
39
40double 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
58
60 if (!m_derivative) {
62 for (size_t i = 1; i < m_coef.size(); i++) {
63 derivCoef.push_back(i * this->m_coef[i]);
64 }
66 }
67 return m_derivative;
68}
69
71 if (!m_indefIntegral) {
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 }
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.
std::shared_ptr< Function > m_indefIntegral
The function representing the indefinite integral (uses lazy initialization)
Definition Polynomial.h:82
Polynomial(std::vector< double > coefficients)
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.
std::unique_ptr< Function > clone() const override
Creates a new polynomial with the same coefficients.
std::shared_ptr< Function > derivative() const override
Returns the derivative of the polynomial.
const std::vector< double > & getCoefficients() const
Returns the coefficients of the polynomial.
T end(T... args)
T move(T... args)
std::array< std::vector< double >, N > Coordinates
Used to pass the grid coordinates to interpn. Internally will make a copy of the required values.
STL namespace.
T resize(T... args)
T size(T... args)
T transform(T... args)