CppADCodeGen  2.3.0
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  Exp, // exp(variable)
53  Expm1, // expm1(variable)
54  Inv, // independent variable
55  Log, // log(variable)
56  Log1p, // log1p(variable)
57  Mul, // a * b
58  Pow, // pow(a, b)
59  Pri, // PrintFor(text, parameter or variable, parameter or variable)
60  Sign, // result = (x > 0)? 1.0:((x == 0)? 0.0:-1)
61  Sinh, // sinh(variable)
62  Sin, // sin(variable)
63  Sqrt, // sqrt(variable)
64  Sub, // a - b
65  Tanh, // tanh(variable)
66  Tan, // tan(variable)
67  UnMinus, // -(a)
68  DependentMultiAssign, // operation which associates a dependent variables with loops and regular operations
69  DependentRefRhs, // operation referencing a dependent variable (right hand side only)
70  IndexDeclaration, // an integer index declaration
71  Index, // an integer index
72  IndexAssign, // assignment of an integer index to an index pattern expression
73  LoopStart, // for() {}
74  LoopIndexedIndep, // indexed independent used by a loop
75  LoopIndexedDep, // indexed output for a dependent variable from a loop
76  LoopIndexedTmp, // indexed output for a temporary variable from a loop
77  LoopEnd, // endfor
78  TmpDcl, // marks the beginning of the use of a temporary variable across several scopes (used by LoopIndexedTmp)
79  Tmp, // reference to a temporary variable defined by TmpDcl
80  IndexCondExpr, // a condition expression which returns a boolean
81  StartIf, // the start of an if statement
82  ElseIf, // else if()
83  Else, // else
84  EndIf, // end of if
85  CondResult, // assignment inside an if branch
86  UserCustom, // a custom type added by a user which has no direct support in CppADCodeGen
87  NumberOp // total number of operation types
88 };
89 
90 inline std::ostream& operator<<(std::ostream& os, const CGOpCode& op) {
91  static const char* OpNameTable[] = {
92  "$1 = $2", // Assign
93  "abs($1)", // Abs
94  "acos($1)", // Acos
95  "acosh($1)", // Acosh
96  "$1 + $2", // Add
97  "alias($1)", // Alias
98  "new array[size]", // ArrayCreation
99  "new sparseArray[size]", // SparseArrayCreation
100  "array[i]", // ArrayElement
101  "asin($1)", // Asin
102  "asinh($1)", // Asinh
103  "atan($1)", // Atan
104  "atanh($1)", // Atanh
105  "atomicFunction.forward(q, p, vx, vy, tx, ty)", // AtomicForward
106  "atomicFunction.reverse(p, tx, ty, px, py)", // AtomicReverse
107  "result = ($1 < $2)? $3 : $4", // ComLt
108  "result = ($1 <= $2)? $3 : $4", // ComLe
109  "result = ($1 == $2)? $3 : $4", // ComEq
110  "result = ($1 >= $2)? $3 : $4", // ComGe
111  "result = ($1 > $2)? $3 : $4", // ComGt
112  "result = ($1 != $2)? $3 : $4", // ComNe
113  "cosh($1)", // Cosh
114  "cos($1)", // Cos
115  "$1 / $2", // Div
116  "erf($1)", // Erf
117  "exp($1)", // Exp
118  "expm1($1)", // Expm1
119  "independent()", // Inv
120  "log($1)", // Log
121  "log1p($1)", // Log1p
122  "$1 * $2", // Mul
123  "pow($1, $2)", // Pow
124  "print($1)", // Pri
125  "sign($1)", // Sign
126  "sinh($1)", // Sinh
127  "sin($1)", // Sin
128  "sqrt($1)", // Sqrt
129  "$1 - $2", // Sub
130  "tanh($1)", // Tanh
131  "tan($1)", // Tan
132  "-($1)", // UnMinus
133  "dep($1) = ($2) + ...", // DependentMultiAssign
134  "depref($1)", // DependentRefRhs
135  "index declaration", // IndexDeclaration
136  "index", // Index
137  "index = expression()", // IndexAssign
138  "for", // LoopStart
139  "loopIndexedIndep", // LoopIndexedIndep
140  "loopIndexedDep", // LoopIndexedDep
141  "loopIndexedTmp", // LoopIndexedTmp
142  "endfor", // LoopEnd
143  "declare tempVar", // TmpDcl
144  "tempVar", // Tmp
145  "bool(index expression)", // IndexCondExpr
146  "if()", // StartIf
147  "else if()", // ElseIf
148  "else", // Else
149  "endif", // EndIf
150  "ifResult =", // CondResult
151  "custom", // UserCustom
152  "numberOp"
153  };
154  // check ensuring conversion to size_t is as expected
155  CPPADCG_ASSERT_UNKNOWN(size_t(CGOpCode::NumberOp) + 1 == sizeof(OpNameTable)/sizeof(OpNameTable[0]));
156 
157  // this test ensures that all indices are within the table
158  CPPADCG_ASSERT_UNKNOWN(int(op) >= 0 && size_t(op) < size_t(CGOpCode::NumberOp));
159 
160  os << OpNameTable[size_t(op)];
161 
162  return os;
163 }
164 
165 } // END cg namespace
166 } // END CppAD namespace
167 
168 #endif