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

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