Program Listing for File StateSpace.h

Program Listing for File StateSpace.h#

Return to documentation for file (include/Karana/Math/StateSpace.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<functional>
#include<memory>
#include<unsupported/Eigen/NumericalDiff>
#include<variant>

namespaceKarana::Math{

template<typenameScalar_=double,intNX=Eigen::Dynamic,intNY=Eigen::Dynamic>
structNumDiffBaseFunctor{
usingScalar=Scalar_;
enum{
InputsAtCompileTime=NX,//NOLINT(readability-identifier-naming)
ValuesAtCompileTime=NY//NOLINT(readability-identifier-naming)
};

usingInputType=Eigen::Matrix<Scalar,InputsAtCompileTime,1>;

usingValueType=Eigen::Matrix<Scalar,ValuesAtCompileTime,1>;

usingJacobianType=
Eigen::Matrix<Scalar,ValuesAtCompileTime,InputsAtCompileTime,Eigen::RowMajor>;

constint_inputs;//NOLINT(readability-identifier-naming)

constint_values;//NOLINT(readability-identifier-naming)
NumDiffBaseFunctor(intinputs,intvalues)
:_inputs(inputs)
,_values(values){}

intinputs()const{return_inputs;}

intvalues()const{return_values;}
};

usingfn=std::function<void(constVec&,Vec&)>;

structNumDiffFunctor:NumDiffBaseFunctor<double,Eigen::Dynamic,Eigen::Dynamic>{
usingBase=NumDiffBaseFunctor<double, Eigen::Dynamic, Eigen::Dynamic>;

fn_f;//NOLINT(readability-identifier-naming)

NumDiffFunctor(intinputs,intvalues,fnf)
:NumDiffBaseFunctor(inputs,values)
,_f(f){};

intoperator()(constVec&x,Vec&fvec)const{
_f(x,fvec);
return0;
}
};

usingnum_diff_functor_type=
std::variant<Eigen::NumericalDiff<NumDiffFunctor,Eigen::Forward>,
Eigen::NumericalDiff<NumDiffFunctor,Eigen::Central>>;

classJacobian{
public:
Jacobian(intinput_dim,intvalue_dim,fnf,std::string_viewmode="forward");

fngetF();

voidsetF(fnf);

std::string_viewgetMode();

voidsetMode(std::string_viewmode);

Matoperator()(constVec&x)const;

private:
int_input_dim;

int_value_dim;

fn_f;

std::string_mode;

std::unique_ptr<num_diff_functor_type>_func;

void_setFunctor();

void_jacobian(Eigen::NumericalDiff<NumDiffFunctor,Eigen::Forward>&func,
constVec&x,
Mat&J)const;

void_jacobian(Eigen::NumericalDiff<NumDiffFunctor,Eigen::Central>&func,
constVec&x,
Mat&J)const;
};

usinglin_fn=std::function<void(constVec&,constVec&,Eigen::Ref<Vec>)>;

classStateSpace{
public:
StateSpace(intnum_inputs,
intnum_outputs,
intnum_states,
lin_fnstate_deriv_fn,
lin_fnoutput_fn,
std::string_viewmode="forward");

structSS{
MatA;

MatB;

MatC;

MatD;
};

SSgenerate(constVec&x,constVec&u);

private:
int_num_inputs;
int_num_outputs;
int_num_states;
lin_fn_state_deriv_fn;
lin_fn_output_fn;
std::string_mode;
};

}//namespaceKarana::Math