CppADCodeGen  HEAD
A C++ Algorithmic Differentiation Package with Source Code Generation
cg.hpp
1 #ifndef CPPAD_CG_CG_INCLUDED
2 #define CPPAD_CG_CG_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 
28 template<class Base>
29 class CG {
30 private:
35  OperationNode<Base>* node_;
40  std::unique_ptr<Base> value_;
41 
42 public:
46  inline CG();
47 
53  inline CG(OperationNode<Base>& node);
54 
58  inline CG(const CG<Base>& orig);
59 
63  inline CG(CG<Base>&& orig);
64 
68  inline CG& operator=(const CG<Base>& rhs);
69 
73  inline CG& operator=(CG<Base>&& rhs);
74 
80  inline CG(const Base& val);
81 
87  inline CG& operator=(const Base& rhs);
88 
89  // destructor
90  virtual ~CG();
91 
96  inline CodeHandler<Base>* getCodeHandler() const;
97 
98  // variable classification methods
105  inline bool isVariable() const;
106 
113  inline bool isParameter() const;
114 
122  inline bool isValueDefined() const;
123 
129  inline const Base& getValue() const;
130 
137  inline void setValue(const Base& val);
138 
139  inline bool isIdenticalZero() const;
140  inline bool isIdenticalOne() const;
141 
142  inline OperationNode<Base>* getOperationNode() const;
143 
144  // computed assignment operators
145  inline CG<Base>& operator+=(const CG<Base>& right);
146  inline CG<Base>& operator-=(const CG<Base>& right);
147  inline CG<Base>& operator*=(const CG<Base>& right);
148  inline CG<Base>& operator/=(const CG<Base>& right);
149  inline CG<Base>& operator+=(const Base& right);
150  inline CG<Base>& operator-=(const Base& right);
151  inline CG<Base>& operator*=(const Base& right);
152  inline CG<Base>& operator/=(const Base& right);
153 
154  template< class T>
155  inline CG<Base>& operator+=(const T &right);
156  template<class T>
157  inline CG<Base>& operator-=(const T &right);
158  template<class T>
159  inline CG<Base>& operator/=(const T &right);
160  template<class T>
161  inline CG<Base>& operator*=(const T &right);
162 
163  // unary operators
164  inline CG<Base> operator+() const;
165  inline CG<Base> operator-() const;
166 protected:
173  inline CG(const Argument<Base>& arg);
174 
175  //
176  inline void makeParameter(const Base& b);
177 
178  inline void makeVariable(OperationNode<Base>& operation);
179 
180  inline void makeVariable(OperationNode<Base>& operation,
181  std::unique_ptr<Base>& value);
182 
183  // creating an argument out of this node
184  inline Argument<Base> argument() const;
185 
186  /***************************************************************************
187  * friends
188  **************************************************************************/
189 
190  friend class CodeHandler<Base>;
191  friend class CGAbstractAtomicFun<Base>;
192  friend class Loop<Base>;
193  friend class LoopModel<Base>;
194 
198  friend CG<Base> CppAD::cg::operator+ <Base>(const CG<Base>& left, const CG<Base>& right);
199  friend CG<Base> CppAD::cg::operator- <Base>(const CG<Base>& left, const CG<Base>& right);
200  friend CG<Base> CppAD::cg::operator* <Base>(const CG<Base>& left, const CG<Base>& right);
201  friend CG<Base> CppAD::cg::operator/ <Base>(const CG<Base>& left, const CG<Base>& right);
202 
206  friend bool GreaterThanZero <Base> (const CG<Base>& x);
207  friend bool GreaterThanOrZero <Base> (const CG<Base>& x);
208  friend bool LessThanZero <Base> (const CG<Base>& x);
209  friend bool LessThanOrZero <Base> (const CG<Base>& x);
210  friend bool abs_geq <Base> (const CG<Base>& x, const CG<Base>& y);
211 
212  // EqualOpSeq function
213  friend bool EqualOpSeq <Base> (const CG<Base>& u, const CG<Base>& v);
214 
215  // NearEqual function
216  friend bool NearEqual <Base> (const CG<Base>& x, const CG<Base>& y, const Base& r, const Base& a);
217  friend bool NearEqual <Base> (const Base& x, const CG<Base>& y, const Base& r, const Base& a);
218  friend bool NearEqual <Base> (const CG<Base>& x, const Base& y, const Base& r, const Base& a);
219 
220  // CondExp function
221  friend CG<Base> CondExp<Base>(CGOpCode op,
222  const CG<Base>& left,
223  const CG<Base>& right,
224  const CG<Base>& trueCase,
225  const CG<Base>& falseCase,
226  bool (*compare)(const Base&, const Base&));
227 
228  friend CG<Base> sign<Base>(const CG<Base>& x);
229 
233  friend CG<Base> pow<Base>(const CG<Base> &x, const CG<Base> &y);
234  friend CG<Base> abs<Base>(const CG<Base>& var);
235  friend CG<Base> fabs<Base>(const CG<Base>& var);
236  friend CG<Base> acos<Base>(const CG<Base>& var);
237  friend CG<Base> asin<Base>(const CG<Base>& var);
238  friend CG<Base> atan<Base>(const CG<Base>& var);
239  friend CG<Base> cos<Base>(const CG<Base>& var);
240  friend CG<Base> cosh<Base>(const CG<Base>& var);
241  friend CG<Base> exp<Base>(const CG<Base>& var);
242  friend CG<Base> log<Base>(const CG<Base>& var);
243  friend CG<Base> sin<Base>(const CG<Base>& var);
244  friend CG<Base> sinh<Base>(const CG<Base>& var);
245  friend CG<Base> sqrt<Base>(const CG<Base>& var);
246  friend CG<Base> tan<Base>(const CG<Base>& var);
247  friend CG<Base> tanh<Base>(const CG<Base>& var);
248 
249 #if CPPAD_USE_CPLUSPLUS_2011
250  // c++11 functions
251  friend CG<Base> erf<Base>(const CG<Base>& var); // error function
252  friend CG<Base> erfc<Base>(const CG<Base>& var); // complementary error function
253  friend CG<Base> asinh<Base>(const CG<Base>& var); // inverse hyperbolic sin
254  friend CG<Base> acosh<Base>(const CG<Base>& var); // inverse hyperbolic cosine
255  friend CG<Base> atanh<Base>(const CG<Base>& var); // inverse hyperbolic tangent
256  friend CG<Base> expm1<Base>(const CG<Base>& var); // exponential of x minus one
257  friend CG<Base> log1p<Base>(const CG<Base>& var); // logarithm of one plus x
258 #endif
259 };
260 
261 
268 template<class Base>
270  // Because of a GCC bug (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66944)
271  // FUNC_OBJ can't be a CGOStreamFunc static member.
272  // Instead, we store it as CGOStreamFunc::FUNC method static variable.
273  static std::function<std::ostream& (std::ostream&, const CG<Base>&)>& FUNC () {
274  static thread_local std::function<std::ostream& (std::ostream&, const CG<Base>&)> FUNC_OBJ = nullptr;
275  return FUNC_OBJ;
276  }
277 };
278 
283 template<class Base>
284 inline std::ostream& operator<<(
285  std::ostream& os, //< stream to write to
286  const CG<Base>& v//< vector that is output
287  ) {
288  if(CGOStreamFunc<Base>::FUNC() != nullptr) {
289  return CGOStreamFunc<Base>::FUNC()(os, v);
290  }
291 
292  if (v.isParameter()) {
293  os << v.getValue();
294  } else {
295  os << *v.getOperationNode();
296  if (v.isValueDefined()) {
297  os << " (" << v.getValue() << ")";
298  }
299  }
300  return os;
301 }
302 
303 template<class Base>
304 inline std::ostringstream& operator<<(
305  std::ostringstream& os, //< steam to write the vector to
306  const CG<Base>& v//< vector that is output
307  ) {
308  if (v.isParameter()) {
309  os << v.getValue();
310  } else {
311  os << *v.getOperationNode();
312  if (v.isValueDefined()) {
313  os << " (" << v.getValue() << ")";
314  }
315  }
316  return os;
317 }
318 
319 template<class Base>
320 inline std::istream& operator>>(
321  std::istream& is, //< stream to load a parameter value
322  CG<Base>& v//< the variable that will be assign the value
323  ) {
324  Base value;
325  is >> value;
326  v = value;
327  return is;
328 }
329 
330 } // END cg namespace
331 
332 template <class Base>
333 int Integer(const CppAD::cg::CG<Base>& x) {
334  if (x.isValueDefined()) {
335  return Integer(x.getValue());
336  } else {
337  CppAD::ErrorHandler::Call(false, __LINE__, __FILE__, "Integer()", "No value defined");
338  return 0;
339  }
340 }
341 
342 } // END CppAD namespace
343 
344 #endif
const Base & getValue() const
Definition: variable.hpp:45
void setValue(const Base &val)
Definition: variable.hpp:54
bool isVariable() const
Definition: variable.hpp:30
bool isValueDefined() const
Definition: variable.hpp:40
CodeHandler< Base > * getCodeHandler() const
Definition: variable.hpp:22
bool isParameter() const
Definition: variable.hpp:35
CG & operator=(const CG< Base > &rhs)
Definition: default.hpp:84