Class deriv_eqi (o2scl)

O2scl : Class List

template<class func_t = funct, class vec_t = boost::numeric::ublas::vector<double>>
class deriv_eqi : public o2scl::deriv_base<funct>

Derivatives for equally-spaced abscissas.

This is an implementation of the formulas for equally-spaced abscissas as indicated below. The level of approximation is specified in set_npoints(). The value of \( p \times h \) can be specified in xoff (default is zero).

Idea for Future:

Finish the second and third derivative formulas.

Two-point formula (note that this is independent of p).

\[ f^{\prime}(x_0+p h)=\frac{1}{h}\left[ f_{1}-f_{0} \right] \]
Three-point formula from Abramowitz and Stegun
\[ f^{\prime}(x_0+p h)=\frac{1}{h}\left[ \frac{2p-1}{2}f_{-1}-2 p f_{0}+\frac{2p+1}{2}f_{1}\right] \]
Four-point formula from Abramowitz and Stegun
\[ f^{\prime}(x_0+p h)=\frac{1}{h}\left[ -\frac{3 p^2-6 p+2}{6}f_{-1} +\frac{3 p^2-4 p -1}{2}f_{0} -\frac{3 p^2-2 p-2}{2}f_{1} +\frac{3 p^2-1}{6}f_{2} \right] \]
Five-point formula from Abramowitz and Stegun
\[\begin{split}\begin{eqnarray*} f^{\prime}(x_0+p h)&=&\frac{1}{h}\left[ \frac{2 p^3-3 p^2-p+1}{12}f_{-2} -\frac{4 p^3-3p^2-8p+4}{6}f_{-1} \right. \\ && \left. +\frac{2p^3-5p}{2}f_{0} -\frac{4p^3+3p^2-8p-4}{6}f_{1} \right. \\ && \left. +\frac{2p^3+3p^2-p-1}{12}f_{2} \right] \end{eqnarray*}\end{split}\]

The relations above can be confined to give formulas for second derivative formulas: Three-point formula

\[ f^{\prime}(x_0+p h)=\frac{1}{h^2} \left[f_{-1}-2 f_0+f_1\right] \]
Four-point formula:
\[ f^{\prime}(x_0+p h)=\frac{1}{2 h^2} \left[\left(1-2p\right)f_{-1}-\left(1-6p\right)f_0 -\left(1+6p\right)f_1+\left(1+2p\right)f_2\right] \]
Five-point formula:
\[ f^{\prime}(x_0+p h)=\frac{1}{4 h^2} \left[\left(1-2p\right)^2f_{-2} +\left(8p-16 p^2\right)f_{-1} -\left(2-24 p^2\right)f_0 -\left(8p+16p^2\right)f_1 +\left(1+2p\right)^2 f_2\right] \]
Six-point formula:
\[\begin{split}\begin{eqnarray*} f^{\prime}(x_0+p h)&=&\frac{1}{12 h^2}\left[ \left(2-10p+15 p^2-6p^3\right)f_{-2} +\left(3+14p-57p^2+30p^3\right)f_{-1} \right. \\ && \left. +\left(-8+20p+78 p^2-60p^3\right)f_0 +\left(-2-44p-42p^2+60p^3\right)f_1 \right. \\ && \left. +\left(6+22p+3p^2-30p^3\right)f_2 +\left(-1-2p+3p^2+6p^3\right)f_3 \right] \end{eqnarray*}\end{split}\]
Seven-point formula:
\[\begin{split}\begin{eqnarray*} f^{\prime}(x_0+p h)&=&\frac{1}{36 h^2}\left[ \left(4-24p+48p^2-36p^3+9p^4\right)f_{-3} +\left(12+12p-162p^2+180p^3-54p^4\right)f_{-2} \right. \\ && \left. +\left(-15+120p+162p^2-360p^3+135p^4\right)f_{-1} -4\left(8+48p-3p^2-90p^3+45p^4\right)f_0 \right. \\ && \left. +3\left(14+32p-36p^2-60p^3+45p^4\right)f_1 +\left(-12-12p+54p^2+36p^3-54p^4\right)f_2 \right. \\ && \left. +\left(1-6p^2+9p^4\right)f_3 \right] \end{eqnarray*}\end{split}\]

Note

Uncertainties are not computed and the code for second and third derivatives is unfinished.

Note

The derivatives given, for example, from the five-point formula can sometimes be more accurate than computing the derivative from the interpolation class. This is especially true near the boundaries of the interpolated region.

