CppADCodeGen  HEAD
A C++ Algorithmic Differentiation Package with Source Code Generation
equation_group.hpp
1 #ifndef CPPAD_CG_EQUATION_GROUP_INCLUDED
2 #define CPPAD_CG_EQUATION_GROUP_INCLUDED
3 /* --------------------------------------------------------------------------
4  * CppADCodeGen: C++ Algorithmic Differentiation with Source Code Generation:
5  * Copyright (C) 2013 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 
24 template<class Base>
26 public:
30  std::set<EquationPattern<Base>*> equations;
35  std::vector<std::set<size_t> > linkedDependents;
40  std::vector<std::set<EquationPattern<Base>*> > linkedEquationsByNonIndexedRel;
41  std::set<EquationPattern<Base>*> linkedEquationsByNonIndexed; // only equation which do not have any shared indexed variables
46  std::vector<std::set<size_t> > iterationDependents;
50  size_t refIteration;
51 public:
52 
53  inline void findReferenceIteration() {
54  CPPADCG_ASSERT_UNKNOWN(!iterationDependents.empty());
55 
56  for (size_t it = 0; it < iterationDependents.size(); it++) {
57  if (iterationDependents[it].size() == equations.size()) {
58  refIteration = it;
59  return;
60  }
61  }
62 
63  CPPADCG_ASSERT_UNKNOWN(false);
64  }
65 
66  inline long findIndexedLinkedDependent(size_t dep) const {
67  size_t size = linkedDependents.size();
68  for (size_t pos = 0; pos < size; pos++) {
69  const std::set<size_t>& sameIndex = linkedDependents[pos];
70  if (sameIndex.find(dep) != sameIndex.end()) {
71  return pos;
72  }
73  }
74 
75  return -1;
76  }
77 
78  inline size_t getLinkedEquationsByNonIndexedCount() const {
79  return linkedEquationsByNonIndexed.size();
80  }
81 
82  inline size_t findNonIndexedLinkedRel(EquationPattern<Base>* eq) const {
83  size_t size = linkedEquationsByNonIndexedRel.size();
84  for (size_t pos = 0; pos < size; pos++) {
85  if (linkedEquationsByNonIndexedRel[pos].find(eq) != linkedEquationsByNonIndexedRel[pos].end()) {
86  return pos;
87  }
88  }
89 
90  return -1;
91  }
92 
93  inline void addLinkedEquationsByNonIndexed(EquationPattern<Base>* eq1,
94  EquationPattern<Base>* eq2) {
95  linkedEquationsByNonIndexed.insert(eq1);
96  linkedEquationsByNonIndexed.insert(eq2);
97 
98  size_t size = linkedEquationsByNonIndexedRel.size();
99  size_t pos1 = size;
100  size_t pos2 = size;
101  for (size_t pos = 0; pos < size; pos++) {
102  if (linkedEquationsByNonIndexedRel[pos].find(eq1) != linkedEquationsByNonIndexedRel[pos].end()) {
103  pos1 = pos;
104  break;
105  }
106  }
107  for (size_t pos = 0; pos < size; pos++) {
108  if (linkedEquationsByNonIndexedRel[pos].find(eq2) != linkedEquationsByNonIndexedRel[pos].end()) {
109  pos2 = pos;
110  break;
111  }
112  }
113 
114  if (pos1 == pos2) {
115  if (pos1 == size) {
116  linkedEquationsByNonIndexedRel.resize(size + 1);
117  linkedEquationsByNonIndexedRel[size].insert(eq1);
118  linkedEquationsByNonIndexedRel[size].insert(eq2);
119  }
120  } else if (pos1 < size) {
121  if (pos2 < size) {
122  // must merge
123  linkedEquationsByNonIndexedRel[pos1].insert(linkedEquationsByNonIndexedRel[pos2].begin(), linkedEquationsByNonIndexedRel[pos2].end());
124  linkedEquationsByNonIndexedRel.erase(linkedEquationsByNonIndexedRel.begin() + pos2);
125  } else {
126  linkedEquationsByNonIndexedRel[pos1].insert(eq2);
127  }
128 
129  } else {
130  CPPADCG_ASSERT_UNKNOWN(pos2 < size);
131  linkedEquationsByNonIndexedRel[pos2].insert(eq1);
132  }
133  }
134 };
135 
136 } // END cg namespace
137 } // END CppAD namespace
138 
139 #endif
std::vector< std::set< size_t > > linkedDependents
std::vector< std::set< size_t > > iterationDependents
std::vector< std::set< EquationPattern< Base > * > > linkedEquationsByNonIndexedRel
std::set< EquationPattern< Base > * > equations