Isis 3 Programmer Reference
NumericalApproximation.h
1#ifndef NumericalApproximation_h
2#define NumericalApproximation_h
8/* SPDX-License-Identifier: CC0-1.0 */
9
10#include <string>
11#include <vector>
12#include <map>
13#include <gsl/gsl_errno.h>
14#include <gsl/gsl_spline.h>
15#include "IException.h"
16
17using namespace std;
18
19namespace Isis {
727 public:
742
743 // CONSTRUCTORS
745 NumericalApproximation(unsigned int n, double *x, double *y,
747 NumericalApproximation(const vector <double> &x, const vector <double> &y,
750 // ASSIGNMENT OPERATOR
752 // DESTRUCTOR
753 virtual ~NumericalApproximation();
754
755
756 // ACCESSOR METHODS FOR OBJECT PROPERTIES
757 string Name() const;
782 return (p_itype);
783 }
784 int MinPoints();
786
787 // ADD DATA TO OBJECT
788 void AddData(const double x, const double y);
789 void AddData(unsigned int n, double *x, double *y);
790 void AddData(const vector <double> &x, const vector <double> &y);
791 void SetCubicClampedEndptDeriv(const double yp1, const double ypn);
792 void AddCubicHermiteDeriv(unsigned int n, double *fprimeOfx);
793 void AddCubicHermiteDeriv(const vector <double> &fprimeOfx);
794 void AddCubicHermiteDeriv(const double fprimeOfx);
795
796 //ACCESSOR METHODS AFTER DATA IS ENTERED
797 double DomainMinimum();
798 double DomainMaximum();
799 bool Contains(double x);
805 inline unsigned int Size() {
806 return(p_x.size());
807 }
808
819 // INTERPOLATION AND EXTRAPOLATION RESULTS
820 double Evaluate(const double a, const ExtrapType &etype = ThrowError);
821 vector <double> Evaluate(const vector <double> &a, const ExtrapType &etype = ThrowError);
822 vector <double> PolynomialNevilleErrorEstimate();
823 vector <double> CubicClampedSecondDerivatives();
824 double EvaluateCubicHermiteFirstDeriv(const double a);
825 double EvaluateCubicHermiteSecDeriv(const double a);
826
827 // DIFFERENTIATION METHODS
828 double GslFirstDerivative(const double a);
829 double BackwardFirstDifference(const double a, const unsigned int n = 3,
830 const double h = 0.1);
831 double ForwardFirstDifference(const double a, const unsigned int n = 3,
832 const double h = 0.1);
833 double CenterFirstDifference(const double a, const unsigned int n = 5,
834 const double h = 0.1);
835
836 double GslSecondDerivative(const double a);
837 double BackwardSecondDifference(const double a, const unsigned int n = 3,
838 const double h = 0.1);
839 double ForwardSecondDifference(const double a, const unsigned int n = 3,
840 const double h = 0.1);
841 double CenterSecondDifference(const double a, const unsigned int n = 5,
842 const double h = 0.1);
843
844 // INTERGRATION METHODS
845 double GslIntegral(const double a, const double b);
846 double TrapezoidalRule(const double a, const double b);
847 double Simpsons3PointRule(const double a, const double b);
848 double Simpsons4PointRule(const double a, const double b);
849 double BoolesRule(const double a, const double b);
850 double RefineExtendedTrap(double a, double b, double s, unsigned int n);
851 double RombergsMethod(double a, double b);
852
853 // ASSIGNMENT OPERATORS
854 void Reset();
857
858 protected:
859 // == CLASS VARIABLES
860 // VARIABLES FOR ALL INTERP TYPES
862 vector<double> p_x;
863 vector<double> p_y;
865 // GSL INTERP VARIABLES
866 typedef const gsl_interp_type *InterpFunctor;
867 typedef map<InterpType, InterpFunctor> FunctorList;
868 typedef FunctorList::const_iterator FunctorConstIter;
869 gsl_interp_accel *p_acc;
870 gsl_spline *p_interp;
872 // CUBIC CLAMPED VARIABLES
877 vector<double> p_clampedSecondDerivs;
878 // POLYNOMIAL NEVILLE VARIABLES
879 vector <double> p_polyNevError;
880 // CUBIC HERMITE VARIABLES
881 vector<double> p_fprimeOfx;
882
883
884 // == PROTECTED METHODS
885 // CREATING, DESTROYING OBJECT
888 void GslAllocation(unsigned int npoints);
889 void GslDeallocation();
891 // VERIFICATION METHODS
892 void GslIntegrityCheck(int gsl_status, const char *src,
893 int line);
894 void ValidateDataSet();
895 bool InsideDomain(const double a);
896 // COMPUTATION AND EVALUATION METHODS
897 bool GslComputed() const;
898 void ComputeGsl();
899 void ComputeCubicClamped();
900 double ValueToExtrapolate(const double a, const ExtrapType &etype);
901 double EvaluateCubicNeighborhood(const double a);
902 vector <double> EvaluateCubicNeighborhood(const vector <double> &a, const ExtrapType &etype);
903 double EvaluateCubicClamped(const double a);
904 double EvaluateCubicHermite(const double a);
905 double EvaluatePolynomialNeville(const double a);
906 vector <double> EvaluateForIntegration(const double a, const double b,
907 const unsigned int n);
908 int FindIntervalLowerIndex(const double a);
909 // STANDARDIZE ERRORS
910 void ReportException(IException::ErrorType type, const string &method,
911 const string &message, const char *filesrc,
912 int lineno) const;
913 };
914};
915
916#endif
ErrorType
Contains a set of exception error types.
Definition IException.h:111
NumericalApproximation provides various numerical analysis methods of interpolation,...
void ValidateDataSet()
Validates the data set before computing interpolation.
gsl_interp_accel * p_acc
Lookup accelorator.
void GslDeallocation()
Deallocate GSL interpolator resources, if used.
void GslAllocation(unsigned int npoints)
Allocates GSL interpolation functions.
double EvaluatePolynomialNeville(const double a)
Performs polynomial interpolation using Neville's algorithm.
vector< double > p_x
List of X values.
vector< double > p_fprimeOfx
List of first derivatives corresponding to each x value in the data set (i.e. each value in p_x)
void ReportException(IException::ErrorType type, const string &method, const string &message, const char *filesrc, int lineno) const
Generalized error report generator.
double GslFirstDerivative(const double a)
Approximates the first derivative of the data set function evaluated at the given domain value for GS...
double p_clampedDerivLastPt
First derivative of last x-value, p_x[n-1]. This is only used for the CubicClamped interpolation type...
int FindIntervalLowerIndex(const double a)
Find the index of the x-value in the data set that is just below the input value, a.
vector< double > CubicClampedSecondDerivatives()
Retrieves the second derivatives of the data set.
double DomainMinimum()
Input data domain minimum value.
bool GslInterpType(NumericalApproximation::InterpType itype) const
Returns whether an interpolation type is adapted from the GSL library.
InterpFunctor GslFunctor(NumericalApproximation::InterpType itype) const
Search for a GSL interpolation function.
bool p_clampedComputed
Flag variable to determine whether ComputeCubicClamped() has been called.
double Evaluate(const double a, const ExtrapType &etype=ThrowError)
Calculates interpolated or extrapolated value of tabulated data set for given domain value.
void Init(NumericalApproximation::InterpType itype)
Initializes the object upon instantiation.
bool InsideDomain(const double a)
Returns whether the passed value is greater than or equal to the domain minimum and less than or equa...
bool p_dataValidated
Flag variable to determine whether ValidateDataSet() has been called.
void GslIntegrityCheck(int gsl_status, const char *src, int line)
Checks the status of the GSL interpolation operations.
void AddCubicHermiteDeriv(unsigned int n, double *fprimeOfx)
Adds values for the first derivatives of the data points.
const gsl_interp_type * InterpFunctor
GSL Interpolation specs.
void ComputeGsl()
Computes the GSL interpolation for a set of (x,y) data points.
string Name() const
Get name of interpolating function assigned to object.
map< InterpType, InterpFunctor > FunctorList
Set up a std::map of GSL interpolator functors. List of function types.
double RombergsMethod(double a, double b)
Uses Romberg's method to approximate the integral of the interpolated data set function on the interv...
InterpType
This enum defines the types of interpolation supported in this class.
@ CubicClamped
Cubic Spline interpolation with clamped boundary conditions.
@ CubicNatPeriodic
Cubic Spline interpolation with periodic boundary conditions.
@ CubicHermite
Cubic Spline interpolation using the Hermite cubic polynomial.
@ CubicNeighborhood
Cubic Spline interpolation using 4-pt Neighborhoods with natural boundary conditions.
@ PolynomialNeville
Polynomial interpolation using Neville's algorithm.
@ CubicNatural
Cubic Spline interpolation with natural boundary conditions.
@ AkimaPeriodic
Non-rounded Akima Spline interpolation with periodic boundary conditions.
@ Polynomial
Polynomial interpolation.
@ Akima
Non-rounded Akima Spline interpolation with natural boundary conditions.
NumericalApproximation & operator=(const NumericalApproximation &numApMeth)
NumericalApproximation assigmment operator sets this object "equal to" another.
double TrapezoidalRule(const double a, const double b)
Uses the trapezoidal rule to approximate the integral of the interpolated data set function on the in...
double RefineExtendedTrap(double a, double b, double s, unsigned int n)
Calculates refinements extended trapezoidal rule to approximate the integral of the interpolated data...
double ValueToExtrapolate(const double a, const ExtrapType &etype)
Returns the domain value at which to evaluate.
double GslSecondDerivative(const double a)
Approximates the second derivative of the interpolated data set function evaluated at the given domai...
void ComputeCubicClamped()
Computes the cubic clamped interpolation for a set of (x,y) data points, given the first derivatives ...
bool Contains(double x)
Returns whether the passed value is an element of the set of x-values in the data set.
void SetInterpType(NumericalApproximation::InterpType itype)
Sets interpolation type.
double Simpsons4PointRule(const double a, const double b)
Uses Simpson's 4-point rule to approximate the integral of the interpolated data set function on the ...
double BackwardFirstDifference(const double a, const unsigned int n=3, const double h=0.1)
Uses an n point backward first difference formula to approximate the first derivative evaluated at a ...
double EvaluateCubicHermite(const double a)
Performs interpolation using the Hermite cubic polynomial.
bool GslComputed() const
Returns whether a GSL interpolation computation of the data set has been performed.
gsl_spline * p_interp
Currently active interpolator.
double GslIntegral(const double a, const double b)
Approximates the integral of the data set function evaluated on the given interval for GSL supported ...
void Reset()
Resets the state of the object.
FunctorList::const_iterator FunctorConstIter
GSL Iterator.
vector< double > p_clampedSecondDerivs
List of second derivatives evaluated at p_x values. This is only used for the CubicClamped interpolat...
bool p_clampedEndptsSet
Flag variable to determine whether SetCubicClampedEndptDeriv() has been called after all data was add...
NumericalApproximation(const NumericalApproximation::InterpType &itype=CubicNatural)
Default constructor creates NumericalApproximation object.
double CenterSecondDifference(const double a, const unsigned int n=5, const double h=0.1)
Uses an n point center second difference formula to approximate the second derivative evaluated at a ...
double p_clampedDerivFirstPt
First derivative of first x-value, p_x[0]. This is only used for the CubicClamped interpolation type.
InterpType p_itype
Interpolation type.
double EvaluateCubicHermiteSecDeriv(const double a)
Approximates the second derivative of the data set function evaluated at the given domain value for C...
double Simpsons3PointRule(const double a, const double b)
Uses Simpson's 3-point rule to approximate the integral of the interpolated data set function on the ...
static FunctorList p_interpFunctors
Maintains list of interpolator options.
double DomainMaximum()
Input data domain maximum value.
double EvaluateCubicClamped(const double a)
Performs cubic spline interpolation with clamped boundary conditions, if possible.
virtual ~NumericalApproximation()
Destructor deallocates memory being used.
double BackwardSecondDifference(const double a, const unsigned int n=3, const double h=0.1)
Uses an n point backward second difference formula to approximate the second derivative evaluated at ...
double ForwardFirstDifference(const double a, const unsigned int n=3, const double h=0.1)
Uses an n point forward first difference formula to approximate the first derivative evaluated at a g...
double BoolesRule(const double a, const double b)
Uses Boole's Rule to approximate the integral of the interpolated data set function on the interval (...
vector< double > p_polyNevError
Estimate of error for interpolation evaluated at x. This is only used for the PolynomialNeville inter...
double EvaluateCubicNeighborhood(const double a)
Performs cubic spline interpolation for a neighborhood about a.
double CenterFirstDifference(const double a, const unsigned int n=5, const double h=0.1)
Uses an n point center first difference formula to approximate the first derivative evaluated at a gi...
double EvaluateCubicHermiteFirstDeriv(const double a)
Approximates the first derivative of the data set function evaluated at the given domain value for Cu...
ExtrapType
This enum defines the manner in which a value outside of the domain should be handled if passed to th...
@ NearestEndpoint
Evaluate() returns the y-value of the nearest endpoint if a is outside of the domain.
@ ThrowError
Evaluate() throws an error if a is outside of the domain.
@ Extrapolate
Evaluate() attempts to extrapolate if a is outside of the domain. This is only valid for NumericalApp...
InterpType InterpolationType()
Returns the enumerated type of interpolation chosen.
unsigned int Size()
Returns the number of the coordinates added to the data set.
int MinPoints()
Minimum number of points required by interpolating function.
double ForwardSecondDifference(const double a, const unsigned int n=3, const double h=0.1)
Uses an n point forward second difference formula to approximate the second derivative evaluated at a...
vector< double > PolynomialNevilleErrorEstimate()
Retrieves the error estimate for the Neville's polynomial interpolation type.
vector< double > p_y
List of Y values.
void SetCubicClampedEndptDeriv(const double yp1, const double ypn)
Sets the values for the first derivatives of the endpoints of the data set.
void AddData(const double x, const double y)
Add a datapoint to the set.
vector< double > EvaluateForIntegration(const double a, const double b, const unsigned int n)
Evaluates data set in order to have enough data points to approximate the function to be integrated.
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16
Namespace for the standard library.