- 3.0.1 core module.
ct::core::DerivativesCppadJIT< IN_DIM, OUT_DIM > Class Template Reference

Jacobian using Auto-Diff Codegeneration. More...

#include <DerivativesCppadJIT.h>

Inheritance diagram for ct::core::DerivativesCppadJIT< IN_DIM, OUT_DIM >:
ct::core::Derivatives< IN_DIM, OUT_DIM, double >

Public Types

using CG_SCALAR = ADCGScalar
 CG_SCALAR type. More...
 
using CG_VALUE_TYPE = ADCGValueType
 autodiff scalar type More...
 
using IN_TYPE_CG = Eigen::Matrix< CG_SCALAR, IN_DIM, 1 >
 function input vector type More...
 
using OUT_TYPE_CG = Eigen::Matrix< CG_SCALAR, OUT_DIM, 1 >
 function output vector type More...
 
using IN_TYPE_D = Eigen::Matrix< double, IN_DIM, 1 >
 function input vector type double More...
 
using OUT_TYPE_D = Eigen::Matrix< double, OUT_DIM, 1 >
 function output vector type More...
 
using JAC_TYPE_D = Eigen::Matrix< double, OUT_DIM, IN_DIM >
 Jacobian type. More...
 
using JAC_TYPE_ROW_MAJOR = Eigen::Matrix< double, OUT_DIM, IN_DIM, Eigen::RowMajor >
 Jac. type in row-major. More...
 
using HES_TYPE_D = Eigen::Matrix< double, IN_DIM, IN_DIM >
 
using HES_TYPE_ROW_MAJOR = Eigen::Matrix< double, IN_DIM, IN_DIM, Eigen::RowMajor >
 
using FUN_TYPE_CG = std::function< OUT_TYPE_CG(const IN_TYPE_CG &)>
 function type More...
 
using DerivativesBase = Derivatives< IN_DIM, OUT_DIM >
 
- Public Types inherited from ct::core::Derivatives< IN_DIM, OUT_DIM, double >
typedef Eigen::Matrix< double, IN_DIM, 1 > IN_TYPE
 function input vector type More...
 
typedef Eigen::Matrix< double, OUT_DIM, 1 > OUT_TYPE
 function output vector type More...
 
typedef Eigen::Matrix< double, OUT_DIM, IN_DIM > JAC_TYPE
 
typedef Eigen::Matrix< double, IN_DIM, IN_DIM > HES_TYPE
 

Public Member Functions

 DerivativesCppadJIT (FUN_TYPE_CG &f, int inputDim=IN_DIM, int outputDim=OUT_DIM)
 Contructs the derivatives for codegeneration using a FUN_TYPE_CG function. More...
 
 DerivativesCppadJIT (const DerivativesCppadJIT &arg)
 copy constructor More...
 
void update (FUN_TYPE_CG &f, const size_t inputDim=IN_DIM, const size_t outputDim=OUT_DIM)
 update the Jacobian with a new function More...
 
virtual ~DerivativesCppadJIT ()=default
 destructor More...
 
DerivativesCppadJITclone () const
 deep cloning of Jacobian More...
 
virtual OUT_TYPE_D forwardZero (const Eigen::VectorXd &x)
 Evaluates the method itself. More...
 
virtual JAC_TYPE_D jacobian (const Eigen::VectorXd &x)
 Evaluates the jacobian with respect to the input. More...
 
virtual void sparseJacobian (const Eigen::VectorXd &x, Eigen::VectorXd &jac, Eigen::VectorXi &iRow, Eigen::VectorXi &jCol)
 Returns the evaluated jacobian in sparse format. More...
 
virtual Eigen::VectorXd sparseJacobianValues (const Eigen::VectorXd &x)
 Returns the non zero values of the jacobian. More...
 
virtual HES_TYPE_D hessian (const Eigen::VectorXd &x, const Eigen::VectorXd &lambda)
 Evaluates the hessian (2nd order derivatives with respect to input) of the method. In case of a vector valued function, the method returns the weighted sum of the hessians with weights w. More...
 
virtual void sparseHessian (const Eigen::VectorXd &x, const Eigen::VectorXd &lambda, Eigen::VectorXd &hes, Eigen::VectorXi &iRow, Eigen::VectorXi &jCol)
 Returns the weighted sum of hessian of the problem in sparse format. More...
 
