+ }
+
+ // Perform contractions
+ bool something_changed = false;
+ GINAC_ASSERT(v.size() > 1);
+ exvector::iterator it1, itend = v.end(), next_to_last = itend - 1;
+ for (it1 = v.begin(); it1 != next_to_last; it1++) {
+
+try_again:
+ if (!is_ex_of_type(*it1, indexed))
+ continue;
+
+ bool first_noncommutative = (it1->return_type() != return_types::commutative);
+
+ // Indexed factor found, get free indices and look for contraction
+ // candidates
+ exvector free1, dummy1;
+ find_free_and_dummy(ex_to<indexed>(*it1).seq.begin() + 1, ex_to<indexed>(*it1).seq.end(), free1, dummy1);
+
+ exvector::iterator it2;
+ for (it2 = it1 + 1; it2 != itend; it2++) {
+
+ if (!is_ex_of_type(*it2, indexed))
+ continue;
+
+ bool second_noncommutative = (it2->return_type() != return_types::commutative);
+
+ // Find free indices of second factor and merge them with free
+ // indices of first factor
+ exvector un;
+ find_free_and_dummy(ex_to<indexed>(*it2).seq.begin() + 1, ex_to<indexed>(*it2).seq.end(), un, dummy1);
+ un.insert(un.end(), free1.begin(), free1.end());
+
+ // Check whether the two factors share dummy indices
+ exvector free, dummy;
+ find_free_and_dummy(un, free, dummy);
+ unsigned num_dummies = dummy.size();
+ if (num_dummies == 0)
+ continue;
+
+ // At least one dummy index, is it a defined scalar product?
+ bool contracted = false;
+ if (free.empty()) {
+ if (sp.is_defined(*it1, *it2)) {
+ *it1 = sp.evaluate(*it1, *it2);
+ *it2 = _ex1();
+ goto contraction_done;
+ }
+ }
+
+ // Try to contract the first one with the second one
+ contracted = it1->op(0).bp->contract_with(it1, it2, v);
+ if (!contracted) {
+
+ // That didn't work; maybe the second object knows how to
+ // contract itself with the first one
+ contracted = it2->op(0).bp->contract_with(it2, it1, v);
+ }
+ if (contracted) {
+contraction_done:
+ if (first_noncommutative || second_noncommutative
+ || is_ex_exactly_of_type(*it1, add) || is_ex_exactly_of_type(*it2, add)
+ || is_ex_exactly_of_type(*it1, mul) || is_ex_exactly_of_type(*it2, mul)
+ || is_ex_exactly_of_type(*it1, ncmul) || is_ex_exactly_of_type(*it2, ncmul)) {
+
+ // One of the factors became a sum or product:
+ // re-expand expression and run again
+ // Non-commutative products are always re-expanded to give
+ // simplify_ncmul() the chance to re-order and canonicalize
+ // the product
+ ex r = (non_commutative ? ex(ncmul(v, true)) : ex(mul(v)));
+ return simplify_indexed(r, free_indices, dummy_indices, sp);
+ }
+
+ // Both objects may have new indices now or they might
+ // even not be indexed objects any more, so we have to
+ // start over
+ something_changed = true;
+ goto try_again;
+ }