Program Listing for File Interpolation.h

Program Listing for File Interpolation.h#

Return to documentation for file (include/Karana/Math/Interpolation.h)

/*
*Copyright(c)2024-2025KaranaDynamicsPtyLtd.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:
virtual~BaseInterpolator()=default;

virtualdoubleoperator()(doublex)const=0;

virtualvoidoperator()(ConstVecSlicex,VecSliceout)const;

virtualVecoperator()(ConstVecSlicex)const;
};

classConstantInterpolator:publicBaseInterpolator{
public:
ConstantInterpolator(doubleconstant);

doubleoperator()(doublex)constoverride;
Vecoperator()(ConstVecSlicex)constoverride;
voidoperator()(ConstVecSlicex,VecSliceout)constoverride;

private:
double_constant;
};

classNearestNeighborInterpolator:publicBaseInterpolator{
public:
NearestNeighborInterpolator(ConstVecSliceindep,ConstVecSlicedep);

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);

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);

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;
};
}//namespaceKarana::Math