CppADCodeGen  2.4.3
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  static thread_local std::function<std::ostream& (std::ostream&, const CG<Base>&)> FUNC;
271 };
272 
273 template<class Base>
274 thread_local std::function<std::ostream& (std::ostream&, const CG<Base>&)> CGOStreamFunc<Base>::FUNC = nullptr;
275 
280 template<class Base>
281 inline std::ostream& operator<<(
282  std::ostream& os, //< stream to write to
283  const CG<Base>& v//< vector that is output
284  ) {
285  if(CGOStreamFunc<Base>::FUNC != nullptr) {
286  return CGOStreamFunc<Base>::FUNC(os, v);
287  }
288 
289  if (v.isParameter()) {
290  os << v.getValue();
291  } else {
292  os << *v.getOperationNode();
293  if (v.isValueDefined()) {
294  os << " (" << v.getValue() << ")";
295  }
296  }
297  return os;
298 }
299 
300 template<class Base>
301 inline std::ostringstream& operator<<(
302  std::ostringstream& os, //< steam to write the vector to
303  const CG<Base>& v//< vector that is output
304  ) {
305  if (v.isParameter()) {
306  os << v.getValue();
307  } else {
308  os << *v.getOperationNode();
309  if (v.isValueDefined()) {
310  os << " (" << v.getValue() << ")";
311  }
312  }
313  return os;
314 }
315 
316 template<class Base>
317 inline std::istream& operator>>(
318  std::istream& is, //< stream to load a parameter value
319  CG<Base>& v//< the variable that will be assign the value
320  ) {
321  Base value;
322  is >> value;
323  v = value;
324  return is;
325 }
326 
327 } // END cg namespace
328 
329 template <class Base>
330 int Integer(const CppAD::cg::CG<Base>& x) {
331  if (x.isValueDefined()) {
332  return Integer(x.getValue());
333  } else {
334  CppAD::ErrorHandler::Call(false, __LINE__, __FILE__, "Integer()", "No value defined");
335  return 0;
336  }
337 }
338 
339 } // END CppAD namespace
340 
341 #endif
CppAD::cg::CG::operator=
CG & operator=(const CG< Base > &rhs)
Definition: default.hpp:84
CppAD::cg::CG::CG
CG()
Definition: default.hpp:25
CppAD::cg::CG::getValue
const Base & getValue() const
Definition: variable.hpp:45
CppAD::cg::CG::isVariable
bool isVariable() const
Definition: variable.hpp:30
CppAD
Definition: abstract_atomic_fun.hpp:19
CppAD::cg::Argument
Definition: argument.hpp:31
CppAD::cg::CG::setValue
void setValue(const Base &val)
Definition: variable.hpp:54
CppAD::cg::CG
Definition: cg.hpp:29
CppAD::cg::CGOStreamFunc
Definition: cg.hpp:269
CppAD::cg::Loop
Definition: declare_cg.hpp:102
CppAD::cg::CodeHandler
Definition: code_handler.hpp:28
CppAD::cg::CGAbstractAtomicFun
Definition: abstract_atomic_fun.hpp:28
CppAD::cg::CG::getCodeHandler
CodeHandler< Base > * getCodeHandler() const
Definition: variable.hpp:22
CppAD::cg::LoopModel
Definition: declare_cg.hpp:108
CppAD::cg::CG::isValueDefined
bool isValueDefined() const
Definition: variable.hpp:40
CppAD::cg::OperationNode
Definition: argument.hpp:22
CppAD::cg::CG::isParameter
bool isParameter() const
Definition: variable.hpp:35