1/* --------------------------------------------------------------------------
2CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-19 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_graph_op$$
13$spell
14    notpos
15    azmul
16    cexp_rel
17    eq
18    lt
19    le
20    Cond
21    Gt
22    log1p
23    expm1
24    erfc
25    erf
26    atanh
27    asinh
28    acosh
29    acos
30    asin
31    atan
32    cos
33    exp
34    sqrt
35    tanh
36    Json
37    arg
38    op
39    mul
40    div
41    chkpoint
42    CppAD
43$$
44
45$section Json AD Graph Operator Definitions$$
46
47$head Notation$$
48
49$subhead op_code$$
50Each operator definition has a $icode op_code$$ value that
51is used to identify it for a particular $icode json_ad_graph$$.
52
53$subhead Arguments$$
54The values $icode first_arg$$, ... ,
55$cref/last_arg/json_ad_graph/op_usage/first_arg, ..., last_arg/$$
56are the node indices for arguments to an operator.
57
58$head Unary Operators$$
59All these operations create one result node and
60have the following Json definition:
61$codei%
62{
63    "op_code":      %op_code%,
64    "name":         %name%,
65    "n_arg":        1
66}
67%$$
68where $icode name$$ is a $cref/string/json_ad_graph/Token/String/$$.
69A corresponding $icode op_usage$$ has the form
70$codei%
71    [ %op_code%, %arg% ]
72%$$
73The possible values for the string $icode name$$ are listed
74in the table below.
75The result is the node value as a function of the
76argument value.
77If c++11 is yes (no),
78then c++11 or higher is required to use the operator with CppAD.
79
80$table
81$icode name$$   $cnext result                           $cnext c++11 $rnext
82$code abs$$     $cnext absolute value                   $cnext no $rnext
83$code acos$$    $cnext inverse cosine                   $cnext no $rnext
84$code asin$$    $cnext inverse sine                     $cnext no $rnext
85$code atan$$    $cnext inverse tangent                  $cnext no $rnext
86$code cosh$$    $cnext hyperbolic cosine                $cnext no $rnext
87$code cos$$     $cnext cosine                           $cnext no $rnext
88$code exp$$     $cnext exponential                      $cnext no $rnext
89$code log$$     $cnext logarithm                        $cnext no $rnext
90$code sign$$    $cnext sign function                    $cnext no $rnext
91$code sinh$$    $cnext hyperbolic sine                  $cnext no $rnext
92$code sin$$     $cnext sine                             $cnext no $rnext
93$code sqrt$$    $cnext square root                      $cnext no $rnext
94$code tanh$$    $cnext hyperbolic tangent               $cnext no $rnext
95$code tan$$     $cnext tangent                          $cnext no $rnext
96$code asinh$$   $cnext inverse hyperbolic sine          $cnext yes $rnext
97$code atanh$$   $cnext inverse hyperbolic sine          $cnext yes $rnext
98$code erf$$     $cnext error functions                  $cnext yes $rnext
99$code erfc$$    $cnext complementary error function     $cnext yes $rnext
100$code expm1$$   $cnext minus one plus the exponential   $cnext yes $rnext
101$code log1p$$   $cnext log  plus one                    $cnext yes $rnext
102$code acosh$$   $cnext inverse hyperbolic cosine        $cnext yes
103$tend
104
105$subhead Example$$
106The file $cref json_unary_op.cpp$$ is an example and test
107for one of these operators.
108
109$head Binary Operators$$
110All these operations create one result node and
111have the following Json definition:
112$codei%
113{
114    "op_code":      %op_code%,
115    "name":         %name%,
116    "n_arg":        2
117}
118%$$
119where $icode name$$ is a $cref/string/json_ad_graph/Token/String/$$.
120A corresponding $icode op_usage$$ has the form
121$codei%
122    [ %op_code%, %first_arg%, %second_arg% ]
123%$$
124The possible values for the string $icode name$$ are listed below:
125
126$subhead add$$
127The result is
128the first argument value plus the second argument value; see
129the example and test $cref json_add_op.cpp$$.
130
131$subhead azmul$$
132If the first argument value is zero, the result is zero
133(even if the second argument value is nan).
134Otherwise the result is
135the first argument value times the second argument value; see
136the example and test $cref json_azmul_op.cpp$$.
137
138$subhead div$$
139The result is
140the first argument value divided by the second argument value; see
141the example and test $cref json_div_op.cpp$$.
142
143$subhead mul$$
144The result is
145the first argument value times the second argument value; see
146the example and test $cref json_mul_op.cpp$$.
147
148$subhead pow$$
149The result is
150the first argument value raised to the second argument value; see
151the example and test $cref json_pow_op.cpp$$.
152
153$subhead sub$$
154The result is
155the first argument value minus the second argument value; see
156the example and test $cref json_sub_op.cpp$$.
157
158$head sum$$
159This operator has the following Json definition:
160$codei%
161{
162    "op_code":      %op_code%,
163    "name":         "sum"
164}
165%$$
166A corresponding $icode op_usage$$ has the form
167$codei%
168    [ %op_code%, %n_result%, %n_arg%, [ %first_arg%, %...%, %last_arg% ] ]
169%$$
170where $icode n_result$$ is always $code 1$$.
171This operation creates one node with value equal to
172the sum of values corresponding to all of its argument nodes.
173
174$subhead Example$$
175The file $cref json_sum_op.cpp$$ is an example and test
176of this operation.
177
178$head Conditional Expressions$$
179These operators are $cref/conditional expressions/CondExp/$$
180and have the following Json definition:
181$codei%
182{
183    "op_code":      %op_code%,
184    "name":         "cexp_%rel%,
185    "n_arg":        4
186}
187%$$
188where $icode rel$$ is $code eq$$ (equal),
189$code le$$ (less than or equal), or
190$code lt$$ (less than).
191The first argument is $cref/left/CondExp/left/$$,
192the second is $cref/right/CondExp/right/$$,
193the third is $cref/if_true/CondExp/if_true/$$,
194the fourth is $cref/if_false/CondExp/if_false/$$,
195the result is given by
196$codei%
197    if( %left% %cop% %right%)
198        %result% = %if_true%;
199    else
200        %result% = %if_false%;
201%$$
202where the comparison $icode cop$$ is define by the cases below:
203
204$subhead cexp_eq$$
205For this operator $icode cop$$ is $code ==$$
206
207$subhead cexp_le$$
208For this operator $icode cop$$ is $code <=$$
209
210$subhead cexp_lt$$
211For this operator $icode cop$$ is $code <$$
212
213$subhead Other Comparisons$$
214Note that
215$codei%
216    CondExpGt(%left%, %right%, %if_true%, %if_false%)
217%$$
218is equivalent to
219$codei%
220    CondExpLe(%left%, %right%, %if_false%, %if_true%)
221%$$
222Similar conversions can be used for all the possible
223$cref/conditional expressions/CondExp/$$.
224
225$subhead Example$$
226The file $cref json_cexp_op.cpp$$ is an example and test
227for one of these operators.
228
229$head Compare Operators$$
230These are $cref/comparison/Compare/$$ operators
231and have the following Json definition:
232$codei%
233{
234    "op_code":      %op_code%,
235    "name":         "comp_%rel%"
236}
237%$$
238where $icode rel$$ is $code eq$$ (equal),
239$code ne$$ (not equal),
240$code le$$ (less than or equal), or
241$code lt$$ (less than).
242A corresponding $icode op_usage$$ has the form
243$codei%
244    [ %op_code%, %n_result%, %n_arg%, [ %left%, %right% ] ]
245%$$
246
247$subhead n_result$$
248This is always zero because a comparison operator does not create
249any new nodes.
250
251$subhead n_arg$$
252This is always two because a comparison operator has two argument nodes
253corresponding to the left and right operands.
254
255$subhead left, right$$
256The logical comparison is defined as the logical expression
257$codei%
258    %left% %cop% %right%
259%$$
260The comparison $icode cop$$ is define by the cases below.
261The Json graph corresponds to the comparison being true.
262If, for a value of the independent parameters and variables,
263the comparison is false,
264the Json graph may no longer be valid.
265For example, the Json graph may only contain the code for the true case below:
266$codei%
267    if( %left% %cop% %right% )
268    {   %source code when result is true% }
269    else
270    {   %source code when result is false% }
271%$$
272Including this operator enables CppAD to detect when the graph
273may no longer be a valid representation of the intended function.
274
275$subhead comp_eq$$
276For this operator $icode cop$$ is $code ==$$
277
278$subhead comp_le$$
279For this operator $icode cop$$ is $code <=$$
280
281$subhead comp_lt$$
282For this operator $icode cop$$ is $code <$$
283
284$subhead comp_ne$$
285For this operator $icode cop$$ is $code !=$$
286
287$subhead Other Comparisons$$
288The comparison result true for $icode%left% > %right%$$
289is equivalent to the comparison result true for $icode%right% < %left%$$.
290The comparison result false for $icode%left% > %right%$$
291is equivalent to the comparison result true for $icode%left% <= %right%$$.
292In a similar fashion, all the possible comparisons results
293can be converted to a true result for one of the comparisons above.
294
295$subhead Example$$
296The file $cref json_comp_op.cpp$$ is an example and test
297for one of these operators.
298
299$head Discrete Functions$$
300This operator has the following Json definition:
301$codei%
302{
303    "op_code":     %op_code%,
304    "name":        "discrete"
305}
306%$$
307A corresponding op_usage has the form
308$codei%
309    [ %op_code%, %name%, %n_result%, %n_arg%, [ %arg% ]  ]
310%$$
311
312$subhead name$$
313The value $icode name$$ is a
314$cref/string/json_ad_graph/Token/String/$$ specifying the
315$cref/name/discrete/name/$$ of the discrete function that is called.
316
317$subhead n_result$$
318This is always $code 1$$ because a discrete function
319creates one new node.
320The result node value is the specified discrete function of the argument value.
321
322$subhead n_arg$$
323This is always $code 1$$ because a discrete function has
324one argument node.
325
326$subhead arg$$
327is the node index for the argument to the discrete function.
328
329$subhead Example$$
330the example and test $cref json_discrete_op.cpp$$.
331
332
333$head Atomic Functions$$
334This operator has the following Json definition:
335$codei%
336{
337    "op_code":      %op_code%,
338    "name":          "atom"
339}
340%$$
341A corresponding $icode op_usage$$ has the form
342$codei%
343    [ %op_code%, %name%, %n_result%, %n_arg%,
344        [ %first_arg%, %...%, %last_arg% ]
345    ]
346%$$
347This operator creates $icode n_result$$ nodes with values equal to
348an evaluation of the $code atomic_three$$ function.
349
350$subhead name$$
351The value $icode name$$ is a
352$cref/string/json_ad_graph/Token/String/$$ specifying the
353$cref/name/atomic_three_ctor/atomic_three/name/$$
354of the $code atomic_three$$ function that is called.
355
356$subhead n_result$$
357is the number of results for this function; i.e.,
358its range space dimension.
359
360$subhead n_arg$$
361is the number of arguments to this function; i.e.,
362its domain space dimension.
363
364$subhead first_arg, ..., last_arg$$
365The values corresponding to the node indices
366$icode first_arg$$, ..., $icode last_arg$$ are the
367arguments (independent variables) for the atomic function evaluation.
368In the case where the atomic function is a $code chkpoint_two$$ function,
369the independent dynamic parameters are specified by calling its
370$cref/new_dynamic/chkpoint_two/Syntax/new_dynamic/$$ routine.
371
372$subhead Example$$
373the example and test $cref json_atom_op.cpp$$.
374
375$head Print$$
376This operator has the following Json definition:
377$codei%
378{
379    "op_code":      %op_code%,
380    "name":          "print"
381}
382%$$
383A corresponding $icode op_usage$$ has the form
384$codei%
385    [ %op_code%, %before%, %after%, %n_result%, %n_arg%, [ %notpos%, %value% ] ]
386%$$
387
388$subhead before$$
389is a $cref/string/json_ad_graph/Token/String/$$ that is printed
390$cref/before/PrintFor/before/$$ the value for this operator.
391
392$subhead after$$
393is a $cref/string/json_ad_graph/Token/String/$$ that is printed
394$cref/after/PrintFor/after/$$ the value for this operator.
395
396$subhead n_result$$
397This is always zero because a print operator does not create
398any new nodes.
399
400$subhead n_arg$$
401This is always two because a print operator has two argument nodes.
402
403$subhead notpos$$
404This is $cref/notpos/PrintFor/notpos/$$
405which determines if the value is printed.
406
407$subhead value$$
408This is the $cref/value/PrintFor/value/$$ that is printed.
409
410$subhead Example$$
411The file $cref json_print_op.cpp$$ is an example and test
412of this operator.
413
414$childtable%
415    example/json/unary_op.cpp%
416    example/json/add_op.cpp%
417    example/json/azmul_op.cpp%
418    example/json/discrete_op.cpp%
419    example/json/div_op.cpp%
420    example/json/mul_op.cpp%
421    example/json/pow_op.cpp%
422    example/json/print_op.cpp%
423    example/json/sub_op.cpp%
424    example/json/sum_op.cpp%
425    example/json/comp_op.cpp%
426    example/json/cexp_op.cpp%
427    example/json/atom_op.cpp
428%$$
429
430$end
431