Alexandria  2.14.1
Please provide a description of the project.
interpolation.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 "ElementsKernel/Logging.h"
27 #include "implementations.h"
28 
29 namespace Euclid {
30 namespace MathUtils {
31 
32 namespace {
33 
35 
36 } // Anonymous namespace
37 
39 
40  if (x.size() != y.size()) {
41  throw InterpolationException() << "Interpolation using vectors of incompatible "
42  << "size: X=" << x.size() << ", Y=" << y.size();
43  }
44 
45  // We remove any duplicate lines and we check that we have only increasing
46  // X values and no step functions
47  std::vector<double> final_x {};
48  std::vector<double> final_y {};
49  final_x.push_back(x[0]);
50  final_y.push_back(y[0]);
51  for (std::size_t i = 1; i < x.size(); ++i) {
52  if (x[i] == x[i-1]) {
53  if (y[i] == y[i-1]) {
54  logger.warn() << "Ignoring duplicate pair (" << x[i] << ", " << y[i]
55  << ") during interpolation";
56  continue;
57  } else {
58  throw InterpolationException() << "Interpolation of step functions is not "
59  << "supported. Entries: (" << x[i] << ", " << y[i] << ") and ("
60  << x[i-1] << ", " << y[i-1] << ")";
61  }
62  }
63  if (x[i] < x[i-1]) {
64  throw InterpolationException() << "Only increasing X values are supported "
65  << "but found " << x[i] << " after " << x[i-1];
66  }
67  final_x.push_back(x[i]);
68  final_y.push_back(y[i]);
69  }
70 
71  switch (type) {
73  return linearInterpolation(final_x, final_y);
75  return splineInterpolation(final_x, final_y);
76  }
77  return nullptr;
78 }
79 
83  for (auto& pair : dataset) {
84  x.push_back(pair.first);
85  y.push_back(pair.second);
86  }
87  return interpolate(x, y, type);
88 }
89 
90 } // End of MathUtils
91 } // end of namespace Euclid
std::unique_ptr< Function > splineInterpolation(const std::vector< double > &x, const std::vector< double > &y)
Performs cubic spline interpolation for the given set of data points.
Definition: spline.cpp:33
std::unique_ptr< Function > linearInterpolation(const std::vector< double > &x, const std::vector< double > &y)
Performs linear interpolation for the given set of data points.
Definition: linear.cpp:33
T push_back(T... args)
void warn(const std::string &logMessage)
static Elements::Logging logger
ELEMENTS_API std::unique_ptr< Function > interpolate(const std::vector< double > &x, const std::vector< double > &y, InterpolationType type)
T size(T... args)
STL class.
This module provides an interface for accessing two dimensional datasets (pairs of (X,...
Definition: XYDataset.h:59
InterpolationType
Enumeration of the different supported interpolation types.
Definition: interpolation.h:41
static Logging getLogger(const std::string &name="")