virtual Eigen::VectorXd sparseHessianValues (const Eigen::VectorXd &x, const Eigen::VectorXd &lambda)
 Returns the non zero elements of the hessian of the problem. More...
 
Eigen::Matrix< bool, Eigen::Dynamic, Eigen::Dynamic > getSparsityPatternJacobian ()
 get Jacobian sparsity pattern More...
 
Eigen::Matrix< bool, Eigen::Dynamic, Eigen::Dynamic > getSparsityPatternHessian ()
 get Hessian sparsity pattern More...
 
void getSparsityPatternJacobian (Eigen::VectorXi &rows, Eigen::VectorXi &columns)
 get Jacobian sparsity pattern More...
 
size_t getNumNonZerosJacobian ()
 Returns the number of nonzeros in the sparse jacobian. More...
 
size_t getNumNonZerosHessian ()
 Returns the number of nonzeros in the sparse hessian. More...
 
void getSparsityPatternHessian (Eigen::VectorXi &rows, Eigen::VectorXi &columns)
 get Hessian sparsity pattern More...
 
void compileJIT (const DerivativesCppadSettings &settings, const std::string &libName="unnamedLib", bool verbose=false)
 Uses just-in-time compilation to compile the Jacobian and other derivatives. More...
 
const std::shared_ptr< CppAD::cg::DynamicLib< double > > getDynamicLib ()
 retrieve the dynamic library, e.g. for testing purposes More...
 
- Public Member Functions inherited from ct::core::Derivatives< IN_DIM, OUT_DIM, double >
 Derivatives ()
 
virtual ~Derivatives ()
 default destructor More...
 

Protected Member Functions

void recordCg ()
 record the Auto-Diff terms for code generation More...
 

Protected Attributes

std::function< OUT_TYPE_CG(const IN_TYPE_CG &)> cgStdFun_
 
int inputDim_
 the function More...
 
int outputDim_
 function input dimension More...
 
CppAD::ADFun< CG_VALUE_TYPEcgCppadFun_
 function output dimension More...
 
bool compiled_
 auto-diff function More...
 
std::string libName_
 flag if Jacobian is compiled More...
 
std::vector< size_t > sparsityRowsJacobian_
 a unique name for this library More...
 
std::vector< size_t > sparsityColsJacobian_
 
std::vector< size_t > sparsityRowsHessian_
 
std::vector< size_t > sparsityColsHessian_
 
Eigen::VectorXi sparsityRowsJacobianEigen_
 
Eigen::VectorXi sparsityColsJacobianEigen_
 
Eigen::VectorXi sparsityRowsHessianEigen_
 
Eigen::VectorXi sparsityColsHessianEigen_
 
CppAD::cg::GccCompiler< double > compiler_
 
CppAD::cg::ClangCompiler< double > compilerClang_
 compile for codegeneration More...
 
std::shared_ptr< CppAD::cg::DynamicLib< double > > dynamicLib_
 
std::shared_ptr< CppAD::cg::GenericModel< double > > model_
 dynamic library to load after compilation More...
 

Detailed Description

template<int IN_DIM, int OUT_DIM>
class ct::core::DerivativesCppadJIT< IN_DIM, OUT_DIM >

Jacobian using Auto-Diff Codegeneration.

Uses Auto-Diff code generation to compute the Jacobian $ J(x_s) = \frac{df}{dx} |_{x=x_s} $ of a regular vector-valued mathematical function $ y = f(x) $ .

x has IN_DIM dimension and y has OUT_DIM dimension. Thus, they can be scalar functions (IN_DIM = 1, OUT_DIM = 1), fixed or variable size (IN_DIM = -1, OUT_DIM = -1) functions.

Note
In fact, this class is called Jacobian but computes also zero order derivatives
Template Parameters
IN_DIMInput dimensionality of the function (use Eigen::Dynamic (-1) for dynamic size)
OUT_DIMOutput dimensionailty of the function (use Eigen::Dynamic (-1) for dynamic size)

Member Typedef Documentation

template<int IN_DIM, int OUT_DIM>
using ct::core::DerivativesCppadJIT< IN_DIM, OUT_DIM >::CG_SCALAR = ADCGScalar

CG_SCALAR type.

template<int IN_DIM, int OUT_DIM>
using ct::core::DerivativesCppadJIT< IN_DIM, OUT_DIM >::CG_VALUE_TYPE = ADCGValueType

autodiff scalar type

