/usr/include/votca/tools/cubicspline.h is in libvotca-tools-dev 1.2.4-1.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 | /*
* Copyright 2009-2011 The VOTCA Development Team (http://www.votca.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#ifndef _CUBICSPLINE_H
#define _CUBICSPLINE_H
#include "spline.h"
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/vector_proxy.hpp>
#include <boost/numeric/ublas/vector_expression.hpp>
#include <iostream>
using namespace std;
namespace votca { namespace tools {
namespace ub = boost::numeric::ublas;
/**
\brief A cubic spline class
This class does cubic piecewise spline interpolation and spline fitting.
As representation of a single spline, the general form
\f[
S_i(x) = A(x,h_i) f_i + B(x,h_i) f_{i+1} + C(x,h_i) f''_i + d(x,h_i) f''_{i+1}
\f]
with
\f[
x_i \le x < x_{i+1}\,,\\
h_i = x_{i+1} - x_{i}
\f]
The \f$f_i\,,\,,f''_i\f$ are the function values and second derivates
at point \f$x_i\f$.
The parameters \f$f''_i\f$ are no free parameters, they are determined by the
smoothing condition that the first derivatives are continuous. So the only free
paramers are the grid points x_i as well as the functon values f_i at these points. A spline can be generated in several ways:
- Interpolation spline
- Fitting spline (fit to noisy data)
- calculate the parameters elsewere and fill the spline class
*/
class CubicSpline : public Spline
{
public:
// default constructor
CubicSpline() {};
//CubicSpline() :
// _boundaries(splineNormal) {}
// destructor
~CubicSpline() {};
// construct an interpolation spline
// x, y are the the points to construct interpolation, both vectors must be of same size
void Interpolate(ub::vector<double> &x, ub::vector<double> &y);
// fit spline through noisy data
// x,y are arrays with noisy data, both vectors must be of same size
void Fit(ub::vector<double> &x, ub::vector<double> &y);
// Calculate the function value
double Calculate(const double &x);
// Calculate the function derivative
double CalculateDerivative(const double &x);
// Calculate the function value for a whole array, story it in y
template<typename vector_type1, typename vector_type2>
void Calculate(vector_type1 &x, vector_type2 &y);
// Calculate the derivative value for a whole array, story it in y
template<typename vector_type1, typename vector_type2>
void CalculateDerivative(vector_type1 &x, vector_type2 &y);
// set spline parameters to values that were externally computed
template<typename vector_type>
void setSplineData(vector_type &f, vector_type &f2) { _f = f; _f2 = f2;}
/**
* \brief Add a point (one entry) to fitting matrix
* \param pointer to matrix
* \param value x
* \param offsets relative to getInterval(x)
* \param scale parameters for terms "A,B,C,D"
* When creating a matrix to fit data with a spline, this function creates
* one entry in that fitting matrix.
*/
template<typename matrix_type>
void AddToFitMatrix(matrix_type &A, double x,
int offset1, int offset2=0, double scale=1);
/**
* \brief Add a vector of points to fitting matrix
* \param pointer to matrix
* \param vector of x values
* \param offsets relative to getInterval(x)
* Same as previous function, but vector-valued and with scale=1.0
*/
template<typename matrix_type, typename vector_type>
void AddToFitMatrix(matrix_type &M, vector_type &x,
int offset1, int offset2=0);
/**
* \brief Add boundary conditions to fitting matrix
* \param pointer to matrix
* \param offsets
*/
template<typename matrix_type>
void AddBCToFitMatrix(matrix_type &A,
int offset1, int offset2=0);
protected:
// A spline can be written in the form
// S_i(x) = A(x,x_i,x_i+1)*f_i + B(x,x_i,x_i+1)*f''_i
// + C(x,x_i,x_i+1)*f_{i+1} + D(x,x_i,x_i+1)*f''_{i+1}
double A(const double &r);
double B(const double &r);
double C(const double &r);
double D(const double &r);
double Aprime(const double &r);
double Bprime(const double &r);
double Cprime(const double &r);
double Dprime(const double &r);
// tabulated derivatives at grid points. Second argument: 0 - left, 1 - right
double A_prime_l(int i);
double A_prime_r(int i);
double B_prime_l(int i);
double B_prime_r(int i);
double C_prime_l(int i);
double C_prime_r(int i);
double D_prime_l(int i);
double D_prime_r(int i);
};
inline double CubicSpline::Calculate(const double &r)
{
int interval = getInterval(r);
return A(r)*_f[interval]
+ B(r)*_f[interval + 1]
+ C(r)*_f2[interval]
+ D(r)*_f2[interval + 1];
}
inline double CubicSpline::CalculateDerivative(const double &r)
{
int interval = getInterval(r);
return Aprime(r)*_f[interval]
+ Bprime(r)*_f[interval + 1]
+ Cprime(r)*_f2[interval]
+ Dprime(r)*_f2[interval + 1];
}
template<typename matrix_type>
inline void CubicSpline::AddToFitMatrix(matrix_type &M, double x,
int offset1, int offset2, double scale)
{
int spi = getInterval(x);
M(offset1, offset2 + spi) += A(x)*scale;
M(offset1, offset2 + spi+1) += B(x)*scale;
M(offset1, offset2 + spi + _r.size()) += C(x)*scale;
M(offset1, offset2 + spi + _r.size() + 1) += D(x)*scale;
}
template<typename matrix_type, typename vector_type>
inline void CubicSpline::AddToFitMatrix(matrix_type &M, vector_type &x,
int offset1, int offset2)
{
for(size_t i=0; i<x.size(); ++i) {
int spi = getInterval(x(i));
M(offset1+i, offset2 + spi) = A(x(i));
M(offset1+i, offset2 + spi+1) = B(x(i));
M(offset1+i, offset2 + spi + _r.size()) = C(x(i));
M(offset1+i, offset2 + spi + _r.size() + 1) = D(x(i));
}
}
template<typename matrix_type>
inline void CubicSpline::AddBCToFitMatrix(matrix_type &M,
int offset1, int offset2)
{
for(size_t i=0; i<_r.size() - 2; ++i) {
M(offset1+i+1, offset2 + i) = A_prime_l(i);
M(offset1+i+1, offset2 + i+1) = B_prime_l(i) - A_prime_r(i);
M(offset1+i+1, offset2 + i+2) = -B_prime_r(i);
M(offset1+i+1, offset2 + _r.size() + i) = C_prime_l(i);
M(offset1+i+1, offset2 + _r.size() + i+1) = D_prime_l(i) - C_prime_r(i);
M(offset1+i+1, offset2 + _r.size() + i+2) = -D_prime_r(i);
}
// currently only natural boundary conditions:
switch(_boundaries) {
case splineNormal:
M(offset1, offset2 + _r.size()) = 1;
M(offset1 + _r.size() - 1, offset2 + 2*_r.size()-1) = 1;
break;
case splineDerivativeZero:
// y
M(offset1+0, offset2 + 0) = -1*A_prime_l(0);
M(offset1+0, offset2 + 1) = -1*B_prime_l(0);
M(offset1+ _r.size()-1, offset2 + _r.size()-2) = A_prime_l(_r.size()-2);
M(offset1+ _r.size()-1, offset2 + _r.size()-1) = B_prime_l(_r.size()-2);
// y''
M(offset1+0, offset2 + _r.size() + 0) = D_prime_l(0);
M(offset1+0, offset2 + _r.size() + 1) = C_prime_l(0);
M(offset1+ _r.size()-1, offset2 + 2*_r.size()-2) = C_prime_l(_r.size()-2);
M(offset1+ _r.size()-1, offset2 + 2*_r.size()-1) = D_prime_l(_r.size()-2);
break;
case splinePeriodic:
M(offset1, offset2) = 1;
M(offset1, offset2 + _r.size()-1) = -1;
M(offset1 + _r.size() - 1, offset2 + _r.size()) = 1;
M(offset1 + _r.size() - 1, offset2 + 2*_r.size()-1) = -1;
break;
}
}
inline double CubicSpline::A(const double &r)
{
return ( 1.0 - (r -_r[getInterval(r)])/(_r[getInterval(r)+1]-_r[getInterval(r)]) );
}
inline double CubicSpline::Aprime(const double &r)
{
return -1.0/(_r[getInterval(r)+1]-_r[getInterval(r)]);
}
inline double CubicSpline::B(const double &r)
{
return (r -_r[getInterval(r)])/(_r[getInterval(r)+1]-_r[getInterval(r)]) ;
}
inline double CubicSpline::Bprime(const double &r)
{
return 1.0/(_r[getInterval(r)+1]-_r[getInterval(r)]);
}
inline double CubicSpline::C(const double &r)
{
double xxi, h;
xxi = r -_r[getInterval(r)];
h = _r[getInterval(r)+1]-_r[getInterval(r)];
return ( 0.5*xxi*xxi - (1.0/6.0)*xxi*xxi*xxi/h - (1.0/3.0)*xxi*h) ;
}
inline double CubicSpline::Cprime(const double &r)
{
double xxi, h;
xxi = r -_r[getInterval(r)];
h = _r[getInterval(r)+1]-_r[getInterval(r)];
return (xxi - 0.5*xxi*xxi/h - h/3);
}
inline double CubicSpline::D(const double &r)
{
double xxi, h;
xxi = r -_r[getInterval(r)];
h = _r[getInterval(r)+1]-_r[getInterval(r)];
return ( (1.0/6.0)*xxi*xxi*xxi/h - (1.0/6.0)*xxi*h ) ;
}
inline double CubicSpline::Dprime(const double &r)
{
double xxi, h;
xxi = r -_r[getInterval(r)];
h = _r[getInterval(r)+1]-_r[getInterval(r)];
return ( 0.5*xxi*xxi/h - (1.0/6.0)*h ) ;
}
/**
inline int CubicSpline::getInterval(double &r)
{
if (r < _r[0] || r > _r[_r.size() - 1]) return -1;
return int( (r - _r[0]) / (_r[_r.size()-1] - _r[0]) * (_r.size() - 1) );
}
**/
inline double CubicSpline::A_prime_l(int i)
{
return -1.0/(_r[i+1]-_r[i]);
}
inline double CubicSpline::B_prime_l(int i)
{
return 1.0/(_r[i+1]-_r[i]);
}
inline double CubicSpline::C_prime_l(int i)
{
return (1.0/6.0)*(_r[i+1]-_r[i]);
}
inline double CubicSpline::D_prime_l(int i)
{
return (1.0/3.0)*(_r[i+1]-_r[i]);
}
inline double CubicSpline::A_prime_r(int i)
{
return -1.0/(_r[i+2]-_r[i+1]);
}
inline double CubicSpline::B_prime_r(int i)
{
return 1.0/(_r[i+2]-_r[i+1]);
}
inline double CubicSpline::C_prime_r(int i)
{
return -(1.0/3.0)*(_r[i+2]-_r[i+1]);
}
inline double CubicSpline::D_prime_r(int i)
{
return -(1.0/6.0)*(_r[i+2]-_r[i+1]);
}
}}
#endif /* _CUBICSPLINE_H */
|