40 const Container<Number>&
X,
41 const Container<Number>& Y,
45 if (
X.size() != Y.size()) {
46 std::cerr <<
"Error in function " << __FUNCTION__
47 <<
": Vectors X (of size " <<
X.size()
48 <<
") and Y (of size " << Y.size()
49 <<
") are not the same size. Returning an empty array..." << std::endl;
63 Container<Number>
coeffs(n * (n - 1));
67#if defined(_OPENMP) && !defined(ALFI_DISABLE_OPENMP)
68#pragma omp parallel for
70 for (
SizeT i = 0; i < n - 1; ++i) {
75 Container<Number> P_i;
81 std::move(P_i.begin(), P_i.end(),
coeffs.begin() + (i * n));
93#if defined(_OPENMP) && !defined(ALFI_DISABLE_OPENMP)
94#pragma omp parallel for
96 for (
SizeT i = 0; i < n - 1; ++i) {
97 for (
SizeT j = 0; j < n; ++j) {
100 for (
SizeT k = 0; k < n - 1; ++k) {
101 for (
SizeT j = 1; j < n - k; ++j) {
115 template <
typename ContainerXType>
119 template <
typename ContainerXType>
121 const Container<Number>& Y,
134 template <
typename ContainerXType>
136 const Container<Number>& Y,
141 if (
coeffs.empty() && !
X.empty()) {
142 std::cerr <<
"Error in function " << __FUNCTION__
143 <<
": failed to construct coefficients. Not changing the object..." << std::endl;
149 _X = std::forward<ContainerXType>(
X);
150 _coeffs = std::move(
coeffs);
153 Number
eval(
const Number& x)
const {
157 if (_coeffs.empty()) {
159 }
else if (_coeffs.size() == 1) {
165 const Number x_seg = x - _X[
segment];
167 const SizeT n = _X.size();
169 Number result = _coeffs[
segment*n];
171 if (std::isnan(result)) {
172 switch (_evaluation_type) {
182 for (
SizeT i = 1; i < n; ++i) {
184 Number current = _coeffs[
segment*n+i];
185 if (std::isnan(current)) {
186 switch (_evaluation_type) {
203 Container<Number>
eval(
const Container<Number>& xx,
bool sorted =
true)
const {
204 Container<Number> result(xx.size());
206 for (
SizeT i = 0, i_x = 0; i < xx.size(); ++i) {
207 const Number& x = xx[i];
208 while (i_x + 1 < _X.size() && x >= _X[i_x+1])
210 result[i] =
eval(x, i_x);
213 for (
SizeT i = 0; i < xx.size(); ++i) {
214 result[i] =
eval(xx[i]);
223 Container<Number>
operator()(
const Container<Number>& xx)
const {
228 return _polynomial_type;
232 return _optimization_type;
236 return _evaluation_type;
239 const Container<Number>&
X() const & {
242 Container<Number>&&
X() && {
243 return std::move(_X);
246 const Container<Number>&
coeffs() const & {
250 return std::move(_coeffs);
254 return {_X.size()*index, _X.size()*(index+1)};
261 Container<Number> _X = {};
262 Container<Number> _coeffs = {};
static Container< Number > compute_coeffs(const Container< Number > &X, const Container< Number > &Y, PolynomialType polynomial_type=PolynomialType::Default, OptimizationType optimization_type=OptimizationType::Default)
Definition polyeqv.h:39
PolynomialType polynomial_type() const
Definition polyeqv.h:227
Container< Number > eval(const Container< Number > &xx, bool sorted=true) const
Definition polyeqv.h:203
PolyEqvSpline & operator=(PolyEqvSpline &&other) noexcept=default
PolyEqvSpline(ContainerXType &&X, const Container< Number > &Y, OptimizationType optimization_type)
Definition polyeqv.h:116
Container< Number > && coeffs() &&
Definition polyeqv.h:249
std::pair< SizeT, SizeT > segment(SizeT index) const
Definition polyeqv.h:253
Number operator()(const Number &x) const
Definition polyeqv.h:220
Number eval(const Number &x, SizeT segment) const
Definition polyeqv.h:156
PolyEqvSpline(ContainerXType &&X, const Container< Number > &Y, PolynomialType polynomial_type=PolynomialType::Default, OptimizationType optimization_type=OptimizationType::Default, EvaluationType evaluation_type=EvaluationType::Default)
Definition polyeqv.h:120
PolyEqvSpline(const PolyEqvSpline &other)=default
PolyEqvSpline(PolyEqvSpline &&other) noexcept=default
void construct(ContainerXType &&X, const Container< Number > &Y, PolynomialType polynomial_type=PolynomialType::Default, OptimizationType optimization_type=OptimizationType::Default, EvaluationType evaluation_type=EvaluationType::Default)
Definition polyeqv.h:135
PolynomialType
Definition polyeqv.h:16
@ NEWTON
Definition polyeqv.h:19
@ LAGRANGE
Definition polyeqv.h:17
@ Default
Definition polyeqv.h:21
@ IMPROVED_LAGRANGE
Definition polyeqv.h:18
Number eval(const Number &x) const
Definition polyeqv.h:153
EvaluationType
Definition polyeqv.h:31
@ Default
Definition polyeqv.h:36
@ NOT_IGNORE_NANS
Definition polyeqv.h:34
@ IGNORE_NANS
Definition polyeqv.h:33
@ IGNORE_NANS_AND_PREVIOUS
Definition polyeqv.h:32
PolyEqvSpline & operator=(const PolyEqvSpline &other)=default
const Container< Number > & X() const &
Definition polyeqv.h:239
EvaluationType evaluation_type() const
Definition polyeqv.h:235
Container< Number > && X() &&
Definition polyeqv.h:242
OptimizationType optimization_type() const
Definition polyeqv.h:231
OptimizationType
Definition polyeqv.h:24
@ ACCURACY
Definition polyeqv.h:25
@ SPEED
Definition polyeqv.h:26
@ Default
Definition polyeqv.h:28
const Container< Number > & coeffs() const &
Definition polyeqv.h:246
Container< Number > operator()(const Container< Number > &xx) const
Definition polyeqv.h:223
Container< Number > newton(const Container< Number > &X, const Container< Number > &Y)
Definition poly.h:249
Container< Number > lagrange(const Container< Number > &X, const Container< Number > &Y)
Definition poly.h:36
Container< Number > imp_lagrange(const Container< Number > &X, const Container< Number > &Y)
Definition poly.h:126
Iterator first_leq_or_begin(Iterator begin, Iterator end, const T &value)
Definition misc.h:27
Container< Number > simple_spline(const Container< Number > &X, const Container< Number > &Y, SizeT degree)
Definition spline.h:11
ALFI_DEFAULT_NUMBER DefaultNumber
Definition config.h:17
ALFI_SIZE_TYPE SizeT
Definition config.h:22
ALFI_DEFAULT_CONTAINER< Number > DefaultContainer
Definition config.h:20