]> www.ginac.de Git - ginac.git/blob - ginac/clifford.cpp
print_tree: only print the "nops=" line when nops() > 0
[ginac.git] / ginac / clifford.cpp
1 /** @file clifford.cpp
2  *
3  *  Implementation of GiNaC's clifford algebra (Dirac gamma) objects. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2003 Johannes Gutenberg University Mainz, Germany
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include <iostream>
24 #include <stdexcept>
25
26 #include "clifford.h"
27 #include "ex.h"
28 #include "idx.h"
29 #include "ncmul.h"
30 #include "symbol.h"
31 #include "numeric.h" // for I
32 #include "symmetry.h"
33 #include "lst.h"
34 #include "relational.h"
35 #include "operators.h"
36 #include "mul.h"
37 #include "print.h"
38 #include "archive.h"
39 #include "utils.h"
40
41 namespace GiNaC {
42
43 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(clifford, indexed,
44   print_func<print_dflt>(&clifford::do_print_dflt).
45   print_func<print_latex>(&clifford::do_print_latex))
46
47 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(diracone, tensor,
48   print_func<print_dflt>(&diracone::do_print).
49   print_func<print_latex>(&diracone::do_print_latex))
50
51 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(diracgamma, tensor,
52   print_func<print_dflt>(&diracgamma::do_print).
53   print_func<print_latex>(&diracgamma::do_print_latex))
54
55 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(diracgamma5, tensor,
56   print_func<print_dflt>(&diracgamma5::do_print).
57   print_func<print_latex>(&diracgamma5::do_print_latex))
58
59 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(diracgammaL, tensor,
60   print_func<print_context>(&diracgammaL::do_print).
61   print_func<print_latex>(&diracgammaL::do_print_latex))
62
63 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(diracgammaR, tensor,
64   print_func<print_context>(&diracgammaR::do_print).
65   print_func<print_latex>(&diracgammaR::do_print_latex))
66
67 //////////
68 // default constructors
69 //////////
70
71 clifford::clifford() : representation_label(0)
72 {
73         tinfo_key = TINFO_clifford;
74 }
75
76 DEFAULT_CTOR(diracone)
77 DEFAULT_CTOR(diracgamma)
78 DEFAULT_CTOR(diracgamma5)
79 DEFAULT_CTOR(diracgammaL)
80 DEFAULT_CTOR(diracgammaR)
81
82 //////////
83 // other constructors
84 //////////
85
86 /** Construct object without any indices. This constructor is for internal
87  *  use only. Use the dirac_ONE() function instead.
88  *  @see dirac_ONE */
89 clifford::clifford(const ex & b, unsigned char rl) : inherited(b), representation_label(rl)
90 {
91         tinfo_key = TINFO_clifford;
92 }
93
94 /** Construct object with one Lorentz index. This constructor is for internal
95  *  use only. Use the dirac_gamma() function instead.
96  *  @see dirac_gamma */
97 clifford::clifford(const ex & b, const ex & mu, unsigned char rl) : inherited(b, mu), representation_label(rl)
98 {
99         GINAC_ASSERT(is_a<varidx>(mu));
100         tinfo_key = TINFO_clifford;
101 }
102
103 clifford::clifford(unsigned char rl, const exvector & v, bool discardable) : inherited(sy_none(), v, discardable), representation_label(rl)
104 {
105         tinfo_key = TINFO_clifford;
106 }
107
108 clifford::clifford(unsigned char rl, exvector * vp) : inherited(sy_none(), vp), representation_label(rl)
109 {
110         tinfo_key = TINFO_clifford;
111 }
112
113 //////////
114 // archiving
115 //////////
116
117 clifford::clifford(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
118 {
119         unsigned rl;
120         n.find_unsigned("label", rl);
121         representation_label = rl;
122 }
123
124 void clifford::archive(archive_node &n) const
125 {
126         inherited::archive(n);
127         n.add_unsigned("label", representation_label);
128 }
129
130 DEFAULT_UNARCHIVE(clifford)
131 DEFAULT_ARCHIVING(diracone)
132 DEFAULT_ARCHIVING(diracgamma)
133 DEFAULT_ARCHIVING(diracgamma5)
134 DEFAULT_ARCHIVING(diracgammaL)
135 DEFAULT_ARCHIVING(diracgammaR)
136
137 //////////
138 // functions overriding virtual functions from base classes
139 //////////
140
141 int clifford::compare_same_type(const basic & other) const
142 {
143         GINAC_ASSERT(is_a<clifford>(other));
144         const clifford &o = static_cast<const clifford &>(other);
145
146         if (representation_label != o.representation_label) {
147                 // different representation label
148                 return representation_label < o.representation_label ? -1 : 1;
149         }
150
151         return inherited::compare_same_type(other);
152 }
153
154 bool clifford::match_same_type(const basic & other) const
155 {
156         GINAC_ASSERT(is_a<clifford>(other));
157         const clifford &o = static_cast<const clifford &>(other);
158
159         return representation_label == o.representation_label;
160 }
161
162 static bool is_dirac_slash(const ex & seq0)
163 {
164         return !is_a<diracgamma5>(seq0) && !is_a<diracgammaL>(seq0) &&
165                !is_a<diracgammaR>(seq0) && !is_a<diracgamma>(seq0) &&
166                !is_a<diracone>(seq0);
167 }
168
169 void clifford::do_print_dflt(const print_dflt & c, unsigned level) const
170 {
171         // dirac_slash() object is printed differently
172         if (is_dirac_slash(seq[0])) {
173                 seq[0].print(c, level);
174                 c.s << "\\";
175         } else
176                 this->print_dispatch<inherited>(c, level);
177 }
178
179 void clifford::do_print_latex(const print_latex & c, unsigned level) const
180 {
181         // dirac_slash() object is printed differently
182         if (is_dirac_slash(seq[0])) {
183                 c.s << "{";
184                 seq[0].print(c, level);
185                 c.s << "\\hspace{-1.0ex}/}";
186         } else
187                 this->print_dispatch<inherited>(c, level);
188 }
189
190 DEFAULT_COMPARE(diracone)
191 DEFAULT_COMPARE(diracgamma)
192 DEFAULT_COMPARE(diracgamma5)
193 DEFAULT_COMPARE(diracgammaL)
194 DEFAULT_COMPARE(diracgammaR)
195
196 DEFAULT_PRINT_LATEX(diracone, "ONE", "\\mathbb{1}")
197 DEFAULT_PRINT_LATEX(diracgamma, "gamma", "\\gamma")
198 DEFAULT_PRINT_LATEX(diracgamma5, "gamma5", "{\\gamma^5}")
199 DEFAULT_PRINT_LATEX(diracgammaL, "gammaL", "{\\gamma_L}")
200 DEFAULT_PRINT_LATEX(diracgammaR, "gammaR", "{\\gamma_R}")
201
202 /** This function decomposes gamma~mu -> (1, mu) and a\ -> (a.ix, ix) */
203 static void base_and_index(const ex & c, ex & b, ex & i)
204 {
205         GINAC_ASSERT(is_a<clifford>(c));
206         GINAC_ASSERT(c.nops() == 2);
207
208         if (is_a<diracgamma>(c.op(0))) { // proper dirac gamma object
209                 i = c.op(1);
210                 b = _ex1;
211         } else if (is_a<diracgamma5>(c.op(0)) || is_a<diracgammaL>(c.op(0)) || is_a<diracgammaR>(c.op(0))) { // gamma5/L/R
212                 i = _ex0;
213                 b = _ex1;
214         } else { // slash object, generate new dummy index
215                 varidx ix((new symbol)->setflag(status_flags::dynallocated), ex_to<idx>(c.op(1)).get_dim());
216                 b = indexed(c.op(0), ix.toggle_variance());
217                 i = ix;
218         }
219 }
220
221 /** Contraction of a gamma matrix with something else. */
222 bool diracgamma::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
223 {
224         GINAC_ASSERT(is_a<clifford>(*self));
225         GINAC_ASSERT(is_a<indexed>(*other));
226         GINAC_ASSERT(is_a<diracgamma>(self->op(0)));
227         unsigned char rl = ex_to<clifford>(*self).get_representation_label();
228
229         ex dim = ex_to<idx>(self->op(1)).get_dim();
230         if (other->nops() > 1)
231                 dim = minimal_dim(dim, ex_to<idx>(other->op(1)).get_dim());
232
233         if (is_a<clifford>(*other)) {
234
235                 // Contraction only makes sense if the represenation labels are equal
236                 if (ex_to<clifford>(*other).get_representation_label() != rl)
237                         return false;
238
239                 // gamma~mu gamma.mu = dim ONE
240                 if (other - self == 1) {
241                         *self = dim;
242                         *other = dirac_ONE(rl);
243                         return true;
244
245                 // gamma~mu gamma~alpha gamma.mu = (2-dim) gamma~alpha
246                 } else if (other - self == 2
247                         && is_a<clifford>(self[1])) {
248                         *self = 2 - dim;
249                         *other = _ex1;
250                         return true;
251
252                 // gamma~mu gamma~alpha gamma~beta gamma.mu = 4 g~alpha~beta + (dim-4) gamam~alpha gamma~beta
253                 } else if (other - self == 3
254                         && is_a<clifford>(self[1])
255                         && is_a<clifford>(self[2])) {
256                         ex b1, i1, b2, i2;
257                         base_and_index(self[1], b1, i1);
258                         base_and_index(self[2], b2, i2);
259                         *self = 4 * lorentz_g(i1, i2) * b1 * b2 * dirac_ONE(rl) + (dim - 4) * self[1] * self[2];
260                         self[1] = _ex1;
261                         self[2] = _ex1;
262                         *other = _ex1;
263                         return true;
264
265                 // gamma~mu gamma~alpha gamma~beta gamma~delta gamma.mu = -2 gamma~delta gamma~beta gamma~alpha - (dim-4) gamam~alpha gamma~beta gamma~delta
266                 } else if (other - self == 4
267                         && is_a<clifford>(self[1])
268                         && is_a<clifford>(self[2])
269                         && is_a<clifford>(self[3])) {
270                         *self = -2 * self[3] * self[2] * self[1] - (dim - 4) * self[1] * self[2] * self[3];
271                         self[1] = _ex1;
272                         self[2] = _ex1;
273                         self[3] = _ex1;
274                         *other = _ex1;
275                         return true;
276
277                 // gamma~mu S gamma~alpha gamma.mu = 2 gamma~alpha S - gamma~mu S gamma.mu gamma~alpha
278                 // (commutate contracted indices towards each other, simplify_indexed()
279                 // will re-expand and re-run the simplification)
280                 } else {
281                         exvector::iterator it = self + 1, next_to_last = other - 1;
282                         while (it != other) {
283                                 if (!is_a<clifford>(*it))
284                                         return false;
285                                 ++it;
286                         }
287
288                         it = self + 1;
289                         ex S = _ex1;
290                         while (it != next_to_last) {
291                                 S *= *it;
292                                 *it++ = _ex1;
293                         }
294
295                         *self = 2 * (*next_to_last) * S - (*self) * S * (*other) * (*next_to_last);
296                         *next_to_last = _ex1;
297                         *other = _ex1;
298                         return true;
299                 }
300
301         } else if (is_a<symbol>(other->op(0)) && other->nops() == 2) {
302
303                 // x.mu gamma~mu -> x-slash
304                 *self = dirac_slash(other->op(0), dim, rl);
305                 *other = _ex1;
306                 return true;
307         }
308
309         return false;
310 }
311
312 /** Perform automatic simplification on noncommutative product of clifford
313  *  objects. This removes superfluous ONEs, permutes gamma5/L/R's to the front
314  *  and removes squares of gamma objects. */
315 ex clifford::eval_ncmul(const exvector & v) const
316 {
317         exvector s;
318         s.reserve(v.size());
319
320         // Remove superfluous ONEs
321         exvector::const_iterator cit = v.begin(), citend = v.end();
322         while (cit != citend) {
323                 if (!is_a<clifford>(*cit) || !is_a<diracone>(cit->op(0)))
324                         s.push_back(*cit);
325                 cit++;
326         }
327
328         bool something_changed = false;
329         int sign = 1;
330
331         // Anticommute gamma5/L/R's to the front
332         if (s.size() >= 2) {
333                 exvector::iterator first = s.begin(), next_to_last = s.end() - 2;
334                 while (true) {
335                         exvector::iterator it = next_to_last;
336                         while (true) {
337                                 exvector::iterator it2 = it + 1;
338                                 if (is_a<clifford>(*it) && is_a<clifford>(*it2)) {
339                                         ex e1 = it->op(0), e2 = it2->op(0);
340
341                                         if (is_a<diracgamma5>(e2)) {
342
343                                                 if (is_a<diracgammaL>(e1) || is_a<diracgammaR>(e1)) {
344
345                                                         // gammaL/R gamma5 -> gamma5 gammaL/R
346                                                         it->swap(*it2);
347                                                         something_changed = true;
348
349                                                 } else if (!is_a<diracgamma5>(e1)) {
350
351                                                         // gamma5 gamma5 -> gamma5 gamma5 (do nothing)
352                                                         // x gamma5 -> -gamma5 x
353                                                         it->swap(*it2);
354                                                         sign = -sign;
355                                                         something_changed = true;
356                                                 }
357
358                                         } else if (is_a<diracgammaL>(e2)) {
359
360                                                 if (is_a<diracgammaR>(e1)) {
361
362                                                         // gammaR gammaL -> 0
363                                                         return _ex0;
364
365                                                 } else if (!is_a<diracgammaL>(e1) && !is_a<diracgamma5>(e1)) {
366
367                                                         // gammaL gammaL -> gammaL gammaL (do nothing)
368                                                         // gamma5 gammaL -> gamma5 gammaL (do nothing)
369                                                         // x gammaL -> gammaR x
370                                                         it->swap(*it2);
371                                                         *it = clifford(diracgammaR(), ex_to<clifford>(*it).get_representation_label());
372                                                         something_changed = true;
373                                                 }
374
375                                         } else if (is_a<diracgammaR>(e2)) {
376
377                                                 if (is_a<diracgammaL>(e1)) {
378
379                                                         // gammaL gammaR -> 0
380                                                         return _ex0;
381
382                                                 } else if (!is_a<diracgammaR>(e1) && !is_a<diracgamma5>(e1)) {
383
384                                                         // gammaR gammaR -> gammaR gammaR (do nothing)
385                                                         // gamma5 gammaR -> gamma5 gammaR (do nothing)
386                                                         // x gammaR -> gammaL x
387                                                         it->swap(*it2);
388                                                         *it = clifford(diracgammaL(), ex_to<clifford>(*it).get_representation_label());
389                                                         something_changed = true;
390                                                 }
391                                         }
392                                 }
393                                 if (it == first)
394                                         break;
395                                 --it;
396                         }
397                         if (next_to_last == first)
398                                 break;
399                         --next_to_last;
400                 }
401         }
402
403         // Remove equal adjacent gammas
404         if (s.size() >= 2) {
405                 exvector::iterator it, itend = s.end() - 1;
406                 for (it = s.begin(); it != itend; ++it) {
407                         ex & a = it[0];
408                         ex & b = it[1];
409                         if (!is_a<clifford>(a) || !is_a<clifford>(b))
410                                 continue;
411
412                         const ex & ag = a.op(0);
413                         const ex & bg = b.op(0);
414                         bool a_is_diracgamma = is_a<diracgamma>(ag);
415                         bool b_is_diracgamma = is_a<diracgamma>(bg);
416
417                         if (a_is_diracgamma && b_is_diracgamma) {
418
419                                 const ex & ia = a.op(1);
420                                 const ex & ib = b.op(1);
421                                 if (ia.is_equal(ib)) { // gamma~alpha gamma~alpha -> g~alpha~alpha
422                                         a = lorentz_g(ia, ib);
423                                         b = dirac_ONE(representation_label);
424                                         something_changed = true;
425                                 }
426
427                         } else if ((is_a<diracgamma5>(ag) && is_a<diracgamma5>(bg))) {
428
429                                 // Remove squares of gamma5
430                                 a = dirac_ONE(representation_label);
431                                 b = dirac_ONE(representation_label);
432                                 something_changed = true;
433
434                         } else if ((is_a<diracgammaL>(ag) && is_a<diracgammaL>(bg))
435                                 || (is_a<diracgammaR>(ag) && is_a<diracgammaR>(bg))) {
436
437                                 // Remove squares of gammaL/R
438                                 b = dirac_ONE(representation_label);
439                                 something_changed = true;
440
441                         } else if (is_a<diracgammaL>(ag) && is_a<diracgammaR>(bg)) {
442
443                                 // gammaL and gammaR are orthogonal
444                                 return _ex0;
445
446                         } else if (is_a<diracgamma5>(ag) && is_a<diracgammaL>(bg)) {
447
448                                 // gamma5 gammaL -> -gammaL
449                                 a = dirac_ONE(representation_label);
450                                 sign = -sign;
451                                 something_changed = true;
452
453                         } else if (is_a<diracgamma5>(ag) && is_a<diracgammaR>(bg)) {
454
455                                 // gamma5 gammaR -> gammaR
456                                 a = dirac_ONE(representation_label);
457                                 something_changed = true;
458
459                         } else if (!a_is_diracgamma && !b_is_diracgamma && ag.is_equal(bg)) {
460
461                                 // a\ a\ -> a^2
462                                 varidx ix((new symbol)->setflag(status_flags::dynallocated), ex_to<idx>(a.op(1)).minimal_dim(ex_to<idx>(b.op(1))));
463                                 a = indexed(ag, ix) * indexed(ag, ix.toggle_variance());
464                                 b = dirac_ONE(representation_label);
465                                 something_changed = true;
466                         }
467                 }
468         }
469
470         if (s.empty())
471                 return clifford(diracone(), representation_label) * sign;
472         if (something_changed)
473                 return reeval_ncmul(s) * sign;
474         else
475                 return hold_ncmul(s) * sign;
476 }
477
478 ex clifford::thiscontainer(const exvector & v) const
479 {
480         return clifford(representation_label, v);
481 }
482
483 ex clifford::thiscontainer(exvector * vp) const
484 {
485         return clifford(representation_label, vp);
486 }
487
488 //////////
489 // global functions
490 //////////
491
492 ex dirac_ONE(unsigned char rl)
493 {
494         return clifford(diracone(), rl);
495 }
496
497 ex dirac_gamma(const ex & mu, unsigned char rl)
498 {
499         if (!is_a<varidx>(mu))
500                 throw(std::invalid_argument("index of Dirac gamma must be of type varidx"));
501
502         return clifford(diracgamma(), mu, rl);
503 }
504
505 ex dirac_gamma5(unsigned char rl)
506 {
507         return clifford(diracgamma5(), rl);
508 }
509
510 ex dirac_gammaL(unsigned char rl)
511 {
512         return clifford(diracgammaL(), rl);
513 }
514
515 ex dirac_gammaR(unsigned char rl)
516 {
517         return clifford(diracgammaR(), rl);
518 }
519
520 ex dirac_slash(const ex & e, const ex & dim, unsigned char rl)
521 {
522         // Slashed vectors are actually stored as a clifford object with the
523         // vector as its base expression and a (dummy) index that just serves
524         // for storing the space dimensionality
525         return clifford(e, varidx(0, dim), rl);
526 }
527
528 /** Check whether a given tinfo key (as returned by return_type_tinfo()
529  *  is that of a clifford object with the specified representation label. */
530 static bool is_clifford_tinfo(unsigned ti, unsigned char rl)
531 {
532         return ti == (TINFO_clifford + rl);
533 }
534
535 /** Check whether a given tinfo key (as returned by return_type_tinfo()
536  *  is that of a clifford object (with an arbitrary representation label). */
537 static bool is_clifford_tinfo(unsigned ti)
538 {
539         return (ti & ~0xff) == TINFO_clifford;
540 }
541
542 /** Take trace of a string of an even number of Dirac gammas given a vector
543  *  of indices. */
544 static ex trace_string(exvector::const_iterator ix, size_t num)
545 {
546         // Tr gamma.mu gamma.nu = 4 g.mu.nu
547         if (num == 2)
548                 return lorentz_g(ix[0], ix[1]);
549
550         // Tr gamma.mu gamma.nu gamma.rho gamma.sig = 4 (g.mu.nu g.rho.sig + g.nu.rho g.mu.sig - g.mu.rho g.nu.sig )
551         else if (num == 4)
552                 return lorentz_g(ix[0], ix[1]) * lorentz_g(ix[2], ix[3])
553                      + lorentz_g(ix[1], ix[2]) * lorentz_g(ix[0], ix[3])
554                      - lorentz_g(ix[0], ix[2]) * lorentz_g(ix[1], ix[3]);
555
556         // Traces of 6 or more gammas are computed recursively:
557         // Tr gamma.mu1 gamma.mu2 ... gamma.mun =
558         //   + g.mu1.mu2 * Tr gamma.mu3 ... gamma.mun
559         //   - g.mu1.mu3 * Tr gamma.mu2 gamma.mu4 ... gamma.mun
560         //   + g.mu1.mu4 * Tr gamma.mu3 gamma.mu3 gamma.mu5 ... gamma.mun
561         //   - ...
562         //   + g.mu1.mun * Tr gamma.mu2 ... gamma.mu(n-1)
563         exvector v(num - 2);
564         int sign = 1;
565         ex result;
566         for (size_t i=1; i<num; i++) {
567                 for (size_t n=1, j=0; n<num; n++) {
568                         if (n == i)
569                                 continue;
570                         v[j++] = ix[n];
571                 }
572                 result += sign * lorentz_g(ix[0], ix[i]) * trace_string(v.begin(), num-2);
573                 sign = -sign;
574         }
575         return result;
576 }
577
578 ex dirac_trace(const ex & e, unsigned char rl, const ex & trONE)
579 {
580         if (is_a<clifford>(e)) {
581
582                 if (!ex_to<clifford>(e).get_representation_label() == rl)
583                         return _ex0;
584                 const ex & g = e.op(0);
585                 if (is_a<diracone>(g))
586                         return trONE;
587                 else if (is_a<diracgammaL>(g) || is_a<diracgammaR>(g))
588                         return trONE/2;
589                 else
590                         return _ex0;
591
592         } else if (is_exactly_a<mul>(e)) {
593
594                 // Trace of product: pull out non-clifford factors
595                 ex prod = _ex1;
596                 for (size_t i=0; i<e.nops(); i++) {
597                         const ex &o = e.op(i);
598                         if (is_clifford_tinfo(o.return_type_tinfo(), rl))
599                                 prod *= dirac_trace(o, rl, trONE);
600                         else
601                                 prod *= o;
602                 }
603                 return prod;
604
605         } else if (is_exactly_a<ncmul>(e)) {
606
607                 if (!is_clifford_tinfo(e.return_type_tinfo(), rl))
608                         return _ex0;
609
610                 // Substitute gammaL/R and expand product, if necessary
611                 ex e_expanded = e.subs(lst(
612                         dirac_gammaL(rl) == (dirac_ONE(rl)-dirac_gamma5(rl))/2,
613                         dirac_gammaR(rl) == (dirac_ONE(rl)+dirac_gamma5(rl))/2
614                 ), subs_options::no_pattern).expand();
615                 if (!is_a<ncmul>(e_expanded))
616                         return dirac_trace(e_expanded, rl, trONE);
617
618                 // gamma5 gets moved to the front so this check is enough
619                 bool has_gamma5 = is_a<diracgamma5>(e.op(0).op(0));
620                 size_t num = e.nops();
621
622                 if (has_gamma5) {
623
624                         // Trace of gamma5 * odd number of gammas and trace of
625                         // gamma5 * gamma.mu * gamma.nu are zero
626                         if ((num & 1) == 0 || num == 3)
627                                 return _ex0;
628
629                         // Tr gamma5 gamma.mu gamma.nu gamma.rho gamma.sigma = 4I * epsilon(mu, nu, rho, sigma)
630                         // (the epsilon is always 4-dimensional)
631                         if (num == 5) {
632                                 ex b1, i1, b2, i2, b3, i3, b4, i4;
633                                 base_and_index(e.op(1), b1, i1);
634                                 base_and_index(e.op(2), b2, i2);
635                                 base_and_index(e.op(3), b3, i3);
636                                 base_and_index(e.op(4), b4, i4);
637                                 return trONE * I * (lorentz_eps(ex_to<idx>(i1).replace_dim(_ex4), ex_to<idx>(i2).replace_dim(_ex4), ex_to<idx>(i3).replace_dim(_ex4), ex_to<idx>(i4).replace_dim(_ex4)) * b1 * b2 * b3 * b4).simplify_indexed();
638                         }
639
640                         // Tr gamma5 S_2k =
641                         //   I/4! * epsilon0123.mu1.mu2.mu3.mu4 * Tr gamma.mu1 gamma.mu2 gamma.mu3 gamma.mu4 S_2k
642                         // (the epsilon is always 4-dimensional)
643                         exvector ix(num-1), bv(num-1);
644                         for (size_t i=1; i<num; i++)
645                                 base_and_index(e.op(i), bv[i-1], ix[i-1]);
646                         num--;
647                         int *iv = new int[num];
648                         ex result;
649                         for (size_t i=0; i<num-3; i++) {
650                                 ex idx1 = ix[i];
651                                 for (size_t j=i+1; j<num-2; j++) {
652                                         ex idx2 = ix[j];
653                                         for (size_t k=j+1; k<num-1; k++) {
654                                                 ex idx3 = ix[k];
655                                                 for (size_t l=k+1; l<num; l++) {
656                                                         ex idx4 = ix[l];
657                                                         iv[0] = i; iv[1] = j; iv[2] = k; iv[3] = l;
658                                                         exvector v;
659                                                         v.reserve(num - 4);
660                                                         for (size_t n=0, t=4; n<num; n++) {
661                                                                 if (n == i || n == j || n == k || n == l)
662                                                                         continue;
663                                                                 iv[t++] = n;
664                                                                 v.push_back(ix[n]);
665                                                         }
666                                                         int sign = permutation_sign(iv, iv + num);
667                                                         result += sign * lorentz_eps(ex_to<idx>(idx1).replace_dim(_ex4), ex_to<idx>(idx2).replace_dim(_ex4), ex_to<idx>(idx3).replace_dim(_ex4), ex_to<idx>(idx4).replace_dim(_ex4))
668                                                                 * trace_string(v.begin(), num - 4);
669                                                 }
670                                         }
671                                 }
672                         }
673                         delete[] iv;
674                         return trONE * I * result * mul(bv);
675
676                 } else { // no gamma5
677
678                         // Trace of odd number of gammas is zero
679                         if ((num & 1) == 1)
680                                 return _ex0;
681
682                         // Tr gamma.mu gamma.nu = 4 g.mu.nu
683                         if (num == 2) {
684                                 ex b1, i1, b2, i2;
685                                 base_and_index(e.op(0), b1, i1);
686                                 base_and_index(e.op(1), b2, i2);
687                                 return trONE * (lorentz_g(i1, i2) * b1 * b2).simplify_indexed();
688                         }
689
690                         exvector iv(num), bv(num);
691                         for (size_t i=0; i<num; i++)
692                                 base_and_index(e.op(i), bv[i], iv[i]);
693
694                         return trONE * (trace_string(iv.begin(), num) * mul(bv)).simplify_indexed();
695                 }
696
697         } else if (e.nops() > 0) {
698
699                 // Trace maps to all other container classes (this includes sums)
700                 pointer_to_map_function_2args<unsigned char, const ex &> fcn(dirac_trace, rl, trONE);
701                 return e.map(fcn);
702
703         } else
704                 return _ex0;
705 }
706
707 ex canonicalize_clifford(const ex & e)
708 {
709         // Scan for any ncmul objects
710         lst srl;
711         ex aux = e.to_rational(srl);
712         for (size_t i=0; i<srl.nops(); i++) {
713
714                 ex o = srl.op(i);
715                 ex lhs = o.lhs();
716                 ex rhs = o.rhs();
717
718                 if (is_exactly_a<ncmul>(rhs)
719                  && rhs.return_type() == return_types::noncommutative
720                  && is_clifford_tinfo(rhs.return_type_tinfo())) {
721
722                         // Expand product, if necessary
723                         ex rhs_expanded = rhs.expand();
724                         if (!is_a<ncmul>(rhs_expanded)) {
725                                 srl[i] = (lhs == canonicalize_clifford(rhs_expanded));
726                                 continue;
727
728                         } else if (!is_a<clifford>(rhs.op(0)))
729                                 continue;
730
731                         exvector v;
732                         v.reserve(rhs.nops());
733                         for (size_t j=0; j<rhs.nops(); j++)
734                                 v.push_back(rhs.op(j));
735
736                         // Stupid recursive bubble sort because we only want to swap adjacent gammas
737                         exvector::iterator it = v.begin(), next_to_last = v.end() - 1;
738                         if (is_a<diracgamma5>(it->op(0)) || is_a<diracgammaL>(it->op(0)) || is_a<diracgammaR>(it->op(0)))
739                                 ++it;
740                         while (it != next_to_last) {
741                                 if (it[0].compare(it[1]) > 0) {
742                                         ex save0 = it[0], save1 = it[1];
743                                         ex b1, i1, b2, i2;
744                                         base_and_index(it[0], b1, i1);
745                                         base_and_index(it[1], b2, i2);
746                                         it[0] = (lorentz_g(i1, i2) * b1 * b2).simplify_indexed();
747                                         it[1] = _ex2;
748                                         ex sum = ncmul(v);
749                                         it[0] = save1;
750                                         it[1] = save0;
751                                         sum -= ncmul(v, true);
752                                         srl[i] = (lhs == canonicalize_clifford(sum));
753                                         goto next_sym;
754                                 }
755                                 ++it;
756                         }
757 next_sym:       ;
758                 }
759         }
760         return aux.subs(srl, subs_options::no_pattern).simplify_indexed();
761 }
762
763 } // namespace GiNaC