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(other.tinfo() == TINFO_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 DEFAULT_COMPARE(diracone)
143 DEFAULT_COMPARE(diracgamma)
144 DEFAULT_COMPARE(diracgamma5)
146 DEFAULT_PRINT_LATEX(diracone, "ONE", "\\mathbb{1}")
147 DEFAULT_PRINT_LATEX(diracgamma, "gamma", "\\gamma")
148 DEFAULT_PRINT_LATEX(diracgamma5, "gamma5", "{\\gamma^5}")
150 /** Contraction of a gamma matrix with something else. */
151 bool diracgamma::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
153 GINAC_ASSERT(is_ex_of_type(*self, clifford));
154 GINAC_ASSERT(is_ex_of_type(*other, indexed));
155 GINAC_ASSERT(is_ex_of_type(self->op(0), diracgamma));
156 unsigned char rl = ex_to<clifford>(*self).get_representation_label();
158 if (is_ex_of_type(*other, clifford)) {
160 ex dim = ex_to<idx>(self->op(1)).get_dim();
162 // gamma~mu gamma.mu = dim ONE
163 if (other - self == 1) {
165 *other = dirac_ONE(rl);
168 // gamma~mu gamma~alpha gamma.mu = (2-dim) gamma~alpha
169 } else if (other - self == 2
170 && is_ex_of_type(self[1], clifford)) {
175 // gamma~mu gamma~alpha gamma~beta gamma.mu = 4 g~alpha~beta + (dim-4) gamam~alpha gamma~beta
176 } else if (other - self == 3
177 && is_ex_of_type(self[1], clifford)
178 && is_ex_of_type(self[2], clifford)) {
179 *self = 4 * lorentz_g(self[1].op(1), self[2].op(1)) * dirac_ONE(rl) + (dim - 4) * self[1] * self[2];
185 // gamma~mu S gamma~alpha gamma.mu = 2 gamma~alpha S - gamma~mu S gamma.mu gamma~alpha
186 // (commutate contracted indices towards each other, simplify_indexed()
187 // will re-expand and re-run the simplification)
189 exvector::iterator it = self + 1, next_to_last = other - 1;
190 while (it != other) {
191 if (!is_ex_of_type(*it, clifford))
198 while (it != next_to_last) {
203 *self = 2 * (*next_to_last) * S - (*self) * S * (*other) * (*next_to_last);
204 *next_to_last = _ex1();
213 /** Perform automatic simplification on noncommutative product of clifford
214 * objects. This removes superfluous ONEs, permutes gamma5's to the front
215 * and removes squares of gamma objects. */
216 ex clifford::simplify_ncmul(const exvector & v) const
221 // Remove superfluous ONEs
222 exvector::const_iterator cit = v.begin(), citend = v.end();
223 while (cit != citend) {
224 if (!is_ex_of_type(cit->op(0), diracone))
229 bool something_changed = false;
232 // Anticommute gamma5's to the front
234 exvector::iterator first = s.begin(), next_to_last = s.end() - 2;
236 exvector::iterator it = next_to_last;
238 exvector::iterator it2 = it + 1;
239 if (!is_ex_of_type(it->op(0), diracgamma5) && is_ex_of_type(it2->op(0), diracgamma5)) {
242 something_changed = true;
248 if (next_to_last == first)
254 // Remove squares of gamma5
255 while (s.size() >= 2 && is_ex_of_type(s[0].op(0), diracgamma5) && is_ex_of_type(s[1].op(0), diracgamma5)) {
256 s.erase(s.begin(), s.begin() + 2);
257 something_changed = true;
260 // Remove equal adjacent gammas
262 exvector::iterator it = s.begin(), itend = s.end() - 1;
263 while (it != itend) {
266 if (is_ex_of_type(a.op(0), diracgamma) && is_ex_of_type(b.op(0), diracgamma)) {
267 const ex & ia = a.op(1);
268 const ex & ib = b.op(1);
269 if (ia.is_equal(ib)) {
270 a = lorentz_g(ia, ib);
271 b = dirac_ONE(representation_label);
272 something_changed = true;
280 return clifford(diracone(), representation_label) * sign;
281 if (something_changed)
282 return nonsimplified_ncmul(s) * sign;
284 return simplified_ncmul(s) * sign;
287 ex clifford::thisexprseq(const exvector & v) const
289 return clifford(representation_label, v);
292 ex clifford::thisexprseq(exvector * vp) const
294 return clifford(representation_label, vp);
301 ex dirac_ONE(unsigned char rl)
303 return clifford(diracone(), rl);
306 ex dirac_gamma(const ex & mu, unsigned char rl)
308 if (!is_ex_of_type(mu, varidx))
309 throw(std::invalid_argument("index of Dirac gamma must be of type varidx"));
311 return clifford(diracgamma(), mu, rl);
314 ex dirac_gamma5(unsigned char rl)
316 return clifford(diracgamma5(), rl);
319 ex dirac_gamma6(unsigned char rl)
321 return clifford(diracone(), rl) + clifford(diracgamma5(), rl);
324 ex dirac_gamma7(unsigned char rl)
326 return clifford(diracone(), rl) - clifford(diracgamma5(), rl);
329 ex dirac_slash(const ex & e, const ex & dim, unsigned char rl)
331 varidx mu((new symbol)->setflag(status_flags::dynallocated), dim);
332 return indexed(e, mu.toggle_variance()) * dirac_gamma(mu, rl);
335 /** Check whether a given tinfo key (as returned by return_type_tinfo()
336 * is that of a clifford object with the specified representation label. */
337 static bool is_clifford_tinfo(unsigned ti, unsigned char rl)
339 return ti == (TINFO_clifford + rl);
342 /** Check whether a given tinfo key (as returned by return_type_tinfo()
343 * is that of a clifford object (with an arbitrary representation label). */
344 static bool is_clifford_tinfo(unsigned ti)
346 return (ti & ~0xff) == TINFO_clifford;
349 /** Take trace of a string of an even number of Dirac gammas given a vector
351 static ex trace_string(exvector::const_iterator ix, unsigned num)
353 // Tr gamma.mu gamma.nu = 4 g.mu.nu
355 return lorentz_g(ix[0], ix[1]);
357 // 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
359 return lorentz_g(ix[0], ix[1]) * lorentz_g(ix[2], ix[3])
360 + lorentz_g(ix[1], ix[2]) * lorentz_g(ix[0], ix[3])
361 - lorentz_g(ix[0], ix[2]) * lorentz_g(ix[1], ix[3]);
363 // Traces of 6 or more gammas are computed recursively:
364 // Tr gamma.mu1 gamma.mu2 ... gamma.mun =
365 // + g.mu1.mu2 * Tr gamma.mu3 ... gamma.mun
366 // - g.mu1.mu3 * Tr gamma.mu2 gamma.mu4 ... gamma.mun
367 // + g.mu1.mu4 * Tr gamma.mu3 gamma.mu3 gamma.mu5 ... gamma.mun
369 // + g.mu1.mun * Tr gamma.mu2 ... gamma.mu(n-1)
373 for (int i=1; i<num; i++) {
374 for (int n=1, j=0; n<num; n++) {
379 result += sign * lorentz_g(ix[0], ix[i]) * trace_string(v.begin(), num-2);
385 ex dirac_trace(const ex & e, unsigned char rl, const ex & trONE)
387 if (is_ex_of_type(e, clifford)) {
389 if (ex_to<clifford>(e).get_representation_label() == rl
390 && is_ex_of_type(e.op(0), diracone))
395 } else if (is_ex_exactly_of_type(e, mul)) {
397 // Trace of product: pull out non-clifford factors
399 for (unsigned i=0; i<e.nops(); i++) {
400 const ex &o = e.op(i);
401 unsigned ti = o.return_type_tinfo();
402 if (is_clifford_tinfo(o.return_type_tinfo(), rl))
403 prod *= dirac_trace(o, rl, trONE);
409 } else if (is_ex_exactly_of_type(e, ncmul)) {
411 if (!is_clifford_tinfo(e.return_type_tinfo(), rl))
414 // Expand product, if necessary
415 ex e_expanded = e.expand();
416 if (!is_ex_of_type(e_expanded, ncmul))
417 return dirac_trace(e_expanded, rl, trONE);
419 // gamma5 gets moved to the front so this check is enough
420 bool has_gamma5 = is_ex_of_type(e.op(0).op(0), diracgamma5);
421 unsigned num = e.nops();
425 // Trace of gamma5 * odd number of gammas and trace of
426 // gamma5 * gamma.mu * gamma.nu are zero
427 if ((num & 1) == 0 || num == 3)
430 // Tr gamma5 gamma.mu gamma.nu gamma.rho gamma.sigma = 4I * epsilon(mu, nu, rho, sigma)
432 return trONE * I * eps0123(e.op(1).op(1), e.op(2).op(1), e.op(3).op(1), e.op(4).op(1));
435 // I/4! * epsilon0123.mu1.mu2.mu3.mu4 * Tr gamma.mu1 gamma.mu2 gamma.mu3 gamma.mu4 S_2k
438 for (unsigned i=1; i<num; i++)
439 ix.push_back(e.op(i).op(1));
441 int *iv = new int[num];
443 for (int i=0; i<num-3; i++) {
445 for (int j=i+1; j<num-2; j++) {
447 for (int k=j+1; k<num-1; k++) {
449 for (int l=k+1; l<num; l++) {
451 iv[0] = i; iv[1] = j; iv[2] = k; iv[3] = l;
454 for (int n=0, t=4; n<num; n++) {
455 if (n == i || n == j || n == k || n == l)
460 int sign = permutation_sign(iv, iv + num);
461 result += sign * eps0123(idx1, idx2, idx3, idx4)
462 * trace_string(v.begin(), num - 4);
468 return trONE * I * result;
470 } else { // no gamma5
472 // Trace of odd number of gammas is zero
476 // Tr gamma.mu gamma.nu = 4 g.mu.nu
478 return trONE * lorentz_g(e.op(0).op(1), e.op(1).op(1));
482 for (unsigned i=0; i<num; i++)
483 iv.push_back(e.op(i).op(1));
485 return trONE * trace_string(iv.begin(), num);
488 } else if (e.nops() > 0) {
490 // Trace maps to all other container classes (this includes sums)
491 pointer_to_map_function_2args<unsigned char, const ex &> fcn(dirac_trace, rl, trONE);
498 ex canonicalize_clifford(const ex & e)
500 // Scan for any ncmul objects
502 ex aux = e.to_rational(srl);
503 for (unsigned i=0; i<srl.nops(); i++) {
505 ex lhs = srl.op(i).lhs();
506 ex rhs = srl.op(i).rhs();
508 if (is_ex_exactly_of_type(rhs, ncmul)
509 && rhs.return_type() == return_types::noncommutative
510 && is_clifford_tinfo(rhs.return_type_tinfo())) {
512 // Expand product, if necessary
513 ex rhs_expanded = rhs.expand();
514 if (!is_ex_of_type(rhs_expanded, ncmul)) {
515 srl.let_op(i) = (lhs == canonicalize_clifford(rhs_expanded));
518 } else if (!is_ex_of_type(rhs.op(0), clifford))
522 v.reserve(rhs.nops());
523 for (unsigned j=0; j<rhs.nops(); j++)
524 v.push_back(rhs.op(j));
526 // Stupid recursive bubble sort because we only want to swap adjacent gammas
527 exvector::iterator it = v.begin(), next_to_last = v.end() - 1;
528 if (is_ex_of_type(it->op(0), diracgamma5))
530 while (it != next_to_last) {
531 if (it[0].op(1).compare(it[1].op(1)) > 0) {
532 ex save0 = it[0], save1 = it[1];
533 it[0] = lorentz_g(it[0].op(1), it[1].op(1));
538 sum -= ncmul(v, true);
539 srl.let_op(i) = (lhs == canonicalize_clifford(sum));
547 return aux.subs(srl);