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