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"
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)
48 // default constructor, destructor, copy constructor assignment operator and helpers
51 clifford::clifford() : representation_label(0)
53 debugmsg("clifford default constructor", LOGLEVEL_CONSTRUCT);
54 tinfo_key = TINFO_clifford;
57 void clifford::copy(const clifford & other)
59 inherited::copy(other);
60 representation_label = other.representation_label;
63 DEFAULT_DESTROY(clifford)
64 DEFAULT_CTORS(diracone)
65 DEFAULT_CTORS(diracgamma)
66 DEFAULT_CTORS(diracgamma5)
72 /** Construct object without any indices. This constructor is for internal
73 * use only. Use the dirac_ONE() function instead.
75 clifford::clifford(const ex & b, unsigned char rl) : inherited(b), representation_label(rl)
77 debugmsg("clifford constructor from ex", LOGLEVEL_CONSTRUCT);
78 tinfo_key = TINFO_clifford;
81 /** Construct object with one Lorentz index. This constructor is for internal
82 * use only. Use the dirac_gamma() function instead.
84 clifford::clifford(const ex & b, const ex & mu, unsigned char rl) : inherited(b, mu), representation_label(rl)
86 debugmsg("clifford constructor from ex,ex", LOGLEVEL_CONSTRUCT);
87 GINAC_ASSERT(is_a<varidx>(mu));
88 tinfo_key = TINFO_clifford;
91 clifford::clifford(unsigned char rl, const exvector & v, bool discardable) : inherited(sy_none(), v, discardable), representation_label(rl)
93 debugmsg("clifford constructor from unsigned char,exvector", LOGLEVEL_CONSTRUCT);
94 tinfo_key = TINFO_clifford;
97 clifford::clifford(unsigned char rl, exvector * vp) : inherited(sy_none(), vp), representation_label(rl)
99 debugmsg("clifford constructor from unsigned char,exvector *", LOGLEVEL_CONSTRUCT);
100 tinfo_key = TINFO_clifford;
107 clifford::clifford(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
109 debugmsg("clifford constructor from archive_node", LOGLEVEL_CONSTRUCT);
111 n.find_unsigned("label", rl);
112 representation_label = rl;
115 void clifford::archive(archive_node &n) const
117 inherited::archive(n);
118 n.add_unsigned("label", representation_label);
121 DEFAULT_UNARCHIVE(clifford)
122 DEFAULT_ARCHIVING(diracone)
123 DEFAULT_ARCHIVING(diracgamma)
124 DEFAULT_ARCHIVING(diracgamma5)
127 // functions overriding virtual functions from base classes
130 int clifford::compare_same_type(const basic & other) const
132 GINAC_ASSERT(is_of_type(other, clifford));
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_of_type(other, clifford));
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 = 0) const
153 if (!is_a<diracgamma5>(seq[0]) && !is_a<diracgamma>(seq[0]) && !is_a<diracone>(seq[0])) {
155 // dirac_slash() object is printed differently
156 if (is_a<print_tree>(c))
157 inherited::print(c, level);
158 else if (is_a<print_latex>(c)) {
160 seq[0].print(c, level);
161 c.s << "\\hspace{-1.0ex}/}";
163 seq[0].print(c, level);
168 inherited::print(c, level);
171 DEFAULT_COMPARE(diracone)
172 DEFAULT_COMPARE(diracgamma)
173 DEFAULT_COMPARE(diracgamma5)
175 DEFAULT_PRINT_LATEX(diracone, "ONE", "\\mathbb{1}")
176 DEFAULT_PRINT_LATEX(diracgamma, "gamma", "\\gamma")
177 DEFAULT_PRINT_LATEX(diracgamma5, "gamma5", "{\\gamma^5}")
179 /** This function decomposes gamma~mu -> (1, mu) and a\ -> (a.ix, ix) */
180 static void base_and_index(const ex & c, ex & b, ex & i)
182 GINAC_ASSERT(is_a<clifford>(c));
183 GINAC_ASSERT(c.nops() == 2);
185 if (is_a<diracgamma>(c.op(0))) { // proper dirac gamma object
188 } else { // slash object, generate new dummy index
189 varidx ix((new symbol)->setflag(status_flags::dynallocated), ex_to<idx>(c.op(1)).get_dim());
190 b = indexed(c.op(0), ix.toggle_variance());
195 /** Contraction of a gamma matrix with something else. */
196 bool diracgamma::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
198 GINAC_ASSERT(is_a<clifford>(*self));
199 GINAC_ASSERT(is_a<indexed>(*other));
200 GINAC_ASSERT(is_a<diracgamma>(self->op(0)));
201 unsigned char rl = ex_to<clifford>(*self).get_representation_label();
203 if (is_a<clifford>(*other)) {
205 ex dim = ex_to<idx>(self->op(1)).get_dim();
207 // gamma~mu gamma.mu = dim ONE
208 if (other - self == 1) {
210 *other = dirac_ONE(rl);
213 // gamma~mu gamma~alpha gamma.mu = (2-dim) gamma~alpha
214 } else if (other - self == 2
215 && is_a<clifford>(self[1])) {
220 // gamma~mu gamma~alpha gamma~beta gamma.mu = 4 g~alpha~beta + (dim-4) gamam~alpha gamma~beta
221 } else if (other - self == 3
222 && is_a<clifford>(self[1])
223 && is_a<clifford>(self[2])) {
225 base_and_index(self[1], b1, i1);
226 base_and_index(self[2], b2, i2);
227 *self = 4 * lorentz_g(i1, i2) * b1 * b2 * dirac_ONE(rl) + (dim - 4) * self[1] * self[2];
233 // 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
234 } else if (other - self == 4
235 && is_a<clifford>(self[1])
236 && is_a<clifford>(self[2])
237 && is_a<clifford>(self[3])) {
238 *self = -2 * self[3] * self[2] * self[1] - (dim - 4) * self[1] * self[2] * self[3];
245 // gamma~mu S gamma~alpha gamma.mu = 2 gamma~alpha S - gamma~mu S gamma.mu gamma~alpha
246 // (commutate contracted indices towards each other, simplify_indexed()
247 // will re-expand and re-run the simplification)
249 exvector::iterator it = self + 1, next_to_last = other - 1;
250 while (it != other) {
251 if (!is_a<clifford>(*it))
258 while (it != next_to_last) {
263 *self = 2 * (*next_to_last) * S - (*self) * S * (*other) * (*next_to_last);
264 *next_to_last = _ex1();
273 /** Perform automatic simplification on noncommutative product of clifford
274 * objects. This removes superfluous ONEs, permutes gamma5's to the front
275 * and removes squares of gamma objects. */
276 ex clifford::simplify_ncmul(const exvector & v) const
281 // Remove superfluous ONEs
282 exvector::const_iterator cit = v.begin(), citend = v.end();
283 while (cit != citend) {
284 if (!is_a<clifford>(*cit) || !is_a<diracone>(cit->op(0)))
289 bool something_changed = false;
292 // Anticommute gamma5's to the front
294 exvector::iterator first = s.begin(), next_to_last = s.end() - 2;
296 exvector::iterator it = next_to_last;
298 exvector::iterator it2 = it + 1;
299 if (is_a<clifford>(*it) && is_a<clifford>(*it2) && !is_a<diracgamma5>(it->op(0)) && is_a<diracgamma5>(it2->op(0))) {
302 something_changed = true;
308 if (next_to_last == first)
314 // Remove squares of gamma5
315 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))) {
316 s.erase(s.begin(), s.begin() + 2);
317 something_changed = true;
320 // Remove equal adjacent gammas
322 exvector::iterator it, itend = s.end() - 1;
323 for (it = s.begin(); it != itend; ++it) {
326 if (!is_a<clifford>(a) || !is_a<clifford>(b))
328 if (is_a<diracgamma>(a.op(0)) && is_a<diracgamma>(b.op(0))) {
329 const ex & ia = a.op(1);
330 const ex & ib = b.op(1);
331 if (ia.is_equal(ib)) { // gamma~alpha gamma~alpha -> g~alpha~alpha
332 a = lorentz_g(ia, ib);
333 b = dirac_ONE(representation_label);
334 something_changed = true;
336 } else if (!is_a<diracgamma>(a.op(0)) && !is_a<diracgamma>(b.op(0))) {
337 const ex & ba = a.op(0);
338 const ex & bb = b.op(0);
339 if (ba.is_equal(bb)) { // a\ a\ -> a^2
340 varidx ix((new symbol)->setflag(status_flags::dynallocated), ex_to<idx>(a.op(1)).get_dim());
341 a = indexed(ba, ix) * indexed(bb, ix.toggle_variance());
342 b = dirac_ONE(representation_label);
343 something_changed = true;
350 return clifford(diracone(), representation_label) * sign;
351 if (something_changed)
352 return nonsimplified_ncmul(s) * sign;
354 return simplified_ncmul(s) * sign;
357 ex clifford::thisexprseq(const exvector & v) const
359 return clifford(representation_label, v);
362 ex clifford::thisexprseq(exvector * vp) const
364 return clifford(representation_label, vp);
371 ex dirac_ONE(unsigned char rl)
373 return clifford(diracone(), rl);
376 ex dirac_gamma(const ex & mu, unsigned char rl)
378 if (!is_a<varidx>(mu))
379 throw(std::invalid_argument("index of Dirac gamma must be of type varidx"));
381 return clifford(diracgamma(), mu, rl);
384 ex dirac_gamma5(unsigned char rl)
386 return clifford(diracgamma5(), rl);
389 ex dirac_gamma6(unsigned char rl)
391 return clifford(diracone(), rl) + clifford(diracgamma5(), rl);
394 ex dirac_gamma7(unsigned char rl)
396 return clifford(diracone(), rl) - clifford(diracgamma5(), rl);
399 ex dirac_slash(const ex & e, const ex & dim, unsigned char rl)
401 // Slashed vectors are actually stored as a clifford object with the
402 // vector as its base expression and a (dummy) index that just serves
403 // for storing the space dimensionality
404 return clifford(e, varidx(0, dim), rl);
407 /** Check whether a given tinfo key (as returned by return_type_tinfo()
408 * is that of a clifford object with the specified representation label. */
409 static bool is_clifford_tinfo(unsigned ti, unsigned char rl)
411 return ti == (TINFO_clifford + rl);
414 /** Check whether a given tinfo key (as returned by return_type_tinfo()
415 * is that of a clifford object (with an arbitrary representation label). */
416 static bool is_clifford_tinfo(unsigned ti)
418 return (ti & ~0xff) == TINFO_clifford;
421 /** Take trace of a string of an even number of Dirac gammas given a vector
423 static ex trace_string(exvector::const_iterator ix, unsigned num)
425 // Tr gamma.mu gamma.nu = 4 g.mu.nu
427 return lorentz_g(ix[0], ix[1]);
429 // 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
431 return lorentz_g(ix[0], ix[1]) * lorentz_g(ix[2], ix[3])
432 + lorentz_g(ix[1], ix[2]) * lorentz_g(ix[0], ix[3])
433 - lorentz_g(ix[0], ix[2]) * lorentz_g(ix[1], ix[3]);
435 // Traces of 6 or more gammas are computed recursively:
436 // Tr gamma.mu1 gamma.mu2 ... gamma.mun =
437 // + g.mu1.mu2 * Tr gamma.mu3 ... gamma.mun
438 // - g.mu1.mu3 * Tr gamma.mu2 gamma.mu4 ... gamma.mun
439 // + g.mu1.mu4 * Tr gamma.mu3 gamma.mu3 gamma.mu5 ... gamma.mun
441 // + g.mu1.mun * Tr gamma.mu2 ... gamma.mu(n-1)
445 for (unsigned i=1; i<num; i++) {
446 for (unsigned n=1, j=0; n<num; n++) {
451 result += sign * lorentz_g(ix[0], ix[i]) * trace_string(v.begin(), num-2);
457 ex dirac_trace(const ex & e, unsigned char rl, const ex & trONE)
459 if (is_a<clifford>(e)) {
461 if (ex_to<clifford>(e).get_representation_label() == rl
462 && is_a<diracone>(e.op(0)))
467 } else if (is_ex_exactly_of_type(e, mul)) {
469 // Trace of product: pull out non-clifford factors
471 for (unsigned i=0; i<e.nops(); i++) {
472 const ex &o = e.op(i);
473 if (is_clifford_tinfo(o.return_type_tinfo(), rl))
474 prod *= dirac_trace(o, rl, trONE);
480 } else if (is_ex_exactly_of_type(e, ncmul)) {
482 if (!is_clifford_tinfo(e.return_type_tinfo(), rl))
485 // Expand product, if necessary
486 ex e_expanded = e.expand();
487 if (!is_a<ncmul>(e_expanded))
488 return dirac_trace(e_expanded, rl, trONE);
490 // gamma5 gets moved to the front so this check is enough
491 bool has_gamma5 = is_a<diracgamma5>(e.op(0).op(0));
492 unsigned num = e.nops();
496 // Trace of gamma5 * odd number of gammas and trace of
497 // gamma5 * gamma.mu * gamma.nu are zero
498 if ((num & 1) == 0 || num == 3)
501 // Tr gamma5 gamma.mu gamma.nu gamma.rho gamma.sigma = 4I * epsilon(mu, nu, rho, sigma)
503 ex b1, i1, b2, i2, b3, i3, b4, i4;
504 base_and_index(e.op(1), b1, i1);
505 base_and_index(e.op(2), b2, i2);
506 base_and_index(e.op(3), b3, i3);
507 base_and_index(e.op(4), b4, i4);
508 return trONE * I * (eps0123(i1, i2, i3, i4) * b1 * b2 * b3 * b4).simplify_indexed();
512 // I/4! * epsilon0123.mu1.mu2.mu3.mu4 * Tr gamma.mu1 gamma.mu2 gamma.mu3 gamma.mu4 S_2k
513 exvector ix(num-1), bv(num-1);
514 for (unsigned i=1; i<num; i++)
515 base_and_index(e.op(i), bv[i-1], ix[i-1]);
517 int *iv = new int[num];
519 for (unsigned i=0; i<num-3; i++) {
521 for (unsigned j=i+1; j<num-2; j++) {
523 for (unsigned k=j+1; k<num-1; k++) {
525 for (unsigned l=k+1; l<num; l++) {
527 iv[0] = i; iv[1] = j; iv[2] = k; iv[3] = l;
530 for (unsigned n=0, t=4; n<num; n++) {
531 if (n == i || n == j || n == k || n == l)
536 int sign = permutation_sign(iv, iv + num);
537 result += sign * eps0123(idx1, idx2, idx3, idx4)
538 * trace_string(v.begin(), num - 4);
544 return trONE * I * result * mul(bv);
546 } else { // no gamma5
548 // Trace of odd number of gammas is zero
552 // Tr gamma.mu gamma.nu = 4 g.mu.nu
555 base_and_index(e.op(0), b1, i1);
556 base_and_index(e.op(1), b2, i2);
557 return trONE * (lorentz_g(i1, i2) * b1 * b2).simplify_indexed();
560 exvector iv(num), bv(num);
561 for (unsigned i=0; i<num; i++)
562 base_and_index(e.op(i), bv[i], iv[i]);
564 return trONE * (trace_string(iv.begin(), num) * mul(bv)).simplify_indexed();
567 } else if (e.nops() > 0) {
569 // Trace maps to all other container classes (this includes sums)
570 pointer_to_map_function_2args<unsigned char, const ex &> fcn(dirac_trace, rl, trONE);
577 ex canonicalize_clifford(const ex & e)
579 // Scan for any ncmul objects
581 ex aux = e.to_rational(srl);
582 for (unsigned i=0; i<srl.nops(); i++) {
584 ex lhs = srl.op(i).lhs();
585 ex rhs = srl.op(i).rhs();
587 if (is_ex_exactly_of_type(rhs, ncmul)
588 && rhs.return_type() == return_types::noncommutative
589 && is_clifford_tinfo(rhs.return_type_tinfo())) {
591 // Expand product, if necessary
592 ex rhs_expanded = rhs.expand();
593 if (!is_a<ncmul>(rhs_expanded)) {
594 srl.let_op(i) = (lhs == canonicalize_clifford(rhs_expanded));
597 } else if (!is_a<clifford>(rhs.op(0)))
601 v.reserve(rhs.nops());
602 for (unsigned j=0; j<rhs.nops(); j++)
603 v.push_back(rhs.op(j));
605 // Stupid recursive bubble sort because we only want to swap adjacent gammas
606 exvector::iterator it = v.begin(), next_to_last = v.end() - 1;
607 if (is_a<diracgamma5>(it->op(0)))
609 while (it != next_to_last) {
610 if (it[0].compare(it[1]) > 0) {
611 ex save0 = it[0], save1 = it[1];
613 base_and_index(it[0], b1, i1);
614 base_and_index(it[1], b2, i2);
615 it[0] = (lorentz_g(i1, i2) * b1 * b2).simplify_indexed();
620 sum -= ncmul(v, true);
621 srl.let_op(i) = (lhs == canonicalize_clifford(sum));
629 return aux.subs(srl).simplify_indexed();