+/** 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_indices(void) const
+{
+ GINAC_ASSERT(seq.size() >= 1);
+ return exvector(seq.begin() + 1, seq.end());
+}
+
+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_dummy_indices(const indexed & other) const
+{
+ exvector indices = get_free_indices();
+ exvector other_indices = other.get_free_indices();
+ indices.insert(indices.end(), other_indices.begin(), other_indices.end());
+ exvector dummy_indices;
+ find_dummy_indices(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, 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, 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(); j++)
+ 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, 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;
+
+ // 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);
+ 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;
+ }
+ }
+
+ // Contraction of symmetric with antisymmetric object is zero
+ if ((ex_to_indexed(*it1).symmetry == indexed::symmetric &&
+ ex_to_indexed(*it2).symmetry == indexed::antisymmetric
+ || ex_to_indexed(*it1).symmetry == indexed::antisymmetric &&
+ ex_to_indexed(*it2).symmetry == indexed::symmetric)
+ && dummy.size() > 1) {
+ free_indices.clear();
+ return _ex0();
+ }
+
+ // 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, free_indices, dummy_indices);
+
+ ex r;
+ if (something_changed) {
+ if (non_commutative)
+ r = ncmul(v);
+ else
+ r = mul(v);
+ } else
+ r = e;
+
+ // Product of indexed object with a scalar?
+ if (is_ex_exactly_of_type(r, mul) && r.nops() == 2
+ && is_ex_exactly_of_type(r.op(1), numeric) && is_ex_of_type(r.op(0), indexed))
+ return r.op(0).op(0).bp->scalar_mul_indexed(r.op(0), ex_to_numeric(r.op(1)));
+ else
+ return r;
+}
+
+/** 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)) {
+ bool first = true;
+ ex sum = _ex0();
+ free_indices.clear();
+
+ for (unsigned i=0; i<e_expanded.nops(); i++) {
+ exvector free_indices_of_term;
+ ex term = simplify_indexed(e_expanded.op(i), free_indices_of_term, sp);
+ if (!term.is_zero()) {
+ if (first) {
+ free_indices = free_indices_of_term;
+ sum = term;
+ first = false;
+ } else {
+ if (!indices_consistent(free_indices, free_indices_of_term))
+ throw (std::runtime_error("simplify_indexed: inconsistent indices in sum"));
+ if (is_ex_of_type(sum, indexed) && is_ex_of_type(term, indexed))
+ sum = sum.op(0).bp->add_indexed(sum, term);
+ else
+ sum += term;
+ }
+ }
+ }
+
+ 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);
+}