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