+ if (is_ex_of_type(e, clifford)) {
+
+ if (ex_to_clifford(e).get_representation_label() == rl
+ && is_ex_of_type(e.op(0), diracone))
+ return _ex4();
+ else
+ return _ex0();
+
+ } else if (is_ex_exactly_of_type(e, add)) {
+
+ // Trace of sum = sum of traces
+ ex sum = _ex0();
+ for (unsigned i=0; i<e.nops(); i++)
+ sum += dirac_trace(e.op(i), rl);
+ return sum;
+
+ } else if (is_ex_exactly_of_type(e, mul)) {
+
+ // Trace of product: pull out non-clifford factors
+ ex prod = _ex1();
+ for (unsigned i=0; i<e.nops(); i++) {
+ const ex &o = e.op(i);
+ if (is_ex_of_type(o, clifford)
+ && ex_to_clifford(o).get_representation_label() == rl)
+ prod *= dirac_trace(o, rl);
+ else if (is_ex_of_type(o, ncmul)
+ && is_ex_of_type(o.op(0), clifford)
+ && ex_to_clifford(o.op(0)).get_representation_label() == rl)
+ prod *= dirac_trace(o, rl);
+ else
+ prod *= o;
+ }
+ return prod;
+
+ } else if (is_ex_exactly_of_type(e, ncmul)) {
+
+ if (!is_ex_of_type(e.op(0), clifford)
+ || ex_to_clifford(e.op(0)).get_representation_label() != rl)
+ return _ex0();
+
+ // gamma5 gets moved to the front so this check is enough
+ bool has_gamma5 = is_ex_of_type(e.op(0).op(0), diracgamma5);
+ unsigned num = e.nops();
+
+ if (has_gamma5) {
+
+ // Trace of gamma5 * odd number of gammas and trace of
+ // gamma5 * gamma_mu * gamma_nu are zero
+ if ((num & 1) == 0 || num == 2)
+ return _ex0();
+
+ } else { // no gamma5
+
+ // Trace of odd number of gammas is zero
+ if ((num & 1) == 1)
+ return _ex0();
+
+ // Tr gamma_mu gamma_nu = 4 g_mu_nu
+ if (num == 2)
+ return 4 * lorentz_g(e.op(0).op(1), e.op(1).op(1));
+ }
+
+ throw (std::logic_error("dirac_trace: don't know how to compute trace"));
+ }