TMB Documentation  v1.9.11
Public Member Functions | Public Attributes | List of all members
TMBad::ADFun< ad > Struct Template Reference

Automatic differentiation function object. More...

#include <TMBad.hpp>

Public Member Functions

std::vector< bool > activeDomain ()
 Which inputs are affecting some outputs.
 
std::vector< bool > activeRange ()
 Which outputs are affected by some inputs.
 
template<class Functor , class ScalarVector >
 ADFun (Functor F, const ScalarVector &x_)
 Constructor of vector input / vector output function.
 
template<class Functor >
 ADFun (Functor F, Scalar x0_)
 Constructor of 1 scalar input / 1 scalar output function. More...
 
template<class Functor >
 ADFun (Functor F, Scalar x0_, Scalar x1_)
 Constructor of 2 scalar input / 1 scalar output function. More...
 
ADFun atomic ()
 Turn this operation sequence into an atomic operator.
 
ADFun compose (ADFun other)
 Construct function composition. More...
 
Decomp2< ADFundecompose (std::vector< Index > nodes)
 Decompose this computational graph. More...
 
Decomp2< ADFundecompose (const char *name)
 Decompose this computational graph by operator name. More...
 
void decompose_refs ()
 Optional optimization step before resolving references. More...
 
size_t DomainInner () const
 Number of inner parameters.
 
size_t DomainOuter () const
 Number of outer parameters.
 
std::vector< bool > DomainOuterMask ()
 Helper: Boolean mask of outer parameters.
 
std::vector< Scalar > DomainVec ()
 Get most recent input parameter vector from the tape.
 
template<class InplaceVector >
Position DomainVecSet (const InplaceVector &x)
 Set the input parameter vector on the tape.
 
void eliminate ()
 Dead operator elimination.
 
Position find_pos (Index inv)
 Helper to find the tape position of an independent variable.
 
void force_update ()
 Next forward pass must traverse the full graph.
 
template<class Vector >
Vector forward (const Vector &x)
 Forward sweep any vector class.
 
std::vector< bool > get_keep_var (std::vector< bool > keep_x, std::vector< bool > keep_y)
 Get necessary variables to keep for given input/output selection.
 
void inactivate (std::vector< Index > nodes)
 Substitute selected operators by void operators. More...
 
bool inner_outer_in_use ()
 Helper: Does tape have inner/outer information ?
 
bool inv_index_is_consecutive ()
 Are the independent variable indices consecutive? More...
 
ADFun JacFun (std::vector< bool > keep_x=std::vector< bool >(0), std::vector< bool > keep_y=std::vector< bool >(0))
 Get Jacobian function object. More...
 
std::vector< Scalar > Jacobian (const std::vector< Scalar > &x)
 Evaluate the Jacobian matrix. More...
 
std::vector< Scalar > Jacobian (const std::vector< Scalar > &x, std::vector< bool > keep_x, std::vector< bool > keep_y)
 Evaluate the Jacobian matrix subset More...
 
std::vector< Scalar > Jacobian (const std::vector< Scalar > &x, const std::vector< Scalar > &w)
 Evaluate the Jacobian matrix multiplied by a vector. More...
 
ADFun marginal_gk (const std::vector< Index > &random, gk_config cfg=gk_config())
 Integrate as many univariate variables as possible. More...
 
ADFun marginal_sr (const std::vector< Index > &random, std::vector< sr_grid > grid, const std::vector< Index > &random2grid, bool perm=true)
 Integrate using sequential reduction.
 
ADFun marginal_sr (const std::vector< Index > &random, sr_grid grid=sr_grid())
 Integrate using sequential reduction.
 
std::vector< Scalar > operator() (const std::vector< Scalar > &x)
 Evaluate function for scalar vector input.
 
std::vector< ad > operator() (const std::vector< ad > &x_) const
 Evaluate function for ad vector input. More...
 
