+ // Filter out the dummy indices with variance
+ exvector variant_dummy_indices;
+ for (it1 = local_dummy_indices.begin(), itend = local_dummy_indices.end(); it1 != itend; ++it1) {
+ if (is_exactly_a<varidx>(*it1))
+ variant_dummy_indices.push_back(*it1);
+ }
+
+ // Any indices with variance present at all?
+ if (!variant_dummy_indices.empty()) {
+
+ // Yes, bring the product into a canonical order that only depends on
+ // the base expressions of indexed objects
+ if (!non_commutative)
+ std::sort(v.begin(), v.end(), ex_base_is_less());
+
+ exvector moved_indices;
+
+ // Iterate over all indexed objects in the product
+ for (it1 = v.begin(), itend = v.end(); it1 != itend; ++it1) {
+ if (!is_ex_of_type(*it1, indexed))
+ continue;
+
+ ex new_it1;
+ bool it1_dirty = false; // It this is true, then new_it1 holds a new value for *it1
+
+ // If a dummy index is encountered for the first time in the
+ // product, pull it up, otherwise, pull it down
+ exvector::iterator it2, it2end;
+ for (it2 = const_cast<indexed &>(ex_to<indexed>(*it1)).seq.begin(), it2end = const_cast<indexed &>(ex_to<indexed>(*it1)).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()) {
+ new_it1 = (it1_dirty ? new_it1 : *it1).subs(*it2 == ex_to<varidx>(*it2).toggle_variance());
+ it1_dirty = true;
+ 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()) {
+ new_it1 = (it1_dirty ? new_it1 : *it1).subs(*it2 == ex_to<varidx>(*it2).toggle_variance());
+ it1_dirty = true;
+ something_changed = true;
+ }
+ goto next_index;
+ }
+ }
+
+next_index: ;
+ }
+
+ if (it1_dirty)
+ *it1 = new_it1;
+ }
+ }
+