- added symmetrize() and antisymmetrize() functions
[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-2001 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 "clifford.h"
24 #include "ex.h"
25 #include "idx.h"
26 #include "ncmul.h"
27 #include "symbol.h"
28 #include "numeric.h" // for I
29 #include "lst.h"
30 #include "relational.h"
31 #include "print.h"
32 #include "archive.h"
33 #include "debugmsg.h"
34 #include "utils.h"
35
36 #include <stdexcept>
37
38 namespace GiNaC {
39
40 GINAC_IMPLEMENT_REGISTERED_CLASS(clifford, indexed)
41 GINAC_IMPLEMENT_REGISTERED_CLASS(diracone, tensor)
42 GINAC_IMPLEMENT_REGISTERED_CLASS(diracgamma, tensor)
43 GINAC_IMPLEMENT_REGISTERED_CLASS(diracgamma5, tensor)
44
45 //////////
46 // default constructor, destructor, copy constructor assignment operator and helpers
47 //////////
48
49 clifford::clifford() : representation_label(0)
50 {
51         debugmsg("clifford default constructor", LOGLEVEL_CONSTRUCT);
52         tinfo_key = TINFO_clifford;
53 }
54
55 void clifford::copy(const clifford & other)
56 {
57         inherited::copy(other);
58         representation_label = other.representation_label;
59 }
60
61 DEFAULT_DESTROY(clifford)
62 DEFAULT_CTORS(diracone)
63 DEFAULT_CTORS(diracgamma)
64 DEFAULT_CTORS(diracgamma5)
65
66 //////////
67 // other constructors
68 //////////
69
70 /** Construct object without any indices. This constructor is for internal
71  *  use only. Use the dirac_ONE() function instead.
72  *  @see dirac_ONE */
73 clifford::clifford(const ex & b, unsigned char rl) : inherited(b), representation_label(rl)
74 {
75         debugmsg("clifford constructor from ex", LOGLEVEL_CONSTRUCT);
76         tinfo_key = TINFO_clifford;
77 }
78
79 /** Construct object with one Lorentz index. This constructor is for internal
80  *  use only. Use the dirac_gamma() function instead.
81  *  @see dirac_gamma */
82 clifford::clifford(const ex & b, const ex & mu, unsigned char rl) : inherited(b, mu), representation_label(rl)
83 {
84         debugmsg("clifford constructor from ex,ex", LOGLEVEL_CONSTRUCT);
85         GINAC_ASSERT(is_ex_of_type(mu, varidx));
86         tinfo_key = TINFO_clifford;
87 }
88
89 clifford::clifford(unsigned char rl, const exvector & v, bool discardable) : inherited(indexed::unknown, v, discardable), representation_label(rl)
90 {
91         debugmsg("clifford constructor from unsigned char,exvector", LOGLEVEL_CONSTRUCT);
92         tinfo_key = TINFO_clifford;
93 }
94
95 clifford::clifford(unsigned char rl, exvector * vp) : inherited(indexed::unknown, vp), representation_label(rl)
96 {
97         debugmsg("clifford constructor from unsigned char,exvector *", LOGLEVEL_CONSTRUCT);
98         tinfo_key = TINFO_clifford;
99 }
100
101 //////////
102 // archiving
103 //////////
104
105 clifford::clifford(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
106 {
107         debugmsg("clifford constructor from archive_node", LOGLEVEL_CONSTRUCT);
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
124 //////////
125 // functions overriding virtual functions from bases classes
126 //////////
127
128 int clifford::compare_same_type(const basic & other) const
129 {
130         GINAC_ASSERT(other.tinfo() == TINFO_clifford);
131         const clifford &o = static_cast<const clifford &>(other);
132
133         if (representation_label != o.representation_label) {
134                 // different representation label
135                 return representation_label < o.representation_label ? -1 : 1;
136         }
137
138         return inherited::compare_same_type(other);
139 }
140
141 DEFAULT_COMPARE(diracone)
142 DEFAULT_COMPARE(diracgamma)
143 DEFAULT_COMPARE(diracgamma5)
144
145 DEFAULT_PRINT_LATEX(diracone, "ONE", "\\mathbb{1}")
146 DEFAULT_PRINT_LATEX(diracgamma, "gamma", "\\gamma")
147 DEFAULT_PRINT_LATEX(diracgamma5, "gamma5", "{\\gamma^5}")
148
149 /** Contraction of a gamma matrix with something else. */
150 bool diracgamma::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
151 {
152         GINAC_ASSERT(is_ex_of_type(*self, clifford));
153         GINAC_ASSERT(is_ex_of_type(*other, indexed));
154         GINAC_ASSERT(is_ex_of_type(self->op(0), diracgamma));
155         unsigned char rl = ex_to_clifford(*self).get_representation_label();
156
157         if (is_ex_of_type(*other, clifford)) {
158
159                 ex dim = ex_to_idx(self->op(1)).get_dim();
160
161                 // gamma~mu gamma.mu = dim ONE
162                 if (other - self == 1) {
163                         *self = dim;
164                         *other = dirac_ONE(rl);
165                         return true;
166
167                 // gamma~mu gamma~alpha gamma.mu = (2-dim) gamma~alpha
168                 } else if (other - self == 2
169                         && is_ex_of_type(self[1], clifford)) {
170                         *self = 2 - dim;
171                         *other = _ex1();
172                         return true;
173
174                 // gamma~mu gamma~alpha gamma~beta gamma.mu = 4 g~alpha~beta + (dim-4) gamam~alpha gamma~beta
175                 } else if (other - self == 3
176                         && is_ex_of_type(self[1], clifford)
177                         && is_ex_of_type(self[2], clifford)) {
178                         *self = 4 * lorentz_g(self[1].op(1), self[2].op(1)) * dirac_ONE(rl) + (dim - 4) * self[1] * self[2];
179                         self[1] = _ex1();
180                         self[2] = _ex1();
181                         *other = _ex1();
182                         return true;
183
184                 // gamma~mu S gamma~alpha gamma.mu = 2 gamma~alpha S - gamma~mu S gamma.mu gamma~alpha
185                 // (commutate contracted indices towards each other, simplify_indexed()
186                 // will re-expand and re-run the simplification)
187                 } else {
188                         exvector::iterator it = self + 1, next_to_last = other - 1;
189                         while (it != other) {
190                                 if (!is_ex_of_type(*it, clifford))
191                                         return false;
192                                 it++;
193                         }
194
195                         it = self + 1;
196                         ex S = _ex1();
197                         while (it != next_to_last) {
198                                 S *= *it;
199                                 *it++ = _ex1();
200                         }
201
202                         *self = 2 * (*next_to_last) * S - (*self) * S * (*other) * (*next_to_last);
203                         *next_to_last = _ex1();
204                         *other = _ex1();
205                         return true;
206                 }
207         }
208
209         return false;
210 }
211
212 /** Perform automatic simplification on noncommutative product of clifford
213  *  objects. This removes superfluous ONEs, permutes gamma5's to the front
214  *  and removes squares of gamma objects. */
215 ex clifford::simplify_ncmul(const exvector & v) const
216 {
217         exvector s;
218         s.reserve(v.size());
219
220         // Remove superfluous ONEs
221         exvector::const_iterator cit = v.begin(), citend = v.end();
222         while (cit != citend) {
223                 if (!is_ex_of_type(cit->op(0), diracone))
224                         s.push_back(*cit);
225                 cit++;
226         }
227
228         bool something_changed = false;
229         int sign = 1;
230
231         // Anticommute gamma5's to the front
232         if (s.size() >= 2) {
233                 exvector::iterator first = s.begin(), next_to_last = s.end() - 2;
234                 while (true) {
235                         exvector::iterator it = next_to_last;
236                         while (true) {
237                                 exvector::iterator it2 = it + 1;
238                                 if (!is_ex_of_type(it->op(0), diracgamma5) && is_ex_of_type(it2->op(0), diracgamma5)) {
239                                         it->swap(*it2);
240                                         sign = -sign;
241                                         something_changed = true;
242                                 }
243                                 if (it == first)
244                                         break;
245                                 it--;
246                         }
247                         if (next_to_last == first)
248                                 break;
249                         next_to_last--;
250                 }
251         }
252
253         // Remove squares of gamma5
254         while (s.size() >= 2 && is_ex_of_type(s[0].op(0), diracgamma5) && is_ex_of_type(s[1].op(0), diracgamma5)) {
255                 s.erase(s.begin(), s.begin() + 2);
256                 something_changed = true;
257         }
258
259         // Remove equal adjacent gammas
260         if (s.size() >= 2) {
261                 exvector::iterator it = s.begin(), itend = s.end() - 1;
262                 while (it != itend) {
263                         ex & a = it[0];
264                         ex & b = it[1];
265                         if (is_ex_of_type(a.op(0), diracgamma) && is_ex_of_type(b.op(0), diracgamma)) {
266                                 const ex & ia = a.op(1);
267                                 const ex & ib = b.op(1);
268                                 if (ia.is_equal(ib)) {
269                                         a = lorentz_g(ia, ib);
270                                         b = dirac_ONE(representation_label);
271                                         something_changed = true;
272                                 }
273                         }
274                         it++;
275                 }
276         }
277
278         if (s.size() == 0)
279                 return clifford(diracone(), representation_label) * sign;
280         if (something_changed)
281                 return nonsimplified_ncmul(s) * sign;
282         else
283                 return simplified_ncmul(s) * sign;
284 }
285
286 ex clifford::thisexprseq(const exvector & v) const
287 {
288         return clifford(representation_label, v);
289 }
290
291 ex clifford::thisexprseq(exvector * vp) const
292 {
293         return clifford(representation_label, vp);
294 }
295
296 //////////
297 // global functions
298 //////////
299
300 ex dirac_ONE(unsigned char rl)
301 {
302         return clifford(diracone(), rl);
303 }
304
305 ex dirac_gamma(const ex & mu, unsigned char rl)
306 {
307         if (!is_ex_of_type(mu, varidx))
308                 throw(std::invalid_argument("index of Dirac gamma must be of type varidx"));
309
310         return clifford(diracgamma(), mu, rl);
311 }
312
313 ex dirac_gamma5(unsigned char rl)
314 {
315         return clifford(diracgamma5(), rl);
316 }
317
318 ex dirac_gamma6(unsigned char rl)
319 {
320         return clifford(diracone(), rl) + clifford(diracgamma5(), rl);
321 }
322
323 ex dirac_gamma7(unsigned char rl)
324 {
325         return clifford(diracone(), rl) - clifford(diracgamma5(), rl);
326 }
327
328 ex dirac_slash(const ex & e, const ex & dim, unsigned char rl)
329 {
330         varidx mu((new symbol)->setflag(status_flags::dynallocated), dim);
331         return indexed(e, mu.toggle_variance()) * dirac_gamma(mu, rl);
332 }
333
334 /** Check whether a given tinfo key (as returned by return_type_tinfo()
335  *  is that of a clifford object with the specified representation label. */
336 static bool is_clifford_tinfo(unsigned ti, unsigned char rl)
337 {
338         return ti == (TINFO_clifford + rl);
339 }
340
341 /** Check whether a given tinfo key (as returned by return_type_tinfo()
342  *  is that of a clifford object (with an arbitrary representation label). */
343 static bool is_clifford_tinfo(unsigned ti)
344 {
345         return (ti & ~0xff) == TINFO_clifford;
346 }
347
348 ex dirac_trace(const ex & e, unsigned char rl, const ex & trONE)
349 {
350         if (is_ex_of_type(e, clifford)) {
351
352                 if (ex_to_clifford(e).get_representation_label() == rl
353                  && is_ex_of_type(e.op(0), diracone))
354                         return trONE;
355                 else
356                         return _ex0();
357
358         } else if (is_ex_exactly_of_type(e, add)) {
359
360                 // Trace of sum = sum of traces
361                 ex sum = _ex0();
362                 for (unsigned i=0; i<e.nops(); i++)
363                         sum += dirac_trace(e.op(i), rl, trONE);
364                 return sum;
365
366         } else if (is_ex_exactly_of_type(e, mul)) {
367
368                 // Trace of product: pull out non-clifford factors
369                 ex prod = _ex1();
370                 for (unsigned i=0; i<e.nops(); i++) {
371                         const ex &o = e.op(i);
372                         unsigned ti = o.return_type_tinfo();
373                         if (is_clifford_tinfo(o.return_type_tinfo(), rl))
374                                 prod *= dirac_trace(o, rl, trONE);
375                         else
376                                 prod *= o;
377                 }
378                 return prod;
379
380         } else if (is_ex_exactly_of_type(e, ncmul)) {
381
382                 if (!is_clifford_tinfo(e.return_type_tinfo(), rl))
383                         return _ex0();
384
385                 // Expand product, if necessary
386                 ex e_expanded = e.expand();
387                 if (!is_ex_of_type(e_expanded, ncmul))
388                         return dirac_trace(e_expanded, rl, trONE);
389
390                 // gamma5 gets moved to the front so this check is enough
391                 bool has_gamma5 = is_ex_of_type(e.op(0).op(0), diracgamma5);
392                 unsigned num = e.nops();
393
394                 if (has_gamma5) {
395
396                         // Trace of gamma5 * odd number of gammas and trace of
397                         // gamma5 * gamma.mu * gamma.nu are zero
398                         if ((num & 1) == 0 || num == 3)
399                                 return _ex0();
400
401                         // Tr gamma5 gamma.mu gamma.nu gamma.rho gamma.sigma = 4I * epsilon(mu, nu, rho, sigma)
402                         if (num == 5)
403                                 return trONE * I * eps0123(e.op(1).op(1), e.op(2).op(1), e.op(3).op(1), e.op(4).op(1));
404
405                         // Tr gamma5 gamma.mu1 gamma.mu2 gamma.mu3 gamma.mu4 gamma.mu5 gamma.mu6 = ...
406                         if (num == 7) {
407                                 ex i1 = e.op(1).op(1), i2 = e.op(2).op(1),
408                                    i3 = e.op(3).op(1), i4 = e.op(4).op(1),
409                                    i5 = e.op(5).op(1), i6 = e.op(6).op(1);
410                                 return trONE * I * (lorentz_g(i1, i2) * eps0123(i3, i4, i5, i6)
411                                                   - lorentz_g(i1, i3) * eps0123(i2, i4, i5, i6)
412                                                   + lorentz_g(i1, i4) * eps0123(i2, i3, i5, i6)
413                                                   - lorentz_g(i1, i5) * eps0123(i2, i3, i4, i6)
414                                                   + lorentz_g(i1, i6) * eps0123(i2, i3, i4, i5)
415                                                   + lorentz_g(i2, i3) * eps0123(i1, i4, i5, i6)
416                                                   - lorentz_g(i2, i4) * eps0123(i1, i3, i5, i6)
417                                                   + lorentz_g(i2, i5) * eps0123(i1, i3, i4, i6)
418                                                   - lorentz_g(i2, i6) * eps0123(i1, i3, i4, i5)
419                                                   + lorentz_g(i3, i4) * eps0123(i1, i2, i5, i6)
420                                                   - lorentz_g(i3, i5) * eps0123(i1, i2, i4, i6)
421                                                   + lorentz_g(i3, i6) * eps0123(i1, i2, i4, i5)
422                                                   + lorentz_g(i4, i5) * eps0123(i1, i2, i3, i6)
423                                                   - lorentz_g(i4, i6) * eps0123(i1, i2, i3, i5)
424                                                   + lorentz_g(i5, i6) * eps0123(i1, i2, i3, i4));
425                         }
426
427                         // Tr gamma5 S_2k =
428                         //   I/4! * epsilon0123.mu1.mu2.mu3.mu4 * Tr gamma.mu1 gamma.mu2 gamma.mu3 gamma.mu4 S_2k
429                         ex result;
430                         for (int i=1; i<num-3; i++) {
431                                 ex idx1 = e.op(i).op(1);
432                                 for (int j=i+1; j<num-2; j++) {
433                                         ex idx2 = e.op(j).op(1);
434                                         for (int k=j+1; k<num-1; k++) {
435                                                 ex idx3 = e.op(k).op(1);
436                                                 for (int l=k+1; l<num; l++) {
437                                                         ex idx4 = e.op(l).op(1);
438                                                         vector<int> iv;
439                                                         iv.reserve(num-1);
440                                                         exvector v;
441                                                         v.reserve(num-1);
442                                                         iv.push_back(i); iv.push_back(j); iv.push_back(k); iv.push_back(l);
443                                                         for (int n=1; n<num; n++) {
444                                                                 if (n == i || n == j || n == k || n == l)
445                                                                         continue;
446                                                                 iv.push_back(n);
447                                                                 v.push_back(e.op(n));
448                                                         }
449                                                         int sign = permutation_sign(iv.begin(), iv.end());
450                                                         result += sign * eps0123(idx1, idx2, idx3, idx4)
451                                                                 * dirac_trace(ncmul(v, true), rl, trONE);
452                                                 }
453                                         }
454                                 }
455                         }
456                         return result * I;
457
458                 } else { // no gamma5
459
460                         // Trace of odd number of gammas is zero
461                         if ((num & 1) == 1)
462                                 return _ex0();
463
464                         // Tr gamma.mu gamma.nu = 4 g.mu.nu
465                         if (num == 2)
466                                 return trONE * lorentz_g(e.op(0).op(1), e.op(1).op(1));
467
468                         // 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
469                         if (num == 4)
470                                 return trONE * (lorentz_g(e.op(0).op(1), e.op(1).op(1)) * lorentz_g(e.op(2).op(1), e.op(3).op(1))
471                                               + lorentz_g(e.op(1).op(1), e.op(2).op(1)) * lorentz_g(e.op(0).op(1), e.op(3).op(1))
472                                               - lorentz_g(e.op(0).op(1), e.op(2).op(1)) * lorentz_g(e.op(1).op(1), e.op(3).op(1)));
473
474                         // Traces of 6 or more gammas are computed recursively:
475                         // Tr gamma.mu1 gamma.mu2 ... gamma.mun =
476                         //   + g.mu1.mu2 * Tr gamma.mu3 ... gamma.mun
477                         //   - g.mu1.mu3 * Tr gamma.mu2 gamma.mu4 ... gamma.mun
478                         //   + g.mu1.mu4 * Tr gamma.mu3 gamma.mu3 gamma.mu5 ... gamma.mun
479                         //   - ...
480                         //   + g.mu1.mun * Tr gamma.mu2 ... gamma.mu(n-1)
481                         exvector v(num - 2);
482                         int sign = 1;
483                         const ex &ix1 = e.op(0).op(1);
484                         ex result;
485                         for (int i=1; i<num; i++) {
486                                 for (int n=1, j=0; n<num; n++) {
487                                         if (n == i)
488                                                 continue;
489                                         v[j++] = e.op(n);
490                                 }
491                                 result += sign * lorentz_g(ix1, e.op(i).op(1)) * dirac_trace(ncmul(v), rl, trONE);
492                                 sign = -sign;
493                         }
494                         return result;
495                 }
496         }
497
498         return _ex0();
499 }
500
501 ex canonicalize_clifford(const ex & e)
502 {
503         // Scan for any ncmul objects
504         lst srl;
505         ex aux = e.to_rational(srl);
506         for (unsigned i=0; i<srl.nops(); i++) {
507
508                 ex lhs = srl.op(i).lhs();
509                 ex rhs = srl.op(i).rhs();
510
511                 if (is_ex_exactly_of_type(rhs, ncmul)
512                  && rhs.return_type() == return_types::noncommutative
513                  && is_clifford_tinfo(rhs.return_type_tinfo())) {
514
515                         // Expand product, if necessary
516                         ex rhs_expanded = rhs.expand();
517                         if (!is_ex_of_type(rhs_expanded, ncmul)) {
518                                 srl.let_op(i) = (lhs == canonicalize_clifford(rhs_expanded));
519                                 continue;
520
521                         } else if (!is_ex_of_type(rhs.op(0), clifford))
522                                 continue;
523
524                         exvector v;
525                         v.reserve(rhs.nops());
526                         for (unsigned j=0; j<rhs.nops(); j++)
527                                 v.push_back(rhs.op(j));
528
529                         // Stupid bubble sort because we only want to swap adjacent gammas
530                         exvector::iterator it = v.begin(), next_to_last = v.end() - 1;
531                         if (is_ex_of_type(it->op(0), diracgamma5))
532                                 it++;
533                         while (it != next_to_last) {
534                                 if (it[0].op(1).compare(it[1].op(1)) > 0) {
535                                         ex save0 = it[0], save1 = it[1];
536                                         it[0] = lorentz_g(it[0].op(1), it[1].op(1));
537                                         it[1] = _ex2();
538                                         ex sum = ncmul(v);
539                                         it[0] = save1;
540                                         it[1] = save0;
541                                         sum -= ncmul(v, true);
542                                         srl.let_op(i) = (lhs == canonicalize_clifford(sum));
543                                         goto next_sym;
544                                 }
545                                 it++;
546                         }
547 next_sym:       ;
548                 }
549         }
550         return aux.subs(srl);
551 }
552
553 } // namespace GiNaC