QMCPACK
CubSpline Class Reference

The CubSpline class is a third-order spline representation of a function. More...

+ Collaboration diagram for CubSpline:

Public Member Functions

int size ()
 Returns the interpolated value. More...
 
double operator() (double x)
 
double Deriv (double x)
 Returns the interpolated first derivative. More...
 
double Deriv2 (double x)
 Returns the interpolated second derivative. More...
 
double Deriv3 (double x)
 Returns the interpolated third derivative. More...
 
void Update ()
 Recompute the second derivatives from the function values. More...
 
std::vector< double > & Data ()
 
void Init (SimpleGrid &newGrid, std::vector< double > yvals, double startderiv, double endderiv)
 Initialize the cubic spline. More...
 
void Init (SimpleGrid &newGrid, std::vector< double > &yvals)
 Simplified form which assumes that the second derivative at both boundaries are zero. More...
 
 CubSpline (SimpleGrid &newGrid, std::vector< double > &yvals)
 Simplified constructor. More...
 
 CubSpline (SimpleGrid &newGrid, std::vector< double > &yvals, double startderiv, double endderiv)
 Full constructor. More...
 
double operator() (int i) const
 Returns the value of the function at the ith grid point. More...
 
double & operator() (int i)
 Returns a reference to the value at the ith grid point. More...
 
 CubSpline ()
 Trivial constructor. More...
 

Public Attributes

SimpleGrid grid
 
bool Initialized
 

Private Attributes

bool UpToDate
 This flag records whether or not the stored second derivatives are in sync with the function values. More...
 
std::vector< double > y
 The function values on the grid points. More...
 
std::vector< double > d2y
 The second derivatives of the function. More...
 
double StartDeriv
 The values of the derivative of the represented function on the boundary. More...
 
double EndDeriv
 

Detailed Description

The CubSpline class is a third-order spline representation of a function.

It stores a pointer to a grid and the values of the function and its second derivative at the points defined by the grid.

Definition at line 26 of file CubicSpline.h.

Constructor & Destructor Documentation

◆ CubSpline() [1/3]

CubSpline ( SimpleGrid newGrid,
std::vector< double > &  yvals 
)
inline

Simplified constructor.

Definition at line 91 of file CubicSpline.h.

References EndDeriv, Init(), StartDeriv, and Update().

92  {
93  StartDeriv = EndDeriv = 5.0e30;
94  Init(newGrid, yvals, 5.0e30, 5.0e30);
95  Update();
96  }
double EndDeriv
Definition: CubicSpline.h:42
double StartDeriv
The values of the derivative of the represented function on the boundary.
Definition: CubicSpline.h:42
void Update()
Recompute the second derivatives from the function values.
void Init(SimpleGrid &newGrid, std::vector< double > yvals, double startderiv, double endderiv)
Initialize the cubic spline.
Definition: CubicSpline.h:63

◆ CubSpline() [2/3]

CubSpline ( SimpleGrid newGrid,
std::vector< double > &  yvals,
double  startderiv,
double  endderiv 
)
inline

Full constructor.

Definition at line 99 of file CubicSpline.h.

References Init(), and Update().

100  {
101  Init(newGrid, yvals, startderiv, endderiv);
102  Update();
103  }
void Update()
Recompute the second derivatives from the function values.
void Init(SimpleGrid &newGrid, std::vector< double > yvals, double startderiv, double endderiv)
Initialize the cubic spline.
Definition: CubicSpline.h:63

◆ CubSpline() [3/3]

CubSpline ( )
inline

Trivial constructor.

Definition at line 115 of file CubicSpline.h.

References Initialized, and UpToDate.

115  : StartDeriv(0.), EndDeriv(0.)
116  {
117  UpToDate = false;
118  Initialized = false;
119  }
double EndDeriv
Definition: CubicSpline.h:42
bool Initialized
Definition: CubicSpline.h:46
bool UpToDate
This flag records whether or not the stored second derivatives are in sync with the function values...
Definition: CubicSpline.h:32
double StartDeriv
The values of the derivative of the represented function on the boundary.
Definition: CubicSpline.h:42

