CppADCodeGen 2.4.3
A C++ Algorithmic Differentiation Package with Source Code Generation
Loading...
Searching...
No Matches
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
18namespace CppAD {
19namespace cg {
20
21template<class Base>
23 if (node_ != nullptr)
24 return node_->getCodeHandler();
25 else
26 return nullptr;
27}
28
29template<class Base>
30inline bool CG<Base>::isVariable() const {
31 return node_ != nullptr;
32}
33
34template<class Base>
35inline bool CG<Base>::isParameter() const {
36 return node_ == nullptr;
37}
38
39template<class Base>
40inline bool CG<Base>::isValueDefined() const {
41 return value_ != nullptr;
42}
43
44template<class Base>
45inline 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
53template<class Base>
54inline 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
62template<class Base>
63inline bool CG<Base>::isIdenticalZero() const {
64 return isParameter() && CppAD::IdenticalZero(getValue());
65}
66
67template<class Base>
68inline bool CG<Base>::isIdenticalOne() const {
69 return isParameter() && CppAD::IdenticalOne(getValue());
70}
71
72template<class Base>
73inline void CG<Base>::makeParameter(const Base &b) {
74 node_ = nullptr;
75 setValue(b);
76}
77
78template<class Base>
79inline void CG<Base>::makeVariable(OperationNode<Base>& operation) {
80 node_ = &operation;
81 value_.reset();
82}
83
84template<class Base>
85inline void CG<Base>::makeVariable(OperationNode<Base>& operation,
86 std::unique_ptr<Base>& value) {
87 node_ = &operation;
88 value_ = std::move(value);
89}
90
91template<class Base>
92inline OperationNode<Base>* CG<Base>::getOperationNode() const {
93 return node_;
94}
95
96template<class Base>
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
void setValue(const Base &val)
Definition variable.hpp:54
const Base & getValue() const
Definition variable.hpp:45
bool isValueDefined() const
Definition variable.hpp:40
CodeHandler< Base > * getCodeHandler() const
Definition variable.hpp:22
bool isParameter() const
Definition variable.hpp:35
bool isVariable() const
Definition variable.hpp:30
bool IdenticalOne(const cg::CG< Base > &x)
Definition identical.hpp:45
bool IdenticalZero(const cg::CG< Base > &x)
Definition identical.hpp:37