1 #ifndef CPPAD_CG_BIPARTITE_NODES_INCLUDED 2 #define CPPAD_CG_BIPARTITE_NODES_INCLUDED 42 inline void color(std::ostream& out = std::cout,
43 Verbosity verbosity = Verbosity::None) {
46 if (verbosity >= Verbosity::High)
47 out <<
" Colored " << nodeType() <<
" " << name() <<
"\n";
50 inline void uncolor() {
54 inline bool isColored()
const {
58 inline size_t index()
const {
62 inline void setIndex(
size_t index) {
66 virtual const std::string& name()
const = 0;
68 virtual std::string nodeType() = 0;
83 static const std::string TYPE;
113 inline Enode(
size_t index,
114 const std::string& name =
"") :
116 differentiation_(nullptr),
117 differentiationOf_(nullptr),
119 name_(name.empty()? (
"Eq" +
std::to_string(index)) : name) {
122 inline Enode(
size_t index,
125 differentiation_(
nullptr),
126 differentiationOf_(differentiationOf),
128 name_(
"Diff(" + differentiationOf->name() +
")") {
129 differentiationOf_->setDerivative(
this);
132 inline virtual ~
Enode() {
135 inline const std::vector<Vnode<Base>*>& variables()
const {
139 inline const std::vector<Vnode<Base>*>& originalVariables()
const {
144 if (std::find(vnodes_orig_.begin(), vnodes_orig_.end(), j) == vnodes_orig_.end()) {
145 vnodes_orig_.push_back(j);
146 if (!j->isDeleted()) {
147 vnodes_.push_back(j);
148 j->addEquation(
this);
166 return differentiation_;
170 return differentiationOf_;
174 if (differentiationOf_ ==
nullptr) {
177 return differentiationOf_->originalEquation();
182 auto it = std::find(vnodes_.begin(), vnodes_.end(), j);
183 if (it != vnodes_.end())
188 differentiation_ = difEq;
191 virtual const std::string& name()
const {
195 virtual std::string nodeType() {
201 inline std::ostream& operator <<(std::ostream& os, const Enode<Base>& i) {
202 if (i.derivativeOf() !=
nullptr) {
203 os <<
"Diff(" << *i.derivativeOf() <<
")";
205 os <<
"Equation " << i.name() <<
" (" << i.index() <<
")";
220 static const std::string TYPE;
258 inline Vnode(
size_t index,
260 const std::string& name) :
265 derivative_(nullptr),
266 antiDerivative_(nullptr),
267 tapeIndex_(tapeIndex),
272 inline Vnode(
size_t index,
275 const std::string& name =
"") :
280 derivative_(
nullptr),
281 antiDerivative_(derivativeOf),
282 tapeIndex_(tapeIndex),
283 name_(name.empty() ?
"d" + derivativeOf->name() +
"dt" : name) {
284 CPPADCG_ASSERT_UNKNOWN(antiDerivative_ !=
nullptr);
286 antiDerivative_->setDerivative(
this);
289 inline virtual ~
Vnode() {
292 inline virtual const std::string& name()
const {
296 inline size_t tapeIndex()
const {
300 inline void setTapeIndex(
size_t tapeIndex) {
301 tapeIndex_ = tapeIndex;
304 inline std::vector<Enode<Base>*>& equations() {
308 inline const std::vector<Enode<Base>*>& equations()
const {
323 return antiDerivative_;
327 if (antiDerivative_ ==
nullptr) {
330 return antiDerivative_->originalVariable();
334 inline Vnode<Base>* originalVariable(
size_t origVarSize) {
335 if (antiDerivative_ ==
nullptr || this->index_ < origVarSize) {
338 return antiDerivative_->originalVariable();
342 inline bool isDeleted()
const {
346 inline void makeParameter(std::ostream& out = std::cout,
347 Verbosity verbosity = Verbosity::None) {
349 deleteNode(out, verbosity);
352 inline bool isParameter()
const {
356 inline void deleteNode(std::ostream& out = std::cout,
357 Verbosity verbosity = Verbosity::None) {
358 if (verbosity >= Verbosity::High)
359 out <<
"Deleting " << *
this <<
"\n";
373 std::ostream& out = std::cout,
374 Verbosity verbosity = Verbosity::None) {
375 if (verbosity >= Verbosity::High)
376 out <<
" Assigning " << *
this <<
" to " << i <<
"\n";
379 i.setAssigmentVariable(*
this);
382 virtual std::string nodeType() {
390 unsigned int order()
const {
391 if (antiDerivative_ ==
nullptr) {
394 return antiDerivative_->order() + 1u;
402 CPPADCG_ASSERT_UNKNOWN(std::find(enodes_.begin(), enodes_.end(), i) == enodes_.end());
403 enodes_.push_back(i);
407 friend class Enode<Base>;
411 inline std::ostream& operator <<(std::ostream& os, const Vnode<Base>& j) {
412 if (j.antiDerivative() !=
nullptr) {
413 os <<
"Diff(" << *j.antiDerivative() <<
")";
415 os <<
"Variable " << j.name();
Enode< Base > * differentiationOf_
Vnode< Base > * derivative_
Vnode< Base > * derivative() const
Enode< Base > * differentiation_
Vnode< Base > *const antiDerivative_
std::vector< Enode< Base > * > enodes_
Enode< Base > * derivative() const
std::vector< Vnode< Base > * > vnodes_orig_
Vnode< Base > * antiDerivative() const
std::vector< Vnode< Base > * > vnodes_