+exvector get_all_dummy_indices_safely(const ex & e)
+{
+ if (is_a<indexed>(e))
+ return ex_to<indexed>(e).get_dummy_indices();
+ else if (is_a<power>(e) && e.op(1)==2) {
+ return e.op(0).get_free_indices();
+ }
+ else if (is_a<mul>(e) || is_a<ncmul>(e)) {
+ exvector dummies;
+ exvector free_indices;
+ for (int i=0; i<e.nops(); ++i) {
+ exvector dummies_of_factor = get_all_dummy_indices_safely(e.op(i));
+ dummies.insert(dummies.end(), dummies_of_factor.begin(),
+ dummies_of_factor.end());
+ exvector free_of_factor = e.op(i).get_free_indices();
+ free_indices.insert(free_indices.begin(), free_of_factor.begin(),
+ free_of_factor.end());
+ }
+ exvector free_out, dummy_out;
+ find_free_and_dummy(free_indices.begin(), free_indices.end(), free_out,
+ dummy_out);
+ dummies.insert(dummies.end(), dummy_out.begin(), dummy_out.end());
+ return dummies;
+ }
+ else if(is_a<add>(e)) {
+ exvector result;
+ for(int i=0; i<e.nops(); ++i) {
+ exvector dummies_of_term = get_all_dummy_indices_safely(e.op(i));
+ sort(dummies_of_term.begin(), dummies_of_term.end());
+ exvector new_vec;
+ set_union(result.begin(), result.end(), dummies_of_term.begin(),
+ dummies_of_term.end(), std::back_inserter<exvector>(new_vec),
+ ex_is_less());
+ result.swap(new_vec);
+ }
+ return result;
+ }
+ return exvector();
+}
+
+/** Returns all dummy indices from the exvector */
+exvector get_all_dummy_indices(const ex & e)
+{
+ exvector p;
+ bool nc;
+ product_to_exvector(e, p, nc);
+ exvector::const_iterator ip = p.begin(), ipend = p.end();
+ exvector v, v1;
+ while (ip != ipend) {
+ if (is_a<indexed>(*ip)) {
+ v1 = ex_to<indexed>(*ip).get_dummy_indices();
+ v.insert(v.end(), v1.begin(), v1.end());
+ exvector::const_iterator ip1 = ip+1;
+ while (ip1 != ipend) {
+ if (is_a<indexed>(*ip1)) {
+ v1 = ex_to<indexed>(*ip).get_dummy_indices(ex_to<indexed>(*ip1));
+ v.insert(v.end(), v1.begin(), v1.end());
+ }
+ ++ip1;
+ }
+ }
+ ++ip;
+ }
+ return v;
+}
+
+lst rename_dummy_indices_uniquely(const exvector & va, const exvector & vb)
+{
+ exvector common_indices;
+ set_intersection(va.begin(), va.end(), vb.begin(), vb.end(), std::back_insert_iterator<exvector>(common_indices), ex_is_less());
+ if (common_indices.empty()) {
+ return lst(lst(), lst());
+ } else {
+ exvector new_indices, old_indices;
+ old_indices.reserve(2*common_indices.size());
+ new_indices.reserve(2*common_indices.size());
+ exvector::const_iterator ip = common_indices.begin(), ipend = common_indices.end();
+ while (ip != ipend) {
+ ex newsym=(new symbol)->setflag(status_flags::dynallocated);
+ ex newidx;
+ if(is_exactly_a<spinidx>(*ip))
+ newidx = (new spinidx(newsym, ex_to<spinidx>(*ip).get_dim(),
+ ex_to<spinidx>(*ip).is_covariant(),
+ ex_to<spinidx>(*ip).is_dotted()))
+ -> setflag(status_flags::dynallocated);
+ else if (is_exactly_a<varidx>(*ip))
+ newidx = (new varidx(newsym, ex_to<varidx>(*ip).get_dim(),
+ ex_to<varidx>(*ip).is_covariant()))
+ -> setflag(status_flags::dynallocated);
+ else
+ newidx = (new idx(newsym, ex_to<idx>(*ip).get_dim()))
+ -> setflag(status_flags::dynallocated);
+ old_indices.push_back(*ip);
+ new_indices.push_back(newidx);
+ if(is_a<varidx>(*ip)) {
+ old_indices.push_back(ex_to<varidx>(*ip).toggle_variance());
+ new_indices.push_back(ex_to<varidx>(newidx).toggle_variance());
+ }
+ ++ip;
+ }
+ return lst(lst(old_indices.begin(), old_indices.end()), lst(new_indices.begin(), new_indices.end()));
+ }
+}
+
+ex rename_dummy_indices_uniquely(const exvector & va, const exvector & vb, const ex & b)
+{
+ lst indices_subs = rename_dummy_indices_uniquely(va, vb);
+ return (indices_subs.op(0).nops()>0 ? b.subs(ex_to<lst>(indices_subs.op(0)), ex_to<lst>(indices_subs.op(1)), subs_options::no_pattern|subs_options::no_index_renaming) : b);
+}
+
+ex rename_dummy_indices_uniquely(const ex & a, const ex & b)
+{
+ exvector va = get_all_dummy_indices_safely(a);
+ if (va.size() > 0) {
+ exvector vb = get_all_dummy_indices_safely(b);
+ if (vb.size() > 0) {
+ sort(va.begin(), va.end(), ex_is_less());
+ sort(vb.begin(), vb.end(), ex_is_less());
+ lst indices_subs = rename_dummy_indices_uniquely(va, vb);
+ if (indices_subs.op(0).nops() > 0)
+ return b.subs(ex_to<lst>(indices_subs.op(0)), ex_to<lst>(indices_subs.op(1)), subs_options::no_pattern|subs_options::no_index_renaming);
+ }
+ }
+ return b;
+}
+
+ex rename_dummy_indices_uniquely(exvector & va, const ex & b, bool modify_va)
+{
+ if (va.size() > 0) {
+ exvector vb = get_all_dummy_indices_safely(b);
+ if (vb.size() > 0) {
+ sort(vb.begin(), vb.end(), ex_is_less());
+ lst indices_subs = rename_dummy_indices_uniquely(va, vb);
+ if (indices_subs.op(0).nops() > 0) {
+ if (modify_va) {
+ for (lst::const_iterator i = ex_to<lst>(indices_subs.op(1)).begin(); i != ex_to<lst>(indices_subs.op(1)).end(); ++i)
+ va.push_back(*i);
+ exvector uncommon_indices;
+ set_difference(vb.begin(), vb.end(), indices_subs.op(0).begin(), indices_subs.op(0).end(), std::back_insert_iterator<exvector>(uncommon_indices), ex_is_less());
+ exvector::const_iterator ip = uncommon_indices.begin(), ipend = uncommon_indices.end();
+ while (ip != ipend) {
+ va.push_back(*ip);
+ ++ip;
+ }
+ sort(va.begin(), va.end(), ex_is_less());
+ }
+ return b.subs(ex_to<lst>(indices_subs.op(0)), ex_to<lst>(indices_subs.op(1)), subs_options::no_pattern|subs_options::no_index_renaming);
+ }
+ }
+ }
+ return b;
+}
+
+ex expand_dummy_sum(const ex & e, bool subs_idx)
+{
+ ex e_expanded = e.expand();
+ pointer_to_map_function_1arg<bool> fcn(expand_dummy_sum, subs_idx);
+ if (is_a<add>(e_expanded) || is_a<lst>(e_expanded) || is_a<matrix>(e_expanded)) {
+ return e_expanded.map(fcn);
+ } else if (is_a<ncmul>(e_expanded) || is_a<mul>(e_expanded) || is_a<power>(e_expanded) || is_a<indexed>(e_expanded)) {
+ exvector v;
+ if (is_a<indexed>(e_expanded))
+ v = ex_to<indexed>(e_expanded).get_dummy_indices();
+ else
+ v = get_all_dummy_indices(e_expanded);
+ ex result = e_expanded;
+ for(exvector::const_iterator it=v.begin(); it!=v.end(); ++it) {
+ ex nu = *it;
+ if (ex_to<idx>(nu).get_dim().info(info_flags::nonnegint)) {
+ int idim = ex_to<numeric>(ex_to<idx>(nu).get_dim()).to_int();
+ ex en = 0;
+ for (int i=0; i < idim; i++) {
+ if (subs_idx && is_a<varidx>(nu)) {
+ ex other = ex_to<varidx>(nu).toggle_variance();
+ en += result.subs(lst(
+ nu == idx(i, idim),
+ other == idx(i, idim)
+ ));
+ } else {
+ en += result.subs( nu.op(0) == i );
+ }
+ }
+ result = en;
+ }
+ }
+ return result;
+ } else {
+ return e;
+ }
+}
+