CppADCodeGen  HEAD
A C++ Algorithmic Differentiation Package with Source Code Generation
variable.hpp
1 #ifndef CPPAD_CG_VARIABLE_INCLUDED
2 #define CPPAD_CG_VARIABLE_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>
23  if (node_ != nullptr)
24  return node_->getCodeHandler();
25  else
26  return nullptr;
27 }
28 
29 template<class Base>
30 inline bool CG<Base>::isVariable() const {
31  return node_ != nullptr;
32 }
33 
34 template<class Base>
35 inline bool CG<Base>::isParameter() const {
36  return node_ == nullptr;
37 }
38 
39 template<class Base>
40 inline bool CG<Base>::isValueDefined() const {
41  return value_ != nullptr;
42 }
43 
44 template<class Base>
45 inline const Base& CG<Base>::getValue() const {
46  if (!isValueDefined()) {
47  throw CGException("No value defined for this variable");
48  }
49 
50  return *value_;
51 }
52 
53 template<class Base>
54 inline void CG<Base>::setValue(const Base& b) {
55  if (value_ != nullptr) {
56  *value_ = b;
57  } else {
58  value_.reset(new Base(b)); // to replace with value_ = std::make_unique once c++14 is used
59  }
60 }
61 
62 template<class Base>
63 inline bool CG<Base>::isIdenticalZero() const {
64  return isParameter() && CppAD::IdenticalZero(getValue());
65 }
66 
67 template<class Base>
68 inline bool CG<Base>::isIdenticalOne() const {
69  return isParameter() && CppAD::IdenticalOne(getValue());
70 }
71 
72 template<class Base>
73 inline void CG<Base>::makeParameter(const Base &b) {
74  node_ = nullptr;
75  setValue(b);
76 }
77 
78 template<class Base>
79 inline void CG<Base>::makeVariable(OperationNode<Base>& operation) {
80  node_ = &operation;
81  value_.reset();
82 }
83 
84 template<class Base>
85 inline void CG<Base>::makeVariable(OperationNode<Base>& operation,
86  std::unique_ptr<Base>& value) {
87  node_ = &operation;
88  value_ = std::move(value);
89 }
90 
91 template<class Base>
93  return node_;
94 }
95 
96 template<class Base>
97 inline Argument<Base> CG<Base>::argument() const {
98  if (node_ != nullptr)
99  return Argument<Base> (*node_);
100  else
101  return Argument<Base> (*value_);
102 }
103 
104 } // END cg namespace
105 } // END CppAD namespace
106 
107 #endif
const Base & getValue() const
Definition: variable.hpp:45
void setValue(const Base &val)
Definition: variable.hpp:54
bool IdenticalOne(const cg::CG< Base > &x)
Definition: identical.hpp:45
bool isVariable() const
Definition: variable.hpp:30
bool IdenticalZero(const cg::CG< Base > &x)
Definition: identical.hpp:37
bool isValueDefined() const
Definition: variable.hpp:40
CodeHandler< Base > * getCodeHandler() const
Definition: variable.hpp:22
bool isParameter() const
Definition: variable.hpp:35