Program Listing for File Interpolation.h#
↰ Return to documentation for file (include/Karana/Math/Interpolation.h)
/*
*Copyright(c)2024-2026KaranaDynamicsPtyLtd.Allrightsreserved.
*
*NOTICETOUSER:
*
*Thissourcecodeand/ordocumentation(the"LicensedMaterials")is
*theconfidentialandproprietaryinformationofKaranaDynamicsInc.
*UseoftheseLicensedMaterialsisgovernedbythetermsandconditions
*ofaseparatesoftwarelicenseagreementbetweenKaranaDynamicsandthe
*Licensee("LicenseAgreement").Unlessexpresslypermittedunderthat
*agreement,anyreproduction,modification,distribution,ordisclosure
*oftheLicensedMaterials,inwholeorinpart,toanythirdparty
*withoutthepriorwrittenconsentofKaranaDynamicsisstrictlyprohibited.
*
*THELICENSEDMATERIALSAREPROVIDED"ASIS"WITHOUTWARRANTYOFANYKIND.
*KARANADYNAMICSDISCLAIMSALLWARRANTIES,EXPRESSORIMPLIED,INCLUDING
*BUTNOTLIMITEDTOWARRANTIESOFMERCHANTABILITY,NON-INFRINGEMENT,AND
*FITNESSFORAPARTICULARPURPOSE.
*
*INNOEVENTSHALLKARANADYNAMICSBELIABLEFORANYDAMAGESWHATSOEVER,
*INCLUDINGBUTNOTLIMITEDTOLOSSOFPROFITS,DATA,ORUSE,EVENIF
*ADVISEDOFTHEPOSSIBILITYOFSUCHDAMAGES,WHETHERINCONTRACT,TORT,
*OROTHERWISEARISINGOUTOFORINCONNECTIONWITHTHELICENSEDMATERIALS.
*
*U.S.GovernmentEndUsers:TheLicensedMaterialsarea"commercialitem"
*asdefinedat48C.F.R.2.101,andareprovidedtotheU.S.Government
*onlyasacommercialenditemunderthetermsofthislicense.
*
*AnyuseoftheLicensedMaterialsinindividualorcommercialsoftwaremust
*include,intheuserdocumentationandinternalsourcecodecomments,
*thisNotice,Disclaimer,andU.S.GovernmentUseProvision.
*/
#pragmaonce
#include"Karana/Math/Defs.h"
#include<optional>
namespaceKarana::Math{
classBaseInterpolator{
public:
BaseInterpolator()=default;
BaseInterpolator(constBaseInterpolator&)=default;
virtual~BaseInterpolator()=default;
virtualdoubleoperator()(doublex)const=0;
virtualvoidoperator()(ConstVecSlicex,VecSliceout)const;
virtualVecoperator()(ConstVecSlicex)const;
};
classConstantInterpolator:publicBaseInterpolator{
public:
ConstantInterpolator(doubleconstant);
ConstantInterpolator(constConstantInterpolator&)=default;
ConstantInterpolator&operator=(constConstantInterpolator&)=default;
doubleoperator()(doublex)constoverride;
Vecoperator()(ConstVecSlicex)constoverride;
voidoperator()(ConstVecSlicex,VecSliceout)constoverride;
private:
double_constant;
};
classNearestNeighborInterpolator:publicBaseInterpolator{
public:
NearestNeighborInterpolator(ConstVecSliceindep,ConstVecSlicedep);
NearestNeighborInterpolator(constNearestNeighborInterpolator&)=default;
NearestNeighborInterpolator&operator=(constNearestNeighborInterpolator&)=default;
usingBaseInterpolator::operator();//Bringinoverloadswearen'toverriding
doubleoperator()(doublex)constoverride;
private:
int_findNearestIndex(doublex)const;
Vec_indep;
Vec_dep;
};
classLinearInterpolator:publicBaseInterpolator{
public:
enumclassExtrapolation{
CONSTANT,
LINEAR,
};
LinearInterpolator(ConstVecSliceindep,
ConstVecSlicedep,
std::optional<Extrapolation>extrapolation=std::nullopt);
LinearInterpolator(constLinearInterpolator&)=default;
LinearInterpolator&operator=(constLinearInterpolator&)=default;
usingBaseInterpolator::operator();//Bringinoverloadswearen'toverriding
doubleoperator()(doublex)constoverride;
private:
double_extendLowerBound(doublex)const;
double_extendUpperBound(doublex)const;
Vec_indep;
Vec_dep;
Vec_slopes;
//Howtoextrapolateoutsidethegivendatapoints
std::optional<Extrapolation>_extrapolation;
};
classAkimaSplineInterpolator:publicBaseInterpolator{
public:
enumclassExtrapolation{
CONSTANT,
LINEAR,
CUBIC,
};
AkimaSplineInterpolator(ConstVecSliceindep,
ConstVecSlicedep,
std::optional<Extrapolation>extrapolation=std::nullopt);
AkimaSplineInterpolator(constAkimaSplineInterpolator&);
AkimaSplineInterpolator&operator=(constAkimaSplineInterpolator&);
usingBaseInterpolator::operator();//Bringinoverloadswearen'toverriding
doubleoperator()(doublex)constoverride;
private:
double_extendLowerBound(doublex)const;
double_extendUpperBound(doublex)const;
Vec_indep;
Vec_dep;
//Slopeofthesplineateachpoint.
//Called`s`insomeformulations.
Vec_spline_slopes;
//Eachsplinesegment'sconstantterms
VecSlice_a;
//Coefficientforeachsplinesegment'slinearterm
VecSlice_b;
//Coefficientforeachsplinesegment'squadraticterm
Vec_c;
//Coefficientforeachsplinesegment'scubicterm
Vec_d;
//Howtoextrapolateoutsidethegivendatapoints
std::optional<Extrapolation>_extrapolation;
};
classCubicHermiteInterpolator:publicBaseInterpolator{
public:
enumclassExtrapolation{
CONSTANT,
LINEAR,
CUBIC,
};
CubicHermiteInterpolator(ConstVecSliceindep,
ConstVecSlicedep,
std::optional<Extrapolation>extrapolation=std::nullopt,
std::optional<ConstVecSlice>deriv=std::nullopt);
CubicHermiteInterpolator(constCubicHermiteInterpolator&)=default;
CubicHermiteInterpolator&operator=(constCubicHermiteInterpolator&)=default;
usingBaseInterpolator::operator();//Bringinoverloadswearen'toverriding
doubleoperator()(doublex)constoverride;
ConstVecSlicederivatives()const;
private:
void_calculateDerivatives();
double_interpolate(size_tseg,doublex)const;
double_extendLowerBound(doublex)const;
double_extendUpperBound(doublex)const;
Vec_indep;
Vec_dep;
std::optional<Extrapolation>_extrapolation;
Vec_deriv;
};
}//namespaceKarana::Math