3 * Implementation of GiNaC's clifford algebra (Dirac gamma) objects. */
6 * GiNaC Copyright (C) 1999-2005 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 #include "numeric.h" // for I
32 #include "relational.h"
33 #include "operators.h"
43 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(clifford, indexed,
44 print_func<print_dflt>(&clifford::do_print_dflt).
45 print_func<print_latex>(&clifford::do_print_latex))
47 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(diracone, tensor,
48 print_func<print_dflt>(&diracone::do_print).
49 print_func<print_latex>(&diracone::do_print_latex))
51 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(cliffordunit, tensor,
52 print_func<print_dflt>(&cliffordunit::do_print).
53 print_func<print_latex>(&cliffordunit::do_print_latex))
55 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(diracgamma, cliffordunit,
56 print_func<print_dflt>(&diracgamma::do_print).
57 print_func<print_latex>(&diracgamma::do_print_latex))
59 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(diracgamma5, tensor,
60 print_func<print_dflt>(&diracgamma5::do_print).
61 print_func<print_latex>(&diracgamma5::do_print_latex))
63 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(diracgammaL, tensor,
64 print_func<print_context>(&diracgammaL::do_print).
65 print_func<print_latex>(&diracgammaL::do_print_latex))
67 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(diracgammaR, tensor,
68 print_func<print_context>(&diracgammaR::do_print).
69 print_func<print_latex>(&diracgammaR::do_print_latex))
72 // default constructors
75 static ex default_metric()
77 static ex m = (new minkmetric)->setflag(status_flags::dynallocated);
81 clifford::clifford() : representation_label(0), metric(default_metric())
83 tinfo_key = TINFO_clifford;
86 DEFAULT_CTOR(diracone)
87 DEFAULT_CTOR(cliffordunit)
88 DEFAULT_CTOR(diracgamma)
89 DEFAULT_CTOR(diracgamma5)
90 DEFAULT_CTOR(diracgammaL)
91 DEFAULT_CTOR(diracgammaR)
97 /** Construct object without any indices. This constructor is for internal
98 * use only. Use the dirac_ONE() function instead.
100 clifford::clifford(const ex & b, unsigned char rl) : inherited(b), representation_label(rl), metric(0)
102 tinfo_key = TINFO_clifford;
105 /** Construct object with one Lorentz index. This constructor is for internal
106 * use only. Use the clifford_unit() or dirac_gamma() functions instead.
108 * @see dirac_gamma */
109 clifford::clifford(const ex & b, const ex & mu, const ex & metr, unsigned char rl) : inherited(b, mu), representation_label(rl), metric(metr)
111 GINAC_ASSERT(is_a<varidx>(mu));
112 tinfo_key = TINFO_clifford;
115 clifford::clifford(unsigned char rl, const ex & metr, const exvector & v, bool discardable) : inherited(not_symmetric(), v, discardable), representation_label(rl), metric(metr)
117 tinfo_key = TINFO_clifford;
120 clifford::clifford(unsigned char rl, const ex & metr, std::auto_ptr<exvector> vp) : inherited(not_symmetric(), vp), representation_label(rl), metric(metr)
122 tinfo_key = TINFO_clifford;
129 clifford::clifford(const archive_node & n, lst & sym_lst) : inherited(n, sym_lst)
132 n.find_unsigned("label", rl);
133 representation_label = rl;
134 n.find_ex("metric", metric, sym_lst);
137 void clifford::archive(archive_node & n) const
139 inherited::archive(n);
140 n.add_unsigned("label", representation_label);
141 n.add_ex("metric", metric);
144 DEFAULT_UNARCHIVE(clifford)
145 DEFAULT_ARCHIVING(diracone)
146 DEFAULT_ARCHIVING(cliffordunit)
147 DEFAULT_ARCHIVING(diracgamma)
148 DEFAULT_ARCHIVING(diracgamma5)
149 DEFAULT_ARCHIVING(diracgammaL)
150 DEFAULT_ARCHIVING(diracgammaR)
153 // functions overriding virtual functions from base classes
156 ex clifford::get_metric(const ex & i, const ex & j) const
158 return indexed(metric, symmetric2(), i, j);
161 bool clifford::same_metric(const ex & other) const
163 if (is_a<clifford>(other)) {
164 return get_metric().is_equal(ex_to<clifford>(other).get_metric());
165 } else if (is_a<indexed>(other)) {
166 return get_metric(other.op(1), other.op(2)).is_equal(other);
171 int clifford::compare_same_type(const basic & other) const
173 GINAC_ASSERT(is_a<clifford>(other));
174 const clifford &o = static_cast<const clifford &>(other);
176 if (representation_label != o.representation_label) {
177 // different representation label
178 return representation_label < o.representation_label ? -1 : 1;
181 return inherited::compare_same_type(other);
184 bool clifford::match_same_type(const basic & other) const
186 GINAC_ASSERT(is_a<clifford>(other));
187 const clifford &o = static_cast<const clifford &>(other);
189 return (representation_label == o.representation_label) && same_metric(o);
192 static bool is_dirac_slash(const ex & seq0)
194 return !is_a<diracgamma5>(seq0) && !is_a<diracgammaL>(seq0) &&
195 !is_a<diracgammaR>(seq0) && !is_a<cliffordunit>(seq0) &&
196 !is_a<diracone>(seq0);
199 void clifford::do_print_dflt(const print_dflt & c, unsigned level) const
201 // dirac_slash() object is printed differently
202 if (is_dirac_slash(seq[0])) {
203 seq[0].print(c, precedence());
206 this->print_dispatch<inherited>(c, level);
209 void clifford::do_print_latex(const print_latex & c, unsigned level) const
211 // dirac_slash() object is printed differently
212 if (is_dirac_slash(seq[0])) {
214 seq[0].print(c, precedence());
215 c.s << "\\hspace{-1.0ex}/}";
217 c.s << "\\clifford[" << int(representation_label) << "]";
218 this->print_dispatch<inherited>(c, level);
222 DEFAULT_COMPARE(diracone)
223 DEFAULT_COMPARE(cliffordunit)
224 DEFAULT_COMPARE(diracgamma)
225 DEFAULT_COMPARE(diracgamma5)
226 DEFAULT_COMPARE(diracgammaL)
227 DEFAULT_COMPARE(diracgammaR)
229 DEFAULT_PRINT_LATEX(diracone, "ONE", "\\mathbf{1}")
230 DEFAULT_PRINT_LATEX(cliffordunit, "e", "e")
231 DEFAULT_PRINT_LATEX(diracgamma, "gamma", "\\gamma")
232 DEFAULT_PRINT_LATEX(diracgamma5, "gamma5", "{\\gamma^5}")
233 DEFAULT_PRINT_LATEX(diracgammaL, "gammaL", "{\\gamma_L}")
234 DEFAULT_PRINT_LATEX(diracgammaR, "gammaR", "{\\gamma_R}")
236 /** This function decomposes gamma~mu -> (1, mu) and a\ -> (a.ix, ix) */
237 static void base_and_index(const ex & c, ex & b, ex & i)
239 GINAC_ASSERT(is_a<clifford>(c));
240 GINAC_ASSERT(c.nops() == 2);
242 if (is_a<cliffordunit>(c.op(0))) { // proper dirac gamma object or clifford unit
245 } else if (is_a<diracgamma5>(c.op(0)) || is_a<diracgammaL>(c.op(0)) || is_a<diracgammaR>(c.op(0))) { // gamma5/L/R
248 } else { // slash object, generate new dummy index
249 varidx ix((new symbol)->setflag(status_flags::dynallocated), ex_to<idx>(c.op(1)).get_dim());
250 b = indexed(c.op(0), ix.toggle_variance());
255 /** Predicate for finding non-clifford objects. */
256 struct is_not_a_clifford : public std::unary_function<ex, bool> {
257 bool operator()(const ex & e)
259 return !is_a<clifford>(e);
263 /** Contraction of a gamma matrix with something else. */
264 bool diracgamma::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
266 GINAC_ASSERT(is_a<clifford>(*self));
267 GINAC_ASSERT(is_a<indexed>(*other));
268 GINAC_ASSERT(is_a<diracgamma>(self->op(0)));
269 unsigned char rl = ex_to<clifford>(*self).get_representation_label();
271 ex dim = ex_to<idx>(self->op(1)).get_dim();
272 if (other->nops() > 1)
273 dim = minimal_dim(dim, ex_to<idx>(other->op(1)).get_dim());
275 if (is_a<clifford>(*other)) {
277 // Contraction only makes sense if the represenation labels are equal
278 if (ex_to<clifford>(*other).get_representation_label() != rl)
281 size_t num = other - self;
283 // gamma~mu gamma.mu = dim ONE
286 *other = dirac_ONE(rl);
289 // gamma~mu gamma~alpha gamma.mu = (2-dim) gamma~alpha
291 && is_a<clifford>(self[1])) {
296 // gamma~mu gamma~alpha gamma~beta gamma.mu = 4 g~alpha~beta + (dim-4) gamam~alpha gamma~beta
298 && is_a<clifford>(self[1])
299 && is_a<clifford>(self[2])) {
301 base_and_index(self[1], b1, i1);
302 base_and_index(self[2], b2, i2);
303 *self = 4 * lorentz_g(i1, i2) * b1 * b2 * dirac_ONE(rl) + (dim - 4) * self[1] * self[2];
309 // 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
311 && is_a<clifford>(self[1])
312 && is_a<clifford>(self[2])
313 && is_a<clifford>(self[3])) {
314 *self = -2 * self[3] * self[2] * self[1] - (dim - 4) * self[1] * self[2] * self[3];
321 // gamma~mu Sodd gamma.mu = -2 Sodd_R
322 // (Chisholm identity in 4 dimensions)
323 } else if (!((other - self) & 1) && dim.is_equal(4)) {
324 if (std::find_if(self + 1, other, is_not_a_clifford()) != other)
327 *self = ncmul(exvector(std::reverse_iterator<exvector::const_iterator>(other), std::reverse_iterator<exvector::const_iterator>(self + 1)), true);
328 std::fill(self + 1, other, _ex1);
332 // gamma~mu Sodd gamma~alpha gamma.mu = 2 gamma~alpha Sodd + 2 Sodd_R gamma~alpha
333 // (commutate contracted indices towards each other, then use
334 // Chisholm identity in 4 dimensions)
335 } else if (((other - self) & 1) && dim.is_equal(4)) {
336 if (std::find_if(self + 1, other, is_not_a_clifford()) != other)
339 exvector::iterator next_to_last = other - 1;
340 ex S = ncmul(exvector(self + 1, next_to_last), true);
341 ex SR = ncmul(exvector(std::reverse_iterator<exvector::const_iterator>(next_to_last), std::reverse_iterator<exvector::const_iterator>(self + 1)), true);
343 *self = (*next_to_last) * S + SR * (*next_to_last);
344 std::fill(self + 1, other, _ex1);
348 // gamma~mu S gamma~alpha gamma.mu = 2 gamma~alpha S - gamma~mu S gamma.mu gamma~alpha
349 // (commutate contracted indices towards each other, simplify_indexed()
350 // will re-expand and re-run the simplification)
352 if (std::find_if(self + 1, other, is_not_a_clifford()) != other)
355 exvector::iterator next_to_last = other - 1;
356 ex S = ncmul(exvector(self + 1, next_to_last), true);
358 *self = 2 * (*next_to_last) * S - (*self) * S * (*other) * (*next_to_last);
359 std::fill(self + 1, other + 1, _ex1);
363 } else if (is_a<symbol>(other->op(0)) && other->nops() == 2) {
365 // x.mu gamma~mu -> x-slash
366 *self = dirac_slash(other->op(0), dim, rl);
374 /** An utility function looking for a given metric within an exvector,
375 * used in cliffordunit::contract_with(). */
376 static int find_same_metric(exvector & v, ex & c)
378 for (int i=0; i<v.size();i++) {
379 if (!is_a<clifford>(v[i]) && is_a<indexed>(v[i])
380 && ex_to<clifford>(c).same_metric(v[i])
381 && (ex_to<varidx>(c.op(1)) == ex_to<indexed>(v[i]).get_indices()[0]
382 || ex_to<varidx>(c.op(1)).toggle_variance() == ex_to<indexed>(v[i]).get_indices()[0])) {
383 return ++i; // next to found
386 return 0; //nothing found
389 /** Contraction of a Clifford unit with something else. */
390 bool cliffordunit::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
392 GINAC_ASSERT(is_a<clifford>(*self));
393 GINAC_ASSERT(is_a<indexed>(*other));
394 GINAC_ASSERT(is_a<cliffordunit>(self->op(0)));
395 clifford unit = ex_to<clifford>(*self);
396 unsigned char rl = unit.get_representation_label();
398 if (is_a<clifford>(*other)) {
399 // Contraction only makes sense if the represenation labels are equal
400 // and the metrics are the same
401 if ((ex_to<clifford>(*other).get_representation_label() != rl)
402 && unit.same_metric(*other))
405 // Find if a previous contraction produces the square of self
406 int prev_square = find_same_metric(v, self[0]);
407 varidx d((new symbol)->setflag(status_flags::dynallocated), ex_to<idx>(self->op(1)).get_dim());
408 ex squared_metric = unit.get_metric(self->op(1), d) * unit.get_metric(d.toggle_variance(), other->op(1));
410 // e~mu e.mu = Tr ONE
411 if (other - self == 1) {
412 if (prev_square != 0) {
413 *self = squared_metric;
414 v[prev_square-1] = _ex1;
416 *self = unit.get_metric(self->op(1), other->op(1));
417 *other = dirac_ONE(rl);
420 // e~mu e~alpha e.mu = (2e~alpha^2-Tr) e~alpha
421 } else if (other - self == 2
422 && is_a<clifford>(self[1])) {
424 const ex & ia = self[1].op(1);
425 const ex & ib = self[1].op(1);
426 if (is_a<tensmetric>(unit.get_metric()))
427 *self = 2 - unit.get_metric(self->op(1), other->op(1));
428 else if (prev_square != 0) {
429 *self = 2-squared_metric;
430 v[prev_square-1] = _ex1;
432 *self = 2*unit.get_metric(ia, ib) - unit.get_metric(self->op(1), other->op(1));
436 // e~mu S e~alpha e.mu = 2 e~alpha^3 S - e~mu S e.mu e~alpha
437 // (commutate contracted indices towards each other, simplify_indexed()
438 // will re-expand and re-run the simplification)
440 exvector::iterator it = self + 1, next_to_last = other - 1;
441 while (it != other) {
442 if (!is_a<clifford>(*it))
449 while (it != next_to_last) {
454 const ex & ia = next_to_last->op(1);
455 const ex & ib = next_to_last->op(1);
456 if (is_a<tensmetric>(unit.get_metric()))
457 *self = 2 * (*next_to_last) * S - (*self) * S * (*other) * (*next_to_last);
458 else if (prev_square != 0) {
459 *self = 2 * (*next_to_last) * S - (*self) * S * (*other) * (*next_to_last)*unit.get_metric(self->op(1),self->op(1));
460 v[prev_square-1] = _ex1;
462 *self = 2 * (*next_to_last) * S* unit.get_metric(ia,ib) - (*self) * S * (*other) * (*next_to_last);
463 *next_to_last = _ex1;
473 /** Perform automatic simplification on noncommutative product of clifford
474 * objects. This removes superfluous ONEs, permutes gamma5/L/R's to the front
475 * and removes squares of gamma objects. */
476 ex clifford::eval_ncmul(const exvector & v) const
481 // Remove superfluous ONEs
482 exvector::const_iterator cit = v.begin(), citend = v.end();
483 while (cit != citend) {
484 if (!is_a<clifford>(*cit) || !is_a<diracone>(cit->op(0)))
489 bool something_changed = false;
492 // Anticommutate gamma5/L/R's to the front
494 exvector::iterator first = s.begin(), next_to_last = s.end() - 2;
496 exvector::iterator it = next_to_last;
498 exvector::iterator it2 = it + 1;
499 if (is_a<clifford>(*it) && is_a<clifford>(*it2)) {
500 ex e1 = it->op(0), e2 = it2->op(0);
502 if (is_a<diracgamma5>(e2)) {
504 if (is_a<diracgammaL>(e1) || is_a<diracgammaR>(e1)) {
506 // gammaL/R gamma5 -> gamma5 gammaL/R
508 something_changed = true;
510 } else if (!is_a<diracgamma5>(e1)) {
512 // gamma5 gamma5 -> gamma5 gamma5 (do nothing)
513 // x gamma5 -> -gamma5 x
516 something_changed = true;
519 } else if (is_a<diracgammaL>(e2)) {
521 if (is_a<diracgammaR>(e1)) {
523 // gammaR gammaL -> 0
526 } else if (!is_a<diracgammaL>(e1) && !is_a<diracgamma5>(e1)) {
528 // gammaL gammaL -> gammaL gammaL (do nothing)
529 // gamma5 gammaL -> gamma5 gammaL (do nothing)
530 // x gammaL -> gammaR x
532 *it = clifford(diracgammaR(), ex_to<clifford>(*it).get_representation_label());
533 something_changed = true;
536 } else if (is_a<diracgammaR>(e2)) {
538 if (is_a<diracgammaL>(e1)) {
540 // gammaL gammaR -> 0
543 } else if (!is_a<diracgammaR>(e1) && !is_a<diracgamma5>(e1)) {
545 // gammaR gammaR -> gammaR gammaR (do nothing)
546 // gamma5 gammaR -> gamma5 gammaR (do nothing)
547 // x gammaR -> gammaL x
549 *it = clifford(diracgammaL(), ex_to<clifford>(*it).get_representation_label());
550 something_changed = true;
558 if (next_to_last == first)
564 // Remove equal adjacent gammas
566 exvector::iterator it, itend = s.end() - 1;
567 for (it = s.begin(); it != itend; ++it) {
570 if (!is_a<clifford>(a) || !is_a<clifford>(b))
573 const ex & ag = a.op(0);
574 const ex & bg = b.op(0);
575 bool a_is_cliffordunit = is_a<cliffordunit>(ag);
576 bool b_is_cliffordunit = is_a<cliffordunit>(bg);
578 if (a_is_cliffordunit && b_is_cliffordunit && ex_to<clifford>(a).same_metric(b)) {
580 const ex & ia = a.op(1);
581 const ex & ib = b.op(1);
582 if (ia.is_equal(ib)) { // gamma~alpha gamma~alpha -> g~alpha~alpha
583 a = ex_to<clifford>(a).get_metric(ia, ib);
584 b = dirac_ONE(representation_label);
585 something_changed = true;
588 } else if ((is_a<diracgamma5>(ag) && is_a<diracgamma5>(bg))) {
590 // Remove squares of gamma5
591 a = dirac_ONE(representation_label);
592 b = dirac_ONE(representation_label);
593 something_changed = true;
595 } else if ((is_a<diracgammaL>(ag) && is_a<diracgammaL>(bg))
596 || (is_a<diracgammaR>(ag) && is_a<diracgammaR>(bg))) {
598 // Remove squares of gammaL/R
599 b = dirac_ONE(representation_label);
600 something_changed = true;
602 } else if (is_a<diracgammaL>(ag) && is_a<diracgammaR>(bg)) {
604 // gammaL and gammaR are orthogonal
607 } else if (is_a<diracgamma5>(ag) && is_a<diracgammaL>(bg)) {
609 // gamma5 gammaL -> -gammaL
610 a = dirac_ONE(representation_label);
612 something_changed = true;
614 } else if (is_a<diracgamma5>(ag) && is_a<diracgammaR>(bg)) {
616 // gamma5 gammaR -> gammaR
617 a = dirac_ONE(representation_label);
618 something_changed = true;
620 } else if (!a_is_cliffordunit && !b_is_cliffordunit && ag.is_equal(bg)) {
623 varidx ix((new symbol)->setflag(status_flags::dynallocated), ex_to<idx>(a.op(1)).minimal_dim(ex_to<idx>(b.op(1))));
625 a = indexed(ag, ix) * indexed(ag, ix.toggle_variance());
626 b = dirac_ONE(representation_label);
627 something_changed = true;
633 return clifford(diracone(), representation_label) * sign;
634 if (something_changed)
635 return reeval_ncmul(s) * sign;
637 return hold_ncmul(s) * sign;
640 ex clifford::thiscontainer(const exvector & v) const
642 return clifford(representation_label, get_metric(), v);
645 ex clifford::thiscontainer(std::auto_ptr<exvector> vp) const
647 return clifford(representation_label, get_metric(), vp);
650 ex diracgamma5::conjugate() const
652 return _ex_1 * (*this);
655 ex diracgammaL::conjugate() const
657 return (new diracgammaR)->setflag(status_flags::dynallocated);
660 ex diracgammaR::conjugate() const
662 return (new diracgammaL)->setflag(status_flags::dynallocated);
669 ex dirac_ONE(unsigned char rl)
671 static ex ONE = (new diracone)->setflag(status_flags::dynallocated);
672 return clifford(ONE, rl);
675 ex clifford_unit(const ex & mu, const ex & metr, unsigned char rl)
677 static ex unit = (new cliffordunit)->setflag(status_flags::dynallocated);
679 if (!is_a<varidx>(mu))
680 throw(std::invalid_argument("index of Clifford unit must be of type varidx"));
682 if (is_a<indexed>(metr))
683 return clifford(unit, mu, metr.op(0), rl);
684 else if(is_a<tensmetric>(metr) || is_a<matrix>(metr))
685 return clifford(unit, mu, metr, rl);
687 throw(std::invalid_argument("metric for Clifford unit must be of type indexed, tensormetric or matrix"));
690 ex dirac_gamma(const ex & mu, unsigned char rl)
692 static ex gamma = (new diracgamma)->setflag(status_flags::dynallocated);
694 if (!is_a<varidx>(mu))
695 throw(std::invalid_argument("index of Dirac gamma must be of type varidx"));
697 return clifford(gamma, mu, default_metric(), rl);
700 ex dirac_gamma5(unsigned char rl)
702 static ex gamma5 = (new diracgamma5)->setflag(status_flags::dynallocated);
703 return clifford(gamma5, rl);
706 ex dirac_gammaL(unsigned char rl)
708 static ex gammaL = (new diracgammaL)->setflag(status_flags::dynallocated);
709 return clifford(gammaL, rl);
712 ex dirac_gammaR(unsigned char rl)
714 static ex gammaR = (new diracgammaR)->setflag(status_flags::dynallocated);
715 return clifford(gammaR, rl);
718 ex dirac_slash(const ex & e, const ex & dim, unsigned char rl)
720 // Slashed vectors are actually stored as a clifford object with the
721 // vector as its base expression and a (dummy) index that just serves
722 // for storing the space dimensionality
723 return clifford(e, varidx(0, dim), default_metric(), rl);
726 /** Check whether a given tinfo key (as returned by return_type_tinfo()
727 * is that of a clifford object with the specified representation label. */
728 static bool is_clifford_tinfo(unsigned ti, unsigned char rl)
730 return ti == (TINFO_clifford + rl);
733 /** Check whether a given tinfo key (as returned by return_type_tinfo()
734 * is that of a clifford object (with an arbitrary representation label). */
735 static bool is_clifford_tinfo(unsigned ti)
737 return (ti & ~0xff) == TINFO_clifford;
740 /** Extract representation label from tinfo key (as returned by
741 * return_type_tinfo()). */
742 static unsigned char get_representation_label(unsigned ti)
747 /** Take trace of a string of an even number of Dirac gammas given a vector
749 static ex trace_string(exvector::const_iterator ix, size_t num)
751 // Tr gamma.mu gamma.nu = 4 g.mu.nu
753 return lorentz_g(ix[0], ix[1]);
755 // 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 )
757 return lorentz_g(ix[0], ix[1]) * lorentz_g(ix[2], ix[3])
758 + lorentz_g(ix[1], ix[2]) * lorentz_g(ix[0], ix[3])
759 - lorentz_g(ix[0], ix[2]) * lorentz_g(ix[1], ix[3]);
761 // Traces of 6 or more gammas are computed recursively:
762 // Tr gamma.mu1 gamma.mu2 ... gamma.mun =
763 // + g.mu1.mu2 * Tr gamma.mu3 ... gamma.mun
764 // - g.mu1.mu3 * Tr gamma.mu2 gamma.mu4 ... gamma.mun
765 // + g.mu1.mu4 * Tr gamma.mu3 gamma.mu3 gamma.mu5 ... gamma.mun
767 // + g.mu1.mun * Tr gamma.mu2 ... gamma.mu(n-1)
771 for (size_t i=1; i<num; i++) {
772 for (size_t n=1, j=0; n<num; n++) {
777 result += sign * lorentz_g(ix[0], ix[i]) * trace_string(v.begin(), num-2);
783 ex dirac_trace(const ex & e, const std::set<unsigned char> & rls, const ex & trONE)
785 if (is_a<clifford>(e)) {
787 unsigned char rl = ex_to<clifford>(e).get_representation_label();
789 // Are we taking the trace over this object's representation label?
790 if (rls.find(rl) == rls.end())
793 // Yes, all elements are traceless, except for dirac_ONE and dirac_L/R
794 const ex & g = e.op(0);
795 if (is_a<diracone>(g))
797 else if (is_a<diracgammaL>(g) || is_a<diracgammaR>(g))
802 } else if (is_exactly_a<mul>(e)) {
804 // Trace of product: pull out non-clifford factors
806 for (size_t i=0; i<e.nops(); i++) {
807 const ex &o = e.op(i);
808 if (is_clifford_tinfo(o.return_type_tinfo()))
809 prod *= dirac_trace(o, rls, trONE);
815 } else if (is_exactly_a<ncmul>(e)) {
817 unsigned char rl = get_representation_label(e.return_type_tinfo());
819 // Are we taking the trace over this string's representation label?
820 if (rls.find(rl) == rls.end())
823 // Substitute gammaL/R and expand product, if necessary
824 ex e_expanded = e.subs(lst(
825 dirac_gammaL(rl) == (dirac_ONE(rl)-dirac_gamma5(rl))/2,
826 dirac_gammaR(rl) == (dirac_ONE(rl)+dirac_gamma5(rl))/2
827 ), subs_options::no_pattern).expand();
828 if (!is_a<ncmul>(e_expanded))
829 return dirac_trace(e_expanded, rls, trONE);
831 // gamma5 gets moved to the front so this check is enough
832 bool has_gamma5 = is_a<diracgamma5>(e.op(0).op(0));
833 size_t num = e.nops();
837 // Trace of gamma5 * odd number of gammas and trace of
838 // gamma5 * gamma.mu * gamma.nu are zero
839 if ((num & 1) == 0 || num == 3)
842 // Tr gamma5 gamma.mu gamma.nu gamma.rho gamma.sigma = 4I * epsilon(mu, nu, rho, sigma)
843 // (the epsilon is always 4-dimensional)
845 ex b1, i1, b2, i2, b3, i3, b4, i4;
846 base_and_index(e.op(1), b1, i1);
847 base_and_index(e.op(2), b2, i2);
848 base_and_index(e.op(3), b3, i3);
849 base_and_index(e.op(4), b4, i4);
850 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();
854 // I/4! * epsilon0123.mu1.mu2.mu3.mu4 * Tr gamma.mu1 gamma.mu2 gamma.mu3 gamma.mu4 S_2k
855 // (the epsilon is always 4-dimensional)
856 exvector ix(num-1), bv(num-1);
857 for (size_t i=1; i<num; i++)
858 base_and_index(e.op(i), bv[i-1], ix[i-1]);
860 int *iv = new int[num];
862 for (size_t i=0; i<num-3; i++) {
864 for (size_t j=i+1; j<num-2; j++) {
866 for (size_t k=j+1; k<num-1; k++) {
868 for (size_t l=k+1; l<num; l++) {
870 iv[0] = i; iv[1] = j; iv[2] = k; iv[3] = l;
873 for (size_t n=0, t=4; n<num; n++) {
874 if (n == i || n == j || n == k || n == l)
879 int sign = permutation_sign(iv, iv + num);
880 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))
881 * trace_string(v.begin(), num - 4);
887 return trONE * I * result * mul(bv);
889 } else { // no gamma5
891 // Trace of odd number of gammas is zero
895 // Tr gamma.mu gamma.nu = 4 g.mu.nu
898 base_and_index(e.op(0), b1, i1);
899 base_and_index(e.op(1), b2, i2);
900 return trONE * (lorentz_g(i1, i2) * b1 * b2).simplify_indexed();
903 exvector iv(num), bv(num);
904 for (size_t i=0; i<num; i++)
905 base_and_index(e.op(i), bv[i], iv[i]);
907 return trONE * (trace_string(iv.begin(), num) * mul(bv)).simplify_indexed();
910 } else if (e.nops() > 0) {
912 // Trace maps to all other container classes (this includes sums)
913 pointer_to_map_function_2args<const std::set<unsigned char> &, const ex &> fcn(dirac_trace, rls, trONE);
920 ex dirac_trace(const ex & e, const lst & rll, const ex & trONE)
922 // Convert list to set
923 std::set<unsigned char> rls;
924 for (lst::const_iterator i = rll.begin(); i != rll.end(); ++i) {
925 if (i->info(info_flags::nonnegint))
926 rls.insert(ex_to<numeric>(*i).to_int());
929 return dirac_trace(e, rls, trONE);
932 ex dirac_trace(const ex & e, unsigned char rl, const ex & trONE)
934 // Convert label to set
935 std::set<unsigned char> rls;
938 return dirac_trace(e, rls, trONE);
942 ex canonicalize_clifford(const ex & e_)
944 pointer_to_map_function fcn(canonicalize_clifford);
946 if (is_a<matrix>(e_) // || is_a<pseries>(e) || is_a<integral>(e)
950 ex e=simplify_indexed(e_);
951 // Scan for any ncmul objects
953 ex aux = e.to_rational(srl);
954 for (exmap::iterator i = srl.begin(); i != srl.end(); ++i) {
959 if (is_exactly_a<ncmul>(rhs)
960 && rhs.return_type() == return_types::noncommutative
961 && is_clifford_tinfo(rhs.return_type_tinfo())) {
963 // Expand product, if necessary
964 ex rhs_expanded = rhs.expand();
965 if (!is_a<ncmul>(rhs_expanded)) {
966 i->second = canonicalize_clifford(rhs_expanded);
969 } else if (!is_a<clifford>(rhs.op(0)))
973 v.reserve(rhs.nops());
974 for (size_t j=0; j<rhs.nops(); j++)
975 v.push_back(rhs.op(j));
977 // Stupid recursive bubble sort because we only want to swap adjacent gammas
978 exvector::iterator it = v.begin(), next_to_last = v.end() - 1;
979 if (is_a<diracgamma5>(it->op(0)) || is_a<diracgammaL>(it->op(0)) || is_a<diracgammaR>(it->op(0)))
981 while (it != next_to_last) {
982 if (it[0].compare(it[1]) > 0) {
983 ex save0 = it[0], save1 = it[1];
985 base_and_index(it[0], b1, i1);
986 base_and_index(it[1], b2, i2);
987 it[0] = (ex_to<clifford>(save0).get_metric(i1, i2) * b1 * b2).simplify_indexed();
988 it[1] = v.size() == 2 ? _ex2 * dirac_ONE(ex_to<clifford>(it[1]).get_representation_label()) : _ex2;
992 sum -= ncmul(v, true);
993 i->second = canonicalize_clifford(sum);
1001 return aux.subs(srl, subs_options::no_pattern).simplify_indexed();
1005 ex clifford_prime(const ex & e)
1007 pointer_to_map_function fcn(clifford_prime);
1008 if (is_a<clifford>(e) && is_a<cliffordunit>(e.op(0))) {
1010 } else if (is_a<add>(e) || is_a<ncmul>(e) || is_a<mul>(e) //|| is_a<pseries>(e) || is_a<integral>(e)
1011 || is_a<matrix>(e) || is_a<lst>(e)) {
1013 } else if (is_a<power>(e)) {
1014 return pow(clifford_prime(e.op(0)), e.op(1));
1019 ex remove_dirac_ONE(const ex & e)
1021 return remove_dirac_ONE(e, 0);
1024 ex remove_dirac_ONE(const ex & e, unsigned char rl)
1026 pointer_to_map_function_1arg<unsigned char> fcn(remove_dirac_ONE, rl);
1027 if (is_a<clifford>(e) && ex_to<clifford>(e).get_representation_label() >= rl) {
1028 if (is_a<diracone>(e.op(0)))
1031 throw(std::invalid_argument("Expression is a non-scalar Clifford number!"));
1032 } else if (is_a<add>(e) || is_a<ncmul>(e) || is_a<mul>(e) // || is_a<pseries>(e) || is_a<integral>(e)
1033 || is_a<matrix>(e) || is_a<lst>(e)) {
1035 } else if (is_a<power>(e)) {
1036 return pow(remove_dirac_ONE(e.op(0)), e.op(1));
1041 ex clifford_norm(const ex & e)
1043 return sqrt(remove_dirac_ONE(canonicalize_clifford(e * clifford_bar(e)).simplify_indexed()));
1046 ex clifford_inverse(const ex & e)
1048 ex norm = clifford_norm(e);
1049 if (!norm.is_zero())
1050 return clifford_bar(e) / pow(norm, 2);
1052 throw(std::invalid_argument("Cannot find inverse of Clifford number with zero norm!"));
1055 ex lst_to_clifford(const ex & v, const ex & mu, const ex & metr, unsigned char rl)
1057 if (!ex_to<idx>(mu).is_dim_numeric())
1058 throw(std::invalid_argument("Index should have a numeric dimension"));
1059 ex e = clifford_unit(mu, metr, rl);
1060 return lst_to_clifford(v, e);
1063 ex lst_to_clifford(const ex & v, const ex & e) {
1066 if (is_a<clifford>(e)) {
1067 varidx mu = ex_to<varidx>(e.op(1));
1068 unsigned dim = (ex_to<numeric>(mu.get_dim())).to_int();
1070 if (is_a<matrix>(v)) {
1071 if (ex_to<matrix>(v).cols() > ex_to<matrix>(v).rows()) {
1072 min = ex_to<matrix>(v).rows();
1073 max = ex_to<matrix>(v).cols();
1075 min = ex_to<matrix>(v).cols();
1076 max = ex_to<matrix>(v).rows();
1080 return indexed(v, ex_to<varidx>(mu).toggle_variance()) * e;
1082 throw(std::invalid_argument("Dimensions of vector and clifford unit mismatch"));
1084 throw(std::invalid_argument("First argument should be a vector vector"));
1085 } else if (is_a<lst>(v)) {
1086 if (dim == ex_to<lst>(v).nops())
1087 return indexed(matrix(dim, 1, ex_to<lst>(v)), ex_to<varidx>(mu).toggle_variance()) * e;
1089 throw(std::invalid_argument("List length and dimension of clifford unit mismatch"));
1091 throw(std::invalid_argument("Cannot construct from anything but list or vector"));
1093 throw(std::invalid_argument("The second argument should be a Clifford unit"));
1096 /** Auxiliary structure to define a function for striping one Clifford unit
1097 * from vectors. Used in clifford_to_lst(). */
1098 static ex get_clifford_comp(const ex & e, const ex & c)
1100 pointer_to_map_function_1arg<const ex &> fcn(get_clifford_comp, c);
1101 int ival = ex_to<numeric>(ex_to<varidx>(c.op(1)).get_value()).to_int();
1103 if (is_a<add>(e) || is_a<lst>(e) // || is_a<pseries>(e) || is_a<integral>(e)
1106 else if (is_a<ncmul>(e) || is_a<mul>(e)) {
1107 // find a Clifford unit with the same metric, delete it and substitute its index
1108 size_t ind = e.nops() + 1;
1109 for (size_t j = 0; j < e.nops(); j++)
1110 if (is_a<clifford>(e.op(j)) && ex_to<clifford>(c).same_metric(e.op(j)))
1114 throw(std::invalid_argument("Expression is a Clifford multi-vector"));
1115 if (ind < e.nops()) {
1117 bool same_value_index, found_dummy;
1118 same_value_index = ( ex_to<varidx>(e.op(ind).op(1)).is_numeric()
1119 && (ival == ex_to<numeric>(ex_to<varidx>(e.op(ind).op(1)).get_value()).to_int()) );
1120 found_dummy = same_value_index;
1121 for(size_t j=0; j < e.nops(); j++)
1123 if (same_value_index)
1126 exvector ind_vec = ex_to<indexed>(e.op(j)).get_dummy_indices(ex_to<indexed>(e.op(ind)));
1127 if (ind_vec.size() > 0) {
1129 exvector::const_iterator it = ind_vec.begin(), itend = ind_vec.end();
1130 while (it != itend) {
1131 S = S * e.op(j).subs(lst(ex_to<varidx>(*it) == ival, ex_to<varidx>(*it).toggle_variance() == ival), subs_options::no_pattern);
1137 return (found_dummy ? S : 0);
1139 throw(std::invalid_argument("Expression is not a Clifford vector to the given units"));
1140 } else if (e.is_zero())
1142 else if (is_a<clifford>(e) && ex_to<clifford>(e).same_metric(c))
1143 if ( ex_to<varidx>(e.op(1)).is_numeric() &&
1144 (ival != ex_to<numeric>(ex_to<varidx>(e.op(1)).get_value()).to_int()) )
1149 throw(std::invalid_argument("Expression is not usable as a Clifford vector"));
1153 lst clifford_to_lst(const ex & e, const ex & c, bool algebraic)
1155 GINAC_ASSERT(is_a<clifford>(c));
1156 varidx mu = ex_to<varidx>(c.op(1));
1157 if (! mu.is_dim_numeric())
1158 throw(std::invalid_argument("Index should have a numeric dimension"));
1159 unsigned int D = ex_to<numeric>(mu.get_dim()).to_int();
1161 if (algebraic) // check if algebraic method is applicable
1162 for (unsigned int i = 0; i < D; i++)
1163 if (pow(c.subs(mu == i), 2).is_zero()
1164 or (not is_a<numeric>(pow(c.subs(mu == i), 2))))
1168 for (unsigned int i = 0; i < D; i++)
1169 V.append(remove_dirac_ONE(
1170 simplify_indexed(canonicalize_clifford(e * c.subs(mu == i) + c.subs(mu == i) * e))
1171 / (2*pow(c.subs(mu == i), 2))));
1173 ex e1 = canonicalize_clifford(e);
1174 for (unsigned int i = 0; i < D; i++)
1175 V.append(get_clifford_comp(e1, c.subs(c.op(1) == i)));
1181 ex clifford_moebius_map(const ex & a, const ex & b, const ex & c, const ex & d, const ex & v, const ex & G, unsigned char rl)
1185 if (! is_a<matrix>(v) && ! is_a<lst>(v))
1186 throw(std::invalid_argument("parameter v should be either vector or list"));
1188 if (is_a<clifford>(G)) {
1191 if (is_a<indexed>(G))
1192 D = ex_to<varidx>(G.op(1)).get_dim();
1193 else if (is_a<matrix>(G))
1194 D = ex_to<matrix>(G).rows();
1195 else throw(std::invalid_argument("metric should be an indexed object, matrix, or a Clifford unit"));
1197 varidx mu((new symbol)->setflag(status_flags::dynallocated), D);
1198 cu = clifford_unit(mu, G, rl);
1201 x = lst_to_clifford(v, cu);
1202 ex e = simplify_indexed(canonicalize_clifford((a * x + b) * clifford_inverse(c * x + d)));
1203 return clifford_to_lst(e, cu, false);
1206 ex clifford_moebius_map(const ex & M, const ex & v, const ex & G, unsigned char rl)
1208 if (is_a<matrix>(M))
1209 return clifford_moebius_map(ex_to<matrix>(M)(0,0), ex_to<matrix>(M)(0,1),
1210 ex_to<matrix>(M)(1,0), ex_to<matrix>(M)(1,1), v, G, rl);
1212 throw(std::invalid_argument("parameter M should be a matrix"));
1215 ex clifford_moebius_map(const ex & M, const ex & v, const ex & G)
1217 return clifford_moebius_map(M, v, G, 0);
1220 ex clifford_moebius_map(const ex & a, const ex & b, const ex & c, const ex & d, const ex & v, const ex & G)
1222 return clifford_moebius_map(a, b, c, d, v, G, 0);
1225 } // namespace GiNaC