+ 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;
+ }
+};
+
+/* 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)
+{
+ // Remember whether the product was commutative or noncommutative
+ // (because we chop it into factors and need to reassemble later)
+ non_commutative = is_exactly_a<ncmul>(e);
+
+ // Collect factors in an exvector, store squares twice
+ v.reserve(e.nops() * 2);
+
+ if (is_exactly_a<power>(e)) {
+ // We only get called for simple squares, split a^2 -> a*a
+ GINAC_ASSERT(e.op(1).is_equal(_ex2));
+ v.push_back(e.op(0));
+ v.push_back(e.op(0));
+ } else {
+ for (size_t i=0; i<e.nops(); i++) {
+ ex f = e.op(i);
+ if (is_exactly_a<power>(f) && f.op(1).is_equal(_ex2)) {
+ v.push_back(f.op(0));
+ v.push_back(f.op(0));
+ } else if (is_exactly_a<ncmul>(f)) {
+ // Noncommutative factor found, split it as well
+ non_commutative = true; // everything becomes noncommutative, ncmul will sort out the commutative factors later
+ for (size_t j=0; j<f.nops(); j++)
+ v.push_back(f.op(j));
+ } else
+ v.push_back(f);
+ }
+ }
+}
+
+/** Simplify product of indexed expressions (commutative, noncommutative and
+ * simple squares), return list of free indices. */
+ex simplify_indexed_product(const ex & e, exvector & free_indices, exvector & dummy_indices, const scalar_products & sp)
+{
+ // Collect factors in an exvector
+ exvector v;
+
+ // Remember whether the product was commutative or noncommutative
+ // (because we chop it into factors and need to reassemble later)
+ bool non_commutative;
+ product_to_exvector(e, v, non_commutative);
+
+ // 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_a<indexed>(*it1))
+ 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_a<indexed>(*it2))
+ 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);
+ size_t 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()) {
+
+ // Find minimal dimension of all indices of both factors
+ exvector::const_iterator dit = ex_to<indexed>(*it1).seq.begin() + 1, ditend = ex_to<indexed>(*it1).seq.end();
+ ex dim = ex_to<idx>(*dit).get_dim();
+ ++dit;
+ for (; dit != ditend; ++dit) {
+ dim = minimal_dim(dim, ex_to<idx>(*dit).get_dim());
+ }
+ dit = ex_to<indexed>(*it2).seq.begin() + 1;
+ ditend = ex_to<indexed>(*it2).seq.end();
+ for (; dit != ditend; ++dit) {
+ dim = minimal_dim(dim, ex_to<idx>(*dit).get_dim());
+ }
+
+ // User-defined scalar product?
+ if (sp.is_defined(*it1, *it2, dim)) {
+
+ // Yes, substitute it
+ *it1 = sp.evaluate(*it1, *it2, dim);
+ *it2 = _ex1;
+ goto contraction_done;
+ }
+ }
+
+ // Try to contract the first one with the second one
+ contracted = ex_to<basic>(it1->op(0)).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 = ex_to<basic>(it2->op(0)).contract_with(it2, it1, v);
+ }
+ if (contracted) {
+contraction_done:
+ if (first_noncommutative || second_noncommutative
+ || is_exactly_a<add>(*it1) || is_exactly_a<add>(*it2)
+ || is_exactly_a<mul>(*it1) || is_exactly_a<mul>(*it2)
+ || is_exactly_a<ncmul>(*it1) || is_exactly_a<ncmul>(*it2)) {
+
+ // One of the factors became a sum or product:
+ // re-expand expression and run again
+ // Non-commutative products are always re-expanded to give
+ // eval_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;
+ }