Alexandria  2.14.1
Please provide a description of the project.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
spline.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 
29 
30 namespace Euclid {
31 namespace MathUtils {
32 
34 
35  // Number of intervals
36  int n = x.size() - 1;
37 
38  // Differences between knot points
39  std::vector<double> h (n, 0.);
40  for(int i=0; i<n; i++)
41  h[i] = x[i+1] - x[i];
42 
43  std::vector<double> mu (n, 0.);
44  std::vector<double> z (n+1, 0.);
45  double g {0};
46  for (int i=1; i<n; ++i) {
47  g = 2.*(x[i+1]-x[i-1]) - h[i-1]*mu[i-1];
48  mu[i] = h[i] / g;
49  z[i] = (3.*(y[i+1]*h[i-1] - y[i]*(x[i+1]-x[i-1])+ y[i-1]*h[i]) / (h[i-1]*h[i]) - h[i-1] * z[i-1]) / g;
50  }
51 
52  // cubic spline coefficients -- b is linear, c quadratic, d is cubic (original y's are constants)
53  std::vector<double> a (n, 0.);
54  std::vector<double> b (n, 0.);
55  std::vector<double> c (n+1, 0.);
56  std::vector<double> d (n, 0.);
57 
58  z[n] = 0.;
59  c[n] = 0.;
60 
61  for (int j=n-1; j>=0; j--) {
62  a[j] = y[j];
63  c[j] = z[j] - mu[j] * c[j+1];
64  b[j] = (y[j+1] - y[j]) / h[j] - h[j] * (c[j+1] + 2. * c[j]) / 3.;
65  d[j] = (c[j+1] - c[j]) / (3. * h[j]);
66  }
67 
68  // The above were taken from SplineInterpolator from Apache commons math. These
69  // polynomials need to be shifted by -x[i] in our case.
70  for (int i=0; i<n; i++) {
71  double x_1 = -x[i];
72  double x_2 = x_1 * x_1;
73  double x_3 = x_1 * x_2;
74  a[i] = a[i] + b[i]*x_1 + c[i]*x_2 + d[i]*x_3;
75  b[i] = b[i] + 2.*c[i]*x_1 + 3.*d[i]*x_2;
76  c[i] = c[i] + 3.*d[i]*x_1;
77  // d[i] keeps the same value
78  }
79 
81  for (int i=0; i<n; i++) {
82  functions.push_back(std::shared_ptr<Function>(new Polynomial{{a[i],b[i],c[i],d[i]}}));
83  }
84 
85  return std::unique_ptr<Function>(new Piecewise{x, std::move(functions)});
86 }
87 
88 } // End of MathUtils
89 } // 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
Represents a polynomial function.
Definition: Polynomial.h:43
T push_back(T...args)
T move(T...args)
T size(T...args)
STL class.
Represents a piecewise function.
Definition: Piecewise.h:48
constexpr double g