CppADCodeGen  2.4.3
A C++ Algorithmic Differentiation Package with Source Code Generation
compare.hpp
1 #ifndef CPPAD_CG_COMPARE_INCLUDED
2 #define CPPAD_CG_COMPARE_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 
21 template<class Base>
22 inline bool operator ==(const CG<Base>& left, const CG<Base>& right) {
23  if (left.isParameter() && right.isParameter()) {
24  return left.getValue() == right.getValue();
25  } else if (left.isParameter() || right.isParameter()) {
26  return false;
27  } else {
28  return left.getOperationNode() == right.getOperationNode();
29  }
30 }
31 
32 template<class Base>
33 inline bool operator !=(const CG<Base>& left, const CG<Base>& right) {
34  if (left.isParameter() && right.isParameter()) {
35  return left.getValue() != right.getValue();
36  } else if (left.isParameter() || right.isParameter()) {
37  return true;
38  } else {
39  return left.getOperationNode() != right.getOperationNode();
40  }
41 }
42 
43 #define CPPAD_CG_OPERATOR(Op) \
44  template<class Base> \
45  inline bool operator Op(const CG<Base> &left, const CG<Base> &right) { \
46  if (left.isParameter() && right.isParameter()) { \
47  return left.getValue() Op right.getValue(); \
48  } else { \
49  throw CGException("Cannot use the "#Op" comparison operator in non parameter variables");\
50  } \
51  }
52 
53 CPPAD_CG_OPERATOR(>)
54 CPPAD_CG_OPERATOR( >=)
55 CPPAD_CG_OPERATOR(<)
56 CPPAD_CG_OPERATOR( <=)
57 
58 template<class Base>
59 inline bool operator==(const CG<Base>& left, const Base& right) {
60  if (left.isParameter()) {
61  return left.getValue() == right;
62  } else {
63  return false;
64  }
65 }
66 
67 template<class Base>
68 inline bool operator==(const Base& left, const CG<Base>& right) {
69  if (right.isParameter()) {
70  return left == right.getValue();
71  } else {
72  return false;
73  }
74 }
75 
76 template<class Base>
77 inline bool operator!=(const CG<Base>& left, Base right) {
78  if (left.isParameter()) {
79  return left.getValue() != right;
80  } else {
81  return true;
82  }
83 }
84 
85 template<class Base>
86 inline bool operator!=(const Base& left, const CG<Base>& right) {
87  if (right.isParameter()) {
88  return left != right.getValue();
89  } else {
90  return true;
91  }
92 }
93 
94 // comparison with double (required by CppAD SparseHessian)
95 template<class Base>
96 inline bool operator!=(const CG<Base> &left, double right) {
97  if (left.isParameter()) {
98  return left.getValue() != right;
99  } else {
100  return true;
101  }
102 }
103 
104 } // END cg namespace
105 } // END CppAD namespace
106 
107 #endif
108 
CppAD
Definition: abstract_atomic_fun.hpp:19