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