ad operator() (ad x0)
 Evaluate function scalar version. More...
 
ad operator() (ad x0, ad x1)
 Evaluate function scalar version. More...
 
void optimize ()
 Tape optimizer. More...
 
std::vector< ADFunparallel_accumulate (size_t num_threads)
 Parallel split this operation sequence Split function f:R^n->R by its accumulation tree. Then parallelize and accumulate each parallel component. Return a list of functions f[i]:R^n->R such that f=sum_i f[i].
 
ADFun parallelize (size_t num_threads)
 Parallelize this operation sequence. More...
 
void print (print_config cfg=print_config())
 Print AD workspace.
 
IndirectAccessor< Scalar > RangeVec ()
 Get most recent result vector from the tape.
 
void reorder (std::vector< Index > last)
 Reorder computational graph to allow quick updates of selected inputs. More...
 
void replay ()
 Replay this operation sequence to a new sequence. More...
 
std::vector< ad_augresolve_refs ()
 Resolve references of this ADFun object. More...
 
template<class Vector >
Vector reverse (const Vector &w)
 Reverse sweep any vector class.
 
void set_inner_outer (ADFun &ans, const std::vector< bool > &outer_mask)
 Helper: Pass on inner/outer information to a new tape. Some parameters are marked as 'outer parameters'. All other prameters are 'inner'. This function passes on inner/outer information to a new tape assuming that the new tape preserves parameter ordering but may increase or reduce the parameter list.
 
void set_inv_positions ()
 Cache tape positions of independent variables. More...
 
void set_tail (const std::vector< Index > &random)
 Set start position needed to get selected independent variable derivatives.
 
Sparse< ADFunSpJacFun (std::vector< bool > keep_x=std::vector< bool >(0), std::vector< bool > keep_y=std::vector< bool >(0), SpJacFun_config config=SpJacFun_config())
 Sparse Jacobian function generator. More...
 
void SwapInner ()
 Temporarily regard this object as function of inner parameters. More...
 
void SwapOuter ()
 Temporarily regard this object as function of outer parameters. More...
 
void unset_tail ()
 Inactivate tail sweep to get derivatives wrt all independent variables.
 
ADFun WgtJacFun (std::vector< bool > keep_x=std::vector< bool >(0), std::vector< bool > keep_y=std::vector< bool >(0))
 Get weighted Jacobian function object. More...
 

Public Attributes

std::vector< Position > inv_pos
 Vector of positions by independent variables. More...
 
Position tail_start
 Mark the tail of the operation sequence A 'tail sweep' is on the subsequence tail_start:end. Only used by teh reverse sweep.
 

Detailed Description

template<class ad = ad_aug>
struct TMBad::ADFun< ad >

Automatic differentiation function object.

The ADFun object represents a mapping \(F:R^n \rightarrow R^m \: (x \rightarrow F(x))\)

API overview

Function object transformations

Name Description
JacFun() Get Jacobian function object
SpJacFun() Get Sparse Jacobian function object
WgtJacFun() Get weighted Jacobian function object

Function object evaluators

Name Description
operator()() Evaluate function object
Jacobian() Evaluate Jacobian of function object

Argument conventions

Definition at line 117 of file TMBad.hpp.

Constructor & Destructor Documentation

§ ADFun() [1/2]

template<class ad = ad_aug>
template<class Functor >
TMBad::ADFun< ad >::ADFun ( Functor  F,
Scalar  x0_ 
)
inline

Constructor of 1 scalar input / 1 scalar output function.

Warning
Experimental - may be removed

Definition at line 139 of file TMBad.hpp.

§ ADFun() [2/2]

template<class ad = ad_aug>
template<class Functor >
TMBad::ADFun< ad >::ADFun ( Functor  F,
Scalar  x0_,
Scalar  x1_ 
)
inline

Constructor of 2 scalar input / 1 scalar output function.

Warning
Experimental - may be removed

Definition at line 155 of file TMBad.hpp.

Member Function Documentation

§ compose()

template<class ad = ad_aug>
ADFun TMBad::ADFun< ad >::compose ( ADFun< ad >  other)
inline

Construct function composition.

Given this function (f) and another function (g) construct the composition f(g(x)).

Definition at line 942 of file TMBad.hpp.

§ decompose() [1/2]

template<class ad = ad_aug>
Decomp2<ADFun> TMBad::ADFun< ad >::decompose ( std::vector< Index >  nodes)
inline

Decompose this computational graph.

Note
This function preserves the inner/outer parameter categories if in use.

Definition at line 958 of file TMBad.hpp.

§ decompose() [2/2]

template<class ad = ad_aug>
Decomp2<ADFun> TMBad::ADFun< ad >::decompose ( const char *  name)
inline

Decompose this computational graph by operator name.

Note
This function preserves the inner/outer parameter categories if in use.

Definition at line 994 of file TMBad.hpp.

§ decompose_refs()

template<class ad = ad_aug>
void TMBad::ADFun< ad >::decompose_refs ( )
inline

Optional optimization step before resolving references.

Replay all RefOp-only dependent sub-expression to active glob. Expand this ADFun with boundary variables.

Definition at line 1002 of file TMBad.hpp.

§ inactivate()

template<class ad = ad_aug>
void TMBad::ADFun< ad >::inactivate ( std::vector< Index >  nodes)
inline

Substitute selected operators by void operators.

Parameters
nodesSelected operator nodes

This function was added as a way to get rid of protected operators.

Definition at line 1144 of file TMBad.hpp.

§ inv_index_is_consecutive()

template<class ad = ad_aug>
bool TMBad::ADFun< ad >::inv_index_is_consecutive ( )
inline

Are the independent variable indices consecutive?

Cheep test to guarantee that independent variable indicies are consecutive.

Definition at line 330 of file TMBad.hpp.

§ JacFun()

template<class ad = ad_aug>
ADFun TMBad::ADFun< ad >::JacFun ( std::vector< bool >  keep_x = std::vector<bool>(0),
std::vector< bool >  keep_y = std::vector<bool>(0) 
)
inline

Get Jacobian function object.

Denote by \(F:R^n \rightarrow R^m\) this function object.

Let

  • keep_x denote a boolean subset of inputs (by default all) with n_:=keep_x.count()
  • keep_y denote a boolean subset of outputs (by default all) with m_:=keep_y.count()
Returns
New function object \(F':R^n \rightarrow R^{m\_*n\_}\) representing the Jacobian (subset).
Parameters
keep_xIf specified, a boolean subset of inputs (Jacobian columns).
keep_yIf specified, a boolean subset of outputs (Jacobian rows).
Note
This function preserves the inner/outer parameter categories if in use.

Definition at line 680 of file TMBad.hpp.

Referenced by autodiff::gradient(), autodiff::hessian(), and autodiff::jacobian().

§ Jacobian() [1/3]

template<class ad = ad_aug>
std::vector<Scalar> TMBad::ADFun< ad >::Jacobian ( const std::vector< Scalar > &  x)
inline

Evaluate the Jacobian matrix.

Denote by f:R^n->R^m this function object. The Jacobian matrix is the m-by-n derivative matrix stored row-major.

Definition at line 484 of file TMBad.hpp.

§ Jacobian() [2/3]

template<class ad = ad_aug>
std::vector<Scalar> TMBad::ADFun< ad >::Jacobian ( const std::vector< Scalar > &  x,
std::vector< bool >  keep_x,
std::vector< bool >  keep_y 
)
inline

Evaluate the Jacobian matrix subset

Denote by f:R^n->R^m this function object. The Jacobian matrix is the m-by-n derivative matrix stored row-major. This function evaluates J[keep_y, keep_x] (keep_x / keep_y cooresponds to input / output respectively)

Definition at line 503 of file TMBad.hpp.

§ Jacobian() [3/3]

template<class ad = ad_aug>
std::vector<Scalar> TMBad::ADFun< ad >::Jacobian ( const std::vector< Scalar > &  x,
const std::vector< Scalar > &  w 
)
inline

Evaluate the Jacobian matrix multiplied by a vector.

Denote by f:R^n->R^m this function object. The Jacobian matrix is the m-by-n derivative matrix stored row-major. This function calculates the derivative d/dx(sum(f(x)*w))

Definition at line 542 of file TMBad.hpp.

§ marginal_gk()

template<class ad = ad_aug>
ADFun TMBad::ADFun< ad >::marginal_gk ( const std::vector< Index > &  random,
gk_config  cfg = gk_config() 
)
inline

Integrate as many univariate variables as possible.

Note
Use activeDomain() to identify which variables have been integrated (integrated variables are no longer active).

Definition at line 907 of file TMBad.hpp.

§ operator()() [1/3]

template<class ad = ad_aug>
std::vector<ad> TMBad::ADFun< ad >::operator() ( const std::vector< ad > &  x_) const
inline

Evaluate function for ad vector input.

Runs a forward replay to current active tape get_glob().

Warning
There must be an active tape and the ad inputs must correspond to the active tape (FIXME: This comment is outdated I think).

Definition at line 437 of file TMBad.hpp.

§ operator()() [2/3]

template<class ad = ad_aug>
ad TMBad::ADFun< ad >::operator() ( ad  x0)
inline

Evaluate function scalar version.

Warning
Experimental - may be removed

Definition at line 463 of file TMBad.hpp.

§ operator()() [3/3]

template<class ad = ad_aug>
ad TMBad::ADFun< ad >::operator() ( ad  x0,
ad  x1 
)
inline

Evaluate function scalar version.

Warning
Experimental - may be removed

Definition at line 472 of file TMBad.hpp.

§ optimize()

template<class ad = ad_aug>
void TMBad::ADFun< ad >::optimize ( )
inline

Tape optimizer.

Does the following two steps

  1. Identical sub-expressions a remapped to their first occurance
  2. Variables that do not affect the end result are removed
Note
Operators with allow_remap=false can have lots of implicit dependencies which would slow down the optimizer. We skip optimization if any such operators are present.
If cached independent variable positions have been set, it is illegal to run the tape optimizer because these variables could be remapped.

Definition at line 195 of file TMBad.hpp.

Referenced by newton::NewtonOperator< Functor, Hessian_Type >::NewtonOperator().

§ parallelize()

template<class ad = ad_aug>
ADFun TMBad::ADFun< ad >::parallelize ( size_t  num_threads)
inline

Parallelize this operation sequence.

Warning
Reverse replay is not supported after parallelization.

Definition at line 732 of file TMBad.hpp.

§ reorder()

template<class ad = ad_aug>
void TMBad::ADFun< ad >::reorder ( std::vector< Index >  last)
inline

Reorder computational graph to allow quick updates of selected inputs.

Permute the computational graph such that the selected independent variables last come as late as possible. Then cache the independent variable positions to allow fast updates of the last subset.

Parameters
lastSorted index vector of selected independent variables satisfying 0 <= last[i] < Domain().

Definition at line 237 of file TMBad.hpp.

§ replay()

template<class ad = ad_aug>
void TMBad::ADFun< ad >::replay ( )
inline

Replay this operation sequence to a new sequence.

Under rare circumstances this may reduce the tape size, e.g. by removing constant operations.

Warning
This is an experimental feature that may be removed.

Definition at line 750 of file TMBad.hpp.

Referenced by TMBad::ADFun<>::decompose_refs().

§ resolve_refs()

template<class ad = ad_aug>
std::vector<ad_aug> TMBad::ADFun< ad >::resolve_refs ( )
inline

Resolve references of this ADFun object.

Assume that an active context (glob) exists.

  1. Locate all 'RefOp' and play them on top of the active glob while storing the corresponding generated variables in a vector.
  2. Substitute all 'RefOp' by 'InvOp' and store the 'inv_index' of the newly generated independent variables.
    Returns
    Vector with variables relative to the current active context (glob).

Definition at line 1055 of file TMBad.hpp.

§ set_inv_positions()

template<class ad = ad_aug>
void TMBad::ADFun< ad >::set_inv_positions ( )
inline

Cache tape positions of independent variables.

An operation sequence may be ordered such that independent variables that are updated frequently come as late as possible. By caching the independent variable positions, the forward pass can jump directly to the first position that must be updated.

Definition at line 222 of file TMBad.hpp.

§ SpJacFun()

template<class ad = ad_aug>
Sparse<ADFun> TMBad::ADFun< ad >::SpJacFun ( std::vector< bool >  keep_x = std::vector<bool>(0),
std::vector< bool >  keep_y = std::vector<bool>(0),
SpJacFun_config  config = SpJacFun_config() 
)
inline

Sparse Jacobian function generator.

Denote by f:R^n->R^m this function object.

By default the return value is a new function object f':R^n->R^l representing the sparse Jacobian. Here l denotes the number of non zeros. The function object is itself an ADFun object, but in addition the sparsity pattern is contained in the output.

If the Jacobian is only needed on a subset of the sparsity pattern, one can use the boolean vectors keep_x and keep_y to select a subset of interest. Jacobian elements outside this subset are considered being identical zero, in order to reduce the caclulations. Note that indices are not remapped. Also note that keep_x / keep_y cooresponds to input / output respectively.

Parameters
keep_xJacobian columns to consider
keep_yJacobian rows to consider
compressApply row-wise compression if it reduces memory ?
Returns
Sparse<ADFun> containing function and sparsity pattern.
Note
This function preserves the inner/outer parameter categories if in use.

Definition at line 776 of file TMBad.hpp.

§ SwapInner()

template<class ad = ad_aug>
void TMBad::ADFun< ad >::SwapInner ( )
inline

Temporarily regard this object as function of inner parameters.

Warning
Don't forget to swap back when done!

Definition at line 1088 of file TMBad.hpp.

§ SwapOuter()

template<class ad = ad_aug>
void TMBad::ADFun< ad >::SwapOuter ( )
inline

Temporarily regard this object as function of outer parameters.

Warning
Don't forget to swap back when done!

Definition at line 1095 of file TMBad.hpp.

§ WgtJacFun()

template<class ad = ad_aug>
ADFun TMBad::ADFun< ad >::WgtJacFun ( std::vector< bool >  keep_x = std::vector<bool>(0),
std::vector< bool >  keep_y = std::vector<bool>(0) 
)
inline

Get weighted Jacobian function object.

Denote by \(F:R^n \rightarrow R^m\) this function object.

Let

  • keep_x denote a boolean subset of inputs (by default all) with n_:=keep_x.count()
  • keep_y denote a boolean subset of outputs (by default all) with m_:=keep_y.count()
Returns
New function object \(F':R^{n+m} \rightarrow R^{n\_} ((x,w)\rightarrow w^T*F') \) representing the Jacobian (subset) multiplied by a weight vector.
Note
This function preserves the inner/outer parameter categories if in use.

Definition at line 702 of file TMBad.hpp.

Member Data Documentation

§ inv_pos

template<class ad = ad_aug>
std::vector<Position> TMBad::ADFun< ad >::inv_pos

Vector of positions by independent variables.

If in use, i.e. inv_pos.size()>0, it means that tail sweeping is enabled. The vector allows to lookup tape postions for any given independent variable. In particular it holds that inv_index[i] == start[i].ptr.second.

Note
inv_index need not be sorted !

Definition at line 313 of file TMBad.hpp.


The documentation for this struct was generated from the following file:
License: GPL v2