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
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)
48 // default ctor, dtor, copy ctor, assignment operator and helpers
51 clifford::clifford() : representation_label(0)
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 tinfo_key = TINFO_clifford;
79 /** Construct object with one Lorentz index. This constructor is for internal
80 * use only. Use the dirac_gamma() function instead.
82 clifford::clifford(const ex & b, const ex & mu, unsigned char rl) : inherited(b, mu), representation_label(rl)
84 GINAC_ASSERT(is_a<varidx>(mu));
85 tinfo_key = TINFO_clifford;
88 clifford::clifford(unsigned char rl, const exvector & v, bool discardable) : inherited(sy_none(), v, discardable), representation_label(rl)
90 tinfo_key = TINFO_clifford;
93 clifford::clifford(unsigned char rl, exvector * vp) : inherited(sy_none(), vp), representation_label(rl)
95 tinfo_key = TINFO_clifford;
102 clifford::clifford(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
105 n.find_unsigned("label", rl);
106 representation_label = rl;
109 void clifford::archive(archive_node &n) const
111 inherited::archive(n);
112 n.add_unsigned("label", representation_label);
115 DEFAULT_UNARCHIVE(clifford)
116 DEFAULT_ARCHIVING(diracone)
117 DEFAULT_ARCHIVING(diracgamma)
118 DEFAULT_ARCHIVING(diracgamma5)
121 // functions overriding virtual functions from base classes
124 int clifford::compare_same_type(const basic & other) const
126 GINAC_ASSERT(is_a<clifford>(other));
127 const clifford &o = static_cast<const clifford &>(other);
129 if (representation_label != o.representation_label) {
130 // different representation label
131 return representation_label < o.representation_label ? -1 : 1;
134 return inherited::compare_same_type(other);
137 bool clifford::match_same_type(const basic & other) const
139 GINAC_ASSERT(is_a<clifford>(other));
140 const clifford &o = static_cast<const clifford &>(other);
142 return representation_label == o.representation_label;
145 void clifford::print(const print_context & c, unsigned level) const
147 if (!is_a<diracgamma5>(seq[0]) && !is_a<diracgamma>(seq[0]) && !is_a<diracone>(seq[0])) {
149 // dirac_slash() object is printed differently
150 if (is_a<print_tree>(c))
151 inherited::print(c, level);
152 else if (is_a<print_latex>(c)) {
154 seq[0].print(c, level);
155 c.s << "\\hspace{-1.0ex}/}";
157 seq[0].print(c, level);
162 inherited::print(c, level);
165 DEFAULT_COMPARE(diracone)
166 DEFAULT_COMPARE(diracgamma)
167 DEFAULT_COMPARE(diracgamma5)
169 DEFAULT_PRINT_LATEX(diracone, "ONE", "\\mathbb{1}")
170 DEFAULT_PRINT_LATEX(diracgamma, "gamma", "\\gamma")
171 DEFAULT_PRINT_LATEX(diracgamma5, "gamma5", "{\\gamma^5}")
173 /** This function decomposes gamma~mu -> (1, mu) and a\ -> (a.ix, ix) */
174 static void base_and_index(const ex & c, ex & b, ex & i)
176 GINAC_ASSERT(is_a<clifford>(c));
177 GINAC_ASSERT(c.nops() == 2);
179 if (is_a<diracgamma>(c.op(0))) { // proper dirac gamma object
182 } else { // slash object, generate new dummy index
183 varidx ix((new symbol)->setflag(status_flags::dynallocated), ex_to<idx>(c.op(1)).get_dim());
184 b = indexed(c.op(0), ix.toggle_variance());
189 /** Contraction of a gamma matrix with something else. */
190 bool diracgamma::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
192 GINAC_ASSERT(is_a<clifford>(*self));
193 GINAC_ASSERT(is_a<indexed>(*other));
194 GINAC_ASSERT(is_a<diracgamma>(self->op(0)));
195 unsigned char rl = ex_to<clifford>(*self).get_representation_label();
197 if (is_a<clifford>(*other)) {
199 ex dim = ex_to<idx>(self->op(1)).get_dim();
201 // gamma~mu gamma.mu = dim ONE
202 if (other - self == 1) {
204 *other = dirac_ONE(rl);
207 // gamma~mu gamma~alpha gamma.mu = (2-dim) gamma~alpha
208 } else if (other - self == 2
209 && is_a<clifford>(self[1])) {
214 // gamma~mu gamma~alpha gamma~beta gamma.mu = 4 g~alpha~beta + (dim-4) gamam~alpha gamma~beta
215 } else if (other - self == 3
216 && is_a<clifford>(self[1])
217 && is_a<clifford>(self[2])) {
219 base_and_index(self[1], b1, i1);
220 base_and_index(self[2], b2, i2);
221 *self = 4 * lorentz_g(i1, i2) * b1 * b2 * dirac_ONE(rl) + (dim - 4) * self[1] * self[2];
227 // 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
228 } else if (other - self == 4
229 && is_a<clifford>(self[1])
230 && is_a<clifford>(self[2])
231 && is_a<clifford>(self[3])) {
232 *self = -2 * self[3] * self[2] * self[1] - (dim - 4) * self[1] * self[2] * self[3];
239 // gamma~mu S gamma~alpha gamma.mu = 2 gamma~alpha S - gamma~mu S gamma.mu gamma~alpha
240 // (commutate contracted indices towards each other, simplify_indexed()
241 // will re-expand and re-run the simplification)
243 exvector::iterator it = self + 1, next_to_last = other - 1;
244 while (it != other) {
245 if (!is_a<clifford>(*it))
252 while (it != next_to_last) {
257 *self = 2 * (*next_to_last) * S - (*self) * S * (*other) * (*next_to_last);
258 *next_to_last = _ex1;
267 /** Perform automatic simplification on noncommutative product of clifford
268 * objects. This removes superfluous ONEs, permutes gamma5's to the front
269 * and removes squares of gamma objects. */
270 ex clifford::simplify_ncmul(const exvector & v) const
275 // Remove superfluous ONEs
276 exvector::const_iterator cit = v.begin(), citend = v.end();
277 while (cit != citend) {
278 if (!is_a<clifford>(*cit) || !is_a<diracone>(cit->op(0)))
283 bool something_changed = false;
286 // Anticommute gamma5's to the front
288 exvector::iterator first = s.begin(), next_to_last = s.end() - 2;
290 exvector::iterator it = next_to_last;
292 exvector::iterator it2 = it + 1;
293 if (is_a<clifford>(*it) && is_a<clifford>(*it2) && !is_a<diracgamma5>(it->op(0)) && is_a<diracgamma5>(it2->op(0))) {
296 something_changed = true;
302 if (next_to_last == first)
308 // Remove squares of gamma5
309 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))) {
310 s.erase(s.begin(), s.begin() + 2);
311 something_changed = true;
314 // Remove equal adjacent gammas
316 exvector::iterator it, itend = s.end() - 1;
317 for (it = s.begin(); it != itend; ++it) {
320 if (!is_a<clifford>(a) || !is_a<clifford>(b))
322 if (is_a<diracgamma>(a.op(0)) && is_a<diracgamma>(b.op(0))) {
323 const ex & ia = a.op(1);
324 const ex & ib = b.op(1);
325 if (ia.is_equal(ib)) { // gamma~alpha gamma~alpha -> g~alpha~alpha
326 a = lorentz_g(ia, ib);
327 b = dirac_ONE(representation_label);
328 something_changed = true;
330 } else if (!is_a<diracgamma>(a.op(0)) && !is_a<diracgamma>(b.op(0))) {
331 const ex & ba = a.op(0);
332 const ex & bb = b.op(0);
333 if (ba.is_equal(bb)) { // a\ a\ -> a^2
334 varidx ix((new symbol)->setflag(status_flags::dynallocated), ex_to<idx>(a.op(1)).get_dim());
335 a = indexed(ba, ix) * indexed(bb, ix.toggle_variance());
336 b = dirac_ONE(representation_label);
337 something_changed = true;
344 return clifford(diracone(), representation_label) * sign;
345 if (something_changed)
346 return nonsimplified_ncmul(s) * sign;
348 return simplified_ncmul(s) * sign;
351 ex clifford::thisexprseq(const exvector & v) const
353 return clifford(representation_label, v);
356 ex clifford::thisexprseq(exvector * vp) const
358 return clifford(representation_label, vp);
365 ex dirac_ONE(unsigned char rl)
367 return clifford(diracone(), rl);
370 ex dirac_gamma(const ex & mu, unsigned char rl)
372 if (!is_a<varidx>(mu))
373 throw(std::invalid_argument("index of Dirac gamma must be of type varidx"));
375 return clifford(diracgamma(), mu, rl);
378 ex dirac_gamma5(unsigned char rl)
380 return clifford(diracgamma5(), rl);
383 ex dirac_gamma6(unsigned char rl)
385 return clifford(diracone(), rl) + clifford(diracgamma5(), rl);
388 ex dirac_gamma7(unsigned char rl)
390 return clifford(diracone(), rl) - clifford(diracgamma5(), rl);
393 ex dirac_slash(const ex & e, const ex & dim, unsigned char rl)
395 // Slashed vectors are actually stored as a clifford object with the
396 // vector as its base expression and a (dummy) index that just serves
397 // for storing the space dimensionality
398 return clifford(e, varidx(0, dim), rl);
401 /** Check whether a given tinfo key (as returned by return_type_tinfo()
402 * is that of a clifford object with the specified representation label. */
403 static bool is_clifford_tinfo(unsigned ti, unsigned char rl)
405 return ti == (TINFO_clifford + rl);
408 /** Check whether a given tinfo key (as returned by return_type_tinfo()
409 * is that of a clifford object (with an arbitrary representation label). */
410 static bool is_clifford_tinfo(unsigned ti)
412 return (ti & ~0xff) == TINFO_clifford;
415 /** Take trace of a string of an even number of Dirac gammas given a vector
417 static ex trace_string(exvector::const_iterator ix, unsigned num)
419 // Tr gamma.mu gamma.nu = 4 g.mu.nu
421 return lorentz_g(ix[0], ix[1]);
423 // 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
425 return lorentz_g(ix[0], ix[1]) * lorentz_g(ix[2], ix[3])
426 + lorentz_g(ix[1], ix[2]) * lorentz_g(ix[0], ix[3])
427 - lorentz_g(ix[0], ix[2]) * lorentz_g(ix[1], ix[3]);
429 // Traces of 6 or more gammas are computed recursively:
430 // Tr gamma.mu1 gamma.mu2 ... gamma.mun =
431 // + g.mu1.mu2 * Tr gamma.mu3 ... gamma.mun
432 // - g.mu1.mu3 * Tr gamma.mu2 gamma.mu4 ... gamma.mun
433 // + g.mu1.mu4 * Tr gamma.mu3 gamma.mu3 gamma.mu5 ... gamma.mun
435 // + g.mu1.mun * Tr gamma.mu2 ... gamma.mu(n-1)
439 for (unsigned i=1; i<num; i++) {
440 for (unsigned n=1, j=0; n<num; n++) {
445 result += sign * lorentz_g(ix[0], ix[i]) * trace_string(v.begin(), num-2);
451 ex dirac_trace(const ex & e, unsigned char rl, const ex & trONE)
453 if (is_a<clifford>(e)) {
455 if (ex_to<clifford>(e).get_representation_label() == rl
456 && is_a<diracone>(e.op(0)))
461 } else if (is_ex_exactly_of_type(e, mul)) {
463 // Trace of product: pull out non-clifford factors
465 for (unsigned i=0; i<e.nops(); i++) {
466 const ex &o = e.op(i);
467 if (is_clifford_tinfo(o.return_type_tinfo(), rl))
468 prod *= dirac_trace(o, rl, trONE);
474 } else if (is_ex_exactly_of_type(e, ncmul)) {
476 if (!is_clifford_tinfo(e.return_type_tinfo(), rl))
479 // Expand product, if necessary
480 ex e_expanded = e.expand();
481 if (!is_a<ncmul>(e_expanded))
482 return dirac_trace(e_expanded, rl, trONE);
484 // gamma5 gets moved to the front so this check is enough
485 bool has_gamma5 = is_a<diracgamma5>(e.op(0).op(0));
486 unsigned num = e.nops();
490 // Trace of gamma5 * odd number of gammas and trace of
491 // gamma5 * gamma.mu * gamma.nu are zero
492 if ((num & 1) == 0 || num == 3)
495 // Tr gamma5 gamma.mu gamma.nu gamma.rho gamma.sigma = 4I * epsilon(mu, nu, rho, sigma)
497 ex b1, i1, b2, i2, b3, i3, b4, i4;
498 base_and_index(e.op(1), b1, i1);
499 base_and_index(e.op(2), b2, i2);
500 base_and_index(e.op(3), b3, i3);
501 base_and_index(e.op(4), b4, i4);
502 return trONE * I * (eps0123(i1, i2, i3, i4) * b1 * b2 * b3 * b4).simplify_indexed();
506 // I/4! * epsilon0123.mu1.mu2.mu3.mu4 * Tr gamma.mu1 gamma.mu2 gamma.mu3 gamma.mu4 S_2k
507 exvector ix(num-1), bv(num-1);
508 for (unsigned i=1; i<num; i++)
509 base_and_index(e.op(i), bv[i-1], ix[i-1]);
511 int *iv = new int[num];
513 for (unsigned i=0; i<num-3; i++) {
515 for (unsigned j=i+1; j<num-2; j++) {
517 for (unsigned k=j+1; k<num-1; k++) {
519 for (unsigned l=k+1; l<num; l++) {
521 iv[0] = i; iv[1] = j; iv[2] = k; iv[3] = l;
524 for (unsigned n=0, t=4; n<num; n++) {
525 if (n == i || n == j || n == k || n == l)
530 int sign = permutation_sign(iv, iv + num);
531 result += sign * eps0123(idx1, idx2, idx3, idx4)
532 * trace_string(v.begin(), num - 4);
538 return trONE * I * result * mul(bv);
540 } else { // no gamma5
542 // Trace of odd number of gammas is zero
546 // Tr gamma.mu gamma.nu = 4 g.mu.nu
549 base_and_index(e.op(0), b1, i1);
550 base_and_index(e.op(1), b2, i2);
551 return trONE * (lorentz_g(i1, i2) * b1 * b2).simplify_indexed();
554 exvector iv(num), bv(num);
555 for (unsigned i=0; i<num; i++)
556 base_and_index(e.op(i), bv[i], iv[i]);
558 return trONE * (trace_string(iv.begin(), num) * mul(bv)).simplify_indexed();
561 } else if (e.nops() > 0) {
563 // Trace maps to all other container classes (this includes sums)
564 pointer_to_map_function_2args<unsigned char, const ex &> fcn(dirac_trace, rl, trONE);
571 ex canonicalize_clifford(const ex & e)
573 // Scan for any ncmul objects
575 ex aux = e.to_rational(srl);
576 for (unsigned i=0; i<srl.nops(); i++) {
578 ex lhs = srl.op(i).lhs();
579 ex rhs = srl.op(i).rhs();
581 if (is_ex_exactly_of_type(rhs, ncmul)
582 && rhs.return_type() == return_types::noncommutative
583 && is_clifford_tinfo(rhs.return_type_tinfo())) {
585 // Expand product, if necessary
586 ex rhs_expanded = rhs.expand();
587 if (!is_a<ncmul>(rhs_expanded)) {
588 srl.let_op(i) = (lhs == canonicalize_clifford(rhs_expanded));
591 } else if (!is_a<clifford>(rhs.op(0)))
595 v.reserve(rhs.nops());
596 for (unsigned j=0; j<rhs.nops(); j++)
597 v.push_back(rhs.op(j));
599 // Stupid recursive bubble sort because we only want to swap adjacent gammas
600 exvector::iterator it = v.begin(), next_to_last = v.end() - 1;
601 if (is_a<diracgamma5>(it->op(0)))
603 while (it != next_to_last) {
604 if (it[0].compare(it[1]) > 0) {
605 ex save0 = it[0], save1 = it[1];
607 base_and_index(it[0], b1, i1);
608 base_and_index(it[1], b2, i2);
609 it[0] = (lorentz_g(i1, i2) * b1 * b2).simplify_indexed();
614 sum -= ncmul(v, true);
615 srl.let_op(i) = (lhs == canonicalize_clifford(sum));
623 return aux.subs(srl).simplify_indexed();