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