3 * Implementation of GiNaC's clifford algebra (Dirac gamma) objects. */
6 * GiNaC Copyright (C) 1999-2003 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
31 #include "numeric.h" // for I
34 #include "relational.h"
42 GINAC_IMPLEMENT_REGISTERED_CLASS(clifford, indexed)
43 GINAC_IMPLEMENT_REGISTERED_CLASS(diracone, tensor)
44 GINAC_IMPLEMENT_REGISTERED_CLASS(diracgamma, tensor)
45 GINAC_IMPLEMENT_REGISTERED_CLASS(diracgamma5, tensor)
46 GINAC_IMPLEMENT_REGISTERED_CLASS(diracgammaL, tensor)
47 GINAC_IMPLEMENT_REGISTERED_CLASS(diracgammaR, tensor)
50 // default ctor, dtor, copy ctor, assignment operator and helpers
53 clifford::clifford() : representation_label(0)
55 tinfo_key = TINFO_clifford;
58 void clifford::copy(const clifford & other)
60 inherited::copy(other);
61 representation_label = other.representation_label;
64 DEFAULT_DESTROY(clifford)
65 DEFAULT_CTORS(diracone)
66 DEFAULT_CTORS(diracgamma)
67 DEFAULT_CTORS(diracgamma5)
68 DEFAULT_CTORS(diracgammaL)
69 DEFAULT_CTORS(diracgammaR)
75 /** Construct object without any indices. This constructor is for internal
76 * use only. Use the dirac_ONE() function instead.
78 clifford::clifford(const ex & b, unsigned char rl) : inherited(b), representation_label(rl)
80 tinfo_key = TINFO_clifford;
83 /** Construct object with one Lorentz index. This constructor is for internal
84 * use only. Use the dirac_gamma() function instead.
86 clifford::clifford(const ex & b, const ex & mu, unsigned char rl) : inherited(b, mu), representation_label(rl)
88 GINAC_ASSERT(is_a<varidx>(mu));
89 tinfo_key = TINFO_clifford;
92 clifford::clifford(unsigned char rl, const exvector & v, bool discardable) : inherited(sy_none(), v, discardable), representation_label(rl)
94 tinfo_key = TINFO_clifford;
97 clifford::clifford(unsigned char rl, exvector * vp) : inherited(sy_none(), vp), representation_label(rl)
99 tinfo_key = TINFO_clifford;
106 clifford::clifford(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
109 n.find_unsigned("label", rl);
110 representation_label = rl;
113 void clifford::archive(archive_node &n) const
115 inherited::archive(n);
116 n.add_unsigned("label", representation_label);
119 DEFAULT_UNARCHIVE(clifford)
120 DEFAULT_ARCHIVING(diracone)
121 DEFAULT_ARCHIVING(diracgamma)
122 DEFAULT_ARCHIVING(diracgamma5)
123 DEFAULT_ARCHIVING(diracgammaL)
124 DEFAULT_ARCHIVING(diracgammaR)
127 // functions overriding virtual functions from base classes
130 int clifford::compare_same_type(const basic & other) const
132 GINAC_ASSERT(is_a<clifford>(other));
133 const clifford &o = static_cast<const clifford &>(other);
135 if (representation_label != o.representation_label) {
136 // different representation label
137 return representation_label < o.representation_label ? -1 : 1;
140 return inherited::compare_same_type(other);
143 bool clifford::match_same_type(const basic & other) const
145 GINAC_ASSERT(is_a<clifford>(other));
146 const clifford &o = static_cast<const clifford &>(other);
148 return representation_label == o.representation_label;
151 void clifford::print(const print_context & c, unsigned level) const
153 if (!is_a<diracgamma5>(seq[0]) && !is_a<diracgammaL>(seq[0]) &&
154 !is_a<diracgammaR>(seq[0]) && !is_a<diracgamma>(seq[0]) &&
155 !is_a<diracone>(seq[0])) {
157 // dirac_slash() object is printed differently
158 if (is_a<print_tree>(c))
159 inherited::print(c, level);
160 else if (is_a<print_latex>(c)) {
162 seq[0].print(c, level);
163 c.s << "\\hspace{-1.0ex}/}";
165 seq[0].print(c, level);
170 inherited::print(c, level);
173 DEFAULT_COMPARE(diracone)
174 DEFAULT_COMPARE(diracgamma)
175 DEFAULT_COMPARE(diracgamma5)
176 DEFAULT_COMPARE(diracgammaL)
177 DEFAULT_COMPARE(diracgammaR)
179 DEFAULT_PRINT_LATEX(diracone, "ONE", "\\mathbb{1}")
180 DEFAULT_PRINT_LATEX(diracgamma, "gamma", "\\gamma")
181 DEFAULT_PRINT_LATEX(diracgamma5, "gamma5", "{\\gamma^5}")
182 DEFAULT_PRINT_LATEX(diracgammaL, "gammaL", "{\\gamma_L}")
183 DEFAULT_PRINT_LATEX(diracgammaR, "gammaR", "{\\gamma_R}")
185 /** This function decomposes gamma~mu -> (1, mu) and a\ -> (a.ix, ix) */
186 static void base_and_index(const ex & c, ex & b, ex & i)
188 GINAC_ASSERT(is_a<clifford>(c));
189 GINAC_ASSERT(c.nops() == 2);
191 if (is_a<diracgamma>(c.op(0))) { // proper dirac gamma object
194 } else if (is_a<diracgamma5>(c.op(0)) || is_a<diracgammaL>(c.op(0)) || is_a<diracgammaR>(c.op(0))) { // gamma5/L/R
197 } else { // slash object, generate new dummy index
198 varidx ix((new symbol)->setflag(status_flags::dynallocated), ex_to<idx>(c.op(1)).get_dim());
199 b = indexed(c.op(0), ix.toggle_variance());
204 /** Contraction of a gamma matrix with something else. */
205 bool diracgamma::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
207 GINAC_ASSERT(is_a<clifford>(*self));
208 GINAC_ASSERT(is_a<indexed>(*other));
209 GINAC_ASSERT(is_a<diracgamma>(self->op(0)));
210 unsigned char rl = ex_to<clifford>(*self).get_representation_label();
212 ex dim = ex_to<idx>(self->op(1)).get_dim();
213 if (other->nops() > 1)
214 dim = minimal_dim(dim, ex_to<idx>(self->op(1)).get_dim());
216 if (is_a<clifford>(*other)) {
218 // Contraction only makes sense if the represenation labels are equal
219 if (ex_to<clifford>(*other).get_representation_label() != rl)
222 // gamma~mu gamma.mu = dim ONE
223 if (other - self == 1) {
225 *other = dirac_ONE(rl);
228 // gamma~mu gamma~alpha gamma.mu = (2-dim) gamma~alpha
229 } else if (other - self == 2
230 && is_a<clifford>(self[1])) {
235 // gamma~mu gamma~alpha gamma~beta gamma.mu = 4 g~alpha~beta + (dim-4) gamam~alpha gamma~beta
236 } else if (other - self == 3
237 && is_a<clifford>(self[1])
238 && is_a<clifford>(self[2])) {
240 base_and_index(self[1], b1, i1);
241 base_and_index(self[2], b2, i2);
242 *self = 4 * lorentz_g(i1, i2) * b1 * b2 * dirac_ONE(rl) + (dim - 4) * self[1] * self[2];
248 // 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
249 } else if (other - self == 4
250 && is_a<clifford>(self[1])
251 && is_a<clifford>(self[2])
252 && is_a<clifford>(self[3])) {
253 *self = -2 * self[3] * self[2] * self[1] - (dim - 4) * self[1] * self[2] * self[3];
260 // gamma~mu S gamma~alpha gamma.mu = 2 gamma~alpha S - gamma~mu S gamma.mu gamma~alpha
261 // (commutate contracted indices towards each other, simplify_indexed()
262 // will re-expand and re-run the simplification)
264 exvector::iterator it = self + 1, next_to_last = other - 1;
265 while (it != other) {
266 if (!is_a<clifford>(*it))
273 while (it != next_to_last) {
278 *self = 2 * (*next_to_last) * S - (*self) * S * (*other) * (*next_to_last);
279 *next_to_last = _ex1;
284 } else if (is_a<symbol>(other->op(0)) && other->nops() == 2) {
286 // x.mu gamma~mu -> x-slash
287 *self = dirac_slash(other->op(0), dim, rl);
295 /** Perform automatic simplification on noncommutative product of clifford
296 * objects. This removes superfluous ONEs, permutes gamma5/L/R's to the front
297 * and removes squares of gamma objects. */
298 ex clifford::simplify_ncmul(const exvector & v) const
303 // Remove superfluous ONEs
304 exvector::const_iterator cit = v.begin(), citend = v.end();
305 while (cit != citend) {
306 if (!is_a<clifford>(*cit) || !is_a<diracone>(cit->op(0)))
311 bool something_changed = false;
314 // Anticommute gamma5/L/R's to the front
316 exvector::iterator first = s.begin(), next_to_last = s.end() - 2;
318 exvector::iterator it = next_to_last;
320 exvector::iterator it2 = it + 1;
321 if (is_a<clifford>(*it) && is_a<clifford>(*it2)) {
322 ex e1 = it->op(0), e2 = it2->op(0);
324 if (is_a<diracgamma5>(e2)) {
326 if (is_a<diracgammaL>(e1) || is_a<diracgammaR>(e1)) {
328 // gammaL/R gamma5 -> gamma5 gammaL/R
330 something_changed = true;
332 } else if (!is_a<diracgamma5>(e1)) {
334 // gamma5 gamma5 -> gamma5 gamma5 (do nothing)
335 // x gamma5 -> -gamma5 x
338 something_changed = true;
341 } else if (is_a<diracgammaL>(e2)) {
343 if (is_a<diracgammaR>(e1)) {
345 // gammaR gammaL -> 0
348 } else if (!is_a<diracgammaL>(e1) && !is_a<diracgamma5>(e1)) {
350 // gammaL gammaL -> gammaL gammaL (do nothing)
351 // gamma5 gammaL -> gamma5 gammaL (do nothing)
352 // x gammaL -> gammaR x
354 *it = clifford(diracgammaR(), ex_to<clifford>(*it).get_representation_label());
355 something_changed = true;
358 } else if (is_a<diracgammaR>(e2)) {
360 if (is_a<diracgammaL>(e1)) {
362 // gammaL gammaR -> 0
365 } else if (!is_a<diracgammaR>(e1) && !is_a<diracgamma5>(e1)) {
367 // gammaR gammaR -> gammaR gammaR (do nothing)
368 // gamma5 gammaR -> gamma5 gammaR (do nothing)
369 // x gammaR -> gammaL x
371 *it = clifford(diracgammaL(), ex_to<clifford>(*it).get_representation_label());
372 something_changed = true;
380 if (next_to_last == first)
386 // Remove equal adjacent gammas
388 exvector::iterator it, itend = s.end() - 1;
389 for (it = s.begin(); it != itend; ++it) {
392 if (!is_a<clifford>(a) || !is_a<clifford>(b))
395 const ex & ag = a.op(0);
396 const ex & bg = b.op(0);
397 bool a_is_diracgamma = is_a<diracgamma>(ag);
398 bool b_is_diracgamma = is_a<diracgamma>(bg);
400 if (a_is_diracgamma && b_is_diracgamma) {
402 const ex & ia = a.op(1);
403 const ex & ib = b.op(1);
404 if (ia.is_equal(ib)) { // gamma~alpha gamma~alpha -> g~alpha~alpha
405 a = lorentz_g(ia, ib);
406 b = dirac_ONE(representation_label);
407 something_changed = true;
410 } else if ((is_a<diracgamma5>(ag) && is_a<diracgamma5>(bg))) {
412 // Remove squares of gamma5
413 a = dirac_ONE(representation_label);
414 b = dirac_ONE(representation_label);
415 something_changed = true;
417 } else if ((is_a<diracgammaL>(ag) && is_a<diracgammaL>(bg))
418 || (is_a<diracgammaR>(ag) && is_a<diracgammaR>(bg))) {
420 // Remove squares of gammaL/R
421 b = dirac_ONE(representation_label);
422 something_changed = true;
424 } else if (is_a<diracgammaL>(ag) && is_a<diracgammaR>(bg)) {
426 // gammaL and gammaR are orthogonal
429 } else if (is_a<diracgamma5>(ag) && is_a<diracgammaL>(bg)) {
431 // gamma5 gammaL -> -gammaL
432 a = dirac_ONE(representation_label);
434 something_changed = true;
436 } else if (is_a<diracgamma5>(ag) && is_a<diracgammaR>(bg)) {
438 // gamma5 gammaR -> gammaR
439 a = dirac_ONE(representation_label);
440 something_changed = true;
442 } else if (!a_is_diracgamma && !b_is_diracgamma && ag.is_equal(bg)) {
445 varidx ix((new symbol)->setflag(status_flags::dynallocated), ex_to<idx>(a.op(1)).minimal_dim(ex_to<idx>(b.op(1))));
446 a = indexed(ag, ix) * indexed(ag, ix.toggle_variance());
447 b = dirac_ONE(representation_label);
448 something_changed = true;
454 return clifford(diracone(), representation_label) * sign;
455 if (something_changed)
456 return nonsimplified_ncmul(s) * sign;
458 return simplified_ncmul(s) * sign;
461 ex clifford::thisexprseq(const exvector & v) const
463 return clifford(representation_label, v);
466 ex clifford::thisexprseq(exvector * vp) const
468 return clifford(representation_label, vp);
475 ex dirac_ONE(unsigned char rl)
477 return clifford(diracone(), rl);
480 ex dirac_gamma(const ex & mu, unsigned char rl)
482 if (!is_a<varidx>(mu))
483 throw(std::invalid_argument("index of Dirac gamma must be of type varidx"));
485 return clifford(diracgamma(), mu, rl);
488 ex dirac_gamma5(unsigned char rl)
490 return clifford(diracgamma5(), rl);
493 ex dirac_gammaL(unsigned char rl)
495 return clifford(diracgammaL(), rl);
498 ex dirac_gammaR(unsigned char rl)
500 return clifford(diracgammaR(), rl);
503 ex dirac_gamma6(unsigned char rl)
505 return clifford(diracone(), rl) + clifford(diracgamma5(), rl);
508 ex dirac_gamma7(unsigned char rl)
510 return clifford(diracone(), rl) - clifford(diracgamma5(), rl);
513 ex dirac_slash(const ex & e, const ex & dim, unsigned char rl)
515 // Slashed vectors are actually stored as a clifford object with the
516 // vector as its base expression and a (dummy) index that just serves
517 // for storing the space dimensionality
518 return clifford(e, varidx(0, dim), rl);
521 /** Check whether a given tinfo key (as returned by return_type_tinfo()
522 * is that of a clifford object with the specified representation label. */
523 static bool is_clifford_tinfo(unsigned ti, unsigned char rl)
525 return ti == (TINFO_clifford + rl);
528 /** Check whether a given tinfo key (as returned by return_type_tinfo()
529 * is that of a clifford object (with an arbitrary representation label). */
530 static bool is_clifford_tinfo(unsigned ti)
532 return (ti & ~0xff) == TINFO_clifford;
535 /** Take trace of a string of an even number of Dirac gammas given a vector
537 static ex trace_string(exvector::const_iterator ix, unsigned num)
539 // Tr gamma.mu gamma.nu = 4 g.mu.nu
541 return lorentz_g(ix[0], ix[1]);
543 // 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
545 return lorentz_g(ix[0], ix[1]) * lorentz_g(ix[2], ix[3])
546 + lorentz_g(ix[1], ix[2]) * lorentz_g(ix[0], ix[3])
547 - lorentz_g(ix[0], ix[2]) * lorentz_g(ix[1], ix[3]);
549 // Traces of 6 or more gammas are computed recursively:
550 // Tr gamma.mu1 gamma.mu2 ... gamma.mun =
551 // + g.mu1.mu2 * Tr gamma.mu3 ... gamma.mun
552 // - g.mu1.mu3 * Tr gamma.mu2 gamma.mu4 ... gamma.mun
553 // + g.mu1.mu4 * Tr gamma.mu3 gamma.mu3 gamma.mu5 ... gamma.mun
555 // + g.mu1.mun * Tr gamma.mu2 ... gamma.mu(n-1)
559 for (unsigned i=1; i<num; i++) {
560 for (unsigned n=1, j=0; n<num; n++) {
565 result += sign * lorentz_g(ix[0], ix[i]) * trace_string(v.begin(), num-2);
571 ex dirac_trace(const ex & e, unsigned char rl, const ex & trONE)
573 if (is_a<clifford>(e)) {
575 if (!ex_to<clifford>(e).get_representation_label() == rl)
577 const ex & g = e.op(0);
578 if (is_a<diracone>(g))
580 else if (is_a<diracgammaL>(g) || is_a<diracgammaR>(g))
585 } else if (is_ex_exactly_of_type(e, mul)) {
587 // Trace of product: pull out non-clifford factors
589 for (unsigned i=0; i<e.nops(); i++) {
590 const ex &o = e.op(i);
591 if (is_clifford_tinfo(o.return_type_tinfo(), rl))
592 prod *= dirac_trace(o, rl, trONE);
598 } else if (is_ex_exactly_of_type(e, ncmul)) {
600 if (!is_clifford_tinfo(e.return_type_tinfo(), rl))
603 // Substitute gammaL/R and expand product, if necessary
604 ex e_expanded = e.subs(lst(
605 dirac_gammaL(rl) == (dirac_ONE(rl)-dirac_gamma5(rl))/2,
606 dirac_gammaR(rl) == (dirac_ONE(rl)+dirac_gamma5(rl))/2
608 if (!is_a<ncmul>(e_expanded))
609 return dirac_trace(e_expanded, rl, trONE);
611 // gamma5 gets moved to the front so this check is enough
612 bool has_gamma5 = is_a<diracgamma5>(e.op(0).op(0));
613 unsigned num = e.nops();
617 // Trace of gamma5 * odd number of gammas and trace of
618 // gamma5 * gamma.mu * gamma.nu are zero
619 if ((num & 1) == 0 || num == 3)
622 // Tr gamma5 gamma.mu gamma.nu gamma.rho gamma.sigma = 4I * epsilon(mu, nu, rho, sigma)
623 // (the epsilon is always 4-dimensional)
625 ex b1, i1, b2, i2, b3, i3, b4, i4;
626 base_and_index(e.op(1), b1, i1);
627 base_and_index(e.op(2), b2, i2);
628 base_and_index(e.op(3), b3, i3);
629 base_and_index(e.op(4), b4, i4);
630 return trONE * I * (lorentz_eps(ex_to<idx>(i1).replace_dim(_ex4), ex_to<idx>(i2).replace_dim(_ex4), ex_to<idx>(i3).replace_dim(_ex4), ex_to<idx>(i4).replace_dim(_ex4)) * b1 * b2 * b3 * b4).simplify_indexed();
634 // I/4! * epsilon0123.mu1.mu2.mu3.mu4 * Tr gamma.mu1 gamma.mu2 gamma.mu3 gamma.mu4 S_2k
635 // (the epsilon is always 4-dimensional)
636 exvector ix(num-1), bv(num-1);
637 for (unsigned i=1; i<num; i++)
638 base_and_index(e.op(i), bv[i-1], ix[i-1]);
640 int *iv = new int[num];
642 for (unsigned i=0; i<num-3; i++) {
644 for (unsigned j=i+1; j<num-2; j++) {
646 for (unsigned k=j+1; k<num-1; k++) {
648 for (unsigned l=k+1; l<num; l++) {
650 iv[0] = i; iv[1] = j; iv[2] = k; iv[3] = l;
653 for (unsigned n=0, t=4; n<num; n++) {
654 if (n == i || n == j || n == k || n == l)
659 int sign = permutation_sign(iv, iv + num);
660 result += sign * lorentz_eps(ex_to<idx>(idx1).replace_dim(_ex4), ex_to<idx>(idx2).replace_dim(_ex4), ex_to<idx>(idx3).replace_dim(_ex4), ex_to<idx>(idx4).replace_dim(_ex4))
661 * trace_string(v.begin(), num - 4);
667 return trONE * I * result * mul(bv);
669 } else { // no gamma5
671 // Trace of odd number of gammas is zero
675 // Tr gamma.mu gamma.nu = 4 g.mu.nu
678 base_and_index(e.op(0), b1, i1);
679 base_and_index(e.op(1), b2, i2);
680 return trONE * (lorentz_g(i1, i2) * b1 * b2).simplify_indexed();
683 exvector iv(num), bv(num);
684 for (unsigned i=0; i<num; i++)
685 base_and_index(e.op(i), bv[i], iv[i]);
687 return trONE * (trace_string(iv.begin(), num) * mul(bv)).simplify_indexed();
690 } else if (e.nops() > 0) {
692 // Trace maps to all other container classes (this includes sums)
693 pointer_to_map_function_2args<unsigned char, const ex &> fcn(dirac_trace, rl, trONE);
700 ex canonicalize_clifford(const ex & e)
702 // Scan for any ncmul objects
704 ex aux = e.to_rational(srl);
705 for (unsigned i=0; i<srl.nops(); i++) {
707 ex lhs = srl.op(i).lhs();
708 ex rhs = srl.op(i).rhs();
710 if (is_ex_exactly_of_type(rhs, ncmul)
711 && rhs.return_type() == return_types::noncommutative
712 && is_clifford_tinfo(rhs.return_type_tinfo())) {
714 // Expand product, if necessary
715 ex rhs_expanded = rhs.expand();
716 if (!is_a<ncmul>(rhs_expanded)) {
717 srl.let_op(i) = (lhs == canonicalize_clifford(rhs_expanded));
720 } else if (!is_a<clifford>(rhs.op(0)))
724 v.reserve(rhs.nops());
725 for (unsigned j=0; j<rhs.nops(); j++)
726 v.push_back(rhs.op(j));
728 // Stupid recursive bubble sort because we only want to swap adjacent gammas
729 exvector::iterator it = v.begin(), next_to_last = v.end() - 1;
730 if (is_a<diracgamma5>(it->op(0)) || is_a<diracgammaL>(it->op(0)) || is_a<diracgammaR>(it->op(0)))
732 while (it != next_to_last) {
733 if (it[0].compare(it[1]) > 0) {
734 ex save0 = it[0], save1 = it[1];
736 base_and_index(it[0], b1, i1);
737 base_and_index(it[1], b2, i2);
738 it[0] = (lorentz_g(i1, i2) * b1 * b2).simplify_indexed();
743 sum -= ncmul(v, true);
744 srl.let_op(i) = (lhs == canonicalize_clifford(sum));
752 return aux.subs(srl).simplify_indexed();