CppADCodeGen  2.4.3
A C++ Algorithmic Differentiation Package with Source Code Generation
operation.hpp
1 #ifndef CPPAD_CG_OPERATION_INCLUDED
2 #define CPPAD_CG_OPERATION_INCLUDED
3 /* --------------------------------------------------------------------------
4  * CppADCodeGen: C++ Algorithmic Differentiation with Source Code Generation:
5  * Copyright (C) 2012 Ciengis
6  *
7  * CppADCodeGen is distributed under multiple licenses:
8  *
9  * - Eclipse Public License Version 1.0 (EPL1), and
10  * - GNU General Public License Version 3 (GPL3).
11  *
12  * EPL1 terms and conditions can be found in the file "epl-v10.txt", while
13  * terms and conditions for the GPL3 can be found in the file "gpl3.txt".
14  * ----------------------------------------------------------------------------
15  * Author: Joao Leal
16  */
17 
18 namespace CppAD {
19 namespace cg {
20 
26 enum class CGOpCode {
27  Assign, // a = b
28  Abs, // abs(variable)
29  Acos, // acos(variable)
30  Acosh, // acosh(variable)
31  Add, // a + b
32  Alias, // alias (reference to another operation)
33  ArrayCreation, // dense array {a, b, c ...}
34  SparseArrayCreation, // {a, b, c ...}; {index1, index2, index3, ...};
35  ArrayElement, // x[i]
36  Asin, // asin(variable)
37  Asinh, // asinh(variable)
38  Atan, // atan(variable)
39  Atanh, // atanh(variable)
40  AtomicForward, // atomicFunction.forward(q, p, vx, vy, tx, ty)
41  AtomicReverse, // atomicFunction.reverse(p, tx, ty, px, py)
42  ComLt, // result = left < right? trueCase: falseCase
43  ComLe, // result = left <= right? trueCase: falseCase
44  ComEq, // result = left == right? trueCase: falseCase
45  ComGe, // result = left >= right? trueCase: falseCase
46  ComGt, // result = left > right? trueCase: falseCase
47  ComNe, // result = left != right? trueCase: falseCase
48  Cosh, // cosh(variable)
49  Cos, // cos(variable)
50  Div, // a / b
51  Erf, // erf(variable)
52  Erfc, // erfc(variable)
53  Exp, // exp(variable)
54  Expm1, // expm1(variable)
55  Inv, // independent variable
56  Log, // log(variable)
57  Log1p, // log1p(variable)
58  Mul, // a * b
59  Pow, // pow(a, b)
60  Pri, // PrintFor(text, parameter or variable, parameter or variable)
61  Sign, // result = (x > 0)? 1.0:((x == 0)? 0.0:-1)
62  Sinh, // sinh(variable)
63  Sin, // sin(variable)
64  Sqrt, // sqrt(variable)
65  Sub, // a - b
66  Tanh, // tanh(variable)
67  Tan, // tan(variable)
68  UnMinus, // -(a)
69  DependentMultiAssign, // operation which associates a dependent variables with loops and regular operations
70  DependentRefRhs, // operation referencing a dependent variable (right hand side only)
71  IndexDeclaration, // an integer index declaration
72  Index, // an integer index
73  IndexAssign, // assignment of an integer index to an index pattern expression
74  LoopStart, // for() {}
75  LoopIndexedIndep, // indexed independent used by a loop
76  LoopIndexedDep, // indexed output for a dependent variable from a loop
77  LoopIndexedTmp, // indexed output for a temporary variable from a loop
78  LoopEnd, // endfor
79  TmpDcl, // marks the beginning of the use of a temporary variable across several scopes (used by LoopIndexedTmp)
80  Tmp, // reference to a temporary variable defined by TmpDcl
81  IndexCondExpr, // a condition expression which returns a boolean
82  StartIf, // the start of an if statement
83  ElseIf, // else if()
84  Else, // else
85  EndIf, // end of if
86  CondResult, // assignment inside an if branch
87  UserCustom, // a custom type added by a user which has no direct support in CppADCodeGen
88  NumberOp // total number of operation types
89 };
90 
91 inline std::ostream& operator<<(std::ostream& os, const CGOpCode& op) {
92  static const char* OpNameTable[] = {
93  "$1 = $2", // Assign
94  "abs($1)", // Abs
95  "acos($1)", // Acos
96  "acosh($1)", // Acosh
97  "$1 + $2", // Add
98  "alias($1)", // Alias
99  "new array[size]", // ArrayCreation
100  "new sparseArray[size]", // SparseArrayCreation
101  "array[i]", // ArrayElement
102  "asin($1)", // Asin
103  "asinh($1)", // Asinh
104  "atan($1)", // Atan
105  "atanh($1)", // Atanh
106  "atomicFunction.forward(q, p, vx, vy, tx, ty)", // AtomicForward
107  "atomicFunction.reverse(p, tx, ty, px, py)", // AtomicReverse
108  "result = ($1 < $2)? $3 : $4", // ComLt
109  "result = ($1 <= $2)? $3 : $4", // ComLe
110  "result = ($1 == $2)? $3 : $4", // ComEq
111  "result = ($1 >= $2)? $3 : $4", // ComGe
112  "result = ($1 > $2)? $3 : $4", // ComGt
113  "result = ($1 != $2)? $3 : $4", // ComNe
114  "cosh($1)", // Cosh
115  "cos($1)", // Cos
116  "$1 / $2", // Div
117  "erf($1)", // Erf
118  "erfc($1)", // Erfc
119  "exp($1)", // Exp
120  "expm1($1)", // Expm1
121  "independent()", // Inv
122  "log($1)", // Log
123  "log1p($1)", // Log1p
124  "$1 * $2", // Mul
125  "pow($1, $2)", // Pow
126  "print($1)", // Pri
127  "sign($1)", // Sign
128  "sinh($1)", // Sinh
129  "sin($1)", // Sin
130  "sqrt($1)", // Sqrt
131  "$1 - $2", // Sub
132  "tanh($1)", // Tanh
133  "tan($1)", // Tan
134  "-($1)", // UnMinus
135  "dep($1) = ($2) + ...", // DependentMultiAssign
136  "depref($1)", // DependentRefRhs
137  "index declaration", // IndexDeclaration
138  "index", // Index
139  "index = expression()", // IndexAssign
140  "for", // LoopStart
141  "loopIndexedIndep", // LoopIndexedIndep
142  "loopIndexedDep", // LoopIndexedDep
143  "loopIndexedTmp", // LoopIndexedTmp
144  "endfor", // LoopEnd
145  "declare tempVar", // TmpDcl
146  "tempVar", // Tmp
147  "bool(index expression)", // IndexCondExpr
148  "if()", // StartIf
149  "else if()", // ElseIf
150  "else", // Else
151  "endif", // EndIf
152  "ifResult =", // CondResult
153  "custom", // UserCustom
154  "numberOp"
155  };
156  // check ensuring conversion to size_t is as expected
157  CPPADCG_ASSERT_UNKNOWN(size_t(CGOpCode::NumberOp) + 1 == sizeof(OpNameTable)/sizeof(OpNameTable[0]));
158 
159  // this test ensures that all indices are within the table
160  CPPADCG_ASSERT_UNKNOWN(int(op) >= 0 && size_t(op) < size_t(CGOpCode::NumberOp));
161 
162  os << OpNameTable[size_t(op)];
163 
164  return os;
165 }
166 
167 } // END cg namespace
168 } // END CppAD namespace
169 
170 #endif