Prev Next

@(@\newcommand{\W}[1]{ \; #1 \; } \newcommand{\R}[1]{ {\rm #1} } \newcommand{\B}[1]{ {\bf #1} } \newcommand{\D}[2]{ \frac{\partial #1}{\partial #2} } \newcommand{\DD}[3]{ \frac{\partial^2 #1}{\partial #2 \partial #3} } \newcommand{\Dpow}[2]{ \frac{\partial^{#1}}{\partial {#2}^{#1}} } \newcommand{\dpow}[2]{ \frac{ {\rm d}^{#1}}{{\rm d}\, {#2}^{#1}} }@)@This is cppad-20221105 documentation. Here is a link to its current documentation .
Generate Source Code and Compile an AD Function

Syntax
# include <cppad/example/code_gen_fun.hpp>

Constructors
code_gen_fun fun_name()
code_gen_fun fun_name(file_name)
code_gen_fun fun_name(file_namecg_fun)
code_gen_fun fun_name(file_namecg_funeval_jac)

swap
fun_name.swap(other_fun)

function
y = fun_name(x)

jacobian
J = fun_name.jacobian(x)

sparse_jacobian
Jrcv = fun_name.sparse_jacobian(x)

Prototype

Constructors


code_gen_fun::code_gen_fun(void)

code_gen_fun::code_gen_fun(const std::string&  file_name )
code_gen_fun::code_gen_fun(
    const std::string&                     file_name  ,
    CppAD::ADFun< CppAD::cg::CG<double> >& cg_fun     ,
    evaluation_enum                        eval_jac   )
Operations


void code_gen_fun::swap(code_gen_fun& other_fun)

CppAD::vector<double>
code_gen_fun::operator()(const CppAD::vector<double>& x)

CppAD::vector<double>
code_gen_fun::jacobian(const CppAD::vector<double>& x)

CppAD::sparse_rcv< CppAD::vector<size_t>, CppAD::vector<double> >
code_gen_fun::sparse_jacobian(const CppAD::vector<double>& x)

CppAD::cg::CG<double>
This is the CppAD Base type for the function cg_fun . It is defined by CppADCodeGen . and used to convert the cg_fun function object to source code, compile the source code, and then link the corresponding function evaluation
    
y = cg_fun.Forward(0, x)

Speed
The conversion to source and linking is expected to take a significant amount of time and the evaluation of the function is expected to be much faster; see the following speed tests:
cppadcg_det_minor.cpp cppadcg Speed: Gradient of Determinant by Minor Expansion
cppadcg_sparse_jacobian.cpp Cppadcg Speed: Sparse Jacobian

fun_name
This is the name of the code_gen_fun object.

other_fun
This is the name of another code_gen_fun object.

file_name
This is the absolute or relative path for the file that contains the dynamic library. It does not include the files extension at the end that is used for dynamic libraries on this system. If cg_fun is not present in the constructor, it must have been present in a previous constructor with the same file_name .

cg_fun
This is a CppAD function object that corresponds to a function @(@ f : \B{R}^n \rightarrow \B{R}^m @)@. If this arguments is present in the constructor, a new dynamic library is created.

eval_jac
If this argument is present in the constructor, it determines which type of Jacobian @(@ f'(x) @)@ will be enabled. The possible choices for eval_jac are:
eval_jac    Available Jacobian
code_gen_fun::none_enum    none
code_gen_fun::dense_enum    fun_name.jacobian
The default value for eval_jac is none.

swap
This exchanges the library in fun_name with the library in other_fun .

x
is a vector of size n specifying the argument value at which the function will be evaluated.

y
This return value has size m and is the value of @(@ f(x) @)@.

jacobian

J
This return value has size m * n and is the value of the Jacobian @(@ f'(x) @)@ where @[@ J[ i \cdot n + j ] = ( \partial f_i / \partial x_j ) (x) @]@

Speed
The speed test cppadcg_det_minor.cpp has the option to pass the determinant function, or the Jacobian of the determinant function, to CppADCodeGen (for the same eventual calculation); see PASS_JACOBIAN_TO_CODE_GEN . This test indicates that both methods have similar setup and derivative calculation times.

sparse_jacobian

Jrcv
This return value is a sparse_rcv sparse matrix representation of the Jacobian.

Speed
The speed test cppadcg_sparse_jacobian.cpp has the option to pass a function (sparse_jac_fun ) or it's Jacobian to CppADCodeGen (for the same eventual calculation); see PASS_SPARSE_JACOBIAN_TO_CODE_GEN . THis test indicates that both methods have similar setup and derivative calculation times.

Examples
code_gen_fun_function.cpp Evaluate a Code Gen Function: Example and Test
code_gen_fun_file.cpp File Store and Retrieve a Code Gen Function: Example and Test
code_gen_fun_jacobian.cpp Evaluate Jacobian of a Code Gen Function: Example and Test
code_gen_fun_jac_as_fun.cpp Pass Jacobian as Code Gen Function: Example and Test
code_gen_fun_sparse_jacobian.cpp Evaluate Sparse Jacobian of a Code Gen Function: Example and Test
code_gen_fun_sparse_jac_as_fun.cpp Pass Sparse Jacobian as Code Gen Function: Example and Test

Implementation
see code_gen_fun.hpp and code_gen_fun.cpp
Input File: cppad_lib/code_gen_fun.cpp