\(\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}} }\)
cpp_ad_graph¶
View page sourceC++ Representation of an AD Graph¶
See Also¶
function¶
This section defines a computational graph representation of a function \(y = f(x, p)\). The vector x is called the independent variable vector, p is called the independent dynamic parameter vector, and y is called the dependent variable vector.
Node Indices¶
The nodes in an AD graph have the following order:
p_0 , ... , p_{np-1} ,
x_0 , ... , x_{nx-1} ,
c_0 , ... , c_{nc-1} ,
r_0 , ... , r_{no-1}
p¶
The sub-vector
p_0, ... , p_{np-1}
is the independent dynamic parameter vector;
see n_dynamic_ind .
The node index corresponding to p_0 is 1
.
x¶
The sub-vector
x_1, ... , x_nx
is the independent variable vector; see n_variable_ind . The node index corresponding to x_0 is the index corresponding to p_0 plus np .
c¶
The sub-vector
c_1, ... , c_nc
is the constant parameter vector; see constant_vec . The node index corresponding to c_0 is the index corresponding to x_0 plus nx .
r¶
The sub-vector
r_i for i =0,…, no -1
is the result vector
for the i-th operator;
see operator_vec .
All of the node arguments for an the i-th operator are nodes
that come before the first element of r_i .
The node index corresponding to the first element of r_0 is
the index corresponding to c_0 plus nc .
For i > 0 ,
The node index corresponding to the first element of r_i is
the index corresponding to the first element of r_ { i-1 } plus
the number of results for the i-1-th operator.
function_name¶
is a std::string
containing the name for the function
corresponding to this graph.
discrete_name_vec¶
is a vector with elements of type std::string
.
A discrete function has one argument, one result, and it derivative
is always zero; e.g., the Heaviside function.
Calls by this function to discrete functions use the index in this
vector to identify the discrete functions; see
discrete_graph_op below.
If there are no calls to discrete functions, this vector can be empty.
atomic_name_vec¶
is a vector with elements of type std::string
.
An atomic function can have any number of arguments and results
and non-zero derivatives.
Calls by this function to other functions use the index in this
vector to identify the other functions; see
atom_graph_op below.
If there are no calls to other functions, this vector can be empty.
Discrete functions are faster, and simpler to create and use
than atomic functions.
print_text_vec¶
is a vector with elements of type std::string
.
The Print operators uses indices
in this vector for the corresponding
before and after values.
If there are no print operators, this vector can be empty.
n_dynamic_ind¶
is the number of independent dynamic parameters in the function (called np above); see dynamic .
n_variable_ind¶
is the number of independent variables in the function (called nx above); see x .
constant_vec¶
is a vector of with elements of type
double
and size nc that can be used to define this function.
operator_vec¶
is a vector with elements of type graph_op_enum
and size no (the number of operators in the graph).
For i = 0, …, no -1
operator_vec [ i ] contains the instructions
for computing the result vector r_i .
operator_arg¶
is a vector with size equal to the sum of the size of each
of its sub-vectors (which are described below).
For i = 0, …, no -1
, we use
first_node [ i ] to denote the index in operator_arg
of the first node argument to the i-th operator.
We use n_node_arg [ i ] to denote the number of node arguments
for the i-th operator.
For j = 0 , …, n_node_arg [ i ] -1
,
the j-th node argument for the i-th operator has node index
operator_arg [ first_node [ i ] + j ]
The operator_arg sub-vector for the i-th operator starts are
first_node [ i ] and has n_node_arg [ i ] elements
except for the following operators:
sum_graph_op
,
discrete_graph_op
,
atom_graph_op
,
print_graph_op
.
print_graph_op¶
In the case where operator_vec [ i ]. op_enum
is
print_graph_op
:
before [ i ] = operator_arg [ first_node [ i ]
- 2
]
is the index in print_text_vec of the text that is printed before the value and
after [ i ] = operator_arg [ first_node [ i ]
- 1
]
is the index in print_text_vec
of the text that is printed after the value.
The operator_arg sub-vector for the i-th operator
starts at index first_node [ i ] -2
and has 4 = n_node_arg [ i ]+2 elements.
The node with index
notpos [ i ] = operator_arg [ first_node [ i ] ]
is checked and if it is positive, nothing is printed by this operator. Otherwise, the value corresponding to the following node is printed:
value [ i ] = operator_arg [ first_node [ i ] + 1 ]
discrete_graph_op¶
In the case where operator_vec [ i ]. op_enum
is
discrete_graph_op
:
name_index [ i ] = operator_arg [ first_node [ i ]
- 1
]
is the index in discrete_name_vec
of the function being called by this operator.
For this operator,
the operator_arg sub-vector for the i-th operator
starts at index first_node [ i ] -1
and has 2 = n_node_arg [ i ]+1 elements.
atom_graph_op¶
In the case where operator_vec [ i ]. op_enum
is
atom_graph_op
:
name_index [ i ] = operator_arg [ first_node [ i ]
- 3
]
is the index in atomic_name_vec of the function being called by this operator.
n_result [ i ] = operator_arg [ first_node [ i ]
- 2
]
is the number of result nodes for this operator.
n_node_arg [ i ] = operator_arg [ first_node [ i ]
- 1
]
is the number of node arguments for this operator.
For this operator,
the operator_arg sub-vector for the i-th operator
starts at index first_node [ i ] -3
and has n_node_arg [ i ]+3 elements.
sum_graph_op¶
In the case where operator_vec [ i ]. op_enum
is
sum_graph_op
:
n_node_arg [ i ] = operator_arg [ first_node [ i ]
- 1
]
is the number of node arguments for this operator.
For this operator,
the operator_arg sub-vector for the i-th operator
starts at index first_node [ i ] -1
and has n_node_arg [ i ]+1 elements.
dependent_vec¶
is a vector with size equal to the number element in y . The i-th element of y corresponds to the node index dependent_vec [ i ] .
cpp_graph¶
The cpp_graph
class implements the data structure above.
It is defined by the documentation sections under Contents below:
Contents¶
Name |
Title |
---|---|
graph_op_enum |
|
cpp_graph |
|
from_graph |
|
to_graph |