template<int IN_DIM, int OUT_DIM>
using ct::core::DerivativesCppadJIT< IN_DIM, OUT_DIM >::IN_TYPE_CG = Eigen::Matrix<CG_SCALAR, IN_DIM, 1>

function input vector type

template<int IN_DIM, int OUT_DIM>
using ct::core::DerivativesCppadJIT< IN_DIM, OUT_DIM >::OUT_TYPE_CG = Eigen::Matrix<CG_SCALAR, OUT_DIM, 1>

function output vector type

template<int IN_DIM, int OUT_DIM>
using ct::core::DerivativesCppadJIT< IN_DIM, OUT_DIM >::IN_TYPE_D = Eigen::Matrix<double, IN_DIM, 1>

function input vector type double

template<int IN_DIM, int OUT_DIM>
using ct::core::DerivativesCppadJIT< IN_DIM, OUT_DIM >::OUT_TYPE_D = Eigen::Matrix<double, OUT_DIM, 1>

function output vector type

template<int IN_DIM, int OUT_DIM>
using ct::core::DerivativesCppadJIT< IN_DIM, OUT_DIM >::JAC_TYPE_D = Eigen::Matrix<double, OUT_DIM, IN_DIM>

Jacobian type.

template<int IN_DIM, int OUT_DIM>
using ct::core::DerivativesCppadJIT< IN_DIM, OUT_DIM >::JAC_TYPE_ROW_MAJOR = Eigen::Matrix<double, OUT_DIM, IN_DIM, Eigen::RowMajor>

Jac. type in row-major.

template<int IN_DIM, int OUT_DIM>
using ct::core::DerivativesCppadJIT< IN_DIM, OUT_DIM >::HES_TYPE_D = Eigen::Matrix<double, IN_DIM, IN_DIM>
template<int IN_DIM, int OUT_DIM>
using ct::core::DerivativesCppadJIT< IN_DIM, OUT_DIM >::HES_TYPE_ROW_MAJOR = Eigen::Matrix<double, IN_DIM, IN_DIM, Eigen::RowMajor>
template<int IN_DIM, int OUT_DIM>
using ct::core::DerivativesCppadJIT< IN_DIM, OUT_DIM >::FUN_TYPE_CG = std::function<OUT_TYPE_CG(const IN_TYPE_CG&)>

function type

template<int IN_DIM, int OUT_DIM>
using ct::core::DerivativesCppadJIT< IN_DIM, OUT_DIM >::DerivativesBase = Derivatives<IN_DIM, OUT_DIM>

Constructor & Destructor Documentation

template<int IN_DIM, int OUT_DIM>
ct::core::DerivativesCppadJIT< IN_DIM, OUT_DIM >::DerivativesCppadJIT ( FUN_TYPE_CG f,
int  inputDim = IN_DIM,
int  outputDim = OUT_DIM 
)
inline

Contructs the derivatives for codegeneration using a FUN_TYPE_CG function.

Warning
If IN_DIM and/our OUT_DIM are set to dynamic (-1), then the actual dimensions of x and y have to be passed here.
Parameters
fThe function to be autodiffed
[in]inputDiminputDim input dimension, must be specified if template parameter IN_DIM is -1 (dynamic)
[in]outputDimoutputDim output dimension, must be specified if template parameter IN_DIM is -1 (dynamic)
template<int IN_DIM, int OUT_DIM>
ct::core::DerivativesCppadJIT< IN_DIM, OUT_DIM >::DerivativesCppadJIT ( const DerivativesCppadJIT< IN_DIM, OUT_DIM > &  arg)
inline

copy constructor

Parameters
arginstance to copy
Note
It is important to not only copy the pointer to the dynamic library, but to load the library properly instead.
template<int IN_DIM, int OUT_DIM>
virtual ct::core::DerivativesCppadJIT< IN_DIM, OUT_DIM >::~DerivativesCppadJIT ( )
virtualdefault

Member Function Documentation

template<int IN_DIM, int OUT_DIM>
void ct::core::DerivativesCppadJIT< IN_DIM, OUT_DIM >::update ( FUN_TYPE_CG f,
const size_t  inputDim = IN_DIM,
const size_t  outputDim = OUT_DIM 
)
inline

update the Jacobian with a new function

Warning
If IN_DIM and/our OUT_DIM are set to dynamic (-1), then the actual dimensions of x and y have to be passed here.
Parameters
fnew function to compute Jacobian of
inputDiminput dimension, must be specified if template parameter IN_DIM is -1 (dynamic)
outputDimoutput dimension, must be specified if template parameter IN_DIM is -1 (dynamic)

