+/** Rename dummy indices in an expression.
+ *
+ * @param e Expression to work on
+ * @param local_dummy_indices The set of dummy indices that appear in the
+ * expression "e"
+ * @param global_dummy_indices The set of dummy indices that have appeared
+ * before and which we would like to use in "e", too. This gets updated
+ * by the function */
+static ex rename_dummy_indices(const ex & e, exvector & global_dummy_indices, exvector & local_dummy_indices)
+{
+ size_t global_size = global_dummy_indices.size(),
+ local_size = local_dummy_indices.size();
+
+ // Any local dummy indices at all?
+ if (local_size == 0)
+ return e;
+
+ if (global_size < local_size) {
+
+ // More local indices than we encountered before, add the new ones
+ // to the global set
+ size_t old_global_size = global_size;
+ int remaining = local_size - global_size;
+ exvector::const_iterator it = local_dummy_indices.begin(), itend = local_dummy_indices.end();
+ while (it != itend && remaining > 0) {
+ if (find_if(global_dummy_indices.begin(), global_dummy_indices.end(), bind2nd(op0_is_equal(), *it)) == global_dummy_indices.end()) {
+ global_dummy_indices.push_back(*it);
+ global_size++;
+ remaining--;
+ }
+ it++;
+ }
+
+ // If this is the first set of local indices, do nothing
+ if (old_global_size == 0)
+ return e;
+ }
+ GINAC_ASSERT(local_size <= global_size);
+
+ // Construct vectors of index symbols
+ exvector local_syms, global_syms;
+ local_syms.reserve(local_size);
+ global_syms.reserve(local_size);
+ for (size_t i=0; i<local_size; i++)
+ local_syms.push_back(local_dummy_indices[i].op(0));
+ shaker_sort(local_syms.begin(), local_syms.end(), ex_is_less(), ex_swap());
+ for (size_t i=0; i<local_size; i++) // don't use more global symbols than necessary
+ global_syms.push_back(global_dummy_indices[i].op(0));
+ shaker_sort(global_syms.begin(), global_syms.end(), ex_is_less(), ex_swap());
+
+ // Remove common indices
+ exvector local_uniq, global_uniq;
+ set_difference(local_syms.begin(), local_syms.end(), global_syms.begin(), global_syms.end(), std::back_insert_iterator<exvector>(local_uniq), ex_is_less());
+ set_difference(global_syms.begin(), global_syms.end(), local_syms.begin(), local_syms.end(), std::back_insert_iterator<exvector>(global_uniq), ex_is_less());
+
+ // Replace remaining non-common local index symbols by global ones
+ if (local_uniq.empty())
+ return e;
+ else {
+ while (global_uniq.size() > local_uniq.size())
+ global_uniq.pop_back();
+ return e.subs(lst(local_uniq.begin(), local_uniq.end()), lst(global_uniq.begin(), global_uniq.end()), subs_options::no_pattern);
+ }
+}
+
+/** Given a set of indices, extract those of class varidx. */
+static void find_variant_indices(const exvector & v, exvector & variant_indices)
+{
+ exvector::const_iterator it1, itend;
+ for (it1 = v.begin(), itend = v.end(); it1 != itend; ++it1) {
+ if (is_exactly_a<varidx>(*it1))
+ variant_indices.push_back(*it1);
+ }
+}
+
+/** Raise/lower dummy indices in a single indexed objects to canonicalize their
+ * variance.
+ *
+ * @param e Object to work on
+ * @param variant_dummy_indices The set of indices that might need repositioning (will be changed by this function)
+ * @param moved_indices The set of indices that have been repositioned (will be changed by this function)
+ * @return true if 'e' was changed */
+bool reposition_dummy_indices(ex & e, exvector & variant_dummy_indices, exvector & moved_indices)
+{
+ bool something_changed = false;
+
+ // If a dummy index is encountered for the first time in the
+ // product, pull it up, otherwise, pull it down
+ exvector::const_iterator it2, it2start, it2end;
+ for (it2start = ex_to<indexed>(e).seq.begin(), it2end = ex_to<indexed>(e).seq.end(), it2 = it2start + 1; it2 != it2end; ++it2) {
+ if (!is_exactly_a<varidx>(*it2))
+ continue;
+
+ exvector::iterator vit, vitend;
+ for (vit = variant_dummy_indices.begin(), vitend = variant_dummy_indices.end(); vit != vitend; ++vit) {
+ if (it2->op(0).is_equal(vit->op(0))) {
+ if (ex_to<varidx>(*it2).is_covariant()) {
+ e = e.subs(lst(
+ *it2 == ex_to<varidx>(*it2).toggle_variance(),
+ ex_to<varidx>(*it2).toggle_variance() == *it2
+ ), subs_options::no_pattern);
+ something_changed = true;
+ it2 = ex_to<indexed>(e).seq.begin() + (it2 - it2start);
+ it2start = ex_to<indexed>(e).seq.begin();
+ it2end = ex_to<indexed>(e).seq.end();
+ }
+ moved_indices.push_back(*vit);
+ variant_dummy_indices.erase(vit);
+ goto next_index;
+ }
+ }
+
+ for (vit = moved_indices.begin(), vitend = moved_indices.end(); vit != vitend; ++vit) {
+ if (it2->op(0).is_equal(vit->op(0))) {
+ if (ex_to<varidx>(*it2).is_contravariant()) {
+ e = e.subs(*it2 == ex_to<varidx>(*it2).toggle_variance(), subs_options::no_pattern);
+ something_changed = true;
+ it2 = ex_to<indexed>(e).seq.begin() + (it2 - it2start);
+ it2start = ex_to<indexed>(e).seq.begin();
+ it2end = ex_to<indexed>(e).seq.end();
+ }
+ goto next_index;
+ }
+ }
+
+next_index: ;
+ }
+
+ return something_changed;
+}
+
+/* Ordering that only compares the base expressions of indexed objects. */
+struct ex_base_is_less : public std::binary_function<ex, ex, bool> {
+ bool operator() (const ex &lh, const ex &rh) const
+ {
+ return (is_a<indexed>(lh) ? lh.op(0) : lh).compare(is_a<indexed>(rh) ? rh.op(0) : rh) < 0;
+ }
+};
+