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 ctor, dtor, copy ctor, assignment operator and helpers
50 clifford::clifford() : representation_label(0)
52 tinfo_key = TINFO_clifford;
55 void clifford::copy(const clifford & other)
57 inherited::copy(other);
58 representation_label = other.representation_label;
61 DEFAULT_DESTROY(clifford)
62 DEFAULT_CTORS(diracone)
63 DEFAULT_CTORS(diracgamma)
64 DEFAULT_CTORS(diracgamma5)
70 /** Construct object without any indices. This constructor is for internal
71 * use only. Use the dirac_ONE() function instead.
73 clifford::clifford(const ex & b, unsigned char rl) : inherited(b), representation_label(rl)
75 tinfo_key = TINFO_clifford;
78 /** Construct object with one Lorentz index. This constructor is for internal
79 * use only. Use the dirac_gamma() function instead.
81 clifford::clifford(const ex & b, const ex & mu, unsigned char rl) : inherited(b, mu), representation_label(rl)
83 GINAC_ASSERT(is_a<varidx>(mu));
84 tinfo_key = TINFO_clifford;
87 clifford::clifford(unsigned char rl, const exvector & v, bool discardable) : inherited(sy_none(), v, discardable), representation_label(rl)
89 tinfo_key = TINFO_clifford;
92 clifford::clifford(unsigned char rl, exvector * vp) : inherited(sy_none(), vp), representation_label(rl)
94 tinfo_key = TINFO_clifford;
101 clifford::clifford(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
104 n.find_unsigned("label", rl);
105 representation_label = rl;
108 void clifford::archive(archive_node &n) const
110 inherited::archive(n);
111 n.add_unsigned("label", representation_label);
114 DEFAULT_UNARCHIVE(clifford)
115 DEFAULT_ARCHIVING(diracone)
116 DEFAULT_ARCHIVING(diracgamma)
117 DEFAULT_ARCHIVING(diracgamma5)
120 // functions overriding virtual functions from base classes
123 int clifford::compare_same_type(const basic & other) const
125 GINAC_ASSERT(is_a<clifford>(other));
126 const clifford &o = static_cast<const clifford &>(other);
128 if (representation_label != o.representation_label) {
129 // different representation label
130 return representation_label < o.representation_label ? -1 : 1;
133 return inherited::compare_same_type(other);
136 bool clifford::match_same_type(const basic & other) const
138 GINAC_ASSERT(is_a<clifford>(other));
139 const clifford &o = static_cast<const clifford &>(other);
141 return representation_label == o.representation_label;
144 void clifford::print(const print_context & c, unsigned level) const
146 if (!is_a<diracgamma5>(seq[0]) && !is_a<diracgamma>(seq[0]) && !is_a<diracone>(seq[0])) {
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)) {
153 seq[0].print(c, level);
154 c.s << "\\hspace{-1.0ex}/}";
156 seq[0].print(c, level);
161 inherited::print(c, level);
164 DEFAULT_COMPARE(diracone)
165 DEFAULT_COMPARE(diracgamma)
166 DEFAULT_COMPARE(diracgamma5)
168 DEFAULT_PRINT_LATEX(diracone, "ONE", "\\mathbb{1}")
169 DEFAULT_PRINT_LATEX(diracgamma, "gamma", "\\gamma")
170 DEFAULT_PRINT_LATEX(diracgamma5, "gamma5", "{\\gamma^5}")
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)
175 GINAC_ASSERT(is_a<clifford>(c));
176 GINAC_ASSERT(c.nops() == 2);
178 if (is_a<diracgamma>(c.op(0))) { // proper dirac gamma object
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());
188 /** Contraction of a gamma matrix with something else. */
189 bool diracgamma::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
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();
196 if (is_a<clifford>(*other)) {
198 ex dim = ex_to<idx>(self->op(1)).get_dim();
200 // gamma~mu gamma.mu = dim ONE
201 if (other - self == 1) {
203 *other = dirac_ONE(rl);
206 // gamma~mu gamma~alpha gamma.mu = (2-dim) gamma~alpha
207 } else if (other - self == 2
208 && is_a<clifford>(self[1])) {
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])) {
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];
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];
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)
242 exvector::iterator it = self + 1, next_to_last = other - 1;
243 while (it != other) {
244 if (!is_a<clifford>(*it))
251 while (it != next_to_last) {
256 *self = 2 * (*next_to_last) * S - (*self) * S * (*other) * (*next_to_last);
257 *next_to_last = _ex1;
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
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)))
282 bool something_changed = false;
285 // Anticommute gamma5's to the front
287 exvector::iterator first = s.begin(), next_to_last = s.end() - 2;
289 exvector::iterator it = next_to_last;
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))) {
295 something_changed = true;
301 if (next_to_last == first)
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;
313 // Remove equal adjacent gammas
315 exvector::iterator it, itend = s.end() - 1;
316 for (it = s.begin(); it != itend; ++it) {
319 if (!is_a<clifford>(a) || !is_a<clifford>(b))
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;
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;
343 return clifford(diracone(), representation_label) * sign;
344 if (something_changed)
345 return nonsimplified_ncmul(s) * sign;
347 return simplified_ncmul(s) * sign;
350 ex clifford::thisexprseq(const exvector & v) const
352 return clifford(representation_label, v);
355 ex clifford::thisexprseq(exvector * vp) const
357 return clifford(representation_label, vp);
364 ex dirac_ONE(unsigned char rl)
366 return clifford(diracone(), rl);
369 ex dirac_gamma(const ex & mu, unsigned char rl)
371 if (!is_a<varidx>(mu))
372 throw(std::invalid_argument("index of Dirac gamma must be of type varidx"));
374 return clifford(diracgamma(), mu, rl);
377 ex dirac_gamma5(unsigned char rl)
379 return clifford(diracgamma5(), rl);
382 ex dirac_gamma6(unsigned char rl)
384 return clifford(diracone(), rl) + clifford(diracgamma5(), rl);
387 ex dirac_gamma7(unsigned char rl)
389 return clifford(diracone(), rl) - clifford(diracgamma5(), rl);
392 ex dirac_slash(const ex & e, const ex & dim, unsigned char rl)
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);
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)
404 return ti == (TINFO_clifford + rl);
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)
411 return (ti & ~0xff) == TINFO_clifford;
414 /** Take trace of a string of an even number of Dirac gammas given a vector
416 static ex trace_string(exvector::const_iterator ix, unsigned num)
418 // Tr gamma.mu gamma.nu = 4 g.mu.nu
420 return lorentz_g(ix[0], ix[1]);
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
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]);
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
434 // + g.mu1.mun * Tr gamma.mu2 ... gamma.mu(n-1)
438 for (unsigned i=1; i<num; i++) {
439 for (unsigned n=1, j=0; n<num; n++) {
444 result += sign * lorentz_g(ix[0], ix[i]) * trace_string(v.begin(), num-2);
450 ex dirac_trace(const ex & e, unsigned char rl, const ex & trONE)
452 if (is_a<clifford>(e)) {
454 if (ex_to<clifford>(e).get_representation_label() == rl
455 && is_a<diracone>(e.op(0)))
460 } else if (is_ex_exactly_of_type(e, mul)) {
462 // Trace of product: pull out non-clifford factors
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);
473 } else if (is_ex_exactly_of_type(e, ncmul)) {
475 if (!is_clifford_tinfo(e.return_type_tinfo(), rl))
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);
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();
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)
494 // Tr gamma5 gamma.mu gamma.nu gamma.rho gamma.sigma = 4I * epsilon(mu, nu, rho, sigma)
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();
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]);
510 int *iv = new int[num];
512 for (unsigned i=0; i<num-3; i++) {
514 for (unsigned j=i+1; j<num-2; j++) {
516 for (unsigned k=j+1; k<num-1; k++) {
518 for (unsigned l=k+1; l<num; l++) {
520 iv[0] = i; iv[1] = j; iv[2] = k; iv[3] = l;
523 for (unsigned n=0, t=4; n<num; n++) {
524 if (n == i || n == j || n == k || n == l)
529 int sign = permutation_sign(iv, iv + num);
530 result += sign * eps0123(idx1, idx2, idx3, idx4)
531 * trace_string(v.begin(), num - 4);
537 return trONE * I * result * mul(bv);
539 } else { // no gamma5
541 // Trace of odd number of gammas is zero
545 // Tr gamma.mu gamma.nu = 4 g.mu.nu
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();
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]);
557 return trONE * (trace_string(iv.begin(), num) * mul(bv)).simplify_indexed();
560 } else if (e.nops() > 0) {
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);
570 ex canonicalize_clifford(const ex & e)
572 // Scan for any ncmul objects
574 ex aux = e.to_rational(srl);
575 for (unsigned i=0; i<srl.nops(); i++) {
577 ex lhs = srl.op(i).lhs();
578 ex rhs = srl.op(i).rhs();
580 if (is_ex_exactly_of_type(rhs, ncmul)
581 && rhs.return_type() == return_types::noncommutative
582 && is_clifford_tinfo(rhs.return_type_tinfo())) {
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));
590 } else if (!is_a<clifford>(rhs.op(0)))
594 v.reserve(rhs.nops());
595 for (unsigned j=0; j<rhs.nops(); j++)
596 v.push_back(rhs.op(j));
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)))
602 while (it != next_to_last) {
603 if (it[0].compare(it[1]) > 0) {
604 ex save0 = it[0], save1 = it[1];
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();
613 sum -= ncmul(v, true);
614 srl.let_op(i) = (lhs == canonicalize_clifford(sum));
622 return aux.subs(srl).simplify_indexed();