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