votca 2024.2-dev
Loading...
Searching...
No Matches
linspline.cc
Go to the documentation of this file.
1/*
2 * Copyright 2009-2020 The VOTCA Development Team (http://www.votca.org)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18// Standard includes
19#include <iostream>
20
21// Local VOTCA includes
22#include "votca/tools/linalg.h"
24
25namespace votca {
26namespace tools {
27
28using namespace std;
29
30double LinSpline::Calculate(double r) {
31 Index interval = getInterval(r);
32 return a(interval) * r + b(interval);
33}
34
36 Index interval = getInterval(r);
37 return a(interval);
38}
39
40void LinSpline::Interpolate(const Eigen::VectorXd &x,
41 const Eigen::VectorXd &y) {
42 if (x.size() != y.size()) {
43 throw std::invalid_argument(
44 "error in LinSpline::Interpolate : sizes of vectors x and y do not "
45 "match");
46 }
47
48 if (x.size() < 2) {
49 throw std::invalid_argument(
50 "error in LinSpline::Interpolate : vectors x and y have to contain at "
51 "least 2 points");
52 }
53
54 const Index N = x.size();
55
56 // adjust the grid
57 r_.resize(N);
58
59 // copy the grid points into f
60 r_ = x;
61
62 // LINEAR SPLINE: a(i) * x + b(i)
63 // where i=number of interval
64
65 // initialize vectors a,b
66 a = Eigen::VectorXd::Zero(N);
67 b = Eigen::VectorXd::Zero(N);
68
69 // boundary conditions not applicable
70
71 // calculate a,b for all intervals 0..(N-2), where interval
72 // [x(i),x(i+1)] shall have number i (this means that the last interval
73 // has number N-2)
74 for (Index i = 0; i < N - 1; i++) {
75 a(i) = (y(i + 1) - y(i)) / (x(i + 1) - x(i));
76 b(i) = y(i) - a(i) * x(i);
77 }
78}
79
80void LinSpline::Fit(const Eigen::VectorXd &x, const Eigen::VectorXd &y) {
81 if (x.size() != y.size()) {
82 throw std::invalid_argument(
83 "error in LinSpline::Fit : sizes of vectors x and y do not match");
84 }
85
86 const Index N = x.size();
87 const Index ngrid = r_.size();
88
89 // construct the equation
90 // A*u = b
91 // The matrix A contains all conditions
92 // s_i(x) = (y(i+1)-y(i)) * (x-r(i))/(r(i+1)-r(i)) + y(i)
93 // where y(i) are the unknown values at grid points r(i), and
94 // the condition y=s_i(x) is to be satisfied at all input points:
95 // therefore b=y and u=vector of all unknown y(i)
96
97 Eigen::MatrixXd A = Eigen::MatrixXd::Zero(N, ngrid);
98
99 // construct matrix A
100 for (Index i = 0; i < N; i++) {
101 Index interval = getInterval(x(i));
102 A(i, interval) =
103 1 - (x(i) - r_(interval)) / (r_(interval + 1) - r_(interval));
104 A(i, interval + 1) =
105 (x(i) - r_(interval)) / (r_(interval + 1) - r_(interval));
106 }
107
108 // now do a qr solve
109 Eigen::HouseholderQR<Eigen::MatrixXd> QR(A);
110 Eigen::VectorXd sol = QR.solve(y);
111
112 // vector "sol" contains all y-values of fitted linear splines at each
113 // interval border
114 // get a(i) and b(i) for piecewise splines out of solution vector "sol"
115 a = Eigen::VectorXd::Zero(ngrid - 1);
116 b = Eigen::VectorXd::Zero(ngrid - 1);
117 for (Index i = 0; i < ngrid - 1; i++) {
118 a(i) = (sol(i + 1) - sol(i)) / (r_(i + 1) - r_(i));
119 b(i) = -a(i) * r_(i) + sol(i);
120 }
121}
122} // namespace tools
123} // namespace votca
Eigen::VectorXd b
Definition linspline.h:64
double CalculateDerivative(double r) override
Calculate y value for a given x value on the derivative of the spline created by function Interpolate...
Definition linspline.cc:35
Eigen::VectorXd a
Definition linspline.h:63
double Calculate(double r) override
Calculate spline function value for a given x value on the spline created by Interpolate() or Fit()
Definition linspline.cc:30
void Interpolate(const Eigen::VectorXd &x, const Eigen::VectorXd &y) override
Calculate interpolating spline for given (x,y) values. Points on resulting spline can be obtained via...
Definition linspline.cc:40
void Fit(const Eigen::VectorXd &x, const Eigen::VectorXd &y) override
Fit spline through noisy (x,y) values. Points on resulting fitted spline can be obtained via Calculat...
Definition linspline.cc:80
Eigen::VectorXd r_
Definition spline.h:173
Index getInterval(double r)
Determine the index of the interval containing value r.
Definition spline.cc:62
STL namespace.
base class for all analysis tools
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26