+ // Remove superfluous ONEs
+ exvector::const_iterator cit = v.begin(), citend = v.end();
+ while (cit != citend) {
+ if (!is_ex_of_type(cit->op(0), diracone))
+ s.push_back(*cit);
+ cit++;
+ }
+
+ bool something_changed = false;
+ int sign = 1;
+
+ // Anticommute gamma5's to the front
+ if (s.size() >= 2) {
+ exvector::iterator first = s.begin(), next_to_last = s.end() - 2;
+ while (true) {
+ exvector::iterator it = next_to_last;
+ while (true) {
+ exvector::iterator it2 = it + 1;
+ if (!is_ex_of_type(it->op(0), diracgamma5) && is_ex_of_type(it2->op(0), diracgamma5)) {
+ it->swap(*it2);
+ sign = -sign;
+ something_changed = true;
+ }
+ if (it == first)
+ break;
+ it--;
+ }
+ if (next_to_last == first)
+ break;
+ next_to_last--;
+ }
+ }
+
+ // Remove squares of gamma5
+ while (s.size() >= 2 && is_ex_of_type(s[0].op(0), diracgamma5) && is_ex_of_type(s[1].op(0), diracgamma5)) {
+ s.erase(s.begin(), s.begin() + 2);
+ something_changed = true;
+ }
+
+ // Remove equal adjacent gammas
+ if (s.size() >= 2) {
+ exvector::iterator it = s.begin(), itend = s.end() - 1;
+ while (it != itend) {
+ ex & a = it[0];
+ ex & b = it[1];
+ if (is_ex_of_type(a.op(0), diracgamma) && is_ex_of_type(b.op(0), diracgamma)) {
+ const ex & ia = a.op(1);
+ const ex & ib = b.op(1);
+ if (ia.is_equal(ib)) {
+ a = lorentz_g(ia, ib);
+ b = dirac_ONE();
+ something_changed = true;
+ }
+ }
+ it++;
+ }