Member Function Documentation

◆ Data()

std::vector<double>& Data ( )
inline

Definition at line 59 of file CubicSpline.h.

References y.

59 { return y; }
std::vector< double > y
The function values on the grid points.
Definition: CubicSpline.h:34

◆ Deriv()

double Deriv ( double  x)
inline

Returns the interpolated first derivative.

Definition at line 163 of file CubicSpline.h.

References d2y, grid, SimpleGrid::NumPoints(), SimpleGrid::ReverseMap(), Update(), UpToDate, and y.

164 {
165  if (!UpToDate)
166  Update();
167 
168  SimpleGrid& X = grid;
169  int hi = X.ReverseMap(x) + 1;
170  int low = hi - 1;
171  if (low < 0)
172  {
173  low = 0;
174  hi = 1;
175  }
176  if (hi > (X.NumPoints() - 1))
177  {
178  hi = (X.NumPoints() - 1);
179  low = hi - 1;
180  }
181 
182  double h = X[hi] - X[low];
183  double hinv = 1.0 / h;
184  double a = (X[hi] - x) * hinv;
185  double b = (x - X[low]) * hinv;
186  double sixinv = 0.1666666666666666666;
187 
188  return ((y[hi] - y[low]) * hinv + (h * sixinv) * ((3.0 * b * b - 1.0) * d2y[hi] - (3.0 * a * a - 1.0) * d2y[low]));
189 }
SimpleGrid grid
Definition: CubicSpline.h:45
bool UpToDate
This flag records whether or not the stored second derivatives are in sync with the function values...
Definition: CubicSpline.h:32
int NumPoints()
Definition: GeneralGrid.h:26
void Update()
Recompute the second derivatives from the function values.
std::vector< double > d2y
The second derivatives of the function.
Definition: CubicSpline.h:36
int ReverseMap(double r)
Returns the index of the nearest point below r.
Definition: GeneralGrid.h:33
std::vector< double > y
The function values on the grid points.
Definition: CubicSpline.h:34

◆ Deriv2()

double Deriv2 ( double  x)
inline

Returns the interpolated second derivative.

Definition at line 191 of file CubicSpline.h.

References d2y, grid, SimpleGrid::NumPoints(), SimpleGrid::ReverseMap(), Update(), and UpToDate.

192 {
193  if (!UpToDate)
194  Update();
195  SimpleGrid& X = grid;
196  int hi = X.ReverseMap(x) + 1;
197  int low = hi - 1;
198  if (low < 0)
199  {
200  low = 0;
201  hi = 1;
202  }
203  if (hi > (X.NumPoints() - 1))
204  {
205  hi = (X.NumPoints() - 1);
206  low = hi - 1;
207  }
208 
209 
210  double h = X[hi] - X[low];
211  double hinv = 1.0 / h;
212  double a = (X[hi] - x) * hinv;
213  double b = (x - X[low]) * hinv;
214  return (a * d2y[low] + b * d2y[hi]);
215 }
SimpleGrid grid
Definition: CubicSpline.h:45
bool UpToDate
This flag records whether or not the stored second derivatives are in sync with the function values...
Definition: CubicSpline.h:32
int NumPoints()
Definition: GeneralGrid.h:26
void Update()
Recompute the second derivatives from the function values.
std::vector< double > d2y
The second derivatives of the function.
Definition: CubicSpline.h:36
int ReverseMap(double r)
Returns the index of the nearest point below r.
Definition: GeneralGrid.h:33

◆ Deriv3()

double Deriv3 ( double  x)
inline

Returns the interpolated third derivative.

Definition at line 218 of file CubicSpline.h.

References d2y, grid, SimpleGrid::NumPoints(), SimpleGrid::ReverseMap(), Update(), and UpToDate.