Public Functions

inline deriv_eqi()
inline int set_npoints(int npoints)

Set the number of points to use for first derivatives (default 5)

Acceptable values are 2-5 (see above).

inline int set_npoints2(int npoints)

Set the number of points to use for second derivatives (default 5)

Acceptable values are 3-5 (see above).

inline virtual int deriv_err(double x, func_t &func, double &dfdx, double &err)

Calculate the first derivative of func w.r.t. x.

inline virtual int deriv2_err(double x, func_t &func, double &dfdx, double &err)

Calculate the second derivative of func w.r.t. x.

inline virtual int deriv3_err(double x, func_t &func, double &dfdx, double &err)

Calculate the third derivative of func w.r.t. x.

inline double deriv_vector(double x, double x0, double dx, size_t nx, const vec_t &y)

Calculate the derivative at x given an array.

This calculates the derivative at x given a function specified in an array y of size nx with equally spaced abscissas. The first abscissa should be given as x0 and the distance between adjacent abscissas should be given as dx. The value x need not be one of the abscissas (i.e. it can lie in between an interval). The appropriate offset is calculated automatically.

inline double deriv2_vector(double x, double x0, double dx, size_t nx, const vec_t &y)

Calculate the second derivative at x given an array.

This calculates the second derivative at x given a function specified in an array y of size nx with equally spaced abscissas. The first abscissa should be given as x0 and the distance between adjacent abscissas should be given as dx. The value x need not be one of the abscissas (i.e. it can lie in between an interval). The appropriate offset is calculated automatically.

inline double deriv3_vector(double x, double x0, double dx, size_t nx, const vec_t &y)

Calculate the third derivative at x given an array.

This calculates the third derivative at x given a function specified in an array y of size nx with equally spaced abscissas. The first abscissa should be given as x0 and the distance between adjacent abscissas should be given as dx. The value x need not be one of the abscissas (i.e. it can lie in between an interval). The appropriate offset is calculated automatically.

inline int deriv_vector(size_t nv, double dx, const vec_t &y, vec_t &dydx)

Calculate the derivative of an entire array.

Right now this uses np=5.

Todo

In deriv_eqi::deriv_vector(): generalize to other values of npoints.

inline virtual const char *type()

Return string denoting type (“deriv_eqi”)

Public Members

double h

Stepsize (Default \( 10^{-4} \) ).

double xoff

Offset (default 0.0)

Protected Functions

inline virtual int deriv_err_int(double x, funct &func, double &dfdx, double &err)

Calculate the first derivative of func w.r.t. x and the uncertainty.

This function doesn’t do anything, and isn’t required for this class since it computes higher-order derivatives directly.

inline double derivp2(double x, double p, func_t &func)

Two-point first derivative.

inline double derivp3(double x, double p, func_t &func)

Three-point first derivative.

inline double derivp4(double x, double p, func_t &func)

Four-point first derivative.

inline double derivp5(double x, double p, func_t &func)

Five-point first derivative.

inline double deriv_vector3(double x, double x0, double dx, size_t nx, const vec_t &y, size_t ix)

Three-point first derivative for arrays.

inline double deriv_vector4(double x, double x0, double dx, size_t nx, const vec_t &y, size_t ix)

Four-point first derivative for arrays.

inline double deriv_vector5(double x, double x0, double dx, size_t nx, const vec_t &y, size_t ix)

Five-point first derivative for arrays.

inline double deriv2p3(double x, double p, func_t &func)

Three-point second derivative.

inline double deriv2p4(double x, double p, func_t &func)

Four-point second derivative.

inline double deriv2p5(double x, double p, func_t &func)

Five-point second derivative.

Protected Attributes

double (deriv_eqi::* cp)(double x, double p, func_t &func)

Pointer to the first derivative function.

double (deriv_eqi::* cap)(double x, double x0, double dx, size_t nx, const vec_t &y, size_t ix)

Pointer to the first derivative for arrays function.

double (deriv_eqi::* c2p)(double x, double p, func_t &func)

Pointer to the second derivative function.

double (deriv_eqi::* c2ap)(double x, double x0, double dx, size_t nx, const vec_t &y, size_t ix)

Pointer to the second derivative for arrays function.

double (deriv_eqi::* c3p)(double x, double h, double p, func_t &func)

Pointer to the third derivative function.

double (deriv_eqi::* c3ap)(double x, double x0, double dx, size_t nx, const vec_t &y, size_t ix)

Pointer to the third derivative for arrays function.