@@ -43,10 +43,6 @@ Algorithm::result_t explicit_indices::apply(iterator& it)
4343 // indices cannot be taken from these.
4444 index_map_t ind_free_sum, ind_dummy_sum;
4545 classify_indices (it, ind_free_sum, ind_dummy_sum);
46- for (auto & k: ind_free_sum)
47- std::cerr << k.first << std::endl;
48- for (auto & k: ind_dummy_sum)
49- std::cerr << k.first << std::endl;
5046
5147 sibling_iterator term=tr.begin (it);
5248 while (term!=tr.end (it)) {
@@ -56,6 +52,7 @@ Algorithm::result_t explicit_indices::apply(iterator& it)
5652
5753 // For each index set, keep track of the last used index in
5854 // building the explicit index line.
55+ index_map_t added_this_term;
5956 std::map<const Indices *, Ex::iterator> index_lines;
6057
6158 sibling_iterator factor=tr.begin (term);
@@ -105,21 +102,29 @@ Algorithm::result_t explicit_indices::apply(iterator& it)
105102 }
106103 ++search;
107104 }
105+ ++iit; // Update now, we may be replacing this index.
108106 if (found) {
109- // This index was added.
110- // Get a new free index.
111-
112- auto ip = kernel.properties .get <Indices>(search->second );
107+ // This index was added.
108+ const Indices *ip = kernel.properties .get <Indices>(search->second );
113109 if (!ip)
114110 throw InternalError (" Do not have Indices property for all implicit indices." );
115111
116- std::cerr << " getting dummy index" << std::endl;
117- auto di = ic.get_dummy (ip, &ind_free_sum, &ind_dummy_sum);
118- std::cerr << di << std::endl;
119- tr.replace (search->second , di.begin ());
112+ // Determine if we have an 'active' index line for
113+ // this index type.
114+ auto line = index_lines.find (ip);
115+ if (line==index_lines.end ()) {
116+ // No active line. Get a new free index.
117+ auto di = ic.get_dummy (ip, &ind_free_sum, &ind_dummy_sum, &added_this_term);
118+ auto loc = tr.replace_index (search->second , di.begin (), true );
119+ added_this_term.insert (index_map_t::value_type (di, loc));
120+ index_lines[ip]=loc;
121+ }
122+ else {
123+ // Use the active line index, then unset the active line.
124+ auto loc = tr.replace_index (search->second , line->second , true );
125+ index_lines.erase (line);
126+ }
120127 }
121-
122- ++iit;
123128 }
124129 }
125130 ++factor;
0 commit comments