219 {
220  if (!UpToDate)
221  Update();
222  SimpleGrid& X = grid;
223  int hi = X.ReverseMap(x) + 1;
224  int low = hi - 1;
225  if (low < 0)
226  {
227  low = 0;
228  hi = 1;
229  }
230  if (hi > (X.NumPoints() - 1))
231  {
232  hi = (X.NumPoints() - 1);
233  low = hi - 1;
234  }
235 
236  double h = X[hi] - X[low];
237 
238  return ((d2y[hi] - d2y[low]) / h);
239 }
SimpleGrid grid
Definition: CubicSpline.h:45
bool UpToDate
This flag records whether or not the stored second derivatives are in sync with the function values...
Definition: CubicSpline.h:32
int NumPoints()
Definition: GeneralGrid.h:26
void Update()
Recompute the second derivatives from the function values.
std::vector< double > d2y
The second derivatives of the function.
Definition: CubicSpline.h:36
int ReverseMap(double r)
Returns the index of the nearest point below r.
Definition: GeneralGrid.h:33

◆ Init() [1/2]

void Init ( SimpleGrid newGrid,
std::vector< double >  yvals,
double  startderiv,
double  endderiv 
)
inline

Initialize the cubic spline.

See notes about start and end deriv above.

Definition at line 63 of file CubicSpline.h.

References d2y, EndDeriv, grid, Initialized, SimpleGrid::NumPoints(), StartDeriv, Update(), and y.

Referenced by CubSpline(), and Init().

64  {
65  StartDeriv = startderiv;
66  EndDeriv = endderiv;
67  if (newGrid.NumPoints() != yvals.size())
68  {
69  std::cerr << "Size mismatch in CubSpline.\n";
70  std::cerr << "Grid Points = " << newGrid.NumPoints() << std::endl;
71  std::cerr << "Y points = " << yvals.size() << std::endl;
72  abort();
73  }
74  grid = newGrid;
75  y.resize(grid.NumPoints());
76  d2y.resize(grid.NumPoints());
77  y = yvals;
78  Update();
79  Initialized = true;
80  }
double EndDeriv
Definition: CubicSpline.h:42
bool Initialized
Definition: CubicSpline.h:46
SimpleGrid grid
Definition: CubicSpline.h:45
int NumPoints()
Definition: GeneralGrid.h:26
double StartDeriv
The values of the derivative of the represented function on the boundary.
Definition: CubicSpline.h:42
void Update()
Recompute the second derivatives from the function values.
std::vector< double > d2y
The second derivatives of the function.
Definition: CubicSpline.h:36
std::vector< double > y
The function values on the grid points.
Definition: CubicSpline.h:34

◆ Init() [2/2]

void Init ( SimpleGrid newGrid,
std::vector< double > &  yvals 
)
inline

Simplified form which assumes that the second derivative at both boundaries are zero.

Definition at line 84 of file CubicSpline.h.

References Init(), and Update().

85  {
86  Init(newGrid, yvals, 5.0e30, 5.0e30);
87  Update();
88  }
void Update()
Recompute the second derivatives from the function values.
void Init(SimpleGrid &newGrid, std::vector< double > yvals, double startderiv, double endderiv)
Initialize the cubic spline.
Definition: CubicSpline.h:63

◆ operator()() [1/3]

double operator() ( double  x)
inline

Definition at line 122 of file CubicSpline.h.

References d2y, SimpleGrid::End(), grid, SimpleGrid::NumPoints(), SimpleGrid::ReverseMap(), Update(), UpToDate, and y.

