+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;
+ }
+ }
+
+ // 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.begin(), un.end(), 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);
+}
+
+//////////
+// helper classes
+//////////
+
+void scalar_products::add(const ex & v1, const ex & v2, const ex & sp)
+{
+ spm[make_key(v1, v2)] = sp;
+}
+
+void scalar_products::clear(void)
+{
+ spm.clear();
+}
+
+/** Check whether scalar product pair is defined. */
+bool scalar_products::is_defined(const ex & v1, const ex & v2) const
+{
+ return spm.find(make_key(v1, v2)) != spm.end();
+}
+
+/** Return value of defined scalar product pair. */
+ex scalar_products::evaluate(const ex & v1, const ex & v2) const
+{
+ return spm.find(make_key(v1, v2))->second;
+}
+
+void scalar_products::debugprint(void) const
+{
+ std::cerr << "map size=" << spm.size() << std::endl;
+ for (spmap::const_iterator cit=spm.begin(); cit!=spm.end(); ++cit) {
+ const spmapkey & k = cit->first;
+ std::cerr << "item key=(" << k.first << "," << k.second;
+ std::cerr << "), value=" << cit->second << std::endl;
+ }
+}
+
+/** Make key from object pair. */
+spmapkey scalar_products::make_key(const ex & v1, const ex & v2)
+{
+ // If indexed, extract base objects
+ ex s1 = is_ex_of_type(v1, indexed) ? v1.op(0) : v1;
+ ex s2 = is_ex_of_type(v2, indexed) ? v2.op(0) : v2;
+
+ // Enforce canonical order in pair
+ if (s1.compare(s2) > 0)
+ return spmapkey(s2, s1);
+ else
+ return spmapkey(s1, s2);
+}
+