Referenced by ct::core::DerivativesCppadJIT< STATE_DIM+CONTROL_DIM+1, 1 >::DerivativesCppadJIT().

template<int IN_DIM, int OUT_DIM>
DerivativesCppadJIT* ct::core::DerivativesCppadJIT< IN_DIM, OUT_DIM >::clone ( ) const
inlinevirtual

deep cloning of Jacobian

Implements ct::core::Derivatives< IN_DIM, OUT_DIM, double >.

template<int IN_DIM, int OUT_DIM>
virtual OUT_TYPE_D ct::core::DerivativesCppadJIT< IN_DIM, OUT_DIM >::forwardZero ( const Eigen::VectorXd &  x)
inlinevirtual

Evaluates the method itself.

Parameters
[in]xThe point of evaluation
Returns
The evalution of the method

Reimplemented from ct::core::Derivatives< IN_DIM, OUT_DIM, double >.

template<int IN_DIM, int OUT_DIM>
virtual JAC_TYPE_D ct::core::DerivativesCppadJIT< IN_DIM, OUT_DIM >::jacobian ( const Eigen::VectorXd &  x)
inlinevirtual

Evaluates the jacobian with respect to the input.

Parameters
[in]xThe point of evaluation
Returns
The evaluated jacobian

Reimplemented from ct::core::Derivatives< IN_DIM, OUT_DIM, double >.

template<int IN_DIM, int OUT_DIM>
virtual void ct::core::DerivativesCppadJIT< IN_DIM, OUT_DIM >::sparseJacobian ( const Eigen::VectorXd &  x,
Eigen::VectorXd &  jac,
Eigen::VectorXi &  iRow,
Eigen::VectorXi &  jCol 
)
inlinevirtual

Returns the evaluated jacobian in sparse format.

Parameters
[in]xThe point of evaluation
[out]jacThe non zero values of the jacobian
[out]iRowThe row indices of the non zero values
[out]jColThe column indices of the non zero values

Reimplemented from ct::core::Derivatives< IN_DIM, OUT_DIM, double >.

template<int IN_DIM, int OUT_DIM>
virtual Eigen::VectorXd ct::core::DerivativesCppadJIT< IN_DIM, OUT_DIM >::sparseJacobianValues ( const Eigen::VectorXd &  x)
inlinevirtual

Returns the non zero values of the jacobian.

Parameters
[in]xThe point of evaluation
Returns
The non zeros values of the jacobian

Reimplemented from ct::core::Derivatives< IN_DIM, OUT_DIM, double >.

template<int IN_DIM, int OUT_DIM>
virtual HES_TYPE_D ct::core::DerivativesCppadJIT< IN_DIM, OUT_DIM >::hessian ( const Eigen::VectorXd &  x,
const Eigen::VectorXd &  lambda 
)
inlinevirtual

Evaluates the hessian (2nd order derivatives with respect to input) of the method. In case of a vector valued function, the method returns the weighted sum of the hessians with weights w.

Parameters
[in]xThe point of evaluation
[in]lambdaThe weights of the sum
Returns
The evaluated hessian

Reimplemented from ct::core::Derivatives< IN_DIM, OUT_DIM, double >.

Referenced by ct::core::DerivativesCppadJIT< STATE_DIM+CONTROL_DIM+1, 1 >::hessian().

template<int IN_DIM, int OUT_DIM>
virtual void ct::core::DerivativesCppadJIT< IN_DIM, OUT_DIM >::sparseHessian ( const Eigen::VectorXd &  x,
const Eigen::VectorXd &  lambda,
Eigen::VectorXd &  hes,
Eigen::VectorXi &  iRow,
Eigen::VectorXi &  jCol 
)
inlinevirtual

Returns the weighted sum of hessian of the problem in sparse format.

Parameters
[in]xThe point of evaluation
[in]lambdaThe weights of the sum of the hessian
hesThe non zero values of the hessian
iRowThe row indices of the non zero values
jColThe column indices of the non zero values

Reimplemented from ct::core::Derivatives< IN_DIM, OUT_DIM, double >.

template<int IN_DIM, int OUT_DIM>
virtual Eigen::VectorXd ct::core::DerivativesCppadJIT< IN_DIM, OUT_DIM >::sparseHessianValues ( const Eigen::VectorXd &  x,
const Eigen::VectorXd &  lambda 
)
inlinevirtual

