A class for forward-mode automatic differentiation with vector values and sparse jacobian matrices. More...
#include <AutoDiffBlock.hpp>
Public Types | |
typedef Eigen::Array< Scalar, Eigen::Dynamic, 1 > | V |
Underlying type for values. | |
typedef AutoDiffMatrix | M |
Underlying type for jacobians. | |
Public Member Functions | |
AutoDiffBlock & | operator+= (const AutoDiffBlock &rhs) |
Elementwise operator +=. | |
AutoDiffBlock & | operator-= (const AutoDiffBlock &rhs) |
Elementwise operator -=. | |
AutoDiffBlock | operator+ (const AutoDiffBlock &rhs) const |
Elementwise operator +. | |
AutoDiffBlock | operator- (const AutoDiffBlock &rhs) const |
Elementwise operator -. | |
AutoDiffBlock | operator* (const AutoDiffBlock &rhs) const |
Elementwise operator *. | |
AutoDiffBlock | operator/ (const AutoDiffBlock &rhs) const |
Elementwise operator /. | |
template<class Ostream > | |
Ostream & | print (Ostream &os) const |
I/O. | |
void | swap (AutoDiffBlock &other) |
Efficient swap function. | |
int | size () const |
Number of elements. | |
int | numBlocks () const |
Number of Jacobian blocks. | |
std::vector< int > | blockPattern () const |
Sizes (number of columns) of Jacobian blocks. | |
const V & | value () const |
Function value. | |
const std::vector< M > & | derivative () const |
Function derivatives. | |
Static Public Member Functions | |
static AutoDiffBlock | null () |
Construct an empty AutoDiffBlock. | |
static AutoDiffBlock | constant (V &&val) |
Create an AutoDiffBlock representing a constant. More... | |
static AutoDiffBlock | constant (const V &val) |
Create an AutoDiffBlock representing a constant. More... | |
static AutoDiffBlock | constant (const V &val, const std::vector< int > &blocksizes) |
Create an AutoDiffBlock representing a constant. More... | |
static AutoDiffBlock | variable (const int index, V &&val, const std::vector< int > &blocksizes) |
Create an AutoDiffBlock representing a single variable block. More... | |
static AutoDiffBlock | variable (const int index, const V &val, const std::vector< int > &blocksizes) |
Create an AutoDiffBlock representing a single variable block. More... | |
static AutoDiffBlock | function (V &&val, std::vector< M > &&jac) |
Create an AutoDiffBlock by directly specifying values and jacobians. More... | |
static AutoDiffBlock | function (const V &val, const std::vector< M > &jac) |
Create an AutoDiffBlock by directly specifying values and jacobians. More... | |
static std::vector< AutoDiffBlock > | variables (const std::vector< V > &initial_values) |
Construct a set of primary variables, each initialized to a given vector. More... | |
A class for forward-mode automatic differentiation with vector values and sparse jacobian matrices.
The class contains a (column) vector of values and multiple sparse matrices representing its partial derivatives. Each such matrix has a number of rows equal to the number of rows in the value vector, and a number of columns equal to the number of discrete variables we want to compute the derivatives with respect to. The reason to have multiple such jacobians instead of just one is to allow simpler grouping of variables, making it easier to implement various preconditioning schemes. Only basic arithmetic operators are implemented for this class, reflecting our needs so far.
The class is built on the Eigen library, using an Eigen array type to contain the values and Eigen sparse matrices for the jacobians. The overloaded operators are intended to behave in a similar way to Eigen arrays, meaning for example that the * operator is elementwise multiplication. The only exception is multiplication with a sparse matrix from the left, which is treated as an Eigen matrix operation.
There are no public constructors, instead we use the Named Constructor pattern. In general, one needs to know which variables one wants to compute the derivatives with respect to before constructing an AutoDiffBlock. Some of the constructors require you to pass a block pattern. This should be a vector containing the number of columns you want for each jacobian matrix.
For example: you want the derivatives with respect to three different variables p, r and s. Assuming that there are 10 elements in p, and 20 in each of r and s, the block pattern is { 10, 20, 20 }. When creating the variables p, r and s in your program you have two options:
After this, the r variable for example will have a size() of 20 and three jacobian matrices. The first is a 20 by 10 zero matrix, the second is a 20 by 20 identity matrix, and the third is a 20 by 20 zero matrix.
|
inlinestatic |
Create an AutoDiffBlock representing a constant.
[in] | val | values |
|
inlinestatic |
Create an AutoDiffBlock representing a constant.
[in] | val | values |
|
inlinestatic |
Create an AutoDiffBlock representing a constant.
This variant requires specifying the block sizes used for the Jacobians even though the Jacobian matrices themselves will be zero.
[in] | val | values |
[in] | blocksizes | block pattern |
|
inlinestatic |
Create an AutoDiffBlock by directly specifying values and jacobians.
This version of function() moves its arguments and is therefore quite efficient, but leaves the argument variables empty (but valid).
[in] | val | values |
[in] | jac | vector of jacobians |
|
inlinestatic |
Create an AutoDiffBlock by directly specifying values and jacobians.
This version of function() copies its arguments and is therefore less efficient than the other (moving) overload.
[in] | val | values |
[in] | jac | vector of jacobians |
|
inlinestatic |
Create an AutoDiffBlock representing a single variable block.
[in] | index | index of the variable you are constructing |
[in] | val | values |
[in] | blocksizes | block pattern The resulting object will have size() equal to block_pattern[index]. Its jacobians will all be zero, except for derivative()[index], which will be an identity matrix. |
|
inlinestatic |
Create an AutoDiffBlock representing a single variable block.
[in] | index | index of the variable you are constructing |
[in] | val | values |
[in] | blocksizes | block pattern The resulting object will have size() equal to block_pattern[index]. Its jacobians will all be zero, except for derivative()[index], which will be an identity matrix. |
|
inlinestatic |
Construct a set of primary variables, each initialized to a given vector.