3 * Implementation of GiNaC's clifford algebra (Dirac gamma) objects. */
6 * GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
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.
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.
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
28 #include "numeric.h" // for I
31 #include "relational.h"
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)
47 // default constructor, destructor, copy constructor assignment operator and helpers
50 clifford::clifford() : representation_label(0)
52 debugmsg("clifford default constructor", LOGLEVEL_CONSTRUCT);
53 tinfo_key = TINFO_clifford;
56 void clifford::copy(const clifford & other)
58 inherited::copy(other);
59 representation_label = other.representation_label;
62 DEFAULT_DESTROY(clifford)
63 DEFAULT_CTORS(diracone)
64 DEFAULT_CTORS(diracgamma)
65 DEFAULT_CTORS(diracgamma5)
71 /** Construct object without any indices. This constructor is for internal
72 * use only. Use the dirac_ONE() function instead.
74 clifford::clifford(const ex & b, unsigned char rl) : inherited(b), representation_label(rl)
76 debugmsg("clifford constructor from ex", LOGLEVEL_CONSTRUCT);
77 tinfo_key = TINFO_clifford;
80 /** Construct object with one Lorentz index. This constructor is for internal
81 * use only. Use the dirac_gamma() function instead.
83 clifford::clifford(const ex & b, const ex & mu, unsigned char rl) : inherited(b, mu), representation_label(rl)
85 debugmsg("clifford constructor from ex,ex", LOGLEVEL_CONSTRUCT);
86 GINAC_ASSERT(is_ex_of_type(mu, varidx));
87 tinfo_key = TINFO_clifford;
90 clifford::clifford(unsigned char rl, const exvector & v, bool discardable) : inherited(sy_none(), v, discardable), representation_label(rl)
92 debugmsg("clifford constructor from unsigned char,exvector", LOGLEVEL_CONSTRUCT);
93 tinfo_key = TINFO_clifford;
96 clifford::clifford(unsigned char rl, exvector * vp) : inherited(sy_none(), vp), representation_label(rl)
98 debugmsg("clifford constructor from unsigned char,exvector *", LOGLEVEL_CONSTRUCT);
99 tinfo_key = TINFO_clifford;
106 clifford::clifford(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
108 debugmsg("clifford constructor from archive_node", LOGLEVEL_CONSTRUCT);
110 n.find_unsigned("label", rl);
111 representation_label = rl;
114 void clifford::archive(archive_node &n) const
116 inherited::archive(n);
117 n.add_unsigned("label", representation_label);
120 DEFAULT_UNARCHIVE(clifford)
121 DEFAULT_ARCHIVING(diracone)
122 DEFAULT_ARCHIVING(diracgamma)
123 DEFAULT_ARCHIVING(diracgamma5)
126 // functions overriding virtual functions from bases classes
129 int clifford::compare_same_type(const basic & other) const
131 GINAC_ASSERT(is_of_type(other, clifford));
132 const clifford &o = static_cast<const clifford &>(other);
134 if (representation_label != o.representation_label) {
135 // different representation label
136 return representation_label < o.representation_label ? -1 : 1;
139 return inherited::compare_same_type(other);
142 bool clifford::match_same_type(const basic & other) const
144 GINAC_ASSERT(is_of_type(other, clifford));
145 const clifford &o = static_cast<const clifford &>(other);
147 return representation_label == o.representation_label;
150 DEFAULT_COMPARE(diracone)
151 DEFAULT_COMPARE(diracgamma)
152 DEFAULT_COMPARE(diracgamma5)
154 DEFAULT_PRINT_LATEX(diracone, "ONE", "\\mathbb{1}")
155 DEFAULT_PRINT_LATEX(diracgamma, "gamma", "\\gamma")
156 DEFAULT_PRINT_LATEX(diracgamma5, "gamma5", "{\\gamma^5}")
158 /** Contraction of a gamma matrix with something else. */
159 bool diracgamma::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
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();
166 if (is_ex_of_type(*other, clifford)) {
168 ex dim = ex_to<idx>(self->op(1)).get_dim();
170 // gamma~mu gamma.mu = dim ONE
171 if (other - self == 1) {
173 *other = dirac_ONE(rl);
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)) {
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];
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)
197 exvector::iterator it = self + 1, next_to_last = other - 1;
198 while (it != other) {
199 if (!is_ex_of_type(*it, clifford))
206 while (it != next_to_last) {
211 *self = 2 * (*next_to_last) * S - (*self) * S * (*other) * (*next_to_last);
212 *next_to_last = _ex1();
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
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))
237 bool something_changed = false;
240 // Anticommute gamma5's to the front
242 exvector::iterator first = s.begin(), next_to_last = s.end() - 2;
244 exvector::iterator it = next_to_last;
246 exvector::iterator it2 = it + 1;
247 if (!is_ex_of_type(it->op(0), diracgamma5) && is_ex_of_type(it2->op(0), diracgamma5)) {
250 something_changed = true;
256 if (next_to_last == first)
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;
268 // Remove equal adjacent gammas
270 exvector::iterator it = s.begin(), itend = s.end() - 1;
271 while (it != itend) {
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;
288 return clifford(diracone(), representation_label) * sign;
289 if (something_changed)
290 return nonsimplified_ncmul(s) * sign;
292 return simplified_ncmul(s) * sign;
295 ex clifford::thisexprseq(const exvector & v) const
297 return clifford(representation_label, v);
300 ex clifford::thisexprseq(exvector * vp) const
302 return clifford(representation_label, vp);
309 ex dirac_ONE(unsigned char rl)
311 return clifford(diracone(), rl);
314 ex dirac_gamma(const ex & mu, unsigned char rl)
316 if (!is_ex_of_type(mu, varidx))
317 throw(std::invalid_argument("index of Dirac gamma must be of type varidx"));
319 return clifford(diracgamma(), mu, rl);
322 ex dirac_gamma5(unsigned char rl)
324 return clifford(diracgamma5(), rl);
327 ex dirac_gamma6(unsigned char rl)
329 return clifford(diracone(), rl) + clifford(diracgamma5(), rl);
332 ex dirac_gamma7(unsigned char rl)
334 return clifford(diracone(), rl) - clifford(diracgamma5(), rl);
337 ex dirac_slash(const ex & e, const ex & dim, unsigned char rl)
339 varidx mu((new symbol)->setflag(status_flags::dynallocated), dim);
340 return indexed(e, mu.toggle_variance()) * dirac_gamma(mu, rl);
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)
347 return ti == (TINFO_clifford + rl);
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)
354 return (ti & ~0xff) == TINFO_clifford;
357 /** Take trace of a string of an even number of Dirac gammas given a vector
359 static ex trace_string(exvector::const_iterator ix, unsigned num)
361 // Tr gamma.mu gamma.nu = 4 g.mu.nu
363 return lorentz_g(ix[0], ix[1]);
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
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]);
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
377 // + g.mu1.mun * Tr gamma.mu2 ... gamma.mu(n-1)
381 for (int i=1; i<num; i++) {
382 for (int n=1, j=0; n<num; n++) {
387 result += sign * lorentz_g(ix[0], ix[i]) * trace_string(v.begin(), num-2);
393 ex dirac_trace(const ex & e, unsigned char rl, const ex & trONE)
395 if (is_ex_of_type(e, clifford)) {
397 if (ex_to<clifford>(e).get_representation_label() == rl
398 && is_ex_of_type(e.op(0), diracone))
403 } else if (is_ex_exactly_of_type(e, mul)) {
405 // Trace of product: pull out non-clifford factors
407 for (unsigned i=0; i<e.nops(); i++) {
408 const ex &o = e.op(i);
409 unsigned ti = o.return_type_tinfo();
410 if (is_clifford_tinfo(o.return_type_tinfo(), rl))
411 prod *= dirac_trace(o, rl, trONE);
417 } else if (is_ex_exactly_of_type(e, ncmul)) {
419 if (!is_clifford_tinfo(e.return_type_tinfo(), rl))
422 // Expand product, if necessary
423 ex e_expanded = e.expand();
424 if (!is_ex_of_type(e_expanded, ncmul))
425 return dirac_trace(e_expanded, rl, trONE);
427 // gamma5 gets moved to the front so this check is enough
428 bool has_gamma5 = is_ex_of_type(e.op(0).op(0), diracgamma5);
429 unsigned num = e.nops();
433 // Trace of gamma5 * odd number of gammas and trace of
434 // gamma5 * gamma.mu * gamma.nu are zero
435 if ((num & 1) == 0 || num == 3)
438 // Tr gamma5 gamma.mu gamma.nu gamma.rho gamma.sigma = 4I * epsilon(mu, nu, rho, sigma)
440 return trONE * I * eps0123(e.op(1).op(1), e.op(2).op(1), e.op(3).op(1), e.op(4).op(1));
443 // I/4! * epsilon0123.mu1.mu2.mu3.mu4 * Tr gamma.mu1 gamma.mu2 gamma.mu3 gamma.mu4 S_2k
446 for (unsigned i=1; i<num; i++)
447 ix.push_back(e.op(i).op(1));
449 int *iv = new int[num];
451 for (int i=0; i<num-3; i++) {
453 for (int j=i+1; j<num-2; j++) {
455 for (int k=j+1; k<num-1; k++) {
457 for (int l=k+1; l<num; l++) {
459 iv[0] = i; iv[1] = j; iv[2] = k; iv[3] = l;
462 for (int n=0, t=4; n<num; n++) {
463 if (n == i || n == j || n == k || n == l)
468 int sign = permutation_sign(iv, iv + num);
469 result += sign * eps0123(idx1, idx2, idx3, idx4)
470 * trace_string(v.begin(), num - 4);
476 return trONE * I * result;
478 } else { // no gamma5
480 // Trace of odd number of gammas is zero
484 // Tr gamma.mu gamma.nu = 4 g.mu.nu
486 return trONE * lorentz_g(e.op(0).op(1), e.op(1).op(1));
490 for (unsigned i=0; i<num; i++)
491 iv.push_back(e.op(i).op(1));
493 return trONE * trace_string(iv.begin(), num);
496 } else if (e.nops() > 0) {
498 // Trace maps to all other container classes (this includes sums)
499 pointer_to_map_function_2args<unsigned char, const ex &> fcn(dirac_trace, rl, trONE);
506 ex canonicalize_clifford(const ex & e)
508 // Scan for any ncmul objects
510 ex aux = e.to_rational(srl);
511 for (unsigned i=0; i<srl.nops(); i++) {
513 ex lhs = srl.op(i).lhs();
514 ex rhs = srl.op(i).rhs();
516 if (is_ex_exactly_of_type(rhs, ncmul)
517 && rhs.return_type() == return_types::noncommutative
518 && is_clifford_tinfo(rhs.return_type_tinfo())) {
520 // Expand product, if necessary
521 ex rhs_expanded = rhs.expand();
522 if (!is_ex_of_type(rhs_expanded, ncmul)) {
523 srl.let_op(i) = (lhs == canonicalize_clifford(rhs_expanded));
526 } else if (!is_ex_of_type(rhs.op(0), clifford))
530 v.reserve(rhs.nops());
531 for (unsigned j=0; j<rhs.nops(); j++)
532 v.push_back(rhs.op(j));
534 // Stupid recursive bubble sort because we only want to swap adjacent gammas
535 exvector::iterator it = v.begin(), next_to_last = v.end() - 1;
536 if (is_ex_of_type(it->op(0), diracgamma5))
538 while (it != next_to_last) {
539 if (it[0].op(1).compare(it[1].op(1)) > 0) {
540 ex save0 = it[0], save1 = it[1];
541 it[0] = lorentz_g(it[0].op(1), it[1].op(1));
546 sum -= ncmul(v, true);
547 srl.let_op(i) = (lhs == canonicalize_clifford(sum));
555 return aux.subs(srl);