123 {
124  if (!UpToDate)
125  Update();
126 
127  SimpleGrid& X = grid;
128 #ifdef DEBUG
129  if (x > X.End())
130  {
131  if (x < (X.End() * 1.000000001))
132  x = X.End();
133  else
134  {
135  std::cerr << "x outside grid in CubSpline.\n";
136  std::cerr << "x = " << x << " X.End = " << X.End() << "\n";
137  abort();
138  }
139  }
140 #endif
141  int hi = X.ReverseMap(x) + 1;
142  int low = hi - 1;
143  if (low < 0)
144  {
145  low = 0;
146  hi = 1;
147  }
148  if (hi > (X.NumPoints() - 1))
149  {
150  hi = (X.NumPoints() - 1);
151  low = hi - 1;
152  }
153 
154  double h = X[hi] - X[low];
155  double hinv = 1.0 / h;
156  double a = (X[hi] - x) * hinv;
157  double b = (x - X[low]) * hinv;
158  double sixinv = 0.1666666666666666666;
159 
160  return (a * y[low] + b * y[hi] + ((a * a * a - a) * d2y[low] + (b * b * b - b) * d2y[hi]) * (h * h * sixinv));
161 }
double End()
Definition: GeneralGrid.h:62
SimpleGrid grid
Definition: CubicSpline.h:45
bool UpToDate
This flag records whether or not the stored second derivatives are in sync with the function values...
Definition: CubicSpline.h:32
int NumPoints()
Definition: GeneralGrid.h:26
void Update()
Recompute the second derivatives from the function values.
std::vector< double > d2y
The second derivatives of the function.
Definition: CubicSpline.h:36
int ReverseMap(double r)
Returns the index of the nearest point below r.
Definition: GeneralGrid.h:33
std::vector< double > y
The function values on the grid points.
Definition: CubicSpline.h:34

◆ operator()() [2/3]

double operator() ( int  i) const
inline

Returns the value of the function at the ith grid point.

Definition at line 106 of file CubicSpline.h.

References y.

106 { return (y[i]); }
std::vector< double > y
The function values on the grid points.
Definition: CubicSpline.h:34

◆ operator()() [3/3]

double& operator() ( int  i)
inline

Returns a reference to the value at the ith grid point.

Definition at line 108 of file CubicSpline.h.

References UpToDate, and y.

109  {
110  UpToDate = false;
111  return (y[i]);
112  }
bool UpToDate
This flag records whether or not the stored second derivatives are in sync with the function values...
Definition: CubicSpline.h:32
std::vector< double > y
The function values on the grid points.
Definition: CubicSpline.h:34

◆ size()

int size ( void  )
inline

Returns the interpolated value.

Definition at line 49 of file CubicSpline.h.

References grid, and SimpleGrid::NumPoints().

49 { return grid.NumPoints(); }
SimpleGrid grid
Definition: CubicSpline.h:45
int NumPoints()
Definition: GeneralGrid.h:26

◆ Update()

void Update ( )

Recompute the second derivatives from the function values.

Referenced by CubSpline(), Deriv(), Deriv2(), Deriv3(), Init(), and operator()().

Member Data Documentation

◆ d2y

std::vector<double> d2y
private

The second derivatives of the function.

Definition at line 36 of file CubicSpline.h.

Referenced by Deriv(), Deriv2(), Deriv3(), Init(), and operator()().

◆ EndDeriv

double EndDeriv
private

Definition at line 42 of file CubicSpline.h.

Referenced by CubSpline(), and Init().

◆ grid

SimpleGrid grid

Definition at line 45 of file CubicSpline.h.

Referenced by Deriv(), Deriv2(), Deriv3(), Init(), operator()(), and size().

◆ Initialized

bool Initialized

Definition at line 46 of file CubicSpline.h.

Referenced by CubSpline(), and Init().

◆ StartDeriv

double StartDeriv
private

The values of the derivative of the represented function on the boundary.

If each value is greater that 1e30, we compute boundary conditions assuming that the second derivative is zero at that boundary.

Definition at line 42 of file CubicSpline.h.

Referenced by CubSpline(), and Init().

◆ UpToDate

bool UpToDate
private

This flag records whether or not the stored second derivatives are in sync with the function values.

It is used to determine whether the second derivatives need recomputation.

Definition at line 32 of file CubicSpline.h.

Referenced by CubSpline(), Deriv(), Deriv2(), Deriv3(), and operator()().

◆ y

std::vector<double> y
private

The function values on the grid points.

Definition at line 34 of file CubicSpline.h.

Referenced by Data(), Deriv(), Init(), and operator()().


The documentation for this class was generated from the following file: