1/* --------------------------------------------------------------------------
2CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-20 Bradley M. Bell
3
4  CppAD is distributed under the terms of the
5               Eclipse Public License Version 2.0.
6
7  This Source Code may also be made available under the following
8  Secondary License when the conditions for such availability set forth
9  in the Eclipse Public License, Version 2.0 are satisfied:
10        GNU General Public License, Version 2.0 or later.
11-------------------------------------------------------------------------- */
12$begin json_ad_graph$$
13$spell
14    Json
15    vec
16    enum_op
17    arg
18    ind
19    acyclic
20    str
21    np nx nc
22$$
23
24$section Json Representation of an AD Graph$$
25
26$head See Also$$
27$tref cpp_ad_graph$$.
28
29$head Node Indices$$
30The nodes in an AD graph have the following order:
31$pre
32    p_0 , ... ,  p_{np-1} ,
33    x_0 , ... ,  x_{nx-1} ,
34    c_0 , ... ,  c_{nc-1} ,
35    r_0 , ... ,  r_{no-1}
36$$
37
38$subhead p$$
39The sub-vector
40$pre p_0, ... , p_{np-1}$$ is the independent dynamic parameter vector;
41see $cref/n_dynamic_ind/json_ad_graph/dynamic_ind_vec/n_dynamic_ind/$$.
42The node index corresponding to $icode p_0$$ is $code 1$$.
43
44$subhead x$$
45The sub-vector
46$pre x_0, ... , x_{nx-1}$$ is the independent variable vector;
47see $cref/n_variable_ind/json_ad_graph/variable_ind_vec/n_variable_ind/$$.
48The node index corresponding to $icode x_0$$ is
49the index corresponding to $icode p_0$$ plus $icode np$$.
50
51$subhead c$$
52The sub-vector
53$pre c_0, ... , c_{nc-1}$$ is the constant parameter vector;
54see $cref/constant_vec/json_ad_graph/constant_vec/$$.
55The node index corresponding to $icode c_0$$ is
56the index corresponding to $icode x_0$$ plus $icode nx$$.
57
58$subhead r_i$$
59For $icode%i%=0,%...%,%no%-1%$$
60the sub-vector $pre r_i$$
61is the result vector for the $th i$$ operator usage;
62see $cref/op_usage_vec/json_ad_graph/op_usage_vec/$$.
63The value $icode no$$ is the number of operator usages; see
64$cref/n_usage/json_ad_graph/op_usage_vec/n_usage/$$ below.
65All of the arguments for an the $th i$$ operator are nodes
66that come before the first element of $icode r_i$$.
67The node index corresponding to the first element of $icode r_0$$ is
68the index corresponding to $icode c_0$$ plus $icode nc$$.
69For $icode%i% > 0%$$,
70The node index corresponding to the first element of $icode r_i$$ is
71the index corresponding to the first element of $icode r_{i-1}$$ plus
72the number of results for the $th i-1$$ operator.
73
74$head Format$$
75A complete description of the format for an AD graph is given below.
76For a general description of Json format see
77$href%
78    https://en.wikipedia.org/wiki/JSON#Data_types_and_syntax%
79    Json data types and syntax
80%$$.
81
82$head Token$$
83
84$subhead White Space$$
85Any sequence of white space, except within a string,
86terminates the current token and is otherwise ignored.
87
88$subhead Non-Negative Integer$$
89A non-negative integer is a non-empty sequence of the following
90characters: $code 0123456789$$.
91
92$subhead Floating Point Number$$
93A floating point number is a non-empty sequence of the following
94characters: $code 0123456789+-eE.$$.
95Note that there can't be any white space between a leading plus
96or minus sign and the rest of the number.
97
98$subhead String$$
99A string starts with the double quote character $code "$$
100and includes all the characters until the next double quote.
101The value of a string is the sequence of characters between the
102double quotes.
103Currently there is no support using the double quote
104as part of the value of a string.
105
106$subhead Single Character$$
107The following is a list of the single character tokens:
108$table
109Token      $cnext Usage                                           $rnext
110$code ,$$  $cnext separates entries in a list                     $rnext
111$code :$$  $cnext separates name from corresponding value         $rnext
112$code {$$  $cnext starts a list of name $code :$$ value pairs     $rnext
113$code }$$  $cnext ends a list of name $code:$$ value pairs        $rnext
114$code [$$  $cnext starts a list of values                         $rnext
115$code ]$$  $cnext ends a list of values
116$tend
117
118$head op_define_vec$$
119This vector has the following Json format:
120$codei%
121    [ %n_define%, [ %first_op_define%, %...%, %last_op_define% ] ]
122%$$
123where the non-negative integer $icode n_define$$
124is the number of operator definitions in this vector.
125
126$subhead op_define$$
127The possible operator definitions $icode op_define$$ are listed in
128section $cref json_graph_op$$.
129If an operator has a fixed number of arguments, one result, and
130only node indices for arguments, its definition has the form
131$codei%
132{
133    "op_code":      %op_code%,
134    "name":         %name%,
135    "n_arg":        %n_arg%
136}
137%$$
138Otherwise the operator definition has the form
139$codei%
140{
141    "op_code":      %op_code%,
142    "name":         %name%
143}
144%$$
145For example, the following is the $icode op_define$$ corresponding to the
146$cref/add/json_graph_op/Binary Operators/add/$$ operator:
147$codei%
148{
149    "op_code":      %op_code%,
150    "name":         "add",
151    "n_arg":        2
152}
153%$$
154
155$subhead op_code$$
156Note that every operator definition has a $icode op_code$$ value.
157These values must start at one and increment by one for each operator
158definition; i.e., the $icode op_code$$ for in $icode first_op_define$$ is
159$code 1$$, the value in the next definition is $code 2$$, and so on.
160The order of the definitions determines the $icode op_code$$ values
161used to specify operators for this computational graph.
162
163$subhead n_arg$$
164This is the number of argument values for the operator; i.e.,
165the dimension of its domain space.
166If it is present in an operator definition,
167it is the same value for every usage of the operator.
168Otherwise it is specified by the operator usage.
169
170$subhead n_result$$
171This is the number of results for the operator; i.e.,
172the dimension of its range space.
173If $icode n_arg$$ is present in an operator definition,
174$icode n_result$$ is one for every usage of the operator.
175Otherwise it is specified by the operator usage.
176
177$head op_usage$$
178Each operation usage
179has the following information:
180
181$subhead n_arg In Definition$$
182If $icode n_arg$$ is present in an operator definition,
183it is not in a corresponding $icode op_usage$$ which as the form
184$codei%
185    [ %op_code%, %first_arg%, %...%, %last_arg% ]
186%$$
187
188$subhead n_arg Not In Definition$$
189If $icode n_arg$$ is not in an operator definition,
190it is in a corresponding $icode op_usage$$.
191If there are no strings in a corresponding usage, it has the form
192$codei%
193    [ %op_code%, %n_result%, %n_arg%, [ %first_arg%, %...%, %last_arg% ] ]
194%$$
195
196$subhead Strings In Usage$$
197If $icode n_arg$$ is not in an operator definition,
198and there are strings in a corresponding usage,
199a corresponding usage has the form
200$codei%
201    [ %op_code%, %first_str%, %...%, %last_str%, %n_result%, %n_arg%,
202        [ %first_arg%, %...%, %last_arg% ]
203    ]
204%$$
205where $icode first_str$$ ..., $icode last_str$$,
206are a fixed number of strings that are part of the corresponding operator.
207
208$subhead first_arg, ..., last_arg$$
209The values $icode first_arg$$, ..., $icode last_arg$$,
210are the node indices corresponding to each of the arguments for this operator.
211They must be less than the node index corresponding to the first
212result for this operator; see
213$cref/r_i/json_ad_graph/Node Indices/r_i/$$ above.
214They specify which previous results (results before this operator in the graph)
215correspond to each of the arguments to this operator.
216As a consequence, there cannot be any cycles in the graph where
217the operators are represented by arcs from the argument to the result nodes;
218i.e., the graph is acyclic.
219
220$head dynamic_ind_vec$$
221This is the independent dynamic parameter vector
222(called $icode p$$ above); see
223$cref/dynamic/Independent/dynamic/$$.
224The function can depend on these parameters,
225but no derivatives are computed with respect to these parameters.
226
227$subhead n_dynamic_ind$$
228We use the non-negative integer $icode n_dynamic_ind$$
229for the number of elements in this vector
230(called $icode np$$ above).
231
232$head variable_ind_vec$$
233This is the independent variable vector
234(called $icode x$$ above); see
235$cref/x/Independent/x/$$.
236The function can depend on these variable and
237derivatives can be computed  with respect to these variables.
238
239$subhead n_variable_ind$$
240We use the non-negative integer $icode n_variable_ind$$
241for the number of element in this vector
242(called $icode nx$$ above).
243
244$head constant_vec$$
245This is the constant parameter vector (called $icode c$$ above).
246These parameters can be used to define the function and cannot change.
247The Json format for $icode constant_vec$$ is
248$codei%
249    %n_constant%, [ %first_constant%, %...%, %last_constant% ]
250%$$
251Each of the elements of this vector,
252e.g., $icode first_constant$$,
253is a $cref/floating point number/json_ad_graph/Token/Floating Point Number/$$
254specifying the value for the corresponding node.
255
256$subhead n_constant$$
257The non-negative integer $icode n_constant$$
258is the number of elements in this vector
259(called $icode nc$$ above).
260
261$head op_usage_vec$$
262The Jason format for an $icode op_usage_vec$$ is
263$codei%
264    [ %n_usage%, [ %first_op_usage%, %...%, %last_op_usage% ] ]
265%$$
266Each of the elements of this vector,
267e.g. $icode first_op_usage$$,
268is an $cref/op_usage/json_ad_graph/op_usage/$$.
269
270$subhead n_usage$$
271The non-negative integer $icode n_usage$$
272is the number of operator usages (called $icode no$$ above).
273
274$head dependent_vec$$
275This is the vector of dependent variable node indices.
276This identifies which nodes in the graph correspond to dependent variables.
277The Json format for $icode dependent_vec$$ is
278$codei%
279    [ %n_dependent%, [ %first_dependent%, %...%, %last_dependent% ] ]
280%$$
281Each of the elements of this vector,
282e.g. $icode first_dependent$$,
283is a $cref/non-negative integer/json_ad_graph/Token/Non-Negative Integer/$$
284corresponding to a node index in the graph.
285
286$subhead n_dependent$$
287The non-negative integer $icode n_dependent$$
288is the number of elements in this vector.
289
290$head AD Graph$$
291Each operator corresponds to a set of arcs from its argument nodes
292to its result nodes.
293The graph is acyclic; see
294$cref/
295    first_arg, ..., last_arg/
296    json_ad_graph/op_usage/
297    first_arg, ..., last_arg
298/$$.
299
300$subhead function_name$$
301A $icode function_name$$ is a
302$cref/string/json_ad_graph/Token/String/$$ that is used to identify
303the function.
304
305$subhead function$$
306The Json AD graph representation of a function is
307$codei%
308{
309    "function_name": %function_name%,
310    "op_define_vec": %op_define_vec%,
311    "n_dynamic_ind": %n_dynamic_ind%,
312    "n_variable_ind": %n_variable_ind%,
313    "constant_vec":  %constant_vec%,
314    "op_usage_vec":  %op_usage_vec%,
315    "dependent_vec": %dependent_vec%
316}
317%$$
318This represents a the $latex y = f(x, p)$$ where
319$latex p$$ is the dynamic parameter vector,
320$latex x$$ is the independent variable vector, and
321$latex y$$ is the dependent variable vector.
322
323$childtable%include/cppad/core/graph/json_graph_op.omh
324    %include/cppad/core/graph/from_json.hpp
325    %include/cppad/core/graph/to_json.hpp
326    %example/json/get_started.cpp
327    %example/json/sparse.cpp
328%$$
329
330$end
331