Returns the non zero elements of the hessian of the problem.

Parameters
[in]xThe point of evaluation
[in]lambdaThe weights of the sum of the hessian
Returns
{ description_of_the_return_value }

Reimplemented from ct::core::Derivatives< IN_DIM, OUT_DIM, double >.

template<int IN_DIM, int OUT_DIM>
Eigen::Matrix<bool, Eigen::Dynamic, Eigen::Dynamic> ct::core::DerivativesCppadJIT< IN_DIM, OUT_DIM >::getSparsityPatternJacobian ( )
inline

get Jacobian sparsity pattern

Auto-Diff automatically detects the sparsity pattern of the Jacobian. This method returns the pattern.

Returns
Sparsity pattern of the Jacobian
template<int IN_DIM, int OUT_DIM>
Eigen::Matrix<bool, Eigen::Dynamic, Eigen::Dynamic> ct::core::DerivativesCppadJIT< IN_DIM, OUT_DIM >::getSparsityPatternHessian ( )
inline

get Hessian sparsity pattern

Returns
Auto-diff automatically detects the sparsity pattern of the Hessian
template<int IN_DIM, int OUT_DIM>
void ct::core::DerivativesCppadJIT< IN_DIM, OUT_DIM >::getSparsityPatternJacobian ( Eigen::VectorXi &  rows,
Eigen::VectorXi &  columns 
)
inline

get Jacobian sparsity pattern

Auto-Diff automatically detects the sparsity pattern of the Jacobian. This method returns the pattern in row-column format. Row and columns contain the indeces of all non-zero entries.

Parameters
rowsrow indeces of non-zero entries
columnscolumn indeces of non-zero entries
template<int IN_DIM, int OUT_DIM>
size_t ct::core::DerivativesCppadJIT< IN_DIM, OUT_DIM >::getNumNonZerosJacobian ( )
inline

Returns the number of nonzeros in the sparse jacobian.

Returns
The number of nonzeros in the sparse jacobian
template<int IN_DIM, int OUT_DIM>
size_t ct::core::DerivativesCppadJIT< IN_DIM, OUT_DIM >::getNumNonZerosHessian ( )
inline

Returns the number of nonzeros in the sparse hessian.

Returns
The number of non zeros in the sparse hessian
template<int IN_DIM, int OUT_DIM>
void ct::core::DerivativesCppadJIT< IN_DIM, OUT_DIM >::getSparsityPatternHessian ( Eigen::VectorXi &  rows,
Eigen::VectorXi &  columns 
)
inline

get Hessian sparsity pattern

Auto-Diff automatically detects the sparsity pattern of the Jacobian. This method returns the pattern in row-column format. Row and columns contain the indeces of all non-zero entries.

Parameters
rowsrow indeces of non-zero entries
columnscolumn indeces of non-zero entries
template<int IN_DIM, int OUT_DIM>
void ct::core::DerivativesCppadJIT< IN_DIM, OUT_DIM >::compileJIT ( const DerivativesCppadSettings settings,
const std::string &  libName = "unnamedLib",
bool  verbose = false 
)
inline

Uses just-in-time compilation to compile the Jacobian and other derivatives.

This method generates source code for the Jacobian and zero order derivative. It then compiles the source code to a dynamically loadable library that then gets loaded.

template<int IN_DIM, int OUT_DIM>
const std::shared_ptr<CppAD::cg::DynamicLib<double> > ct::core::DerivativesCppadJIT< IN_DIM, OUT_DIM >::getDynamicLib ( )
inline

retrieve the dynamic library, e.g. for testing purposes

template<int IN_DIM, int OUT_DIM>
void ct::core::DerivativesCppadJIT< IN_DIM, OUT_DIM >::recordCg ( )
inlineprotected

record the Auto-Diff terms for code generation

Referenced by ct::core::DerivativesCppadJIT< STATE_DIM+CONTROL_DIM+1, 1 >::update().

Member Data Documentation

template<int IN_DIM, int OUT_DIM>
CppAD::cg::GccCompiler<double> ct::core::DerivativesCppadJIT< IN_DIM, OUT_DIM >::compiler_
protected
template<int IN_DIM, int OUT_DIM>
CppAD::cg::ClangCompiler<double> ct::core::DerivativesCppadJIT< IN_DIM, OUT_DIM >::compilerClang_
protected

compile for codegeneration


The documentation for this class was generated from the following file: