+ex symmetrize_cyclic(const ex & e, exvector::const_iterator first, exvector::const_iterator last)
+{
+ // Need at least 2 objects for this operation
+ int num = last - first;
+ if (num < 2)
+ return e;
+
+ // Transform object vector to a list
+ exlist iv_lst;
+ iv_lst.insert(iv_lst.begin(), first, last);
+ lst orig_lst(iv_lst, true);
+ lst new_lst = orig_lst;
+
+ // Loop over all cyclic permutations (the first permutation, which is
+ // the identity, is unrolled)
+ ex sum = e;
+ for (unsigned i=0; i<num-1; i++) {
+ ex perm = new_lst.op(0);
+ new_lst.remove_first().append(perm);
+ sum += e.subs(orig_lst, new_lst);
+ }
+ return sum / num;
+}
+