GiNaC 1.8.8
clifford.cpp
Go to the documentation of this file.
1
5/*
6 * GiNaC Copyright (C) 1999-2025 Johannes Gutenberg University Mainz, Germany
7 *
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.
12 *
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.
17 *
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
21 */
22
23#include "clifford.h"
24
25#include "ex.h"
26#include "idx.h"
27#include "ncmul.h"
28#include "symbol.h"
29#include "numeric.h" // for I
30#include "symmetry.h"
31#include "lst.h"
32#include "relational.h"
33#include "operators.h"
34#include "add.h"
35#include "mul.h"
36#include "power.h"
37#include "matrix.h"
38#include "archive.h"
39#include "utils.h"
40
41#include <algorithm>
42#include <stdexcept>
43
44namespace GiNaC {
45
48 print_func<print_latex>(&clifford::do_print_latex).
49 print_func<print_tree>(&clifford::do_print_tree))
50
52 print_func<print_dflt>(&diracone::do_print).
53 print_func<print_latex>(&diracone::do_print_latex))
54
56 print_func<print_dflt>(&cliffordunit::do_print).
57 print_func<print_latex>(&cliffordunit::do_print_latex))
58
60 print_func<print_dflt>(&diracgamma::do_print).
61 print_func<print_latex>(&diracgamma::do_print_latex))
62
64 print_func<print_dflt>(&diracgamma5::do_print).
65 print_func<print_latex>(&diracgamma5::do_print_latex))
66
68 print_func<print_context>(&diracgammaL::do_print).
69 print_func<print_latex>(&diracgammaL::do_print_latex))
70
72 print_func<print_context>(&diracgammaR::do_print).
73 print_func<print_latex>(&diracgammaR::do_print_latex))
74
76// default constructors
78
79clifford::clifford() : representation_label(0), metric(0), commutator_sign(-1)
80{
81}
82
83DEFAULT_CTOR(diracone)
84DEFAULT_CTOR(cliffordunit)
85DEFAULT_CTOR(diracgamma)
86DEFAULT_CTOR(diracgamma5)
87DEFAULT_CTOR(diracgammaL)
88DEFAULT_CTOR(diracgammaR)
89
90
91// other constructors
93
97clifford::clifford(const ex & b, unsigned char rl) : inherited(b), representation_label(rl), metric(0), commutator_sign(-1)
98{
99}
100
105clifford::clifford(const ex & b, const ex & mu, const ex & metr, unsigned char rl, int comm_sign) : inherited(b, mu), representation_label(rl), metric(metr), commutator_sign(comm_sign)
106{
107 GINAC_ASSERT(is_a<idx>(mu));
108}
109
110clifford::clifford(unsigned char rl, const ex & metr, int comm_sign, const exvector & v) : inherited(not_symmetric(), v), representation_label(rl), metric(metr), commutator_sign(comm_sign)
111{
112}
113
114clifford::clifford(unsigned char rl, const ex & metr, int comm_sign, exvector && v) : inherited(not_symmetric(), std::move(v)), representation_label(rl), metric(metr), commutator_sign(comm_sign)
115{
116}
117
119{
120 return make_return_type_t<clifford>(representation_label);
121}
122
124// archiving
126
128{
129 inherited::read_archive(n, sym_lst);
130 unsigned rl;
131 n.find_unsigned("label", rl);
133 n.find_ex("metric", metric, sym_lst);
134 n.find_unsigned("commutator_sign+1", rl);
135 commutator_sign = rl - 1;
136}
137
139{
140 inherited::archive(n);
141 n.add_unsigned("label", representation_label);
142 n.add_ex("metric", metric);
143 n.add_unsigned("commutator_sign+1", commutator_sign+1);
144}
145
153
154
155ex clifford::get_metric(const ex & i, const ex & j, bool symmetrised) const
156{
157 if (is_a<indexed>(metric)) {
158 if (symmetrised && !(ex_to<symmetry>(ex_to<indexed>(metric).get_symmetry()).has_symmetry())) {
159 if (is_a<matrix>(metric.op(0))) {
160 return indexed((ex_to<matrix>(metric.op(0)).add(ex_to<matrix>(metric.op(0)).transpose())).mul(numeric(1, 2)),
161 symmetric2(), i, j);
162 } else {
163 return simplify_indexed(indexed(metric.op(0)*_ex1_2, i, j) + indexed(metric.op(0)*_ex1_2, j, i));
164 }
165 } else {
166 return metric.subs(lst{metric.op(1) == i, metric.op(2) == j}, subs_options::no_pattern);
167 }
168 } else {
169 exvector indices = metric.get_free_indices();
170 if (symmetrised)
171 return _ex1_2*simplify_indexed(metric.subs(lst{indices[0] == i, indices[1] == j}, subs_options::no_pattern)
172 + metric.subs(lst{indices[0] == j, indices[1] == i}, subs_options::no_pattern));
173 else
174 return metric.subs(lst{indices[0] == i, indices[1] == j}, subs_options::no_pattern);
175 }
176}
177
178bool clifford::same_metric(const ex & other) const
179{
180 ex metr;
181 if (is_a<clifford>(other))
182 metr = ex_to<clifford>(other).get_metric();
183 else
184 metr = other;
185
186 if (is_a<indexed>(metr))
187 return metr.op(0).is_equal(get_metric().op(0));
188 else {
189 exvector indices = metr.get_free_indices();
190 return (indices.size() == 2)
191 && simplify_indexed(get_metric(indices[0], indices[1])-metr).is_zero();
192 }
193}
194
196// functions overriding virtual functions from base classes
198
199ex clifford::op(size_t i) const
200{
201 GINAC_ASSERT(i<nops());
202 if (nops()-i == 1)
204 else
205 return inherited::op(i);
206}
207
209{
210 GINAC_ASSERT(i<nops());
211
212 static ex rl = numeric(representation_label);
214 if (nops()-i == 1)
215 return rl;
216 else
217 return inherited::let_op(i);
218}
219
220ex clifford::subs(const exmap & m, unsigned options) const
221{
222 ex subsed = inherited::subs(m, options);
223 if(is_a<clifford>(subsed)) {
224 ex prevmetric = ex_to<clifford>(subsed).metric;
225 ex newmetric = prevmetric.subs(m, options);
226 if(!are_ex_trivially_equal(prevmetric, newmetric)) {
227 clifford c = ex_to<clifford>(subsed);
228 c.metric = newmetric;
229 subsed = c;
230 }
231 }
232 return subsed;
233}
234
235int clifford::compare_same_type(const basic & other) const
236{
237 GINAC_ASSERT(is_a<clifford>(other));
238 const clifford &o = static_cast<const clifford &>(other);
239
241 // different representation label
242 return representation_label < o.representation_label ? -1 : 1;
243 }
244
245 return inherited::compare_same_type(other);
246}
247
248bool clifford::match_same_type(const basic & other) const
249{
250 GINAC_ASSERT(is_a<clifford>(other));
251 const clifford &o = static_cast<const clifford &>(other);
252
254}
255
256static bool is_dirac_slash(const ex & seq0)
257{
258 return !is_a<diracgamma5>(seq0) && !is_a<diracgammaL>(seq0) &&
259 !is_a<diracgammaR>(seq0) && !is_a<cliffordunit>(seq0) &&
260 !is_a<diracone>(seq0);
261}
262
263void clifford::do_print_dflt(const print_dflt & c, unsigned level) const
264{
265 // dirac_slash() object is printed differently
266 if (is_dirac_slash(seq[0])) {
267 seq[0].print(c, precedence());
268 c.s << "\\";
269 } else { // We do not print representation label if it is 0
270 if (representation_label == 0) {
271 this->print_dispatch<inherited>(c, level);
272 } else { // otherwise we put it before indices in square brackets; the code is borrowed from indexed.cpp
273 if (precedence() <= level) {
274 c.s << '(';
275 }
276 seq[0].print(c, precedence());
277 c.s << '[' << int(representation_label) << ']';
278 printindices(c, level);
279 if (precedence() <= level) {
280 c.s << ')';
281 }
282 }
283 }
284}
285
286void clifford::do_print_latex(const print_latex & c, unsigned level) const
287{
288 // dirac_slash() object is printed differently
289 if (is_dirac_slash(seq[0])) {
290 c.s << "{";
291 seq[0].print(c, precedence());
292 c.s << "\\hspace{-1.0ex}/}";
293 } else {
294 c.s << "\\clifford[" << int(representation_label) << "]";
295 this->print_dispatch<inherited>(c, level);
296 }
297}
298
299void clifford::do_print_tree(const print_tree & c, unsigned level) const
300{
301 c.s << std::string(level, ' ') << class_name() << " @" << this
302 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
303 << ", " << seq.size()-1 << " indices"
304 << ", symmetry=" << symtree << std::endl;
305 metric.print(c, level + c.delta_indent);
306 seq[0].print(c, level + c.delta_indent);
307 printindices(c, level + c.delta_indent);
308}
309
316
317DEFAULT_PRINT_LATEX(diracone, "ONE", "\\mathbf{1}")
319DEFAULT_PRINT_LATEX(diracgamma, "gamma", "\\gamma")
320DEFAULT_PRINT_LATEX(diracgamma5, "gamma5", "{\\gamma^5}")
321DEFAULT_PRINT_LATEX(diracgammaL, "gammaL", "{\\gamma_L}")
322DEFAULT_PRINT_LATEX(diracgammaR, "gammaR", "{\\gamma_R}")
323
325static void base_and_index(const ex & c, ex & b, ex & i)
326{
327 GINAC_ASSERT(is_a<clifford>(c));
328 GINAC_ASSERT(c.nops() == 2+1);
329
330 if (is_a<cliffordunit>(c.op(0))) { // proper dirac gamma object or clifford unit
331 i = c.op(1);
332 b = _ex1;
333 } else if (is_a<diracgamma5>(c.op(0)) || is_a<diracgammaL>(c.op(0)) || is_a<diracgammaR>(c.op(0))) { // gamma5/L/R
334 i = _ex0;
335 b = _ex1;
336 } else { // slash object, generate new dummy index
337 varidx ix(dynallocate<symbol>(), ex_to<idx>(c.op(1)).get_dim());
338 b = indexed(c.op(0), ix.toggle_variance());
339 i = ix;
340 }
341}
342
344struct is_not_a_clifford {
345 bool operator()(const ex & e)
346 {
347 return !is_a<clifford>(e);
348 }
349};
350
352bool diracgamma::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
353{
354 GINAC_ASSERT(is_a<clifford>(*self));
355 GINAC_ASSERT(is_a<indexed>(*other));
356 GINAC_ASSERT(is_a<diracgamma>(self->op(0)));
357 unsigned char rl = ex_to<clifford>(*self).get_representation_label();
358
359 ex dim = ex_to<idx>(self->op(1)).get_dim();
360 if (other->nops() > 1)
361 dim = minimal_dim(dim, ex_to<idx>(other->op(1)).get_dim());
362
363 if (is_a<clifford>(*other)) {
364
365 // Contraction only makes sense if the representation labels are equal
366 if (ex_to<clifford>(*other).get_representation_label() != rl)
367 return false;
368
369 size_t num = other - self;
370
371 // gamma~mu gamma.mu = dim ONE
372 if (num == 1) {
373 *self = dim;
374 *other = dirac_ONE(rl);
375 return true;
376
377 // gamma~mu gamma~alpha gamma.mu = (2-dim) gamma~alpha
378 } else if (num == 2
379 && is_a<clifford>(self[1])) {
380 *self = 2 - dim;
381 *other = _ex1;
382 return true;
383
384 // gamma~mu gamma~alpha gamma~beta gamma.mu = 4 g~alpha~beta + (dim-4) gamam~alpha gamma~beta
385 } else if (num == 3
386 && is_a<clifford>(self[1])
387 && is_a<clifford>(self[2])) {
388 ex b1, i1, b2, i2;
389 base_and_index(self[1], b1, i1);
390 base_and_index(self[2], b2, i2);
391 *self = 4 * lorentz_g(i1, i2) * b1 * b2 * dirac_ONE(rl) + (dim - 4) * self[1] * self[2];
392 self[1] = _ex1;
393 self[2] = _ex1;
394 *other = _ex1;
395 return true;
396
397 // 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
398 } else if (num == 4
399 && is_a<clifford>(self[1])
400 && is_a<clifford>(self[2])
401 && is_a<clifford>(self[3])) {
402 *self = -2 * self[3] * self[2] * self[1] - (dim - 4) * self[1] * self[2] * self[3];
403 self[1] = _ex1;
404 self[2] = _ex1;
405 self[3] = _ex1;
406 *other = _ex1;
407 return true;
408
409 // gamma~mu Sodd gamma.mu = -2 Sodd_R
410 // (Chisholm identity in 4 dimensions)
411 } else if (!((other - self) & 1) && dim.is_equal(4)) {
412 if (std::find_if(self + 1, other, is_not_a_clifford()) != other)
413 return false;
414
415 *self = ncmul(exvector(std::reverse_iterator<exvector::const_iterator>(other), std::reverse_iterator<exvector::const_iterator>(self + 1)));
416 std::fill(self + 1, other, _ex1);
417 *other = _ex_2;
418 return true;
419
420 // gamma~mu Sodd gamma~alpha gamma.mu = 2 gamma~alpha Sodd + 2 Sodd_R gamma~alpha
421 // (commutate contracted indices towards each other, then use
422 // Chisholm identity in 4 dimensions)
423 } else if (((other - self) & 1) && dim.is_equal(4)) {
424 if (std::find_if(self + 1, other, is_not_a_clifford()) != other)
425 return false;
426
427 auto next_to_last = other - 1;
428 ex S = ncmul(exvector(self + 1, next_to_last));
429 ex SR = ncmul(exvector(std::reverse_iterator<exvector::const_iterator>(next_to_last), std::reverse_iterator<exvector::const_iterator>(self + 1)));
430
431 *self = (*next_to_last) * S + SR * (*next_to_last);
432 std::fill(self + 1, other, _ex1);
433 *other = _ex2;
434 return true;
435
436 // gamma~mu S gamma~alpha gamma.mu = 2 gamma~alpha S - gamma~mu S gamma.mu gamma~alpha
437 // (commutate contracted indices towards each other, simplify_indexed()
438 // will re-expand and re-run the simplification)
439 } else {
440 if (std::find_if(self + 1, other, is_not_a_clifford()) != other)
441 return false;
442
443 auto next_to_last = other - 1;
444 ex S = ncmul(exvector(self + 1, next_to_last));
445
446 *self = 2 * (*next_to_last) * S - (*self) * S * (*other) * (*next_to_last);
447 std::fill(self + 1, other + 1, _ex1);
448 return true;
449 }
450
451 } else if (is_a<symbol>(other->op(0)) && other->nops() == 2) {
452
453 // x.mu gamma~mu -> x-slash
454 *self = dirac_slash(other->op(0), dim, rl);
455 *other = _ex1;
456 return true;
457 }
458
459 return false;
460}
461
463bool cliffordunit::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
464{
465 GINAC_ASSERT(is_a<clifford>(*self));
466 GINAC_ASSERT(is_a<indexed>(*other));
467 GINAC_ASSERT(is_a<cliffordunit>(self->op(0)));
468 clifford unit = ex_to<clifford>(*self);
469 unsigned char rl = unit.get_representation_label();
470
471 if (is_a<clifford>(*other)) {
472 // Contraction only makes sense if the representation labels are equal
473 // and the metrics are the same
474 if ((ex_to<clifford>(*other).get_representation_label() != rl)
475 && unit.same_metric(*other))
476 return false;
477
478 auto before_other = other - 1;
479 ex mu = self->op(1);
480 ex mu_toggle = other->op(1);
481 ex alpha = before_other->op(1);
482
483 // e~mu e.mu = Tr ONE
484 if (other - self == 1) {
485 *self = unit.get_metric(mu, mu_toggle, true);
486 *other = dirac_ONE(rl);
487 return true;
488
489 } else if (other - self == 2) {
490 if (is_a<clifford>(*before_other) && ex_to<clifford>(*before_other).get_representation_label() == rl) {
491 // e~mu e~alpha e.mu = 2*e~mu B(alpha, mu.toggle_variance())-Tr(B) e~alpha
492 *self = 2 * (*self) * unit.get_metric(alpha, mu_toggle, true) - unit.get_metric(mu, mu_toggle, true) * (*before_other);
493 *before_other = _ex1;
494 *other = _ex1;
495 return true;
496
497 } else {
498 // e~mu S e.mu = Tr S ONE
499 *self = unit.get_metric(mu, mu_toggle, true);
500 *other = dirac_ONE(rl);
501 return true;
502 }
503 } else {
504 // e~mu S e~alpha e.mu = 2 e~mu S B(alpha, mu.toggle_variance()) - e~mu S e.mu e~alpha
505 // (commutate contracted indices towards each other, simplify_indexed()
506 // will re-expand and re-run the simplification)
507 if (std::find_if(self + 1, other, is_not_a_clifford()) != other) {
508 return false;
509 }
510
511 ex S = ncmul(exvector(self + 1, before_other));
512
513 if (is_a<clifford>(*before_other) && ex_to<clifford>(*before_other).get_representation_label() == rl) {
514 *self = 2 * (*self) * S * unit.get_metric(alpha, mu_toggle, true) - (*self) * S * (*other) * (*before_other);
515 } else {
516 // simply commutes
517 *self = (*self) * S * (*other) * (*before_other);
518 }
519
520 std::fill(self + 1, other + 1, _ex1);
521 return true;
522 }
523 }
524 return false;
525}
526
530ex clifford::eval_ncmul(const exvector & v) const
531{
532 exvector s;
533 s.reserve(v.size());
534
535 // Remove superfluous ONEs
536 for (auto & it : v) {
537 if (!is_a<clifford>(it) || !is_a<diracone>(it.op(0)))
538 s.push_back(it);
539 }
540
541 bool something_changed = false;
542 int sign = 1;
543
544 // Anticommutate gamma5/L/R's to the front
545 if (s.size() >= 2) {
546 auto first = s.begin(), next_to_last = s.end() - 2;
547 while (true) {
548 auto it = next_to_last;
549 while (true) {
550 auto it2 = it + 1;
551 if (is_a<clifford>(*it) && is_a<clifford>(*it2)) {
552 ex e1 = it->op(0), e2 = it2->op(0);
553
554 if (is_a<diracgamma5>(e2)) {
555
556 if (is_a<diracgammaL>(e1) || is_a<diracgammaR>(e1)) {
557
558 // gammaL/R gamma5 -> gamma5 gammaL/R
559 it->swap(*it2);
560 something_changed = true;
561
562 } else if (!is_a<diracgamma5>(e1)) {
563
564 // gamma5 gamma5 -> gamma5 gamma5 (do nothing)
565 // x gamma5 -> -gamma5 x
566 it->swap(*it2);
567 sign = -sign;
568 something_changed = true;
569 }
570
571 } else if (is_a<diracgammaL>(e2)) {
572
573 if (is_a<diracgammaR>(e1)) {
574
575 // gammaR gammaL -> 0
576 return _ex0;
577
578 } else if (!is_a<diracgammaL>(e1) && !is_a<diracgamma5>(e1)) {
579
580 // gammaL gammaL -> gammaL gammaL (do nothing)
581 // gamma5 gammaL -> gamma5 gammaL (do nothing)
582 // x gammaL -> gammaR x
583 it->swap(*it2);
584 *it = clifford(diracgammaR(), ex_to<clifford>(*it).get_representation_label());
585 something_changed = true;
586 }
587
588 } else if (is_a<diracgammaR>(e2)) {
589
590 if (is_a<diracgammaL>(e1)) {
591
592 // gammaL gammaR -> 0
593 return _ex0;
594
595 } else if (!is_a<diracgammaR>(e1) && !is_a<diracgamma5>(e1)) {
596
597 // gammaR gammaR -> gammaR gammaR (do nothing)
598 // gamma5 gammaR -> gamma5 gammaR (do nothing)
599 // x gammaR -> gammaL x
600 it->swap(*it2);
601 *it = clifford(diracgammaL(), ex_to<clifford>(*it).get_representation_label());
602 something_changed = true;
603 }
604 }
605 }
606 if (it == first)
607 break;
608 --it;
609 }
610 if (next_to_last == first)
611 break;
612 --next_to_last;
613 }
614 }
615
616 // Remove equal adjacent gammas
617 if (s.size() >= 2) {
618 exvector::iterator it, itend = s.end() - 1;
619 for (it = s.begin(); it != itend; ++it) {
620 ex & a = it[0];
621 ex & b = it[1];
622 if (!is_a<clifford>(a) || !is_a<clifford>(b))
623 continue;
624
625 const ex & ag = a.op(0);
626 const ex & bg = b.op(0);
627 bool a_is_cliffordunit = is_a<cliffordunit>(ag);
628 bool b_is_cliffordunit = is_a<cliffordunit>(bg);
629
630 if (a_is_cliffordunit && b_is_cliffordunit && ex_to<clifford>(a).same_metric(b)
631 && (ex_to<clifford>(a).get_commutator_sign() == -1)) {
632 // This is done only for Clifford algebras
633
634 const ex & ia = a.op(1);
635 const ex & ib = b.op(1);
636 if (ia.is_equal(ib)) { // gamma~alpha gamma~alpha -> g~alpha~alpha
637 a = ex_to<clifford>(a).get_metric(ia, ib, true);
638 b = dirac_ONE(representation_label);
639 something_changed = true;
640 }
641
642 } else if ((is_a<diracgamma5>(ag) && is_a<diracgamma5>(bg))) {
643
644 // Remove squares of gamma5
645 a = dirac_ONE(representation_label);
646 b = dirac_ONE(representation_label);
647 something_changed = true;
648
649 } else if ((is_a<diracgammaL>(ag) && is_a<diracgammaL>(bg))
650 || (is_a<diracgammaR>(ag) && is_a<diracgammaR>(bg))) {
651
652 // Remove squares of gammaL/R
653 b = dirac_ONE(representation_label);
654 something_changed = true;
655
656 } else if (is_a<diracgammaL>(ag) && is_a<diracgammaR>(bg)) {
657
658 // gammaL and gammaR are orthogonal
659 return _ex0;
660
661 } else if (is_a<diracgamma5>(ag) && is_a<diracgammaL>(bg)) {
662
663 // gamma5 gammaL -> -gammaL
664 a = dirac_ONE(representation_label);
665 sign = -sign;
666 something_changed = true;
667
668 } else if (is_a<diracgamma5>(ag) && is_a<diracgammaR>(bg)) {
669
670 // gamma5 gammaR -> gammaR
671 a = dirac_ONE(representation_label);
672 something_changed = true;
673
674 } else if (!a_is_cliffordunit && !b_is_cliffordunit && ag.is_equal(bg)) {
675
676 // a\ a\ -> a^2
677 varidx ix(dynallocate<symbol>(), ex_to<idx>(a.op(1)).minimal_dim(ex_to<idx>(b.op(1))));
678
679 a = indexed(ag, ix) * indexed(ag, ix.toggle_variance());
680 b = dirac_ONE(representation_label);
681 something_changed = true;
682 }
683 }
684 }
685
686 if (s.empty())
687 return dirac_ONE(representation_label) * sign;
688 if (something_changed)
689 return reeval_ncmul(s) * sign;
690 else
691 return hold_ncmul(s) * sign;
692}
693
694ex clifford::thiscontainer(const exvector & v) const
695{
696 return clifford(representation_label, metric, commutator_sign, v);
697}
698
699ex clifford::thiscontainer(exvector && v) const
700{
701 return clifford(representation_label, metric, commutator_sign, std::move(v));
702}
703
704ex diracgamma5::conjugate() const
705{
706 return _ex_1 * (*this);
707}
708
709ex diracgammaL::conjugate() const
710{
711 return dynallocate<diracgammaR>();
712}
713
714ex diracgammaR::conjugate() const
715{
716 return dynallocate<diracgammaL>();
717}
718
720// global functions
722
723ex dirac_ONE(unsigned char rl)
724{
725 static ex ONE = dynallocate<diracone>();
726 return clifford(ONE, rl);
727}
728
729static unsigned get_dim_uint(const ex& e)
730{
731 if (!is_a<idx>(e))
732 throw std::invalid_argument("get_dim_uint: argument is not an index");
733 ex dim = ex_to<idx>(e).get_dim();
734 if (!dim.info(info_flags::posint))
735 throw std::invalid_argument("get_dim_uint: dimension of index should be a positive integer");
736 unsigned d = ex_to<numeric>(dim).to_int();
737 return d;
738}
739
740ex clifford_unit(const ex & mu, const ex & metr, unsigned char rl)
741{
742 ex unit = dynallocate<cliffordunit>();
743
744 if (!is_a<idx>(mu))
745 throw(std::invalid_argument("clifford_unit(): index of Clifford unit must be of type idx or varidx"));
746
747 exvector indices = metr.get_free_indices();
748
749 if (indices.size() == 2) {
750 return clifford(unit, mu, metr, rl);
751 } else if (is_a<matrix>(metr)) {
752 matrix M = ex_to<matrix>(metr);
753 unsigned n = M.rows();
754 bool symmetric = true;
755
756 //static idx xi(dynallocate<symbol>(), n),
757 // chi(dynallocate<symbol>(), n);
758 idx xi(dynallocate<symbol>(), n),
759 chi(dynallocate<symbol>(), n);
760 if ((n == M.cols()) && (n == get_dim_uint(mu))) {
761 for (unsigned i = 0; i < n; i++) {
762 for (unsigned j = i+1; j < n; j++) {
763 if (!M(i, j).is_equal(M(j, i))) {
764 symmetric = false;
765 }
766 }
767 }
768 return clifford(unit, mu, indexed(metr, symmetric?symmetric2():not_symmetric(), xi, chi), rl);
769 } else {
770 throw(std::invalid_argument("clifford_unit(): metric for Clifford unit must be a square matrix with the same dimensions as index"));
771 }
772 } else if (indices.size() == 0) { // a tensor or other expression without indices
773 //static varidx xi(dynallocate<symbol>(), ex_to<idx>(mu).get_dim()),
774 // chi(dynallocate<symbol>(), ex_to<idx>(mu).get_dim());
775 varidx xi(dynallocate<symbol>(), ex_to<idx>(mu).get_dim()),
776 chi(dynallocate<symbol>(), ex_to<idx>(mu).get_dim());
777 return clifford(unit, mu, indexed(metr, xi, chi), rl);
778 } else
779 throw(std::invalid_argument("clifford_unit(): metric for Clifford unit must be of type tensor, matrix or an expression with two free indices"));
780}
781
782ex dirac_gamma(const ex & mu, unsigned char rl)
783{
784 static ex gamma = dynallocate<diracgamma>();
785
786 if (!is_a<varidx>(mu))
787 throw(std::invalid_argument("dirac_gamma(): index of Dirac gamma must be of type varidx"));
788
789 static varidx xi(dynallocate<symbol>(), ex_to<varidx>(mu).get_dim()),
790 chi(dynallocate<symbol>(), ex_to<varidx>(mu).get_dim());
791 return clifford(gamma, mu, indexed(dynallocate<minkmetric>(), symmetric2(), xi, chi), rl);
792}
793
794ex dirac_gamma5(unsigned char rl)
795{
796 static ex gamma5 = dynallocate<diracgamma5>();
797 return clifford(gamma5, rl);
798}
799
800ex dirac_gammaL(unsigned char rl)
801{
802 static ex gammaL = dynallocate<diracgammaL>();
803 return clifford(gammaL, rl);
804}
805
806ex dirac_gammaR(unsigned char rl)
807{
808 static ex gammaR = dynallocate<diracgammaR>();
809 return clifford(gammaR, rl);
810}
811
812ex dirac_slash(const ex & e, const ex & dim, unsigned char rl)
813{
814 // Slashed vectors are actually stored as a clifford object with the
815 // vector as its base expression and a (dummy) index that just serves
816 // for storing the space dimensionality
817
818 static varidx xi(dynallocate<symbol>(), dim),
819 chi(dynallocate<symbol>(), dim);
820 return clifford(e, varidx(0, dim), indexed(dynallocate<minkmetric>(), symmetric2(), xi, chi), rl);
821}
822
825static unsigned char get_representation_label(const return_type_t& ti)
826{
827 return (unsigned char)ti.rl;
828}
829
832static ex trace_string(exvector::const_iterator ix, size_t num)
833{
834 // Tr gamma.mu gamma.nu = 4 g.mu.nu
835 if (num == 2)
836 return lorentz_g(ix[0], ix[1]);
837
838 // 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 )
839 else if (num == 4)
840 return lorentz_g(ix[0], ix[1]) * lorentz_g(ix[2], ix[3])
841 + lorentz_g(ix[1], ix[2]) * lorentz_g(ix[0], ix[3])
842 - lorentz_g(ix[0], ix[2]) * lorentz_g(ix[1], ix[3]);
843
844 // Traces of 6 or more gammas are computed recursively:
845 // Tr gamma.mu1 gamma.mu2 ... gamma.mun =
846 // + g.mu1.mu2 * Tr gamma.mu3 ... gamma.mun
847 // - g.mu1.mu3 * Tr gamma.mu2 gamma.mu4 ... gamma.mun
848 // + g.mu1.mu4 * Tr gamma.mu3 gamma.mu3 gamma.mu5 ... gamma.mun
849 // - ...
850 // + g.mu1.mun * Tr gamma.mu2 ... gamma.mu(n-1)
851 exvector v(num - 2);
852 int sign = 1;
853 ex result;
854 for (size_t i=1; i<num; i++) {
855 for (size_t n=1, j=0; n<num; n++) {
856 if (n == i)
857 continue;
858 v[j++] = ix[n];
859 }
860 result += sign * lorentz_g(ix[0], ix[i]) * trace_string(v.begin(), num-2);
861 sign = -sign;
862 }
863 return result;
864}
865
866ex dirac_trace(const ex & e, const std::set<unsigned char> & rls, const ex & trONE)
867{
868 if (is_a<clifford>(e)) {
869
870 unsigned char rl = ex_to<clifford>(e).get_representation_label();
871
872 // Are we taking the trace over this object's representation label?
873 if (rls.find(rl) == rls.end())
874 return e;
875
876 // Yes, all elements are traceless, except for dirac_ONE and dirac_L/R
877 const ex & g = e.op(0);
878 if (is_a<diracone>(g))
879 return trONE;
880 else if (is_a<diracgammaL>(g) || is_a<diracgammaR>(g))
881 return trONE/2;
882 else
883 return _ex0;
884
885 } else if (is_exactly_a<mul>(e)) {
886
887 // Trace of product: pull out non-clifford factors
888 ex prod = _ex1;
889 for (size_t i=0; i<e.nops(); i++) {
890 const ex &o = e.op(i);
891 if (is_clifford_tinfo(o.return_type_tinfo()))
892 prod *= dirac_trace(o, rls, trONE);
893 else
894 prod *= o;
895 }
896 return prod;
897
898 } else if (is_exactly_a<ncmul>(e)) {
899
900 unsigned char rl = get_representation_label(e.return_type_tinfo());
901
902 // Are we taking the trace over this string's representation label?
903 if (rls.find(rl) == rls.end())
904 return e;
905
906 // Substitute gammaL/R and expand product, if necessary
907 ex e_expanded = e.subs(lst{
908 dirac_gammaL(rl) == (dirac_ONE(rl)-dirac_gamma5(rl))/2,
909 dirac_gammaR(rl) == (dirac_ONE(rl)+dirac_gamma5(rl))/2
910 }, subs_options::no_pattern).expand();
911 if (!is_a<ncmul>(e_expanded))
912 return dirac_trace(e_expanded, rls, trONE);
913
914 // gamma5 gets moved to the front so this check is enough
915 bool has_gamma5 = is_a<diracgamma5>(e.op(0).op(0));
916 size_t num = e.nops();
917
918 if (has_gamma5) {
919
920 // Trace of gamma5 * odd number of gammas and trace of
921 // gamma5 * gamma.mu * gamma.nu are zero
922 if ((num & 1) == 0 || num == 3)
923 return _ex0;
924
925 // Tr gamma5 gamma.mu gamma.nu gamma.rho gamma.sigma = 4I * epsilon(mu, nu, rho, sigma)
926 // (the epsilon is always 4-dimensional)
927 if (num == 5) {
928 ex b1, i1, b2, i2, b3, i3, b4, i4;
929 base_and_index(e.op(1), b1, i1);
930 base_and_index(e.op(2), b2, i2);
931 base_and_index(e.op(3), b3, i3);
932 base_and_index(e.op(4), b4, i4);
933 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();
934 }
935
936 // Tr gamma5 S_2k =
937 // I/4! * epsilon0123.mu1.mu2.mu3.mu4 * Tr gamma.mu1 gamma.mu2 gamma.mu3 gamma.mu4 S_2k
938 // (the epsilon is always 4-dimensional)
939 exvector ix(num-1), bv(num-1);
940 for (size_t i=1; i<num; i++)
941 base_and_index(e.op(i), bv[i-1], ix[i-1]);
942 num--;
943 int *iv = new int[num];
944 ex result;
945 for (size_t i=0; i<num-3; i++) {
946 ex idx1 = ix[i];
947 for (size_t j=i+1; j<num-2; j++) {
948 ex idx2 = ix[j];
949 for (size_t k=j+1; k<num-1; k++) {
950 ex idx3 = ix[k];
951 for (size_t l=k+1; l<num; l++) {
952 ex idx4 = ix[l];
953 iv[0] = i; iv[1] = j; iv[2] = k; iv[3] = l;
954 exvector v;
955 v.reserve(num - 4);
956 for (size_t n=0, t=4; n<num; n++) {
957 if (n == i || n == j || n == k || n == l)
958 continue;
959 iv[t++] = n;
960 v.push_back(ix[n]);
961 }
962 int sign = permutation_sign(iv, iv + num);
963 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))
964 * trace_string(v.begin(), num - 4);
965 }
966 }
967 }
968 }
969 delete[] iv;
970 return trONE * I * result * mul(bv);
971
972 } else { // no gamma5
973
974 // Trace of odd number of gammas is zero
975 if ((num & 1) == 1)
976 return _ex0;
977
978 // Tr gamma.mu gamma.nu = 4 g.mu.nu
979 if (num == 2) {
980 ex b1, i1, b2, i2;
981 base_and_index(e.op(0), b1, i1);
982 base_and_index(e.op(1), b2, i2);
983 return trONE * (lorentz_g(i1, i2) * b1 * b2).simplify_indexed();
984 }
985
986 exvector iv(num), bv(num);
987 for (size_t i=0; i<num; i++)
988 base_and_index(e.op(i), bv[i], iv[i]);
989
990 return trONE * (trace_string(iv.begin(), num) * mul(bv)).simplify_indexed();
991 }
992
993 } else if (e.nops() > 0) {
994
995 // Trace maps to all other container classes (this includes sums)
996 pointer_to_map_function_2args<const std::set<unsigned char> &, const ex &> fcn(dirac_trace, rls, trONE);
997 return e.map(fcn);
998
999 } else
1000 return _ex0;
1001}
1002
1003ex dirac_trace(const ex & e, const lst & rll, const ex & trONE)
1004{
1005 // Convert list to set
1006 std::set<unsigned char> rls;
1007 for (const auto & i : rll) {
1008 if (i.info(info_flags::nonnegint))
1009 rls.insert(ex_to<numeric>(i).to_int());
1010 }
1011
1012 return dirac_trace(e, rls, trONE);
1013}
1014
1015ex dirac_trace(const ex & e, unsigned char rl, const ex & trONE)
1016{
1017 // Convert label to set
1018 std::set<unsigned char> rls;
1019 rls.insert(rl);
1020
1021 return dirac_trace(e, rls, trONE);
1022}
1023
1024
1025ex canonicalize_clifford(const ex & e_)
1026{
1027 pointer_to_map_function fcn(canonicalize_clifford);
1028
1029 if (is_a<matrix>(e_) // || is_a<pseries>(e) || is_a<integral>(e)
1030 || e_.info(info_flags::list)) {
1031 return e_.map(fcn);
1032 } else {
1033 ex e=simplify_indexed(e_);
1034 // Scan for any ncmul objects
1035 exmap srl;
1036 ex aux = e.to_rational(srl);
1037 for (auto & i : srl) {
1038
1039 ex lhs = i.first;
1040 ex rhs = i.second;
1041
1042 if (is_exactly_a<ncmul>(rhs)
1043 && rhs.return_type() == return_types::noncommutative
1044 && is_clifford_tinfo(rhs.return_type_tinfo())) {
1045
1046 // Expand product, if necessary
1047 ex rhs_expanded = rhs.expand();
1048 if (!is_a<ncmul>(rhs_expanded)) {
1049 i.second = canonicalize_clifford(rhs_expanded);
1050 continue;
1051
1052 } else if (!is_a<clifford>(rhs.op(0)))
1053 continue;
1054
1055 exvector v;
1056 v.reserve(rhs.nops());
1057 for (size_t j=0; j<rhs.nops(); j++)
1058 v.push_back(rhs.op(j));
1059
1060 // Stupid recursive bubble sort because we only want to swap adjacent gammas
1061 auto it = v.begin(), next_to_last = v.end() - 1;
1062 if (is_a<diracgamma5>(it->op(0)) || is_a<diracgammaL>(it->op(0)) || is_a<diracgammaR>(it->op(0)))
1063 ++it;
1064
1065 while (it != next_to_last) {
1066 if (it[0].compare(it[1]) > 0) {
1067
1068 ex save0 = it[0], save1 = it[1];
1069 ex b1, i1, b2, i2;
1070 base_and_index(it[0], b1, i1);
1071 base_and_index(it[1], b2, i2);
1072 // for Clifford algebras (commutator_sign == -1) metric should be symmetrised
1073 it[0] = (ex_to<clifford>(save0).get_metric(i1, i2, ex_to<clifford>(save0).get_commutator_sign() == -1) * b1 * b2).simplify_indexed();
1074 it[1] = v.size() ? _ex2 * dirac_ONE(ex_to<clifford>(save0).get_representation_label()) : _ex2;
1075 ex sum = ncmul(v);
1076 it[0] = save1;
1077 it[1] = save0;
1078 sum += ex_to<clifford>(save0).get_commutator_sign() * ncmul(std::move(v));
1079 i.second = canonicalize_clifford(sum);
1080 goto next_sym;
1081 }
1082 ++it;
1083 }
1084next_sym: ;
1085 }
1086 }
1087 return aux.subs(srl, subs_options::no_pattern).simplify_indexed();
1088 }
1089}
1090
1091ex clifford_star_bar(const ex & e, bool do_bar, unsigned options)
1092{
1093 pointer_to_map_function_2args<bool, unsigned> fcn(clifford_star_bar, do_bar, options | 1);
1094
1095 // is a child, no need to expand
1096 ex e1= (options & 1 ? e : e.expand());
1097
1098 if (is_a<ncmul>(e1) ) { // reversing order of clifford units
1099 exvector ev, cv;
1100 ev.reserve(e1.nops());
1101 cv.reserve(e1.nops());
1102 // separate clifford and non-clifford entries
1103 for (size_t i= 0; i < e1.nops(); ++i) {
1104 if (is_a<clifford>(e1.op(i)) && is_a<cliffordunit>(e1.op(i).op(0)))
1105 cv.push_back(e1.op(i));
1106 else
1107 ev.push_back(e1.op(i));
1108 }
1109 for (auto i=cv.rbegin(); i!=cv.rend(); ++i) { // reverse order of Clifford units
1110 ev.push_back(i->conjugate());
1111 }
1112 // For clifford_bar an odd number of clifford units reverts the sign
1113 if (do_bar && (cv.size() % 2 == 1))
1114 return -dynallocate<ncmul>(std::move(ev));
1115 else
1116 return dynallocate<ncmul>(std::move(ev));
1117 } else if (is_a<clifford>(e1) && is_a<cliffordunit>(e1.op(0))) {
1118 if (do_bar)
1119 return -e;
1120 else
1121 return e;
1122 } else if (is_a<power>(e1)) {
1123 // apply the procedure to the base of a power
1124 return pow(clifford_star_bar(e1.op(0), do_bar, 0), e1.op(1));
1125 } else if (is_a<add>(e1) || is_a<mul>(e1) || e.info(info_flags::list)) {
1126 // recurse into subexpressions
1127 return e1.map(fcn);
1128 } else // nothing meaningful can be done
1129 return e;
1130}
1131
1132ex clifford_prime(const ex & e)
1133{
1134 pointer_to_map_function fcn(clifford_prime);
1135 if (is_a<clifford>(e) && is_a<cliffordunit>(e.op(0))) {
1136 return -e;
1137 } else if (is_a<add>(e) || is_a<ncmul>(e) || is_a<mul>(e) //|| is_a<pseries>(e) || is_a<integral>(e)
1138 || is_a<matrix>(e) || e.info(info_flags::list)) {
1139 return e.map(fcn);
1140 } else if (is_a<power>(e)) {
1141 return pow(clifford_prime(e.op(0)), e.op(1));
1142 } else
1143 return e;
1144}
1145
1146ex remove_dirac_ONE(const ex & e, unsigned char rl, unsigned options)
1147{
1148 pointer_to_map_function_2args<unsigned char, unsigned> fcn(remove_dirac_ONE, rl, options | 1);
1149 bool need_reevaluation = false;
1150 ex e1 = e;
1151 if (! (options & 1) ) { // is not a child
1152 if (options & 2)
1153 e1 = expand_dummy_sum(e, true);
1154 e1 = canonicalize_clifford(e1);
1155 }
1156
1157 if (is_a<clifford>(e1) && ex_to<clifford>(e1).get_representation_label() >= rl) {
1158 if (is_a<diracone>(e1.op(0)))
1159 return 1;
1160 else
1161 throw(std::invalid_argument("remove_dirac_ONE(): expression is a non-scalar Clifford number!"));
1162 } else if (is_a<add>(e1) || is_a<ncmul>(e1) || is_a<mul>(e1)
1163 || is_a<matrix>(e1) || e1.info(info_flags::list)) {
1164 if (options & 3) // is a child or was already expanded
1165 return e1.map(fcn);
1166 else
1167 try {
1168 return e1.map(fcn);
1169 } catch (std::exception &p) {
1170 need_reevaluation = true;
1171 }
1172 } else if (is_a<power>(e1)) {
1173 if (options & 3) // is a child or was already expanded
1174 return pow(remove_dirac_ONE(e1.op(0), rl, options | 1), e1.op(1));
1175 else
1176 try {
1177 return pow(remove_dirac_ONE(e1.op(0), rl, options | 1), e1.op(1));
1178 } catch (std::exception &p) {
1179 need_reevaluation = true;
1180 }
1181 }
1182 if (need_reevaluation)
1183 return remove_dirac_ONE(e, rl, options | 2);
1184 return e1;
1185}
1186
1187int clifford_max_label(const ex & e, bool ignore_ONE)
1188{
1189 if (is_a<clifford>(e))
1190 if (ignore_ONE && is_a<diracone>(e.op(0)))
1191 return -1;
1192 else
1193 return ex_to<clifford>(e).get_representation_label();
1194 else {
1195 int rl = -1;
1196 for (size_t i=0; i < e.nops(); i++)
1197 rl = (rl > clifford_max_label(e.op(i), ignore_ONE)) ? rl : clifford_max_label(e.op(i), ignore_ONE);
1198 return rl;
1199 }
1200}
1201
1202ex clifford_norm(const ex & e)
1203{
1204 return sqrt(remove_dirac_ONE(e * clifford_bar(e)));
1205}
1206
1207ex clifford_inverse(const ex & e)
1208{
1209 ex norm = clifford_norm(e);
1210 if (!norm.is_zero())
1211 return clifford_bar(e) / pow(norm, 2);
1212 else
1213 throw(std::invalid_argument("clifford_inverse(): cannot find inverse of Clifford number with zero norm!"));
1214}
1215
1216ex lst_to_clifford(const ex & v, const ex & mu, const ex & metr, unsigned char rl)
1217{
1218 if (!ex_to<idx>(mu).is_dim_numeric())
1219 throw(std::invalid_argument("lst_to_clifford(): Index should have a numeric dimension"));
1220 ex e = clifford_unit(mu, metr, rl);
1221 return lst_to_clifford(v, e);
1222}
1223
1224ex lst_to_clifford(const ex & v, const ex & e) {
1225 unsigned min, max;
1226
1227 if (is_a<clifford>(e)) {
1228 ex mu = e.op(1);
1229 ex mu_toggle
1230 = is_a<varidx>(mu) ? ex_to<varidx>(mu).toggle_variance() : mu;
1231 unsigned dim = get_dim_uint(mu);
1232
1233 if (is_a<matrix>(v)) {
1234 if (ex_to<matrix>(v).cols() > ex_to<matrix>(v).rows()) {
1235 min = ex_to<matrix>(v).rows();
1236 max = ex_to<matrix>(v).cols();
1237 } else {
1238 min = ex_to<matrix>(v).cols();
1239 max = ex_to<matrix>(v).rows();
1240 }
1241 if (min == 1) {
1242 if (dim == max)
1243 return indexed(v, mu_toggle) * e;
1244 else if (max - dim == 1) {
1245 if (ex_to<matrix>(v).cols() > ex_to<matrix>(v).rows())
1246 return v.op(0) * dirac_ONE(ex_to<clifford>(e).get_representation_label()) + indexed(sub_matrix(ex_to<matrix>(v), 0, 1, 1, dim), mu_toggle) * e;
1247 else
1248 return v.op(0) * dirac_ONE(ex_to<clifford>(e).get_representation_label()) + indexed(sub_matrix(ex_to<matrix>(v), 1, dim, 0, 1), mu_toggle) * e;
1249 } else
1250 throw(std::invalid_argument("lst_to_clifford(): dimensions of vector and clifford unit mismatch"));
1251 } else
1252 throw(std::invalid_argument("lst_to_clifford(): first argument should be a vector (nx1 or 1xn matrix)"));
1253 } else if (v.info(info_flags::list)) {
1254 if (dim == ex_to<lst>(v).nops())
1255 return indexed(matrix(dim, 1, ex_to<lst>(v)), mu_toggle) * e;
1256 else if (ex_to<lst>(v).nops() - dim == 1)
1257 return v.op(0) * dirac_ONE(ex_to<clifford>(e).get_representation_label()) + indexed(sub_matrix(matrix(dim+1, 1, ex_to<lst>(v)), 1, dim, 0, 1), mu_toggle) * e;
1258 else
1259 throw(std::invalid_argument("lst_to_clifford(): list length and dimension of clifford unit mismatch"));
1260 } else
1261 throw(std::invalid_argument("lst_to_clifford(): cannot construct from anything but list or vector"));
1262 } else
1263 throw(std::invalid_argument("lst_to_clifford(): the second argument should be a Clifford unit"));
1264}
1265
1268static ex get_clifford_comp(const ex & e, const ex & c, bool root=true)
1269{
1270 // make expansion on the top-level call only
1271 ex e1=(root? e.expand() : e);
1272
1273 pointer_to_map_function_2args<const ex &, bool> fcn(get_clifford_comp, c, false);
1274 int ival = ex_to<numeric>(ex_to<idx>(c.op(1)).get_value()).to_int();
1275 int rl=ex_to<clifford>(c).get_representation_label();
1276
1277 if ( (is_a<add>(e1) || e1.info(info_flags::list) || is_a<matrix>(e1))) {
1278 return e1.map(fcn);
1279 } else if (is_a<ncmul>(e1) || is_a<mul>(e1)) {
1280 // searches are done within products only
1281 exvector ev, all_dummy=get_all_dummy_indices(e1);
1282 bool found=false, same_value_found=false;
1283 ex dummy_ind=0;
1284 ev.reserve(e1.nops());
1285 for (size_t i=0; i < e1.nops(); ++i) {
1286 // look for a Clifford unit with the same metric and representation label,
1287 // if found remember its index
1288 if (is_a<clifford>(e1.op(i)) && ex_to<clifford>(e1.op(i)).get_representation_label() == rl
1289 && is_a<cliffordunit>(e1.op(i).op(0)) && ex_to<clifford>(e1.op(i)).same_metric(c)) { // same Clifford unit
1290 if (found)
1291 throw(std::invalid_argument("get_clifford_comp(): expression is a Clifford multi-vector"));
1292 found=true;
1293 if (ex_to<idx>(e1.op(i).op(1)).is_numeric() &&
1294 (ival == ex_to<numeric>(ex_to<idx>(e1.op(i).op(1)).get_value()).to_int())) {
1295 same_value_found = true; // desired index value is found
1296 } else if ((std::find(all_dummy.begin(), all_dummy.end(), e1.op(i).op(1)) != all_dummy.end())
1297 || (is_a<varidx>(e1.op(i).op(1))
1298 && std::find(all_dummy.begin(), all_dummy.end(),
1299 ex_to<varidx>(e1.op(i).op(1)).toggle_variance()) != all_dummy.end())) {
1300 dummy_ind=(e1.op(i).op(1)); // suitable dummy index found
1301 } else
1302 ev.push_back(e.op(i)); // another index value
1303 } else
1304 ev.push_back(e1.op(i));
1305 }
1306
1307 if (! found) // no Clifford units found at all
1308 throw(std::invalid_argument("get_clifford_comp(): expression is not a Clifford vector to the given units"));
1309
1310 ex res=dynallocate<ncmul>(std::move(ev));
1311 if (same_value_found) {
1312 return res;
1313 } else if (! dummy_ind.is_zero()) { // a dummy index was found
1314 if (is_a<varidx>(dummy_ind))
1315 dummy_ind = ex_to<varidx>(dummy_ind).toggle_variance();
1316 return res.subs(dummy_ind==ival, subs_options::no_pattern);
1317 } else // found a Clifford unit with another index
1318 return 0;
1319 } else if (e1.is_zero()) {
1320 return 0;
1321 } else if (is_a<clifford>(e1) && is_a<cliffordunit>(e1.op(0)) && ex_to<clifford>(e1).same_metric(c)) {
1322 if (ex_to<idx>(e1.op(1)).is_numeric() &&
1323 (ival == ex_to<numeric>(ex_to<idx>(e1.op(1)).get_value()).to_int()) )
1324 return 1;
1325 else
1326 return 0;
1327 } else
1328 throw(std::invalid_argument("get_clifford_comp(): expression is not usable as a Clifford vector"));
1329}
1330
1331lst clifford_to_lst(const ex & e, const ex & c, bool algebraic)
1332{
1333 GINAC_ASSERT(is_a<clifford>(c));
1334 ex mu = c.op(1);
1335 if (! ex_to<idx>(mu).is_dim_numeric())
1336 throw(std::invalid_argument("clifford_to_lst(): index should have a numeric dimension"));
1337 unsigned int D = ex_to<numeric>(ex_to<idx>(mu).get_dim()).to_int();
1338
1339 if (algebraic) // check if algebraic method is applicable
1340 for (unsigned int i = 0; i < D; i++)
1341 if (pow(c.subs(mu == i, subs_options::no_pattern), 2).is_zero()
1342 || (! is_a<numeric>(pow(c.subs(mu == i, subs_options::no_pattern), 2))))
1343 algebraic = false;
1344 lst V;
1345 ex v0 = remove_dirac_ONE(canonicalize_clifford(e+clifford_prime(e)))/2;
1346 if (! v0.is_zero())
1347 V.append(v0);
1348 ex e1 = canonicalize_clifford(e - v0 * dirac_ONE(ex_to<clifford>(c).get_representation_label()));
1349 if (algebraic) {
1350 for (unsigned int i = 0; i < D; i++)
1351 V.append(remove_dirac_ONE(
1352 simplify_indexed(canonicalize_clifford(e1 * c.subs(mu == i, subs_options::no_pattern) + c.subs(mu == i, subs_options::no_pattern) * e1))
1353 / (2*pow(c.subs(mu == i, subs_options::no_pattern), 2))));
1354 } else {
1355 try {
1356 for (unsigned int i = 0; i < D; i++)
1357 V.append(get_clifford_comp(e1, c.subs(c.op(1) == i, subs_options::no_pattern)));
1358 } catch (std::exception &p) {
1359 /* Try to expand dummy summations to simplify the expression*/
1360 e1 = canonicalize_clifford(expand_dummy_sum(e, true));
1361 V.remove_all();
1362 v0 = remove_dirac_ONE(canonicalize_clifford(e1+clifford_prime(e1)))/2;
1363 if (! v0.is_zero()) {
1364 V.append(v0);
1365 e1 = canonicalize_clifford(e1 - v0 * dirac_ONE(ex_to<clifford>(c).get_representation_label()));
1366 }
1367 for (unsigned int i = 0; i < D; i++)
1368 V.append(get_clifford_comp(e1, c.subs(c.op(1) == i, subs_options::no_pattern)));
1369 }
1370 }
1371 return V;
1372}
1373
1374
1375ex clifford_moebius_map(const ex & a, const ex & b, const ex & c, const ex & d, const ex & v, const ex & G, unsigned char rl)
1376{
1377 ex x, D, cu;
1378
1379 if (! is_a<matrix>(v) && ! v.info(info_flags::list))
1380 throw(std::invalid_argument("clifford_moebius_map(): parameter v should be either vector or list"));
1381
1382 if (is_a<clifford>(G)) {
1383 cu = G;
1384 } else {
1385 if (is_a<indexed>(G)) {
1386 D = ex_to<idx>(G.op(1)).get_dim();
1387 varidx mu(dynallocate<symbol>(), D);
1388 cu = clifford_unit(mu, G, rl);
1389 } else if (is_a<matrix>(G)) {
1390 D = ex_to<matrix>(G).rows();
1391 idx mu(dynallocate<symbol>(), D);
1392 cu = clifford_unit(mu, G, rl);
1393 } else throw(std::invalid_argument("clifford_moebius_map(): metric should be an indexed object, matrix, or a Clifford unit"));
1394
1395 }
1396
1397 x = lst_to_clifford(v, cu);
1398 ex e = clifford_to_lst(simplify_indexed(canonicalize_clifford((a * x + b) * clifford_inverse(c * x + d))), cu, false);
1399 return (is_a<matrix>(v) ? matrix(ex_to<matrix>(v).rows(), ex_to<matrix>(v).cols(), ex_to<lst>(e)) : e);
1400}
1401
1402ex clifford_moebius_map(const ex & M, const ex & v, const ex & G, unsigned char rl)
1403{
1404 if (is_a<matrix>(M) && (ex_to<matrix>(M).rows() == 2) && (ex_to<matrix>(M).cols() == 2))
1405 return clifford_moebius_map(M.op(0), M.op(1), M.op(2), M.op(3), v, G, rl);
1406 else
1407 throw(std::invalid_argument("clifford_moebius_map(): parameter M should be a 2x2 matrix"));
1408}
1409
1410} // namespace GiNaC
Interface to GiNaC's sums of expressions.
Archiving of GiNaC expressions.
#define GINAC_ASSERT(X)
Assertion macro for checking invariances.
Definition assertion.h:33
This class stores all properties needed to record/retrieve the state of one object of class basic (or...
Definition archive.h:49
This class is the ABC (abstract base class) of GiNaC's class hierarchy.
Definition basic.h:105
unsigned hashvalue
hash value
Definition basic.h:303
void ensure_if_modifiable() const
Ensure the object may be modified without hurting others, throws if this is not the case.
Definition basic.cpp:894
unsigned flags
of type status_flags
Definition basic.h:302
virtual int compare_same_type(const basic &other) const
Returns order relation between two objects of same type.
Definition basic.cpp:719
This class holds an object representing an element of the Clifford algebra (the Dirac gamma matrices)...
Definition clifford.h:41
ex get_metric() const
Definition clifford.h:67
ex metric
Metric of the space, all constructors make it an indexed object.
Definition clifford.h:85
bool match_same_type(const basic &other) const override
Returns true if the attributes of two objects are similar enough for a match.
Definition clifford.cpp:248
bool same_metric(const ex &other) const
Definition clifford.cpp:178
void do_print_tree(const print_tree &c, unsigned level) const
Definition clifford.cpp:299
void do_print_dflt(const print_dflt &c, unsigned level) const
Definition clifford.cpp:263
size_t nops() const override
Number of operands/members.
Definition clifford.h:72
ex & let_op(size_t i) override
Return modifiable operand/member at position i.
Definition clifford.cpp:208
unsigned precedence() const override
Return relative operator precedence (for parenthezing output).
Definition clifford.h:54
void do_print_latex(const print_latex &c, unsigned level) const
Definition clifford.cpp:286
ex subs(const exmap &m, unsigned options=0) const override
Substitute a set of objects by arbitrary expressions.
Definition clifford.cpp:220
ex op(size_t i) const override
Return operand/member at position i.
Definition clifford.cpp:199
int commutator_sign
It is the sign in the definition e~i e~j +/- e~j e~i = B(i, j) + B(j, i)
Definition clifford.h:86
return_type_t return_type_tinfo() const override
Definition clifford.cpp:118
unsigned char representation_label
Representation label to distinguish independent spin lines.
Definition clifford.h:84
clifford(const ex &b, unsigned char rl=0)
Construct object without any indices.
Definition clifford.cpp:97
void read_archive(const archive_node &n, lst &sym_lst) override
Load (deserialize) the object from an archive node.
Definition clifford.cpp:127
void archive(archive_node &n) const override
Save (serialize) the object into archive node.
Definition clifford.cpp:138
int get_commutator_sign() const
Definition clifford.h:70
This class represents the Clifford algebra generators (units).
Definition clifford.h:105
Wrapper template for making GiNaC classes out of STL containers.
Definition container.h:73
This class represents the Dirac gamma5 object which anticommutates with all other gammas.
Definition clifford.h:140
This class represents the Dirac gammaL object which behaves like 1/2 (1-gamma5).
Definition clifford.h:157
This class represents the Dirac gammaL object which behaves like 1/2 (1+gamma5).
Definition clifford.h:174
This class represents the Dirac gamma Lorentz vector.
Definition clifford.h:122
This class represents the Clifford algebra unity element.
Definition clifford.h:92
Lightweight wrapper for GiNaC's symbolic objects.
Definition ex.h:73
exvector get_free_indices() const
Definition ex.h:207
bool is_equal(const ex &other) const
Definition ex.h:346
ex subs(const exmap &m, unsigned options=0) const
Definition ex.h:842
bool is_zero() const
Definition ex.h:214
void print(const print_context &c, unsigned level=0) const
Print expression to stream.
Definition ex.cpp:55
ex op(size_t i) const
Definition ex.h:137
This class holds one index of an indexed object.
Definition idx.h:36
This class holds an indexed expression.
Definition indexed.h:40
friend ex simplify_indexed(const ex &e, exvector &free_indices, exvector &dummy_indices, const scalar_products &sp)
Simplify indexed expression, return list of free indices.
Definition indexed.cpp:1045
void printindices(const print_context &c, unsigned level) const
Definition indexed.cpp:166
ex get_symmetry() const
Return symmetry properties.
Definition indexed.h:180
ex symtree
Index symmetry (tree of symmetry objects)
Definition indexed.h:192
Symbolic matrices.
Definition matrix.h:38
This class is a wrapper around CLN-numbers within the GiNaC class hierarchy.
Definition numeric.h:82
Base class for print_contexts.
Definition print.h:102
Context for default (ginsh-parsable) output.
Definition print.h:114
Context for latex-parsable output.
Definition print.h:122
Context for tree-like output for debugging.
Definition print.h:146
@ no_pattern
disable pattern matching
Definition flags.h:51
This class holds one of GiNaC's predefined special tensors such as the delta and the metric tensors.
Definition tensor.h:35
This class holds an index with a variance (co- or contravariant).
Definition idx.h:112
Interface to GiNaC's clifford algebra (Dirac gamma) objects.
Interface to GiNaC's light-weight expression handles.
ex unit
Definition factor.cpp:2191
unsigned options
Definition factor.cpp:2474
size_t n
Definition factor.cpp:1432
size_t c
Definition factor.cpp:757
mvec m
Definition factor.cpp:758
Interface to GiNaC's indices.
Definition of GiNaC's lst.
Interface to symbolic matrices.
Interface to GiNaC's products of expressions.
Definition add.cpp:36
static bool is_dirac_slash(const ex &seq0)
Definition clifford.cpp:256
ex remove_dirac_ONE(const ex &e, unsigned char rl, unsigned options)
Replaces dirac_ONE's (with a representation_label no less than rl) in e with 1.
ex clifford_inverse(const ex &e)
Calculation of the inverse in the Clifford algebra.
const symmetry & not_symmetric()
Definition symmetry.cpp:349
ex clifford_unit(const ex &mu, const ex &metr, unsigned char rl)
Create a Clifford unit object.
Definition clifford.cpp:740
const ex _ex1_2
Definition utils.cpp:381
std::map< ex, ex, ex_is_less > exmap
Definition basic.h:50
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)
Calculations of Moebius transformations (conformal map) defined by a 2x2 Clifford matrix (a b\c d) in...
static unsigned get_dim_uint(const ex &e)
Definition clifford.cpp:729
matrix inverse(const matrix &m)
Definition matrix.h:150
bool are_ex_trivially_equal(const ex &e1, const ex &e2)
Compare two objects of class quickly without doing a deep tree traversal.
Definition ex.h:700
ex dirac_gamma(const ex &mu, unsigned char rl)
Create a Dirac gamma object.
Definition clifford.cpp:782
print_func< print_dflt >(&diracone::do_print). print_func< print_latex >(&diracone
Definition clifford.cpp:52
static ex get_clifford_comp(const ex &e, const ex &c, bool root=true)
Auxiliary structure to define a function for striping one Clifford unit from vectors.
const symmetry & symmetric2()
Definition symmetry.cpp:355
lst clifford_to_lst(const ex &e, const ex &c, bool algebraic)
An inverse function to lst_to_clifford().
bool find(const ex &thisex, const ex &pattern, exset &found)
Definition ex.h:746
GINAC_IMPLEMENT_REGISTERED_CLASS_OPT_T(lst, basic, print_func< print_context >(&lst::do_print). print_func< print_tree >(&lst::do_print_tree)) template<> bool lst GINAC_BIND_UNARCHIVER(lst)
Specialization of container::info() for lst.
Definition lst.cpp:42
ex lst_to_clifford(const ex &v, const ex &mu, const ex &metr, unsigned char rl)
List or vector conversion into the Clifford vector.
std::vector< ex > exvector
Definition basic.h:48
Definition ex.h:988
Interface to GiNaC's non-commutative products of expressions.
Makes the interface to the underlying bignum package available.
Interface to GiNaC's overloaded operators.
Interface to GiNaC's symbolic exponentiation (basis^exponent).
#define GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(classname, supername, options)
Macro for inclusion in the implementation of each registered class.
Definition registrar.h:184
Interface to relations between expressions.
To distinguish between different kinds of non-commutative objects.
Definition registrar.h:43
Interface to GiNaC's symbolic objects.
Interface to GiNaC's symmetry definitions.
Interface to several small and furry utilities needed within GiNaC but not of any interest to the use...
#define DEFAULT_PRINT_LATEX(classname, text, latex)
Definition utils.h:622
#define DEFAULT_CTOR(classname)
Definition utils.h:606
#define DEFAULT_COMPARE(classname)
Definition utils.h:609

This page is part of the GiNaC developer's reference. It was generated automatically by doxygen. For an introduction, see the tutorial.