+template<class T> size_t number_of_type(const exvector&v)
+{
+ size_t number = 0;
+ for(exvector::const_iterator i=v.begin(); i!=v.end(); ++i)
+ if(is_exactly_a<T>(*i))
+ ++number;
+ return number;
+}
+
+/** 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 */
+template<class T> static ex rename_dummy_indices(const ex & e, exvector & global_dummy_indices, exvector & local_dummy_indices)
+{
+ size_t global_size = number_of_type<T>(global_dummy_indices),
+ local_size = number_of_type<T>(local_dummy_indices);
+
+ // 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 (is_exactly_a<T>(*it) && find_if(global_dummy_indices.begin(), global_dummy_indices.end(), bind2nd(idx_is_equal_ignore_dim(), *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; local_syms.size()!=local_size; i++)
+ if(is_exactly_a<T>(local_dummy_indices[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; global_syms.size()!=local_size; i++) // don't use more global symbols than necessary
+ if(is_exactly_a<T>(global_dummy_indices[i]))
+ 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;
+
+ // Find dummy symbols that occur twice in the same indexed object.
+ exvector local_var_dummies;
+ local_var_dummies.reserve(e.nops()/2);
+ for (size_t i=1; i<e.nops(); ++i) {
+ if (!is_a<varidx>(e.op(i)))
+ continue;
+ for (size_t j=i+1; j<e.nops(); ++j) {
+ if (is_dummy_pair(e.op(i), e.op(j))) {
+ local_var_dummies.push_back(e.op(i));
+ for (exvector::iterator k = variant_dummy_indices.begin();
+ k!=variant_dummy_indices.end(); ++k) {
+ if (e.op(i).op(0) == k->op(0)) {
+ variant_dummy_indices.erase(k);
+ break;
+ }
+ }
+ break;
+ }
+ }
+ }
+
+ // In the case where a dummy symbol occurs twice in the same indexed object
+ // we try all posibilities of raising/lowering and keep the least one in
+ // the sense of ex_is_less.
+ ex optimal_e = e;
+ size_t numpossibs = 1 << local_var_dummies.size();
+ for (size_t i=0; i<numpossibs; ++i) {
+ ex try_e = e;
+ for (size_t j=0; j<local_var_dummies.size(); ++j) {
+ exmap m;
+ if (1<<j & i) {
+ ex curr_idx = local_var_dummies[j];
+ ex curr_toggle = ex_to<varidx>(curr_idx).toggle_variance();
+ m[curr_idx] = curr_toggle;
+ m[curr_toggle] = curr_idx;
+ }
+ try_e = e.subs(m, subs_options::no_pattern);
+ }
+ if(ex_is_less()(try_e, optimal_e))
+ { optimal_e = try_e;
+ something_changed = true;
+ }
+ }
+ e = optimal_e;
+
+ if (!is_a<indexed>(e))
+ return true;
+
+ exvector seq = ex_to<indexed>(e).seq;
+
+ // If a dummy index is encountered for the first time in the
+ // product, pull it up, otherwise, pull it down
+ for (exvector::iterator it2 = seq.begin()+1, it2end = seq.end();
+ 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()) {
+ /*
+ * N.B. we don't want to use
+ *
+ * e = e.subs(lst(
+ * *it2 == ex_to<varidx>(*it2).toggle_variance(),
+ * ex_to<varidx>(*it2).toggle_variance() == *it2
+ * ), subs_options::no_pattern);
+ *
+ * since this can trigger non-trivial repositioning of indices,
+ * e.g. due to non-trivial symmetry properties of e, thus
+ * invalidating iterators
+ */
+ *it2 = ex_to<varidx>(*it2).toggle_variance();
+ something_changed = true;
+ }
+ 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()) {
+ *it2 = ex_to<varidx>(*it2).toggle_variance();
+ something_changed = true;
+ }
+ goto next_index;
+ }
+ }
+
+next_index: ;
+ }
+
+ if (something_changed)
+ e = ex_to<indexed>(e).thiscontainer(seq);
+
+ 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;
+ }
+};
+
+/* An auxiliary function used by simplify_indexed() and expand_dummy_sum()
+ * It returns an exvector of factors from the supplied product */
+static void product_to_exvector(const ex & e, exvector & v, bool & non_commutative)