Isis Developer Reference
NumericalApproximation.h
Go to the documentation of this file.
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,...
Definition NumericalApproximation.h:726
void ValidateDataSet()
Validates the data set before computing interpolation.
Definition NumericalApproximation.cpp:2563
gsl_interp_accel * p_acc
Lookup accelorator.
Definition NumericalApproximation.h:869
void GslDeallocation()
Deallocate GSL interpolator resources, if used.
Definition NumericalApproximation.cpp:2466
void GslAllocation(unsigned int npoints)
Allocates GSL interpolation functions.
Definition NumericalApproximation.cpp:2444
double EvaluatePolynomialNeville(const double a)
Performs polynomial interpolation using Neville's algorithm.
Definition NumericalApproximation.cpp:3242
vector< double > p_x
List of X values.
Definition NumericalApproximation.h:862
vector< double > p_fprimeOfx
List of first derivatives corresponding to each x value in the data set (i.e. each value in p_x)
Definition NumericalApproximation.h:881
void ReportException(IException::ErrorType type, const string &method, const string &message, const char *filesrc, int lineno) const
Generalized error report generator.
Definition NumericalApproximation.cpp:3372
double GslFirstDerivative(const double a)
Approximates the first derivative of the data set function evaluated at the given domain value for GS...
Definition NumericalApproximation.cpp:1020
double p_clampedDerivLastPt
First derivative of last x-value, p_x[n-1]. This is only used for the CubicClamped interpolation type...
Definition NumericalApproximation.h:876
int FindIntervalLowerIndex(const double a)
Find the index of the x-value in the data set that is just below the input value, a.
Definition NumericalApproximation.cpp:3183
vector< double > CubicClampedSecondDerivatives()
Retrieves the second derivatives of the data set.
Definition NumericalApproximation.cpp:687
double DomainMinimum()
Input data domain minimum value.
Definition NumericalApproximation.cpp:723
bool GslInterpType(NumericalApproximation::InterpType itype) const
Returns whether an interpolation type is adapted from the GSL library.
Definition NumericalApproximation.cpp:2418
InterpFunctor GslFunctor(NumericalApproximation::InterpType itype) const
Search for a GSL interpolation function.
Definition NumericalApproximation.cpp:2492
bool p_clampedComputed
Flag variable to determine whether ComputeCubicClamped() has been called.
Definition NumericalApproximation.h:874
double Evaluate(const double a, const ExtrapType &etype=ThrowError)
Calculates interpolated or extrapolated value of tabulated data set for given domain value.
Definition NumericalApproximation.cpp:836
void Init(NumericalApproximation::InterpType itype)
Initializes the object upon instantiation.
Definition NumericalApproximation.cpp:2375
bool InsideDomain(const double a)
Returns whether the passed value is greater than or equal to the domain minimum and less than or equa...
Definition NumericalApproximation.cpp:2619
bool p_dataValidated
Flag variable to determine whether ValidateDataSet() has been called.
Definition NumericalApproximation.h:864
void GslIntegrityCheck(int gsl_status, const char *src, int line)
Checks the status of the GSL interpolation operations.
Definition NumericalApproximation.cpp:2523
void AddCubicHermiteDeriv(unsigned int n, double *fprimeOfx)
Adds values for the first derivatives of the data points.
Definition NumericalApproximation.cpp:592
const gsl_interp_type * InterpFunctor
GSL Interpolation specs.
Definition NumericalApproximation.h:866
void ComputeGsl()
Computes the GSL interpolation for a set of (x,y) data points.
Definition NumericalApproximation.cpp:2683
string Name() const
Get name of interpolating function assigned to object.
Definition NumericalApproximation.cpp:282
map< InterpType, InterpFunctor > FunctorList
Set up a std::map of GSL interpolator functors. List of function types.
Definition NumericalApproximation.h:867
double RombergsMethod(double a, double b)
Uses Romberg's method to approximate the integral of the interpolated data set function on the interv...
Definition NumericalApproximation.cpp:2179
InterpType
This enum defines the types of interpolation supported in this class.
Definition NumericalApproximation.h:731
@ CubicClamped
Cubic Spline interpolation with clamped boundary conditions.
Definition NumericalApproximation.h:735
@ CubicNatPeriodic
Cubic Spline interpolation with periodic boundary conditions.
Definition NumericalApproximation.h:736
@ Linear
Linear interpolation.
Definition NumericalApproximation.h:731
@ CubicHermite
Cubic Spline interpolation using the Hermite cubic polynomial.
Definition NumericalApproximation.h:738
@ CubicNeighborhood
Cubic Spline interpolation using 4-pt Neighborhoods with natural boundary conditions.
Definition NumericalApproximation.h:737
@ PolynomialNeville
Polynomial interpolation using Neville's algorithm.
Definition NumericalApproximation.h:733
@ CubicNatural
Cubic Spline interpolation with natural boundary conditions.
Definition NumericalApproximation.h:734
@ AkimaPeriodic
Non-rounded Akima Spline interpolation with periodic boundary conditions.
Definition NumericalApproximation.h:740
@ Polynomial
Polynomial interpolation.
Definition NumericalApproximation.h:732
@ Akima
Non-rounded Akima Spline interpolation with natural boundary conditions.
Definition NumericalApproximation.h:739
NumericalApproximation & operator=(const NumericalApproximation &numApMeth)
NumericalApproximation assigmment operator sets this object "equal to" another.
Definition NumericalApproximation.cpp:220
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...
Definition NumericalApproximation.cpp:1868
double RefineExtendedTrap(double a, double b, double s, unsigned int n)
Calculates refinements extended trapezoidal rule to approximate the integral of the interpolated data...
Definition NumericalApproximation.cpp:2096
double ValueToExtrapolate(const double a, const ExtrapType &etype)
Returns the domain value at which to evaluate.
Definition NumericalApproximation.cpp:2830
double GslSecondDerivative(const double a)
Approximates the second derivative of the interpolated data set function evaluated at the given domai...
Definition NumericalApproximation.cpp:1422
void ComputeCubicClamped()
Computes the cubic clamped interpolation for a set of (x,y) data points, given the first derivatives ...
Definition NumericalApproximation.cpp:2735
bool Contains(double x)
Returns whether the passed value is an element of the set of x-values in the data set.
Definition NumericalApproximation.cpp:793
void SetInterpType(NumericalApproximation::InterpType itype)
Sets interpolation type.
Definition NumericalApproximation.cpp:2319
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 ...
Definition NumericalApproximation.cpp:1972
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 ...
Definition NumericalApproximation.cpp:1159
double EvaluateCubicHermite(const double a)
Performs interpolation using the Hermite cubic polynomial.
Definition NumericalApproximation.cpp:3121
bool GslComputed() const
Returns whether a GSL interpolation computation of the data set has been performed.
Definition NumericalApproximation.cpp:2653
gsl_spline * p_interp
Currently active interpolator.
Definition NumericalApproximation.h:870
double GslIntegral(const double a, const double b)
Approximates the integral of the data set function evaluated on the given interval for GSL supported ...
Definition NumericalApproximation.cpp:1797
void Reset()
Resets the state of the object.
Definition NumericalApproximation.cpp:2251
FunctorList::const_iterator FunctorConstIter
GSL Iterator.
Definition NumericalApproximation.h:868
vector< double > p_clampedSecondDerivs
List of second derivatives evaluated at p_x values. This is only used for the CubicClamped interpolat...
Definition NumericalApproximation.h:877
bool p_clampedEndptsSet
Flag variable to determine whether SetCubicClampedEndptDeriv() has been called after all data was add...
Definition NumericalApproximation.h:873
NumericalApproximation(const NumericalApproximation::InterpType &itype=CubicNatural)
Default constructor creates NumericalApproximation object.
Definition NumericalApproximation.cpp:41
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 ...
Definition NumericalApproximation.cpp:1716
double p_clampedDerivFirstPt
First derivative of first x-value, p_x[0]. This is only used for the CubicClamped interpolation type.
Definition NumericalApproximation.h:875
InterpType p_itype
Interpolation type.
Definition NumericalApproximation.h:861
double EvaluateCubicHermiteSecDeriv(const double a)
Approximates the second derivative of the data set function evaluated at the given domain value for C...
Definition NumericalApproximation.cpp:1474
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 ...
Definition NumericalApproximation.cpp:1920
static FunctorList p_interpFunctors
Maintains list of interpolator options.
Definition NumericalApproximation.h:871
double DomainMaximum()
Input data domain maximum value.
Definition NumericalApproximation.cpp:760
double EvaluateCubicClamped(const double a)
Performs cubic spline interpolation with clamped boundary conditions, if possible.
Definition NumericalApproximation.cpp:3060
virtual ~NumericalApproximation()
Destructor deallocates memory being used.
Definition NumericalApproximation.cpp:256
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 ...
Definition NumericalApproximation.cpp:1546
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...
Definition NumericalApproximation.cpp:1248
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 (...
Definition NumericalApproximation.cpp:2027
vector< double > p_polyNevError
Estimate of error for interpolation evaluated at x. This is only used for the PolynomialNeville inter...
Definition NumericalApproximation.h:879
double EvaluateCubicNeighborhood(const double a)
Performs cubic spline interpolation for a neighborhood about a.
Definition NumericalApproximation.cpp:2893
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...
Definition NumericalApproximation.cpp:1338
double EvaluateCubicHermiteFirstDeriv(const double a)
Approximates the first derivative of the data set function evaluated at the given domain value for Cu...
Definition NumericalApproximation.cpp:1071
ExtrapType
This enum defines the manner in which a value outside of the domain should be handled if passed to th...
Definition NumericalApproximation.h:813
@ NearestEndpoint
Evaluate() returns the y-value of the nearest endpoint if a is outside of the domain.
Definition NumericalApproximation.h:817
@ ThrowError
Evaluate() throws an error if a is outside of the domain.
Definition NumericalApproximation.h:813
@ Extrapolate
Evaluate() attempts to extrapolate if a is outside of the domain. This is only valid for NumericalApp...
Definition NumericalApproximation.h:814
InterpType InterpolationType()
Returns the enumerated type of interpolation chosen.
Definition NumericalApproximation.h:781
unsigned int Size()
Returns the number of the coordinates added to the data set.
Definition NumericalApproximation.h:805
int MinPoints()
Minimum number of points required by interpolating function.
Definition NumericalApproximation.cpp:333
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...
Definition NumericalApproximation.cpp:1628
vector< double > PolynomialNevilleErrorEstimate()
Retrieves the error estimate for the Neville's polynomial interpolation type.
Definition NumericalApproximation.cpp:970
vector< double > p_y
List of Y values.
Definition NumericalApproximation.h:863
void SetCubicClampedEndptDeriv(const double yp1, const double ypn)
Sets the values for the first derivatives of the endpoints of the data set.
Definition NumericalApproximation.cpp:565
void AddData(const double x, const double y)
Add a datapoint to the set.
Definition NumericalApproximation.cpp:440
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.
Definition NumericalApproximation.cpp:3306
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16
Namespace for the standard library.