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