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