]> www.ginac.de Git - ginac.git/blob - ginac/clifford.cpp
b12e385ce4f03f2ca6f9bc7d6fd09342f5f6ef28
[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 "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 (other->nops() > 1)
214               dim = minimal_dim(dim, ex_to<idx>(self->op(1)).get_dim());
215         if (is_a<clifford>(*other)) {
216
217                 // Contraction only makes sense if the represenation labels are equal
218                 if (ex_to<clifford>(*other).get_representation_label() != rl)
219                         return false;
220
221                 // gamma~mu gamma.mu = dim ONE
222                 if (other - self == 1) {
223                         *self = dim;
224                         *other = dirac_ONE(rl);
225                         return true;
226
227                 // gamma~mu gamma~alpha gamma.mu = (2-dim) gamma~alpha
228                 } else if (other - self == 2
229                         && is_a<clifford>(self[1])) {
230                         *self = 2 - dim;
231                         *other = _ex1;
232                         return true;
233
234                 // gamma~mu gamma~alpha gamma~beta gamma.mu = 4 g~alpha~beta + (dim-4) gamam~alpha gamma~beta
235                 } else if (other - self == 3
236                         && is_a<clifford>(self[1])
237                         && is_a<clifford>(self[2])) {
238                         ex b1, i1, b2, i2;
239                         base_and_index(self[1], b1, i1);
240                         base_and_index(self[2], b2, i2);
241                         *self = 4 * lorentz_g(i1, i2) * b1 * b2 * dirac_ONE(rl) + (dim - 4) * self[1] * self[2];
242                         self[1] = _ex1;
243                         self[2] = _ex1;
244                         *other = _ex1;
245                         return true;
246
247                 // 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
248                 } else if (other - self == 4
249                         && is_a<clifford>(self[1])
250                         && is_a<clifford>(self[2])
251                         && is_a<clifford>(self[3])) {
252                         *self = -2 * self[3] * self[2] * self[1] - (dim - 4) * self[1] * self[2] * self[3];
253                         self[1] = _ex1;
254                         self[2] = _ex1;
255                         self[3] = _ex1;
256                         *other = _ex1;
257                         return true;
258
259                 // gamma~mu S gamma~alpha gamma.mu = 2 gamma~alpha S - gamma~mu S gamma.mu gamma~alpha
260                 // (commutate contracted indices towards each other, simplify_indexed()
261                 // will re-expand and re-run the simplification)
262                 } else {
263                         exvector::iterator it = self + 1, next_to_last = other - 1;
264                         while (it != other) {
265                                 if (!is_a<clifford>(*it))
266                                         return false;
267                                 ++it;
268                         }
269
270                         it = self + 1;
271                         ex S = _ex1;
272                         while (it != next_to_last) {
273                                 S *= *it;
274                                 *it++ = _ex1;
275                         }
276
277                         *self = 2 * (*next_to_last) * S - (*self) * S * (*other) * (*next_to_last);
278                         *next_to_last = _ex1;
279                         *other = _ex1;
280                         return true;
281                 }
282
283         } else if (is_a<symbol>(other->op(0)) && other->nops() == 2) {
284
285                 // x.mu gamma~mu -> x-slash
286                 *self = dirac_slash(other->op(0), dim, rl);
287                 *other = _ex1;
288                 return true;
289         }
290
291         return false;
292 }
293
294 /** Perform automatic simplification on noncommutative product of clifford
295  *  objects. This removes superfluous ONEs, permutes gamma5/L/R's to the front
296  *  and removes squares of gamma objects. */
297 ex clifford::simplify_ncmul(const exvector & v) const
298 {
299         exvector s;
300         s.reserve(v.size());
301
302         // Remove superfluous ONEs
303         exvector::const_iterator cit = v.begin(), citend = v.end();
304         while (cit != citend) {
305                 if (!is_a<clifford>(*cit) || !is_a<diracone>(cit->op(0)))
306                         s.push_back(*cit);
307                 cit++;
308         }
309
310         bool something_changed = false;
311         int sign = 1;
312
313         // Anticommute gamma5/L/R's to the front
314         if (s.size() >= 2) {
315                 exvector::iterator first = s.begin(), next_to_last = s.end() - 2;
316                 while (true) {
317                         exvector::iterator it = next_to_last;
318                         while (true) {
319                                 exvector::iterator it2 = it + 1;
320                                 if (is_a<clifford>(*it) && is_a<clifford>(*it2)) {
321                                         ex e1 = it->op(0), e2 = it2->op(0);
322
323                                         if (is_a<diracgamma5>(e2)) {
324
325                                                 if (is_a<diracgammaL>(e1) || is_a<diracgammaR>(e1)) {
326
327                                                         // gammaL/R gamma5 -> gamma5 gammaL/R
328                                                         it->swap(*it2);
329                                                         something_changed = true;
330
331                                                 } else if (!is_a<diracgamma5>(e1)) {
332
333                                                         // gamma5 gamma5 -> gamma5 gamma5 (do nothing)
334                                                         // x gamma5 -> -gamma5 x
335                                                         it->swap(*it2);
336                                                         sign = -sign;
337                                                         something_changed = true;
338                                                 }
339
340                                         } else if (is_a<diracgammaL>(e2)) {
341
342                                                 if (is_a<diracgammaR>(e1)) {
343
344                                                         // gammaR gammaL -> 0
345                                                         return _ex0;
346
347                                                 } else if (!is_a<diracgammaL>(e1) && !is_a<diracgamma5>(e1)) {
348
349                                                         // gammaL gammaL -> gammaL gammaL (do nothing)
350                                                         // gamma5 gammaL -> gamma5 gammaL (do nothing)
351                                                         // x gammaL -> gammaR x
352                                                         it->swap(*it2);
353                                                         *it = clifford(diracgammaR(), ex_to<clifford>(*it).get_representation_label());
354                                                         something_changed = true;
355                                                 }
356
357                                         } else if (is_a<diracgammaR>(e2)) {
358
359                                                 if (is_a<diracgammaL>(e1)) {
360
361                                                         // gammaL gammaR -> 0
362                                                         return _ex0;
363
364                                                 } else if (!is_a<diracgammaR>(e1) && !is_a<diracgamma5>(e1)) {
365
366                                                         // gammaR gammaR -> gammaR gammaR (do nothing)
367                                                         // gamma5 gammaR -> gamma5 gammaR (do nothing)
368                                                         // x gammaR -> gammaL x
369                                                         it->swap(*it2);
370                                                         *it = clifford(diracgammaL(), ex_to<clifford>(*it).get_representation_label());
371                                                         something_changed = true;
372                                                 }
373                                         }
374                                 }
375                                 if (it == first)
376                                         break;
377                                 --it;
378                         }
379                         if (next_to_last == first)
380                                 break;
381                         --next_to_last;
382                 }
383         }
384
385         // Remove equal adjacent gammas
386         if (s.size() >= 2) {
387                 exvector::iterator it, itend = s.end() - 1;
388                 for (it = s.begin(); it != itend; ++it) {
389                         ex & a = it[0];
390                         ex & b = it[1];
391                         if (!is_a<clifford>(a) || !is_a<clifford>(b))
392                                 continue;
393
394                         const ex & ag = a.op(0);
395                         const ex & bg = b.op(0);
396                         bool a_is_diracgamma = is_a<diracgamma>(ag);
397                         bool b_is_diracgamma = is_a<diracgamma>(bg);
398
399                         if (a_is_diracgamma && b_is_diracgamma) {
400
401                                 const ex & ia = a.op(1);
402                                 const ex & ib = b.op(1);
403                                 if (ia.is_equal(ib)) { // gamma~alpha gamma~alpha -> g~alpha~alpha
404                                         a = lorentz_g(ia, ib);
405                                         b = dirac_ONE(representation_label);
406                                         something_changed = true;
407                                 }
408
409                         } else if ((is_a<diracgamma5>(ag) && is_a<diracgamma5>(bg))) {
410
411                                 // Remove squares of gamma5
412                                 a = dirac_ONE(representation_label);
413                                 b = dirac_ONE(representation_label);
414                                 something_changed = true;
415
416                         } else if ((is_a<diracgammaL>(ag) && is_a<diracgammaL>(bg))
417                                 || (is_a<diracgammaR>(ag) && is_a<diracgammaR>(bg))) {
418
419                                 // Remove squares of gammaL/R
420                                 b = dirac_ONE(representation_label);
421                                 something_changed = true;
422
423                         } else if (is_a<diracgammaL>(ag) && is_a<diracgammaR>(bg)) {
424
425                                 // gammaL and gammaR are orthogonal
426                                 return _ex0;
427
428                         } else if (is_a<diracgamma5>(ag) && is_a<diracgammaL>(bg)) {
429
430                                 // gamma5 gammaL -> -gammaL
431                                 a = dirac_ONE(representation_label);
432                                 sign = -sign;
433                                 something_changed = true;
434
435                         } else if (is_a<diracgamma5>(ag) && is_a<diracgammaR>(bg)) {
436
437                                 // gamma5 gammaR -> gammaR
438                                 a = dirac_ONE(representation_label);
439                                 something_changed = true;
440
441                         } else if (!a_is_diracgamma && !b_is_diracgamma && ag.is_equal(bg)) {
442
443                                 // a\ a\ -> a^2
444                                 varidx ix((new symbol)->setflag(status_flags::dynallocated), ex_to<idx>(a.op(1)).minimal_dim(ex_to<idx>(b.op(1))));
445                                 a = indexed(ag, ix) * indexed(ag, ix.toggle_variance());
446                                 b = dirac_ONE(representation_label);
447                                 something_changed = true;
448                         }
449                 }
450         }
451
452         if (s.empty())
453                 return clifford(diracone(), representation_label) * sign;
454         if (something_changed)
455                 return nonsimplified_ncmul(s) * sign;
456         else
457                 return simplified_ncmul(s) * sign;
458 }
459
460 ex clifford::thisexprseq(const exvector & v) const
461 {
462         return clifford(representation_label, v);
463 }
464
465 ex clifford::thisexprseq(exvector * vp) const
466 {
467         return clifford(representation_label, vp);
468 }
469
470 //////////
471 // global functions
472 //////////
473
474 ex dirac_ONE(unsigned char rl)
475 {
476         return clifford(diracone(), rl);
477 }
478
479 ex dirac_gamma(const ex & mu, unsigned char rl)
480 {
481         if (!is_a<varidx>(mu))
482                 throw(std::invalid_argument("index of Dirac gamma must be of type varidx"));
483
484         return clifford(diracgamma(), mu, rl);
485 }
486
487 ex dirac_gamma5(unsigned char rl)
488 {
489         return clifford(diracgamma5(), rl);
490 }
491
492 ex dirac_gammaL(unsigned char rl)
493 {
494         return clifford(diracgammaL(), rl);
495 }
496
497 ex dirac_gammaR(unsigned char rl)
498 {
499         return clifford(diracgammaR(), rl);
500 }
501
502 ex dirac_gamma6(unsigned char rl)
503 {
504         return clifford(diracone(), rl) + clifford(diracgamma5(), rl);
505 }
506
507 ex dirac_gamma7(unsigned char rl)
508 {
509         return clifford(diracone(), rl) - clifford(diracgamma5(), rl);
510 }
511
512 ex dirac_slash(const ex & e, const ex & dim, unsigned char rl)
513 {
514         // Slashed vectors are actually stored as a clifford object with the
515         // vector as its base expression and a (dummy) index that just serves
516         // for storing the space dimensionality
517         return clifford(e, varidx(0, dim), rl);
518 }
519
520 /** Check whether a given tinfo key (as returned by return_type_tinfo()
521  *  is that of a clifford object with the specified representation label. */
522 static bool is_clifford_tinfo(unsigned ti, unsigned char rl)
523 {
524         return ti == (TINFO_clifford + rl);
525 }
526
527 /** Check whether a given tinfo key (as returned by return_type_tinfo()
528  *  is that of a clifford object (with an arbitrary representation label). */
529 static bool is_clifford_tinfo(unsigned ti)
530 {
531         return (ti & ~0xff) == TINFO_clifford;
532 }
533
534 /** Take trace of a string of an even number of Dirac gammas given a vector
535  *  of indices. */
536 static ex trace_string(exvector::const_iterator ix, unsigned num)
537 {
538         // Tr gamma.mu gamma.nu = 4 g.mu.nu
539         if (num == 2)
540                 return lorentz_g(ix[0], ix[1]);
541
542         // 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
543         else if (num == 4)
544                 return lorentz_g(ix[0], ix[1]) * lorentz_g(ix[2], ix[3])
545                      + lorentz_g(ix[1], ix[2]) * lorentz_g(ix[0], ix[3])
546                      - lorentz_g(ix[0], ix[2]) * lorentz_g(ix[1], ix[3]);
547
548         // Traces of 6 or more gammas are computed recursively:
549         // Tr gamma.mu1 gamma.mu2 ... gamma.mun =
550         //   + g.mu1.mu2 * Tr gamma.mu3 ... gamma.mun
551         //   - g.mu1.mu3 * Tr gamma.mu2 gamma.mu4 ... gamma.mun
552         //   + g.mu1.mu4 * Tr gamma.mu3 gamma.mu3 gamma.mu5 ... gamma.mun
553         //   - ...
554         //   + g.mu1.mun * Tr gamma.mu2 ... gamma.mu(n-1)
555         exvector v(num - 2);
556         int sign = 1;
557         ex result;
558         for (unsigned i=1; i<num; i++) {
559                 for (unsigned n=1, j=0; n<num; n++) {
560                         if (n == i)
561                                 continue;
562                         v[j++] = ix[n];
563                 }
564                 result += sign * lorentz_g(ix[0], ix[i]) * trace_string(v.begin(), num-2);
565                 sign = -sign;
566         }
567         return result;
568 }
569
570 ex dirac_trace(const ex & e, unsigned char rl, const ex & trONE)
571 {
572         if (is_a<clifford>(e)) {
573
574                 if (!ex_to<clifford>(e).get_representation_label() == rl)
575                         return _ex0;
576                 const ex & g = e.op(0);
577                 if (is_a<diracone>(g))
578                         return trONE;
579                 else if (is_a<diracgammaL>(g) || is_a<diracgammaR>(g))
580                         return trONE/2;
581                 else
582                         return _ex0;
583
584         } else if (is_ex_exactly_of_type(e, mul)) {
585
586                 // Trace of product: pull out non-clifford factors
587                 ex prod = _ex1;
588                 for (unsigned i=0; i<e.nops(); i++) {
589                         const ex &o = e.op(i);
590                         if (is_clifford_tinfo(o.return_type_tinfo(), rl))
591                                 prod *= dirac_trace(o, rl, trONE);
592                         else
593                                 prod *= o;
594                 }
595                 return prod;
596
597         } else if (is_ex_exactly_of_type(e, ncmul)) {
598
599                 if (!is_clifford_tinfo(e.return_type_tinfo(), rl))
600                         return _ex0;
601
602                 // Substitute gammaL/R and expand product, if necessary
603                 ex e_expanded = e.subs(lst(
604                         dirac_gammaL(rl) == (dirac_ONE(rl)-dirac_gamma5(rl))/2,
605                         dirac_gammaR(rl) == (dirac_ONE(rl)+dirac_gamma5(rl))/2
606                 )).expand();
607                 if (!is_a<ncmul>(e_expanded))
608                         return dirac_trace(e_expanded, rl, trONE);
609
610                 // gamma5 gets moved to the front so this check is enough
611                 bool has_gamma5 = is_a<diracgamma5>(e.op(0).op(0));
612                 unsigned num = e.nops();
613
614                 if (has_gamma5) {
615
616                         // Trace of gamma5 * odd number of gammas and trace of
617                         // gamma5 * gamma.mu * gamma.nu are zero
618                         if ((num & 1) == 0 || num == 3)
619                                 return _ex0;
620
621                         // Tr gamma5 gamma.mu gamma.nu gamma.rho gamma.sigma = 4I * epsilon(mu, nu, rho, sigma)
622                         // (the epsilon is always 4-dimensional)
623                         if (num == 5) {
624                                 ex b1, i1, b2, i2, b3, i3, b4, i4;
625                                 base_and_index(e.op(1), b1, i1);
626                                 base_and_index(e.op(2), b2, i2);
627                                 base_and_index(e.op(3), b3, i3);
628                                 base_and_index(e.op(4), b4, i4);
629                                 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();
630                         }
631
632                         // Tr gamma5 S_2k =
633                         //   I/4! * epsilon0123.mu1.mu2.mu3.mu4 * Tr gamma.mu1 gamma.mu2 gamma.mu3 gamma.mu4 S_2k
634                         // (the epsilon is always 4-dimensional)
635                         exvector ix(num-1), bv(num-1);
636                         for (unsigned i=1; i<num; i++)
637                                 base_and_index(e.op(i), bv[i-1], ix[i-1]);
638                         num--;
639                         int *iv = new int[num];
640                         ex result;
641                         for (unsigned i=0; i<num-3; i++) {
642                                 ex idx1 = ix[i];
643                                 for (unsigned j=i+1; j<num-2; j++) {
644                                         ex idx2 = ix[j];
645                                         for (unsigned k=j+1; k<num-1; k++) {
646                                                 ex idx3 = ix[k];
647                                                 for (unsigned l=k+1; l<num; l++) {
648                                                         ex idx4 = ix[l];
649                                                         iv[0] = i; iv[1] = j; iv[2] = k; iv[3] = l;
650                                                         exvector v;
651                                                         v.reserve(num - 4);
652                                                         for (unsigned n=0, t=4; n<num; n++) {
653                                                                 if (n == i || n == j || n == k || n == l)
654                                                                         continue;
655                                                                 iv[t++] = n;
656                                                                 v.push_back(ix[n]);
657                                                         }
658                                                         int sign = permutation_sign(iv, iv + num);
659                                                         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))
660                                                                 * trace_string(v.begin(), num - 4);
661                                                 }
662                                         }
663                                 }
664                         }
665                         delete[] iv;
666                         return trONE * I * result * mul(bv);
667
668                 } else { // no gamma5
669
670                         // Trace of odd number of gammas is zero
671                         if ((num & 1) == 1)
672                                 return _ex0;
673
674                         // Tr gamma.mu gamma.nu = 4 g.mu.nu
675                         if (num == 2) {
676                                 ex b1, i1, b2, i2;
677                                 base_and_index(e.op(0), b1, i1);
678                                 base_and_index(e.op(1), b2, i2);
679                                 return trONE * (lorentz_g(i1, i2) * b1 * b2).simplify_indexed();
680                         }
681
682                         exvector iv(num), bv(num);
683                         for (unsigned i=0; i<num; i++)
684                                 base_and_index(e.op(i), bv[i], iv[i]);
685
686                         return trONE * (trace_string(iv.begin(), num) * mul(bv)).simplify_indexed();
687                 }
688
689         } else if (e.nops() > 0) {
690
691                 // Trace maps to all other container classes (this includes sums)
692                 pointer_to_map_function_2args<unsigned char, const ex &> fcn(dirac_trace, rl, trONE);
693                 return e.map(fcn);
694
695         } else
696                 return _ex0;
697 }
698
699 ex canonicalize_clifford(const ex & e)
700 {
701         // Scan for any ncmul objects
702         lst srl;
703         ex aux = e.to_rational(srl);
704         for (unsigned i=0; i<srl.nops(); i++) {
705
706                 ex lhs = srl.op(i).lhs();
707                 ex rhs = srl.op(i).rhs();
708
709                 if (is_ex_exactly_of_type(rhs, ncmul)
710                  && rhs.return_type() == return_types::noncommutative
711                  && is_clifford_tinfo(rhs.return_type_tinfo())) {
712
713                         // Expand product, if necessary
714                         ex rhs_expanded = rhs.expand();
715                         if (!is_a<ncmul>(rhs_expanded)) {
716                                 srl.let_op(i) = (lhs == canonicalize_clifford(rhs_expanded));
717                                 continue;
718
719                         } else if (!is_a<clifford>(rhs.op(0)))
720                                 continue;
721
722                         exvector v;
723                         v.reserve(rhs.nops());
724                         for (unsigned j=0; j<rhs.nops(); j++)
725                                 v.push_back(rhs.op(j));
726
727                         // Stupid recursive bubble sort because we only want to swap adjacent gammas
728                         exvector::iterator it = v.begin(), next_to_last = v.end() - 1;
729                         if (is_a<diracgamma5>(it->op(0)) || is_a<diracgammaL>(it->op(0)) || is_a<diracgammaR>(it->op(0)))
730                                 ++it;
731                         while (it != next_to_last) {
732                                 if (it[0].compare(it[1]) > 0) {
733                                         ex save0 = it[0], save1 = it[1];
734                                         ex b1, i1, b2, i2;
735                                         base_and_index(it[0], b1, i1);
736                                         base_and_index(it[1], b2, i2);
737                                         it[0] = (lorentz_g(i1, i2) * b1 * b2).simplify_indexed();
738                                         it[1] = _ex2;
739                                         ex sum = ncmul(v);
740                                         it[0] = save1;
741                                         it[1] = save0;
742                                         sum -= ncmul(v, true);
743                                         srl.let_op(i) = (lhs == canonicalize_clifford(sum));
744                                         goto next_sym;
745                                 }
746                                 ++it;
747                         }
748 next_sym:       ;
749                 }
750         }
751         return aux.subs(srl).simplify_indexed();
752 }
753
754 } // namespace GiNaC