+/** Given a vector of indices, split them into two vectors, one containing
+ * the free indices, the other containing the dummy indices. */
+static void find_free_and_dummy(exvector::const_iterator it, exvector::const_iterator itend, exvector & out_free, exvector & out_dummy)
+{
+ out_free.clear();
+ out_dummy.clear();
+
+ // No indices? Then do nothing
+ if (it == itend)
+ return;
+
+ // Only one index? Then it is a free one if it's not numeric
+ if (itend - it == 1) {
+ if (ex_to_idx(*it).is_symbolic())
+ out_free.push_back(*it);
+ return;
+ }
+
+ // Sort index vector. This will cause dummy indices come to lie next
+ // to each other (because the sort order is defined to guarantee this).
+ exvector v(it, itend);
+ sort_index_vector(v);
+
+ // Find dummy pairs and free indices
+ it = v.begin(); itend = v.end();
+ exvector::const_iterator last = it++;
+ while (it != itend) {
+ if (is_dummy_pair(*it, *last)) {
+ out_dummy.push_back(*last);
+ it++;
+ if (it == itend)
+ return;
+ } else {
+ if (!it->is_equal(*last) && ex_to_idx(*last).is_symbolic())
+ out_free.push_back(*last);
+ }
+ last = it++;
+ }
+ if (ex_to_idx(*last).is_symbolic())
+ out_free.push_back(*last);
+}
+
+/** Check whether two sorted index vectors are consistent (i.e. equal). */
+static bool indices_consistent(const exvector & v1, const exvector & v2)
+{
+ // Number of indices must be the same
+ if (v1.size() != v2.size())
+ return false;
+
+ // And also the indices themselves
+ exvector::const_iterator ait = v1.begin(), aitend = v1.end(),
+ bit = v2.begin(), bitend = v2.end();
+ while (ait != aitend) {
+ if (!ait->is_equal(*bit))
+ return false;
+ ait++; bit++;
+ }
+ return true;
+}
+
+exvector indexed::get_dummy_indices(void) const
+{
+ exvector free_indices, dummy_indices;
+ find_free_and_dummy(seq.begin() + 1, seq.end(), free_indices, dummy_indices);
+ return dummy_indices;
+}
+
+exvector indexed::get_free_indices(void) const
+{
+ exvector free_indices, dummy_indices;
+ find_free_and_dummy(seq.begin() + 1, seq.end(), free_indices, dummy_indices);
+ return free_indices;
+}
+
+exvector add::get_free_indices(void) const
+{
+ exvector free_indices;
+ for (unsigned i=0; i<nops(); i++) {
+ if (i == 0)
+ free_indices = op(i).get_free_indices();
+ else {
+ exvector free_indices_of_term = op(i).get_free_indices();
+ if (!indices_consistent(free_indices, free_indices_of_term))
+ throw (std::runtime_error("add::get_free_indices: inconsistent indices in sum"));
+ }
+ }
+ return free_indices;
+}
+
+exvector mul::get_free_indices(void) const
+{
+ // Concatenate free indices of all factors
+ exvector un;
+ for (unsigned i=0; i<nops(); i++) {
+ exvector free_indices_of_factor = op(i).get_free_indices();
+ un.insert(un.end(), free_indices_of_factor.begin(), free_indices_of_factor.end());
+ }
+
+ // And remove the dummy indices
+ exvector free_indices, dummy_indices;
+ find_free_and_dummy(un.begin(), un.end(), free_indices, dummy_indices);
+ return free_indices;
+}
+
+exvector ncmul::get_free_indices(void) const
+{
+ // Concatenate free indices of all factors
+ exvector un;
+ for (unsigned i=0; i<nops(); i++) {
+ exvector free_indices_of_factor = op(i).get_free_indices();
+ un.insert(un.end(), free_indices_of_factor.begin(), free_indices_of_factor.end());
+ }
+
+ // And remove the dummy indices
+ exvector free_indices, dummy_indices;
+ find_free_and_dummy(un.begin(), un.end(), free_indices, dummy_indices);
+ return free_indices;
+}
+
+exvector power::get_free_indices(void) const
+{
+ // Return free indices of basis
+ return basis.get_free_indices();
+}
+
+/** 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, const scalar_products & sp)
+{
+ // Remember whether the product was commutative or noncommutative
+ // (because we chop it into factors and need to reassemble later)
+ bool non_commutative = is_ex_exactly_of_type(e, ncmul);
+
+ // Collect factors in an exvector, store squares twice
+ exvector v;
+ v.reserve(e.nops() * 2);
+
+ if (is_ex_exactly_of_type(e, power)) {
+ // 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 (int i=0; i<e.nops(); i++) {
+ ex f = e.op(i);
+ if (is_ex_exactly_of_type(f, power) && f.op(1).is_equal(_ex2())) {
+ v.push_back(f.op(0));
+ v.push_back(f.op(0));
+ } else if (is_ex_exactly_of_type(f, ncmul)) {
+ // Noncommutative factor found, split it as well
+ non_commutative = true; // everything becomes noncommutative, ncmul will sort out the commutative factors later
+ for (int j=0; j<f.nops(); i++)
+ v.push_back(f.op(j));
+ } else
+ v.push_back(f);
+ }
+ }
+
+ // 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;
+
+ // Indexed factor found, look for contraction candidates
+ exvector::iterator it2;
+ for (it2 = it1 + 1; it2 != itend; it2++) {
+
+ if (!is_ex_of_type(*it2, indexed))
+ continue;
+
+ // Check whether the two factors share dummy indices
+ exvector un(ex_to_indexed(*it1).seq.begin() + 1, ex_to_indexed(*it1).seq.end());
+ un.insert(un.end(), ex_to_indexed(*it2).seq.begin() + 1, ex_to_indexed(*it2).seq.end());
+ exvector free, dummy;
+ find_free_and_dummy(un.begin(), un.end(), free, dummy);
+ if (dummy.size() == 0)
+ continue;
+
+ // At least one dummy index, is it a defined scalar product?
+ if (free.size() == 0) {
+ if (sp.is_defined(*it1, *it2)) {
+ *it1 = sp.evaluate(*it1, *it2);
+ *it2 = _ex1();
+ something_changed = true;
+ goto try_again;
+ }
+ }
+
+ // Try to contract the first one with the second one
+ bool 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) {
+ something_changed = true;
+
+ // Both objects may have new indices now or they might
+ // even not be indexed objects any more, so we have to
+ // start over
+ goto try_again;
+ }
+ }
+ }
+
+ // Find free indices (concatenate them all and call find_free_and_dummy())
+ exvector un, dummy_indices;
+ it1 = v.begin(); itend = v.end();
+ while (it1 != itend) {
+ if (is_ex_of_type(*it1, indexed)) {
+ const indexed & o = ex_to_indexed(*it1);
+ un.insert(un.end(), o.seq.begin() + 1, o.seq.end());
+ }
+ it1++;
+ }
+ find_free_and_dummy(un.begin(), un.end(), free_indices, dummy_indices);
+
+ if (something_changed) {
+ if (non_commutative)
+ return ncmul(v);
+ else
+ return mul(v);
+ } else
+ return e;
+}
+
+/** Simplify indexed expression, return list of free indices. */
+ex simplify_indexed(const ex & e, exvector & free_indices, const scalar_products & sp)
+{
+ // Expand the expression
+ ex e_expanded = e.expand();
+
+ // Simplification of single indexed object: just find the free indices
+ if (is_ex_of_type(e_expanded, indexed)) {
+ const indexed &i = ex_to_indexed(e_expanded);
+ exvector dummy_indices;
+ find_free_and_dummy(i.seq.begin() + 1, i.seq.end(), free_indices, dummy_indices);
+ return e_expanded;
+ }
+
+ // Simplification of sum = sum of simplifications, check consistency of
+ // free indices in each term
+ if (is_ex_exactly_of_type(e_expanded, add)) {
+ ex sum = _ex0();
+
+ for (unsigned i=0; i<e_expanded.nops(); i++) {
+ exvector free_indices_of_term;
+ sum += simplify_indexed(e_expanded.op(i), free_indices_of_term, sp);
+ if (i == 0)
+ free_indices = free_indices_of_term;
+ else if (!indices_consistent(free_indices, free_indices_of_term))
+ throw (std::runtime_error("simplify_indexed: inconsistent indices in sum"));
+ }
+
+ return sum;
+ }
+
+ // Simplification of products
+ if (is_ex_exactly_of_type(e_expanded, mul)
+ || is_ex_exactly_of_type(e_expanded, ncmul)
+ || (is_ex_exactly_of_type(e_expanded, power) && is_ex_of_type(e_expanded.op(0), indexed) && e_expanded.op(1).is_equal(_ex2())))
+ return simplify_indexed_product(e_expanded, free_indices, sp);
+
+ // Cannot do anything
+ free_indices.clear();
+ return e_expanded;
+}
+
+ex simplify_indexed(const ex & e)
+{
+ exvector free_indices;
+ scalar_products sp;
+ return simplify_indexed(e, free_indices, sp);
+}
+
+ex simplify_indexed(const ex & e, const scalar_products & sp)
+{
+ exvector free_indices;
+ return simplify_indexed(e, free_indices, sp);
+}