]> www.ginac.de Git - ginac.git/blob - ginac/clifford.cpp
- dirac_trace() handles arbitrary expressions (even unexpanded), including
[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 "print.h"
29 #include "archive.h"
30 #include "debugmsg.h"
31 #include "utils.h"
32
33 #include <stdexcept>
34
35 namespace GiNaC {
36
37 GINAC_IMPLEMENT_REGISTERED_CLASS(clifford, indexed)
38 GINAC_IMPLEMENT_REGISTERED_CLASS(diracone, tensor)
39 GINAC_IMPLEMENT_REGISTERED_CLASS(diracgamma, tensor)
40 GINAC_IMPLEMENT_REGISTERED_CLASS(diracgamma5, tensor)
41
42 //////////
43 // default constructor, destructor, copy constructor assignment operator and helpers
44 //////////
45
46 clifford::clifford() : representation_label(0)
47 {
48         debugmsg("clifford default constructor", LOGLEVEL_CONSTRUCT);
49         tinfo_key = TINFO_clifford;
50 }
51
52 void clifford::copy(const clifford & other)
53 {
54         inherited::copy(other);
55         representation_label = other.representation_label;
56 }
57
58 DEFAULT_DESTROY(clifford)
59 DEFAULT_CTORS(diracone)
60 DEFAULT_CTORS(diracgamma)
61 DEFAULT_CTORS(diracgamma5)
62
63 //////////
64 // other constructors
65 //////////
66
67 /** Construct object without any indices. This constructor is for internal
68  *  use only. Use the dirac_ONE() function instead.
69  *  @see dirac_ONE */
70 clifford::clifford(const ex & b, unsigned char rl) : inherited(b), representation_label(rl)
71 {
72         debugmsg("clifford constructor from ex", LOGLEVEL_CONSTRUCT);
73         tinfo_key = TINFO_clifford;
74 }
75
76 /** Construct object with one Lorentz index. This constructor is for internal
77  *  use only. Use the dirac_gamma() function instead.
78  *  @see dirac_gamma */
79 clifford::clifford(const ex & b, const ex & mu, unsigned char rl) : inherited(b, mu), representation_label(rl)
80 {
81         debugmsg("clifford constructor from ex,ex", LOGLEVEL_CONSTRUCT);
82         GINAC_ASSERT(is_ex_of_type(mu, varidx));
83         tinfo_key = TINFO_clifford;
84 }
85
86 clifford::clifford(unsigned char rl, const exvector & v, bool discardable) : inherited(indexed::unknown, v, discardable), representation_label(rl)
87 {
88         debugmsg("clifford constructor from unsigned char,exvector", LOGLEVEL_CONSTRUCT);
89         tinfo_key = TINFO_clifford;
90 }
91
92 clifford::clifford(unsigned char rl, exvector * vp) : inherited(indexed::unknown, vp), representation_label(rl)
93 {
94         debugmsg("clifford constructor from unsigned char,exvector *", LOGLEVEL_CONSTRUCT);
95         tinfo_key = TINFO_clifford;
96 }
97
98 //////////
99 // archiving
100 //////////
101
102 clifford::clifford(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
103 {
104         debugmsg("clifford constructor from archive_node", LOGLEVEL_CONSTRUCT);
105         unsigned rl;
106         n.find_unsigned("label", rl);
107         representation_label = rl;
108 }
109
110 void clifford::archive(archive_node &n) const
111 {
112         inherited::archive(n);
113         n.add_unsigned("label", representation_label);
114 }
115
116 DEFAULT_UNARCHIVE(clifford)
117 DEFAULT_ARCHIVING(diracone)
118 DEFAULT_ARCHIVING(diracgamma)
119 DEFAULT_ARCHIVING(diracgamma5)
120
121 //////////
122 // functions overriding virtual functions from bases classes
123 //////////
124
125 int clifford::compare_same_type(const basic & other) const
126 {
127         GINAC_ASSERT(other.tinfo() == TINFO_clifford);
128         const clifford &o = static_cast<const clifford &>(other);
129
130         if (representation_label != o.representation_label) {
131                 // different representation label
132                 return representation_label < o.representation_label ? -1 : 1;
133         }
134
135         return inherited::compare_same_type(other);
136 }
137
138 DEFAULT_COMPARE(diracone)
139 DEFAULT_COMPARE(diracgamma)
140 DEFAULT_COMPARE(diracgamma5)
141
142 DEFAULT_PRINT(diracone, "ONE")
143 DEFAULT_PRINT(diracgamma, "gamma")
144 DEFAULT_PRINT(diracgamma5, "gamma5")
145
146 /** Contraction of a gamma matrix with something else. */
147 bool diracgamma::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
148 {
149         GINAC_ASSERT(is_ex_of_type(*self, clifford));
150         GINAC_ASSERT(is_ex_of_type(*other, indexed));
151         GINAC_ASSERT(is_ex_of_type(self->op(0), diracgamma));
152         unsigned char rl = ex_to_clifford(*self).get_representation_label();
153
154         if (is_ex_of_type(other->op(0), diracgamma)) {
155
156                 ex dim = ex_to_idx(self->op(1)).get_dim();
157
158                 // gamma~mu*gamma.mu = dim*ONE
159                 if (other - self == 1) {
160                         *self = dim;
161                         *other = dirac_ONE(rl);
162                         return true;
163
164                 // gamma~mu*gamma~alpha*gamma.mu = (2-dim)*gamma~alpha
165                 } else if (other - self == 2
166                         && is_ex_of_type(self[1], clifford)) {
167                         *self = 2 - dim;
168                         *other = _ex1();
169                         return true;
170
171                 // gamma~mu*gamma~alpha*gamma~beta*gamma.mu = 4*g~alpha~beta+(dim-4)*gamam~alpha*gamma~beta
172                 } else if (other - self == 3
173                         && is_ex_of_type(self[1], clifford)
174                         && is_ex_of_type(self[2], clifford)) {
175                         *self = 4 * metric_tensor(self[1].op(1), self[2].op(1)) * dirac_ONE(rl) + (dim - 4) * self[1] * self[2];
176                         self[1] = _ex1();
177                         self[2] = _ex1();
178                         *other = _ex1();
179                         return true;
180
181                 // gamma~mu*gamma~alpha*gamma~beta*gamma~delta*gamma.mu = -2*gamma~delta*gamma~beta*gamma~alpha+(4-dim)*gamma~alpha*gamma~beta*gamma~delta
182                 } else if (other - self == 4
183                         && is_ex_of_type(self[1], clifford)
184                         && is_ex_of_type(self[2], clifford)
185                         && is_ex_of_type(self[3], clifford)) {
186                         *self = -2 * self[3] * self[2] * self[1] + (4 - dim) * self[1] * self[2] * self[3];
187                         self[1] = _ex1();
188                         self[2] = _ex1();
189                         self[3] = _ex1();
190                         *other = _ex1();
191                         return true;
192                 }
193         }
194
195         return false;
196 }
197
198 /** Perform automatic simplification on noncommutative product of clifford
199  *  objects. This removes superfluous ONEs, permutes gamma5's to the front
200  *  and removes squares of gamma objects. */
201 ex clifford::simplify_ncmul(const exvector & v) const
202 {
203         exvector s;
204         s.reserve(v.size());
205         unsigned rl = ex_to_clifford(v[0]).get_representation_label();
206
207         // Remove superfluous ONEs
208         exvector::const_iterator cit = v.begin(), citend = v.end();
209         while (cit != citend) {
210                 if (!is_ex_of_type(cit->op(0), diracone))
211                         s.push_back(*cit);
212                 cit++;
213         }
214
215         bool something_changed = false;
216         int sign = 1;
217
218         // Anticommute gamma5's to the front
219         if (s.size() >= 2) {
220                 exvector::iterator first = s.begin(), next_to_last = s.end() - 2;
221                 while (true) {
222                         exvector::iterator it = next_to_last;
223                         while (true) {
224                                 exvector::iterator it2 = it + 1;
225                                 if (!is_ex_of_type(it->op(0), diracgamma5) && is_ex_of_type(it2->op(0), diracgamma5)) {
226                                         it->swap(*it2);
227                                         sign = -sign;
228                                         something_changed = true;
229                                 }
230                                 if (it == first)
231                                         break;
232                                 it--;
233                         }
234                         if (next_to_last == first)
235                                 break;
236                         next_to_last--;
237                 }
238         }
239
240         // Remove squares of gamma5
241         while (s.size() >= 2 && is_ex_of_type(s[0].op(0), diracgamma5) && is_ex_of_type(s[1].op(0), diracgamma5)) {
242                 s.erase(s.begin(), s.begin() + 2);
243                 something_changed = true;
244         }
245
246         // Remove equal adjacent gammas
247         if (s.size() >= 2) {
248                 exvector::iterator it = s.begin(), itend = s.end() - 1;
249                 while (it != itend) {
250                         ex & a = it[0];
251                         ex & b = it[1];
252                         if (is_ex_of_type(a.op(0), diracgamma) && is_ex_of_type(b.op(0), diracgamma)) {
253                                 const ex & ia = a.op(1);
254                                 const ex & ib = b.op(1);
255                                 if (ia.is_equal(ib)) {
256                                         a = lorentz_g(ia, ib);
257                                         b = dirac_ONE(rl);
258                                         something_changed = true;
259                                 }
260                         }
261                         it++;
262                 }
263         }
264
265         if (s.size() == 0)
266                 return clifford(diracone(), rl) * sign;
267         if (something_changed)
268                 return nonsimplified_ncmul(s) * sign;
269         else
270                 return simplified_ncmul(s) * sign;
271 }
272
273 ex clifford::thisexprseq(const exvector & v) const
274 {
275         return clifford(representation_label, v);
276 }
277
278 ex clifford::thisexprseq(exvector * vp) const
279 {
280         return clifford(representation_label, vp);
281 }
282
283 //////////
284 // global functions
285 //////////
286
287 ex dirac_ONE(unsigned char rl)
288 {
289         return clifford(diracone(), rl);
290 }
291
292 ex dirac_gamma(const ex & mu, unsigned char rl)
293 {
294         if (!is_ex_of_type(mu, varidx))
295                 throw(std::invalid_argument("index of Dirac gamma must be of type varidx"));
296
297         return clifford(diracgamma(), mu, rl);
298 }
299
300 ex dirac_gamma5(unsigned char rl)
301 {
302         return clifford(diracgamma5(), rl);
303 }
304
305 ex dirac_slash(const ex & e, const ex & dim, unsigned char rl)
306 {
307         varidx mu((new symbol)->setflag(status_flags::dynallocated), dim);
308         return indexed(e, mu.toggle_variance()) * dirac_gamma(mu, rl);
309 }
310
311 /** Check whether a given tinfo key (as returned by return_type_tinfo()
312  *  is that of a clifford object with the specified representation label. */
313 static bool is_clifford_tinfo(unsigned ti, unsigned char rl)
314 {
315         return ti == (TINFO_clifford + rl);
316 }
317
318 ex dirac_trace(const ex & e, unsigned char rl)
319 {
320         if (is_ex_of_type(e, clifford)) {
321
322                 if (ex_to_clifford(e).get_representation_label() == rl
323                  && is_ex_of_type(e.op(0), diracone))
324                         return _ex4();
325                 else
326                         return _ex0();
327
328         } else if (is_ex_exactly_of_type(e, add)) {
329
330                 // Trace of sum = sum of traces
331                 ex sum = _ex0();
332                 for (unsigned i=0; i<e.nops(); i++)
333                         sum += dirac_trace(e.op(i), rl);
334                 return sum;
335
336         } else if (is_ex_exactly_of_type(e, mul)) {
337
338                 // Trace of product: pull out non-clifford factors
339                 ex prod = _ex1();
340                 for (unsigned i=0; i<e.nops(); i++) {
341                         const ex &o = e.op(i);
342                         unsigned ti = o.return_type_tinfo();
343                         if (is_clifford_tinfo(o.return_type_tinfo(), rl))
344                                 prod *= dirac_trace(o, rl);
345                         else
346                                 prod *= o;
347                 }
348                 return prod;
349
350         } else if (is_ex_exactly_of_type(e, ncmul)) {
351
352                 if (!is_clifford_tinfo(e.return_type_tinfo(), rl))
353                         return _ex0();
354
355                 // Expand product, if necessary
356                 ex e_expanded = e.expand();
357                 if (!is_ex_of_type(e_expanded, ncmul))
358                         return dirac_trace(e_expanded, rl);
359
360                 // gamma5 gets moved to the front so this check is enough
361                 bool has_gamma5 = is_ex_of_type(e.op(0).op(0), diracgamma5);
362                 unsigned num = e.nops();
363
364                 if (has_gamma5) {
365
366                         // Trace of gamma5 * odd number of gammas and trace of
367                         // gamma5 * gamma_mu * gamma_nu are zero
368                         if ((num & 1) == 0 || num == 3)
369                                 return _ex0();
370
371                         // Tr gamma5 S_2k =
372                         //   epsilon0123_mu1_mu2_mu3_mu4 * Tr gamma_mu1 gamma_mu2 gamma_mu3 gamma_mu4 S_2k
373                         ex dim = ex_to_idx(e.op(1).op(1)).get_dim();
374                         varidx mu1((new symbol)->setflag(status_flags::dynallocated), dim),
375                                mu2((new symbol)->setflag(status_flags::dynallocated), dim),
376                                mu3((new symbol)->setflag(status_flags::dynallocated), dim),
377                                mu4((new symbol)->setflag(status_flags::dynallocated), dim);
378                         exvector v;
379                         v.reserve(num + 3);
380                         v.push_back(dirac_gamma(mu1, rl));
381                         v.push_back(dirac_gamma(mu2, rl));
382                         v.push_back(dirac_gamma(mu3, rl));
383                         v.push_back(dirac_gamma(mu4, rl));
384                         for (int i=1; i<num; i++)
385                                 v.push_back(e.op(i));
386
387                         return (eps0123(mu1.toggle_variance(), mu2.toggle_variance(), mu3.toggle_variance(), mu4.toggle_variance()) *
388                                 dirac_trace(ncmul(v), rl)).simplify_indexed() / 24;
389
390                 } else { // no gamma5
391
392                         // Trace of odd number of gammas is zero
393                         if ((num & 1) == 1)
394                                 return _ex0();
395
396                         // Tr gamma_mu gamma_nu = 4 g_mu_nu
397                         if (num == 2)
398                                 return 4 * lorentz_g(e.op(0).op(1), e.op(1).op(1));
399
400                         // Traces of 4 or more gammas are computed recursively:
401                         // Tr gamma_mu1 gamma_mu2 ... gamma_mun =
402                         //   + eta_mu1_mu2 * Tr gamma_mu3 ... gamma_mun
403                         //   - eta_mu1_mu3 * Tr gamma_mu2 gamma_mu4 ... gamma_mun
404                         //   + eta_mu1_mu4 * Tr gamma_mu3 gamma_mu3 gamma_mu5 ... gamma_mun
405                         //   - ...
406                         //   + eta_mu1_mun * Tr gamma_mu2 ... gamma_mu(n-1)
407                         exvector v(num - 2);
408                         int sign = 1;
409                         const ex &ix1 = e.op(0).op(1);
410                         ex result;
411                         for (int i=1; i<num; i++) {
412                                 for (int n=1, j=0; n<num; n++) {
413                                         if (n == i)
414                                                 continue;
415                                         v[j++] = e.op(n);
416                                 }
417                                 result += sign * lorentz_g(ix1, e.op(i).op(1)) * dirac_trace(ncmul(v), rl);
418                                 sign = -sign;
419                         }
420                         return result;
421                 }
422         }
423
424         return _ex0();
425 }
426
427 } // namespace GiNaC