Skip to content

Commit d90e886

Browse files
committed
Fix index renaming to handle new cases in explicit_indices.
1 parent e73240f commit d90e886

File tree

8 files changed

+386
-364
lines changed

8 files changed

+386
-364
lines changed

cmake/version.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
set(CADABRA_VERSION_MAJOR 2)
22
set(CADABRA_VERSION_MINOR 3)
33
set(CADABRA_VERSION_PATCH 5)
4-
set(CADABRA_VERSION_TWEAK 3)
4+
set(CADABRA_VERSION_TWEAK 4)
55
set(COPYRIGHT_YEARS "2001-2020")
66
math(EXPR SYSTEM_BITS "${CMAKE_SIZEOF_VOID_P} * 8")
77
find_program(GIT git PATHS ${GIT_DIR})

core/Algorithm.cc

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
4040

4141
#include <sstream>
4242

43-
// #define DEBUG
43+
//#define DEBUG
4444

4545
using namespace cadabra;
4646

@@ -272,8 +272,10 @@ Algorithm::result_t Algorithm::apply_deep(Ex::iterator& it)
272272
result_t res = apply(work);
273273
if(res==Algorithm::result_t::l_applied || res==Algorithm::result_t::l_applied_no_new_dummies) {
274274
some_changes_somewhere=result_t::l_applied;
275-
if(res==Algorithm::result_t::l_applied)
275+
if(res==Algorithm::result_t::l_applied) {
276+
// std::cerr << "rename replacement on " << work << std::endl;
276277
rename_replacement_dummies(work, true);
278+
}
277279
deepest_action=tr.depth(work);
278280
// If we got a zero at 'work', we need to propagate this up the tree and
279281
// then restart our post-order traversal such that everything that has
@@ -660,7 +662,7 @@ bool Algorithm::rename_replacement_dummies(iterator two, bool still_inside_algo)
660662
index_map_t ind_free, ind_dummy;
661663
index_map_t ind_free_full, ind_dummy_full;
662664

663-
if(still_inside_algo) {
665+
if(false && still_inside_algo) {
664666
if(tr.is_head(two)==false)
665667
classify_indices_up(tr.parent(two), ind_free_full, ind_dummy_full);
666668
}
@@ -675,6 +677,9 @@ bool Algorithm::rename_replacement_dummies(iterator two, bool still_inside_algo)
675677
std::cerr << "free indices above us" << std::endl;
676678
for(auto& ii: ind_free_full)
677679
std::cerr << ii.first << std::endl;
680+
std::cerr << "dummy indices above us" << std::endl;
681+
for(auto& ii: ind_dummy_full)
682+
std::cerr << ii.first << std::endl;
678683
#endif
679684

680685
index_map_t must_be_empty;
@@ -684,7 +689,9 @@ bool Algorithm::rename_replacement_dummies(iterator two, bool still_inside_algo)
684689
determine_intersection(ind_dummy_full, ind_dummy, must_be_empty);
685690
index_map_t::iterator it=must_be_empty.begin();
686691
while(it!=must_be_empty.end()) {
687-
// std::cerr << "double index pair" << std::endl;
692+
#ifdef DEBUG
693+
std::cerr << "double index pair" << std::endl;
694+
#endif
688695
Ex the_key=(*it).first;
689696
const Indices *dums=kernel.properties.get<Indices>(it->second, true);
690697
if(!dums)

core/Algorithm.hh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,11 @@ namespace cadabra {
131131
// The set to which the first index belongs..
132132
std::string get_index_set_name(iterator it) const;
133133

134+
/// Rename the dummies in the sub-tree starting with head at the given iterator.
135+
/// Ensures that no dummies in this sub-tree overlap with the indices elsewhere
136+
/// in the tree.
134137
bool rename_replacement_dummies(iterator, bool still_inside_algo=false);
135138

136-
137139
/// Determines whether the indicated node is 'like a term in a
138140
/// sum'. This requires that the node is not a `\sum` node, not
139141
/// a child of a `\prod` node, and that its parent rel is of

core/IndexClassifier.cc

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "properties/IndexInherit.hh"
99
#include <sstream>
1010

11-
// #define DEBUG
11+
// #define DEBUG 1
1212

1313
using namespace cadabra;
1414

@@ -156,11 +156,11 @@ void IndexClassifier::classify_add_index(Ex::iterator it, index_map_t& ind_free,
156156
}
157157
}
158158

159-
// This classifies indices bottom-up, that is, given a node, it goes up the tree to find
160-
// all free and dummy indices in the product in which this node would end up if a full
161-
// distribute would be done on the entire expression.
162159
void IndexClassifier::classify_indices_up(Ex::iterator it, index_map_t& ind_free, index_map_t& ind_dummy) const
163160
{
161+
#ifdef DEBUG
162+
std::cerr << "classify_indices_up at " << it << std::endl;
163+
#endif
164164
loopie:
165165
if(Ex::is_head(it)) return;
166166
Ex::iterator par=Ex::parent(it);
@@ -180,8 +180,14 @@ void IndexClassifier::classify_indices_up(Ex::iterator it, index_map_t& ind_free
180180
// children; add the indices thus found to the maps since they will end up in our factor.
181181
Ex::sibling_iterator sit=par.begin();
182182
while(sit!=par.end()) {
183+
#ifdef DEBUG
184+
std::cerr << "checking " << sit << std::endl;
185+
#endif
183186
if(sit!=Ex::sibling_iterator(it)) {
184187
if(sit->is_index()==false) {
188+
#ifdef DEBUG
189+
std::cerr << "classifying at " << sit << std::endl;
190+
#endif
185191
index_map_t factor_free, factor_dummy;
186192
classify_indices(sit, factor_free, factor_dummy);
187193

@@ -264,8 +270,6 @@ void IndexClassifier::dumpmap(std::ostream& str, const index_map_t& mp) const
264270
str << std::endl;
265271
}
266272

267-
// This classifies indices top-down, that is, finds the free indices and all dummy
268-
// index pairs used in the full subtree below a given node.
269273
void IndexClassifier::classify_indices(Ex::iterator it, index_map_t& ind_free, index_map_t& ind_dummy) const
270274
{
271275
const IndexInherit *inh=kernel.properties.get<IndexInherit>(it);

core/IndexClassifier.hh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,18 @@ namespace cadabra {
4646
bool move_out=false) const;
4747

4848
void classify_add_index(Ex::iterator it, index_map_t& ind_free, index_map_t& ind_dummy) const;
49+
50+
/// Classify indices bottom-up, that is, given a node, it goes up the tree to find
51+
/// all free and dummy indices in the product in which this node would end up if a full
52+
/// distribute would be done on the entire expression.
53+
4954
void classify_indices_up(Ex::iterator, index_map_t& ind_free, index_map_t& ind_dummy) const;
55+
56+
/// Classify indices top-down, that is, finds the free indices and all dummy
57+
/// index pairs used in the full subtree below a given node.
58+
5059
void classify_indices(Ex::iterator, index_map_t& ind_free, index_map_t& ind_dummy) const;
60+
5161
int max_numbered_name_one(const std::string& nm, const index_map_t * one) const;
5262
int max_numbered_name(const std::string&, const index_map_t *m1, const index_map_t *m2=0,
5363
const index_map_t *m3=0, const index_map_t *m4=0, const index_map_t *m5=0) const;

core/algorithms/explicit_indices.cc

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ Algorithm::result_t explicit_indices::apply(iterator& it)
8989

9090
// Classify all free and dummy indices already present. Any new
9191
// indices cannot be taken from these.
92+
// std::cerr << "acting at " << it << std::endl;
93+
9294
ind_free_sum.clear();
9395
ind_dummy_sum.clear();
9496
classify_indices(it, ind_free_sum, ind_dummy_sum);
@@ -116,13 +118,15 @@ Algorithm::result_t explicit_indices::apply(iterator& it)
116118
sibling_iterator args=tr.begin(factor);
117119
while(args!=tr.end(factor)) {
118120
if(args->fl.parent_rel==str_node::p_none) {
121+
res=result_t::l_applied;
119122
handle_factor(args, trace!=0);
120123
break;
121124
}
122125
++args;
123126
}
124127
}
125128
else {
129+
res=result_t::l_applied;
126130
handle_factor(factor, trace!=0);
127131
}
128132
++factor;
@@ -147,8 +151,17 @@ Algorithm::result_t explicit_indices::apply(iterator& it)
147151
it=parit;
148152
it = tr.flatten_and_erase(it);
149153
}
150-
cleanup_dispatch(kernel, tr, it);
151-
154+
if(*it->name=="\\sum" && tr.number_of_children(it)==1)
155+
it = tr.flatten_and_erase(it);
156+
// It looks like we will have trouble if we also flatten the
157+
// \prod node, becuase then rename_dummies will only act on
158+
// the first factor we introduced, and fail to rename
159+
// properly.
160+
// cleanup_dispatch(kernel, tr, it);
161+
162+
// std::cerr << "after explicit_indicex: " << tr.begin() << std::endl;
163+
// std::cerr << "and it is now " << it << std::endl;
164+
152165
return res;
153166
}
154167

0 commit comments

Comments
 (0)