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