* Indentation and alignment fixes.
[ginac.git] / ginac / clifford.cpp
1 /** @file clifford.cpp
2  *
3  *  Implementation of GiNaC's clifford algebra (Dirac gamma) objects. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2005 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 namespace GiNaC {
42
43 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(clifford, indexed,
44   print_func<print_dflt>(&clifford::do_print_dflt).
45   print_func<print_latex>(&clifford::do_print_latex))
46
47 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(diracone, tensor,
48   print_func<print_dflt>(&diracone::do_print).
49   print_func<print_latex>(&diracone::do_print_latex))
50
51 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(cliffordunit, tensor,
52   print_func<print_dflt>(&cliffordunit::do_print).
53   print_func<print_latex>(&cliffordunit::do_print_latex))
54
55 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(diracgamma, cliffordunit,
56   print_func<print_dflt>(&diracgamma::do_print).
57   print_func<print_latex>(&diracgamma::do_print_latex))
58
59 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(diracgamma5, tensor,
60   print_func<print_dflt>(&diracgamma5::do_print).
61   print_func<print_latex>(&diracgamma5::do_print_latex))
62
63 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(diracgammaL, tensor,
64   print_func<print_context>(&diracgammaL::do_print).
65   print_func<print_latex>(&diracgammaL::do_print_latex))
66
67 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(diracgammaR, tensor,
68   print_func<print_context>(&diracgammaR::do_print).
69   print_func<print_latex>(&diracgammaR::do_print_latex))
70
71 //////////
72 // default constructors
73 //////////
74
75 static ex default_metric()
76 {
77         static ex m = (new minkmetric)->setflag(status_flags::dynallocated);
78         return m;
79 }
80
81 clifford::clifford() : representation_label(0), metric(default_metric())
82 {
83         tinfo_key = TINFO_clifford;
84 }
85
86 DEFAULT_CTOR(diracone)
87 DEFAULT_CTOR(cliffordunit)
88 DEFAULT_CTOR(diracgamma)
89 DEFAULT_CTOR(diracgamma5)
90 DEFAULT_CTOR(diracgammaL)
91 DEFAULT_CTOR(diracgammaR)
92
93 //////////
94 // other constructors
95 //////////
96
97 /** Construct object without any indices. This constructor is for internal
98  *  use only. Use the dirac_ONE() function instead.
99  *  @see dirac_ONE */
100 clifford::clifford(const ex & b, unsigned char rl) : inherited(b), representation_label(rl), metric(0)
101 {
102         tinfo_key = TINFO_clifford;
103 }
104
105 /** Construct object with one Lorentz index. This constructor is for internal
106  *  use only. Use the clifford_unit() or dirac_gamma() functions instead.
107  *  @see clifford_unit
108  *  @see dirac_gamma */
109 clifford::clifford(const ex & b, const ex & mu, const ex & metr, unsigned char rl) : inherited(b, mu), representation_label(rl), metric(metr)
110 {
111         GINAC_ASSERT(is_a<varidx>(mu));
112         tinfo_key = TINFO_clifford;
113 }
114
115 clifford::clifford(unsigned char rl, const ex & metr, const exvector & v, bool discardable) : inherited(not_symmetric(), v, discardable), representation_label(rl), metric(metr)
116 {
117         tinfo_key = TINFO_clifford;
118 }
119
120 clifford::clifford(unsigned char rl, const ex & metr, std::auto_ptr<exvector> vp) : inherited(not_symmetric(), vp), representation_label(rl), metric(metr)
121 {
122         tinfo_key = TINFO_clifford;
123 }
124
125 //////////
126 // archiving
127 //////////
128
129 clifford::clifford(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
130 {
131         unsigned rl;
132         n.find_unsigned("label", rl);
133         representation_label = rl;
134         n.find_ex("metric", metric, sym_lst);
135 }
136
137 void clifford::archive(archive_node &n) const
138 {
139         inherited::archive(n);
140         n.add_unsigned("label", representation_label);
141         n.add_ex("metric", metric);
142 }
143
144 DEFAULT_UNARCHIVE(clifford)
145 DEFAULT_ARCHIVING(diracone)
146 DEFAULT_ARCHIVING(cliffordunit)
147 DEFAULT_ARCHIVING(diracgamma)
148 DEFAULT_ARCHIVING(diracgamma5)
149 DEFAULT_ARCHIVING(diracgammaL)
150 DEFAULT_ARCHIVING(diracgammaR)
151
152 //////////
153 // functions overriding virtual functions from base classes
154 //////////
155
156 ex clifford::get_metric(const ex & i, const ex & j) const
157 {
158         return indexed(metric, i, j);
159 }
160
161 bool clifford::same_metric(const ex & other) const
162 {
163         if (is_a<clifford>(other)) {
164                 return get_metric().is_equal(ex_to<clifford>(other).get_metric());
165         } else if (is_a<indexed>(other)) {
166                 return get_metric(other.op(1), other.op(2)).is_equal(other);
167         } else
168                 return false;
169 }
170
171 int clifford::compare_same_type(const basic & other) const
172 {
173         GINAC_ASSERT(is_a<clifford>(other));
174         const clifford &o = static_cast<const clifford &>(other);
175
176         if (representation_label != o.representation_label) {
177                 // different representation label
178                 return representation_label < o.representation_label ? -1 : 1;
179         }
180
181         return inherited::compare_same_type(other);
182 }
183
184 bool clifford::match_same_type(const basic & other) const
185 {
186         GINAC_ASSERT(is_a<clifford>(other));
187         const clifford &o = static_cast<const clifford &>(other);
188
189         return (representation_label == o.representation_label) && same_metric(o);
190 }
191
192 static bool is_dirac_slash(const ex & seq0)
193 {
194         return !is_a<diracgamma5>(seq0) && !is_a<diracgammaL>(seq0) &&
195                !is_a<diracgammaR>(seq0) && !is_a<cliffordunit>(seq0) &&
196                !is_a<diracone>(seq0);
197 }
198
199 void clifford::do_print_dflt(const print_dflt & c, unsigned level) const
200 {
201         // dirac_slash() object is printed differently
202         if (is_dirac_slash(seq[0])) {
203                 seq[0].print(c, level);
204                 c.s << "\\";
205         } else
206                 this->print_dispatch<inherited>(c, level);
207 }
208
209 void clifford::do_print_latex(const print_latex & c, unsigned level) const
210 {
211         // dirac_slash() object is printed differently
212         if (is_dirac_slash(seq[0])) {
213                 c.s << "{";
214                 seq[0].print(c, level);
215                 c.s << "\\hspace{-1.0ex}/}";
216         } else {
217                 c.s << "\\clifford[" << int(representation_label) << "]";
218                 this->print_dispatch<inherited>(c, level);
219         }
220 }
221
222 DEFAULT_COMPARE(diracone)
223 DEFAULT_COMPARE(cliffordunit)
224 DEFAULT_COMPARE(diracgamma)
225 DEFAULT_COMPARE(diracgamma5)
226 DEFAULT_COMPARE(diracgammaL)
227 DEFAULT_COMPARE(diracgammaR)
228
229 DEFAULT_PRINT_LATEX(diracone, "ONE", "\\mathbf{1}")
230 DEFAULT_PRINT_LATEX(cliffordunit, "e", "e")
231 DEFAULT_PRINT_LATEX(diracgamma, "gamma", "\\gamma")
232 DEFAULT_PRINT_LATEX(diracgamma5, "gamma5", "{\\gamma^5}")
233 DEFAULT_PRINT_LATEX(diracgammaL, "gammaL", "{\\gamma_L}")
234 DEFAULT_PRINT_LATEX(diracgammaR, "gammaR", "{\\gamma_R}")
235
236 /** This function decomposes gamma~mu -> (1, mu) and a\ -> (a.ix, ix) */
237 static void base_and_index(const ex & c, ex & b, ex & i)
238 {
239         GINAC_ASSERT(is_a<clifford>(c));
240         GINAC_ASSERT(c.nops() == 2);
241
242         if (is_a<cliffordunit>(c.op(0))) { // proper dirac gamma object or clifford unit
243                 i = c.op(1);
244                 b = _ex1;
245         } else if (is_a<diracgamma5>(c.op(0)) || is_a<diracgammaL>(c.op(0)) || is_a<diracgammaR>(c.op(0))) { // gamma5/L/R
246                 i = _ex0;
247                 b = _ex1;
248         } else { // slash object, generate new dummy index
249                 varidx ix((new symbol)->setflag(status_flags::dynallocated), ex_to<idx>(c.op(1)).get_dim());
250                 b = indexed(c.op(0), ix.toggle_variance());
251                 i = ix;
252         }
253 }
254
255 /** Predicate for finding non-clifford objects. */
256 struct is_not_a_clifford : public std::unary_function<ex, bool> {
257         bool operator()(const ex & e)
258         {
259                 return !is_a<clifford>(e);
260         }
261 };
262
263 /** Contraction of a gamma matrix with something else. */
264 bool diracgamma::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
265 {
266         GINAC_ASSERT(is_a<clifford>(*self));
267         GINAC_ASSERT(is_a<indexed>(*other));
268         GINAC_ASSERT(is_a<diracgamma>(self->op(0)));
269         unsigned char rl = ex_to<clifford>(*self).get_representation_label();
270
271         ex dim = ex_to<idx>(self->op(1)).get_dim();
272         if (other->nops() > 1)
273                 dim = minimal_dim(dim, ex_to<idx>(other->op(1)).get_dim());
274
275         if (is_a<clifford>(*other)) {
276
277                 // Contraction only makes sense if the represenation labels are equal
278                 if (ex_to<clifford>(*other).get_representation_label() != rl)
279                         return false;
280
281                 size_t num = other - self;
282
283                 // gamma~mu gamma.mu = dim ONE
284                 if (num == 1) {
285                         *self = dim;
286                         *other = dirac_ONE(rl);
287                         return true;
288
289                 // gamma~mu gamma~alpha gamma.mu = (2-dim) gamma~alpha
290                 } else if (num == 2
291                         && is_a<clifford>(self[1])) {
292                         *self = 2 - dim;
293                         *other = _ex1;
294                         return true;
295
296                 // gamma~mu gamma~alpha gamma~beta gamma.mu = 4 g~alpha~beta + (dim-4) gamam~alpha gamma~beta
297                 } else if (num == 3
298                         && is_a<clifford>(self[1])
299                         && is_a<clifford>(self[2])) {
300                         ex b1, i1, b2, i2;
301                         base_and_index(self[1], b1, i1);
302                         base_and_index(self[2], b2, i2);
303                         *self = 4 * lorentz_g(i1, i2) * b1 * b2 * dirac_ONE(rl) + (dim - 4) * self[1] * self[2];
304                         self[1] = _ex1;
305                         self[2] = _ex1;
306                         *other = _ex1;
307                         return true;
308
309                 // gamma~mu gamma~alpha gamma~beta gamma~delta gamma.mu = -2 gamma~delta gamma~beta gamma~alpha - (dim-4) gamam~alpha gamma~beta gamma~delta
310                 } else if (num == 4
311                         && is_a<clifford>(self[1])
312                         && is_a<clifford>(self[2])
313                         && is_a<clifford>(self[3])) {
314                         *self = -2 * self[3] * self[2] * self[1] - (dim - 4) * self[1] * self[2] * self[3];
315                         self[1] = _ex1;
316                         self[2] = _ex1;
317                         self[3] = _ex1;
318                         *other = _ex1;
319                         return true;
320
321                 // gamma~mu Sodd gamma.mu = -2 Sodd_R
322                 // (Chisholm identity in 4 dimensions)
323                 } else if (!((other - self) & 1) && dim.is_equal(4)) {
324                         if (std::find_if(self + 1, other, is_not_a_clifford()) != other)
325                                 return false;
326
327                         *self = ncmul(exvector(std::reverse_iterator<exvector::const_iterator>(other), std::reverse_iterator<exvector::const_iterator>(self + 1)), true);
328                         std::fill(self + 1, other, _ex1);
329                         *other = _ex_2;
330                         return true;
331
332                 // gamma~mu Sodd gamma~alpha gamma.mu = 2 gamma~alpha Sodd + 2 Sodd_R gamma~alpha
333                 // (commutate contracted indices towards each other, then use
334                 // Chisholm identity in 4 dimensions)
335                 } else if (((other - self) & 1) && dim.is_equal(4)) {
336                         if (std::find_if(self + 1, other, is_not_a_clifford()) != other)
337                                 return false;
338
339                         exvector::iterator next_to_last = other - 1;
340                         ex S = ncmul(exvector(self + 1, next_to_last), true);
341                         ex SR = ncmul(exvector(std::reverse_iterator<exvector::const_iterator>(next_to_last), std::reverse_iterator<exvector::const_iterator>(self + 1)), true);
342
343                         *self = (*next_to_last) * S + SR * (*next_to_last);
344                         std::fill(self + 1, other, _ex1);
345                         *other = _ex2;
346                         return true;
347
348                 // gamma~mu S gamma~alpha gamma.mu = 2 gamma~alpha S - gamma~mu S gamma.mu gamma~alpha
349                 // (commutate contracted indices towards each other, simplify_indexed()
350                 // will re-expand and re-run the simplification)
351                 } else {
352                         if (std::find_if(self + 1, other, is_not_a_clifford()) != other)
353                                 return false;
354
355                         exvector::iterator next_to_last = other - 1;
356                         ex S = ncmul(exvector(self + 1, next_to_last), true);
357
358                         *self = 2 * (*next_to_last) * S - (*self) * S * (*other) * (*next_to_last);
359                         std::fill(self + 1, other + 1, _ex1);
360                         return true;
361                 }
362
363         } else if (is_a<symbol>(other->op(0)) && other->nops() == 2) {
364
365                 // x.mu gamma~mu -> x-slash
366                 *self = dirac_slash(other->op(0), dim, rl);
367                 *other = _ex1;
368                 return true;
369         }
370
371         return false;
372 }
373
374 /** An utility function looking for a given metric within an exvector,
375  *  used in cliffordunit::contract_with(). */
376 static int find_same_metric(exvector & v, ex & c)
377 {
378         for (int i=0; i<v.size();i++) {
379                 if (!is_a<clifford>(v[i]) && is_a<indexed>(v[i])
380                     && ex_to<clifford>(c).same_metric(v[i]) 
381                     && (ex_to<varidx>(c.op(1)) == ex_to<indexed>(v[i]).get_indices()[0]
382                         || ex_to<varidx>(c.op(1)).toggle_variance() == ex_to<indexed>(v[i]).get_indices()[0])) {
383                         return ++i; // next to found
384                 }
385         }
386         return 0; //nothing found
387 }
388
389 /** Contraction of a Clifford unit with something else. */
390 bool cliffordunit::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
391 {
392         GINAC_ASSERT(is_a<clifford>(*self));
393         GINAC_ASSERT(is_a<indexed>(*other));
394         GINAC_ASSERT(is_a<cliffordunit>(self->op(0)));
395         clifford unit = ex_to<clifford>(*self);
396         unsigned char rl = unit.get_representation_label();
397
398         if (is_a<clifford>(*other)) {
399                 // Contraction only makes sense if the represenation labels are equal
400                 // and the metrics are the same
401                 if ((ex_to<clifford>(*other).get_representation_label() != rl) 
402                     && unit.same_metric(*other))
403                         return false;
404
405                 // Find if a previous contraction produces the square of self
406                 int prev_square = find_same_metric(v, self[0]);
407                 varidx d((new symbol)->setflag(status_flags::dynallocated), ex_to<idx>(self->op(1)).get_dim());
408                 ex squared_metric = unit.get_metric(self->op(1), d) * unit.get_metric(d.toggle_variance(), other->op(1));
409
410                 // e~mu e.mu = Tr ONE
411                 if (other - self == 1) {
412                         if (prev_square != 0) {
413                                 *self = squared_metric;
414                                 v[prev_square-1] = _ex1;
415                         } else
416                                 *self = unit.get_metric(self->op(1), other->op(1));
417                         *other = dirac_ONE(rl);
418                         return true;
419
420                 // e~mu e~alpha e.mu = (2e~alpha^2-Tr) e~alpha
421                 } else if (other - self == 2
422                         && is_a<clifford>(self[1])) {
423
424                         const ex & ia = self[1].op(1);
425                         const ex & ib = self[1].op(1);
426                         if (is_a<tensmetric>(unit.get_metric()))
427                                 *self = 2 - unit.get_metric(self->op(1), other->op(1));
428                         else if (prev_square != 0) {
429                                 *self = 2-squared_metric;
430                                 v[prev_square-1] = _ex1;
431                         } else 
432                                 *self = 2*unit.get_metric(ia, ib) - unit.get_metric(self->op(1), other->op(1));
433                         *other = _ex1;
434                         return true;
435
436                 // e~mu S e~alpha e.mu = 2 e~alpha^3 S - e~mu S e.mu e~alpha
437                 // (commutate contracted indices towards each other, simplify_indexed()
438                 // will re-expand and re-run the simplification)
439                 } else {
440                         exvector::iterator it = self + 1, next_to_last = other - 1;
441                         while (it != other) {
442                                 if (!is_a<clifford>(*it))
443                                         return false;
444                                 ++it;
445                         }
446
447                         it = self + 1;
448                         ex S = _ex1;
449                         while (it != next_to_last) {
450                                 S *= *it;
451                                 *it++ = _ex1;
452                         }
453
454                         const ex & ia = next_to_last->op(1);
455                         const ex & ib = next_to_last->op(1);
456                         if (is_a<tensmetric>(unit.get_metric()))
457                                 *self = 2 * (*next_to_last) * S - (*self) * S * (*other) * (*next_to_last);
458                         else if (prev_square != 0) {
459                                 *self = 2 * (*next_to_last) * S  - (*self) * S * (*other) * (*next_to_last)*unit.get_metric(self->op(1),self->op(1));
460                                 v[prev_square-1] = _ex1;
461                         } else 
462                                 *self = 2 * (*next_to_last) * S* unit.get_metric(ia,ib) - (*self) * S * (*other) * (*next_to_last);
463                         *next_to_last = _ex1;
464                         *other = _ex1;
465                         return true;
466                 }
467
468         } 
469
470         return false;
471 }
472
473 /** Perform automatic simplification on noncommutative product of clifford
474  *  objects. This removes superfluous ONEs, permutes gamma5/L/R's to the front
475  *  and removes squares of gamma objects. */
476 ex clifford::eval_ncmul(const exvector & v) const
477 {
478         exvector s;
479         s.reserve(v.size());
480
481         // Remove superfluous ONEs
482         exvector::const_iterator cit = v.begin(), citend = v.end();
483         while (cit != citend) {
484                 if (!is_a<clifford>(*cit) || !is_a<diracone>(cit->op(0)))
485                         s.push_back(*cit);
486                 cit++;
487         }
488
489         bool something_changed = false;
490         int sign = 1;
491
492         // Anticommutate gamma5/L/R's to the front
493         if (s.size() >= 2) {
494                 exvector::iterator first = s.begin(), next_to_last = s.end() - 2;
495                 while (true) {
496                         exvector::iterator it = next_to_last;
497                         while (true) {
498                                 exvector::iterator it2 = it + 1;
499                                 if (is_a<clifford>(*it) && is_a<clifford>(*it2)) {
500                                         ex e1 = it->op(0), e2 = it2->op(0);
501
502                                         if (is_a<diracgamma5>(e2)) {
503
504                                                 if (is_a<diracgammaL>(e1) || is_a<diracgammaR>(e1)) {
505
506                                                         // gammaL/R gamma5 -> gamma5 gammaL/R
507                                                         it->swap(*it2);
508                                                         something_changed = true;
509
510                                                 } else if (!is_a<diracgamma5>(e1)) {
511
512                                                         // gamma5 gamma5 -> gamma5 gamma5 (do nothing)
513                                                         // x gamma5 -> -gamma5 x
514                                                         it->swap(*it2);
515                                                         sign = -sign;
516                                                         something_changed = true;
517                                                 }
518
519                                         } else if (is_a<diracgammaL>(e2)) {
520
521                                                 if (is_a<diracgammaR>(e1)) {
522
523                                                         // gammaR gammaL -> 0
524                                                         return _ex0;
525
526                                                 } else if (!is_a<diracgammaL>(e1) && !is_a<diracgamma5>(e1)) {
527
528                                                         // gammaL gammaL -> gammaL gammaL (do nothing)
529                                                         // gamma5 gammaL -> gamma5 gammaL (do nothing)
530                                                         // x gammaL -> gammaR x
531                                                         it->swap(*it2);
532                                                         *it = clifford(diracgammaR(), ex_to<clifford>(*it).get_representation_label());
533                                                         something_changed = true;
534                                                 }
535
536                                         } else if (is_a<diracgammaR>(e2)) {
537
538                                                 if (is_a<diracgammaL>(e1)) {
539
540                                                         // gammaL gammaR -> 0
541                                                         return _ex0;
542
543                                                 } else if (!is_a<diracgammaR>(e1) && !is_a<diracgamma5>(e1)) {
544
545                                                         // gammaR gammaR -> gammaR gammaR (do nothing)
546                                                         // gamma5 gammaR -> gamma5 gammaR (do nothing)
547                                                         // x gammaR -> gammaL x
548                                                         it->swap(*it2);
549                                                         *it = clifford(diracgammaL(), ex_to<clifford>(*it).get_representation_label());
550                                                         something_changed = true;
551                                                 }
552                                         }
553                                 }
554                                 if (it == first)
555                                         break;
556                                 --it;
557                         }
558                         if (next_to_last == first)
559                                 break;
560                         --next_to_last;
561                 }
562         }
563
564         // Remove equal adjacent gammas
565         if (s.size() >= 2) {
566                 exvector::iterator it, itend = s.end() - 1;
567                 for (it = s.begin(); it != itend; ++it) {
568                         ex & a = it[0];
569                         ex & b = it[1];
570                         if (!is_a<clifford>(a) || !is_a<clifford>(b))
571                                 continue;
572
573                         const ex & ag = a.op(0);
574                         const ex & bg = b.op(0);
575                         bool a_is_cliffordunit = is_a<cliffordunit>(ag);
576                         bool b_is_cliffordunit =  is_a<cliffordunit>(bg);
577
578                         if (a_is_cliffordunit && b_is_cliffordunit && ex_to<clifford>(a).same_metric(b)) {
579
580                                 const ex & ia = a.op(1);
581                                 const ex & ib = b.op(1);
582                                 if (ia.is_equal(ib)) { // gamma~alpha gamma~alpha -> g~alpha~alpha
583                                         a = ex_to<clifford>(a).get_metric(ia, ib);
584                                         b = dirac_ONE(representation_label);
585                                         something_changed = true;
586                                 }
587
588                         } else if ((is_a<diracgamma5>(ag) && is_a<diracgamma5>(bg))) {
589
590                                 // Remove squares of gamma5
591                                 a = dirac_ONE(representation_label);
592                                 b = dirac_ONE(representation_label);
593                                 something_changed = true;
594
595                         } else if ((is_a<diracgammaL>(ag) && is_a<diracgammaL>(bg))
596                                 || (is_a<diracgammaR>(ag) && is_a<diracgammaR>(bg))) {
597
598                                 // Remove squares of gammaL/R
599                                 b = dirac_ONE(representation_label);
600                                 something_changed = true;
601
602                         } else if (is_a<diracgammaL>(ag) && is_a<diracgammaR>(bg)) {
603
604                                 // gammaL and gammaR are orthogonal
605                                 return _ex0;
606
607                         } else if (is_a<diracgamma5>(ag) && is_a<diracgammaL>(bg)) {
608
609                                 // gamma5 gammaL -> -gammaL
610                                 a = dirac_ONE(representation_label);
611                                 sign = -sign;
612                                 something_changed = true;
613
614                         } else if (is_a<diracgamma5>(ag) && is_a<diracgammaR>(bg)) {
615
616                                 // gamma5 gammaR -> gammaR
617                                 a = dirac_ONE(representation_label);
618                                 something_changed = true;
619
620                         } else if (!a_is_cliffordunit && !b_is_cliffordunit && ag.is_equal(bg)) {
621
622                                 // a\ a\ -> a^2
623                                 varidx ix((new symbol)->setflag(status_flags::dynallocated), ex_to<idx>(a.op(1)).minimal_dim(ex_to<idx>(b.op(1))));
624                                 
625                                 a = indexed(ag, ix) * indexed(ag, ix.toggle_variance());
626                                 b = dirac_ONE(representation_label);
627                                 something_changed = true;
628                         }
629                 }
630         }
631
632         if (s.empty())
633                 return clifford(diracone(), representation_label) * sign;
634         if (something_changed)
635                 return reeval_ncmul(s) * sign;
636         else
637                 return hold_ncmul(s) * sign;
638 }
639
640 ex clifford::thiscontainer(const exvector & v) const
641 {
642         return clifford(representation_label, get_metric(), v);
643 }
644
645 ex clifford::thiscontainer(std::auto_ptr<exvector> vp) const
646 {
647         return clifford(representation_label, get_metric(), vp);
648 }
649
650 ex diracgamma5::conjugate() const
651 {       
652         return _ex_1 * (*this);
653 }
654
655 ex diracgammaL::conjugate() const
656 {
657         return (new diracgammaR)->setflag(status_flags::dynallocated);
658 }
659
660 ex diracgammaR::conjugate() const
661 {
662         return (new diracgammaL)->setflag(status_flags::dynallocated);
663 }
664
665 //////////
666 // global functions
667 //////////
668
669 ex dirac_ONE(unsigned char rl)
670 {
671         static ex ONE = (new diracone)->setflag(status_flags::dynallocated);
672         return clifford(ONE, rl);
673 }
674
675 ex clifford_unit(const ex & mu, const ex & metr, unsigned char rl)
676 {
677         static ex unit = (new cliffordunit)->setflag(status_flags::dynallocated);
678
679         if (!is_a<varidx>(mu))
680                 throw(std::invalid_argument("index of Clifford unit must be of type varidx"));
681
682         if (is_a<indexed>(metr))
683                 return clifford(unit, mu, metr.op(0), rl);
684         else if(is_a<tensmetric>(metr) || is_a<matrix>(metr)) 
685                 return clifford(unit, mu, metr, rl);
686         else
687                 throw(std::invalid_argument("metric for Clifford unit must be of type indexed, tensormetric or matrix"));
688 }
689
690 ex dirac_gamma(const ex & mu, unsigned char rl)
691 {
692         static ex gamma = (new diracgamma)->setflag(status_flags::dynallocated);
693
694         if (!is_a<varidx>(mu))
695                 throw(std::invalid_argument("index of Dirac gamma must be of type varidx"));
696
697         return clifford(gamma, mu, default_metric(), rl);
698 }
699
700 ex dirac_gamma5(unsigned char rl)
701 {
702         static ex gamma5 = (new diracgamma5)->setflag(status_flags::dynallocated);
703         return clifford(gamma5, rl);
704 }
705
706 ex dirac_gammaL(unsigned char rl)
707 {
708         static ex gammaL = (new diracgammaL)->setflag(status_flags::dynallocated);
709         return clifford(gammaL, rl);
710 }
711
712 ex dirac_gammaR(unsigned char rl)
713 {
714         static ex gammaR = (new diracgammaR)->setflag(status_flags::dynallocated);
715         return clifford(gammaR, rl);
716 }
717
718 ex dirac_slash(const ex & e, const ex & dim, unsigned char rl)
719 {
720         // Slashed vectors are actually stored as a clifford object with the
721         // vector as its base expression and a (dummy) index that just serves
722         // for storing the space dimensionality
723         return clifford(e, varidx(0, dim), default_metric(), rl);
724 }
725
726 /** Check whether a given tinfo key (as returned by return_type_tinfo()
727  *  is that of a clifford object with the specified representation label. */
728 static bool is_clifford_tinfo(unsigned ti, unsigned char rl)
729 {
730         return ti == (TINFO_clifford + rl);
731 }
732
733 /** Check whether a given tinfo key (as returned by return_type_tinfo()
734  *  is that of a clifford object (with an arbitrary representation label). */
735 static bool is_clifford_tinfo(unsigned ti)
736 {
737         return (ti & ~0xff) == TINFO_clifford;
738 }
739
740 /** Extract representation label from tinfo key (as returned by
741  *  return_type_tinfo()). */
742 static unsigned char get_representation_label(unsigned ti)
743 {
744         return ti & 0xff;
745 }
746
747 /** Take trace of a string of an even number of Dirac gammas given a vector
748  *  of indices. */
749 static ex trace_string(exvector::const_iterator ix, size_t num)
750 {
751         // Tr gamma.mu gamma.nu = 4 g.mu.nu
752         if (num == 2)
753                 return lorentz_g(ix[0], ix[1]);
754
755         // Tr gamma.mu gamma.nu gamma.rho gamma.sig = 4 (g.mu.nu g.rho.sig + g.nu.rho g.mu.sig - g.mu.rho g.nu.sig )
756         else if (num == 4)
757                 return lorentz_g(ix[0], ix[1]) * lorentz_g(ix[2], ix[3])
758                      + lorentz_g(ix[1], ix[2]) * lorentz_g(ix[0], ix[3])
759                      - lorentz_g(ix[0], ix[2]) * lorentz_g(ix[1], ix[3]);
760
761         // Traces of 6 or more gammas are computed recursively:
762         // Tr gamma.mu1 gamma.mu2 ... gamma.mun =
763         //   + g.mu1.mu2 * Tr gamma.mu3 ... gamma.mun
764         //   - g.mu1.mu3 * Tr gamma.mu2 gamma.mu4 ... gamma.mun
765         //   + g.mu1.mu4 * Tr gamma.mu3 gamma.mu3 gamma.mu5 ... gamma.mun
766         //   - ...
767         //   + g.mu1.mun * Tr gamma.mu2 ... gamma.mu(n-1)
768         exvector v(num - 2);
769         int sign = 1;
770         ex result;
771         for (size_t i=1; i<num; i++) {
772                 for (size_t n=1, j=0; n<num; n++) {
773                         if (n == i)
774                                 continue;
775                         v[j++] = ix[n];
776                 }
777                 result += sign * lorentz_g(ix[0], ix[i]) * trace_string(v.begin(), num-2);
778                 sign = -sign;
779         }
780         return result;
781 }
782
783 ex dirac_trace(const ex & e, const std::set<unsigned char> & rls, const ex & trONE)
784 {
785         if (is_a<clifford>(e)) {
786
787                 unsigned char rl = ex_to<clifford>(e).get_representation_label();
788
789                 // Are we taking the trace over this object's representation label?
790                 if (rls.find(rl) == rls.end())
791                         return e;
792
793                 // Yes, all elements are traceless, except for dirac_ONE and dirac_L/R
794                 const ex & g = e.op(0);
795                 if (is_a<diracone>(g))
796                         return trONE;
797                 else if (is_a<diracgammaL>(g) || is_a<diracgammaR>(g))
798                         return trONE/2;
799                 else
800                         return _ex0;
801
802         } else if (is_exactly_a<mul>(e)) {
803
804                 // Trace of product: pull out non-clifford factors
805                 ex prod = _ex1;
806                 for (size_t i=0; i<e.nops(); i++) {
807                         const ex &o = e.op(i);
808                         if (is_clifford_tinfo(o.return_type_tinfo()))
809                                 prod *= dirac_trace(o, rls, trONE);
810                         else
811                                 prod *= o;
812                 }
813                 return prod;
814
815         } else if (is_exactly_a<ncmul>(e)) {
816
817                 unsigned char rl = get_representation_label(e.return_type_tinfo());
818
819                 // Are we taking the trace over this string's representation label?
820                 if (rls.find(rl) == rls.end())
821                         return e;
822
823                 // Substitute gammaL/R and expand product, if necessary
824                 ex e_expanded = e.subs(lst(
825                         dirac_gammaL(rl) == (dirac_ONE(rl)-dirac_gamma5(rl))/2,
826                         dirac_gammaR(rl) == (dirac_ONE(rl)+dirac_gamma5(rl))/2
827                 ), subs_options::no_pattern).expand();
828                 if (!is_a<ncmul>(e_expanded))
829                         return dirac_trace(e_expanded, rls, trONE);
830
831                 // gamma5 gets moved to the front so this check is enough
832                 bool has_gamma5 = is_a<diracgamma5>(e.op(0).op(0));
833                 size_t num = e.nops();
834
835                 if (has_gamma5) {
836
837                         // Trace of gamma5 * odd number of gammas and trace of
838                         // gamma5 * gamma.mu * gamma.nu are zero
839                         if ((num & 1) == 0 || num == 3)
840                                 return _ex0;
841
842                         // Tr gamma5 gamma.mu gamma.nu gamma.rho gamma.sigma = 4I * epsilon(mu, nu, rho, sigma)
843                         // (the epsilon is always 4-dimensional)
844                         if (num == 5) {
845                                 ex b1, i1, b2, i2, b3, i3, b4, i4;
846                                 base_and_index(e.op(1), b1, i1);
847                                 base_and_index(e.op(2), b2, i2);
848                                 base_and_index(e.op(3), b3, i3);
849                                 base_and_index(e.op(4), b4, i4);
850                                 return trONE * I * (lorentz_eps(ex_to<idx>(i1).replace_dim(_ex4), ex_to<idx>(i2).replace_dim(_ex4), ex_to<idx>(i3).replace_dim(_ex4), ex_to<idx>(i4).replace_dim(_ex4)) * b1 * b2 * b3 * b4).simplify_indexed();
851                         }
852
853                         // Tr gamma5 S_2k =
854                         //   I/4! * epsilon0123.mu1.mu2.mu3.mu4 * Tr gamma.mu1 gamma.mu2 gamma.mu3 gamma.mu4 S_2k
855                         // (the epsilon is always 4-dimensional)
856                         exvector ix(num-1), bv(num-1);
857                         for (size_t i=1; i<num; i++)
858                                 base_and_index(e.op(i), bv[i-1], ix[i-1]);
859                         num--;
860                         int *iv = new int[num];
861                         ex result;
862                         for (size_t i=0; i<num-3; i++) {
863                                 ex idx1 = ix[i];
864                                 for (size_t j=i+1; j<num-2; j++) {
865                                         ex idx2 = ix[j];
866                                         for (size_t k=j+1; k<num-1; k++) {
867                                                 ex idx3 = ix[k];
868                                                 for (size_t l=k+1; l<num; l++) {
869                                                         ex idx4 = ix[l];
870                                                         iv[0] = i; iv[1] = j; iv[2] = k; iv[3] = l;
871                                                         exvector v;
872                                                         v.reserve(num - 4);
873                                                         for (size_t n=0, t=4; n<num; n++) {
874                                                                 if (n == i || n == j || n == k || n == l)
875                                                                         continue;
876                                                                 iv[t++] = n;
877                                                                 v.push_back(ix[n]);
878                                                         }
879                                                         int sign = permutation_sign(iv, iv + num);
880                                                         result += sign * lorentz_eps(ex_to<idx>(idx1).replace_dim(_ex4), ex_to<idx>(idx2).replace_dim(_ex4), ex_to<idx>(idx3).replace_dim(_ex4), ex_to<idx>(idx4).replace_dim(_ex4))
881                                                                 * trace_string(v.begin(), num - 4);
882                                                 }
883                                         }
884                                 }
885                         }
886                         delete[] iv;
887                         return trONE * I * result * mul(bv);
888
889                 } else { // no gamma5
890
891                         // Trace of odd number of gammas is zero
892                         if ((num & 1) == 1)
893                                 return _ex0;
894
895                         // Tr gamma.mu gamma.nu = 4 g.mu.nu
896                         if (num == 2) {
897                                 ex b1, i1, b2, i2;
898                                 base_and_index(e.op(0), b1, i1);
899                                 base_and_index(e.op(1), b2, i2);
900                                 return trONE * (lorentz_g(i1, i2) * b1 * b2).simplify_indexed();
901                         }
902
903                         exvector iv(num), bv(num);
904                         for (size_t i=0; i<num; i++)
905                                 base_and_index(e.op(i), bv[i], iv[i]);
906
907                         return trONE * (trace_string(iv.begin(), num) * mul(bv)).simplify_indexed();
908                 }
909
910         } else if (e.nops() > 0) {
911
912                 // Trace maps to all other container classes (this includes sums)
913                 pointer_to_map_function_2args<const std::set<unsigned char> &, const ex &> fcn(dirac_trace, rls, trONE);
914                 return e.map(fcn);
915
916         } else
917                 return _ex0;
918 }
919
920 ex dirac_trace(const ex & e, const lst & rll, const ex & trONE)
921 {
922         // Convert list to set
923         std::set<unsigned char> rls;
924         for (lst::const_iterator i = rll.begin(); i != rll.end(); ++i) {
925                 if (i->info(info_flags::nonnegint))
926                         rls.insert(ex_to<numeric>(*i).to_int());
927         }
928
929         return dirac_trace(e, rls, trONE);
930 }
931
932 ex dirac_trace(const ex & e, unsigned char rl, const ex & trONE)
933 {
934         // Convert label to set
935         std::set<unsigned char> rls;
936         rls.insert(rl);
937
938         return dirac_trace(e, rls, trONE);
939 }
940
941
942 ex canonicalize_clifford(const ex & e_)
943 {
944         pointer_to_map_function fcn(canonicalize_clifford);
945
946         if (is_a<matrix>(e_)    // || is_a<pseries>(e) || is_a<integral>(e)
947                 || is_a<lst>(e_)) {
948                 return e_.map(fcn);
949         } else {
950                 ex e=simplify_indexed(e_);
951                 // Scan for any ncmul objects
952                 exmap srl;
953                 ex aux = e.to_rational(srl);
954                 for (exmap::iterator i = srl.begin(); i != srl.end(); ++i) {
955
956                         ex lhs = i->first;
957                         ex rhs = i->second;
958
959                         if (is_exactly_a<ncmul>(rhs)
960                                         && rhs.return_type() == return_types::noncommutative
961                                         && is_clifford_tinfo(rhs.return_type_tinfo())) {
962
963                                 // Expand product, if necessary
964                                 ex rhs_expanded = rhs.expand();
965                                 if (!is_a<ncmul>(rhs_expanded)) {
966                                         i->second = canonicalize_clifford(rhs_expanded);
967                                         continue;
968
969                                 } else if (!is_a<clifford>(rhs.op(0)))
970                                         continue;
971
972                                 exvector v;
973                                 v.reserve(rhs.nops());
974                                 for (size_t j=0; j<rhs.nops(); j++)
975                                         v.push_back(rhs.op(j));
976
977                                 // Stupid recursive bubble sort because we only want to swap adjacent gammas
978                                 exvector::iterator it = v.begin(), next_to_last = v.end() - 1;
979                                 if (is_a<diracgamma5>(it->op(0)) || is_a<diracgammaL>(it->op(0)) || is_a<diracgammaR>(it->op(0)))
980                                         ++it;
981                                 while (it != next_to_last) {
982                                         if (it[0].compare(it[1]) > 0) {
983                                                 ex save0 = it[0], save1 = it[1];
984                                                 ex b1, i1, b2, i2;
985                                                 base_and_index(it[0], b1, i1);
986                                                 base_and_index(it[1], b2, i2);
987                                                 it[0] = (ex_to<clifford>(save0).get_metric(i1, i2) * b1 * b2).simplify_indexed();
988                                                 it[1] = v.size() == 2 ? _ex2 * dirac_ONE(ex_to<clifford>(it[1]).get_representation_label()) : _ex2;
989                                                 ex sum = ncmul(v);
990                                                 it[0] = save1;
991                                                 it[1] = save0;
992                                                 sum -= ncmul(v, true);
993                                                 i->second = canonicalize_clifford(sum);
994                                                 goto next_sym;
995                                         }
996                                         ++it;
997                                 }
998 next_sym:       ;
999                         }
1000                 }
1001                 return aux.subs(srl, subs_options::no_pattern).simplify_indexed();
1002         }
1003 }
1004
1005 ex clifford_prime(const ex & e)
1006 {
1007         pointer_to_map_function fcn(clifford_prime);
1008         if (is_a<clifford>(e) && is_a<cliffordunit>(e.op(0))) {
1009                 return -e;
1010         } else if (is_a<add>(e) || is_a<ncmul>(e) || is_a<mul>(e) //|| is_a<pseries>(e) || is_a<integral>(e)
1011                            || is_a<matrix>(e) || is_a<lst>(e)) {
1012                 return e.map(fcn);
1013         } else if (is_a<power>(e)) {
1014                 return pow(clifford_prime(e.op(0)), e.op(1));
1015         } else
1016                 return e;
1017 }
1018
1019 ex remove_dirac_ONE(const ex & e,  unsigned char rl)
1020 {
1021         pointer_to_map_function_1arg<unsigned char> fcn(remove_dirac_ONE, rl);
1022         if (is_a<clifford>(e) && ex_to<clifford>(e).get_representation_label() >= rl) {
1023                 if (is_a<diracone>(e.op(0)))
1024                         return 1;
1025                 else
1026                         throw(std::invalid_argument("Expression is a non-scalar Clifford number!"));
1027         } else if (is_a<add>(e) || is_a<ncmul>(e) || is_a<mul>(e)  // || is_a<pseries>(e) || is_a<integral>(e)
1028                            || is_a<matrix>(e) || is_a<lst>(e)) {
1029                 return e.map(fcn);
1030         } else if (is_a<power>(e)) {
1031                 return pow(remove_dirac_ONE(e.op(0)), e.op(1));
1032         } else
1033                 return e;
1034 }
1035
1036 ex clifford_norm(const ex & e)
1037 {
1038         return sqrt(remove_dirac_ONE(canonicalize_clifford(e * clifford_bar(e)).simplify_indexed()));
1039 }
1040
1041 ex clifford_inverse(const ex & e)
1042 {
1043         ex norm = clifford_norm(e);
1044         if (!norm.is_zero())
1045                 return clifford_bar(e) / pow(norm, 2);
1046         else 
1047                 throw(std::invalid_argument("Cannot find inverse of Clifford number with zero norm!"));
1048 }
1049
1050 ex lst_to_clifford(const ex & v, const ex & mu, const ex & metr, unsigned char rl)
1051 {
1052         if (!ex_to<idx>(mu).is_dim_numeric())
1053                 throw(std::invalid_argument("Index should have a numeric dimension"));
1054         ex e = clifford_unit(mu, metr, rl);
1055         return lst_to_clifford(v, e);
1056 }
1057
1058 ex lst_to_clifford(const ex & v, const ex & e) {
1059         unsigned min, max;
1060
1061         if (is_a<clifford>(e)) {
1062                 varidx mu = ex_to<varidx>(e.op(1));
1063                 unsigned dim = (ex_to<numeric>(mu.get_dim())).to_int();
1064
1065                 if (is_a<matrix>(v)) {
1066                         if (ex_to<matrix>(v).cols() > ex_to<matrix>(v).rows()) {
1067                                 min = ex_to<matrix>(v).rows();
1068                                 max = ex_to<matrix>(v).cols();
1069                         } else {
1070                                 min = ex_to<matrix>(v).cols();
1071                                 max = ex_to<matrix>(v).rows();
1072                         }
1073                         if (min == 1) {
1074                                 if (dim == max)
1075                                         return indexed(v, ex_to<varidx>(mu).toggle_variance()) * e;
1076                                 else
1077                                         throw(std::invalid_argument("Dimensions of vector and clifford unit mismatch"));
1078                         } else
1079                                 throw(std::invalid_argument("First argument should be a vector vector"));
1080                 } else if (is_a<lst>(v)) {
1081                         if (dim == ex_to<lst>(v).nops())
1082                                 return indexed(matrix(dim, 1, ex_to<lst>(v)), ex_to<varidx>(mu).toggle_variance()) * e;
1083                         else
1084                                 throw(std::invalid_argument("List length and dimension of clifford unit mismatch"));
1085                 } else
1086                         throw(std::invalid_argument("Cannot construct from anything but list or vector"));
1087         } else
1088                 throw(std::invalid_argument("The second argument should be a Clifford unit"));
1089 }
1090
1091 /** Auxiliary structure to define a function for striping one Clifford unit
1092  * from vectors. Used in  clifford_to_lst(). */
1093 static ex get_clifford_comp(const ex & e, const ex & c) 
1094 {
1095         pointer_to_map_function_1arg<const ex &> fcn(get_clifford_comp, c);
1096         int ival = ex_to<numeric>(ex_to<varidx>(c.op(1)).get_value()).to_int();
1097                 
1098         if (is_a<add>(e) || is_a<lst>(e) // || is_a<pseries>(e) || is_a<integral>(e)
1099                 || is_a<matrix>(e)) 
1100                 return e.map(fcn);
1101         else if (is_a<ncmul>(e) || is_a<mul>(e)) {
1102                 // find a Clifford unit with the same metric, delete it and substitute its index
1103                 size_t ind = e.nops() + 1;
1104                 for (size_t j = 0; j < e.nops(); j++) 
1105                         if (is_a<clifford>(e.op(j)) && ex_to<clifford>(c).same_metric(e.op(j)))
1106                                 if (ind > e.nops()) 
1107                                         ind = j;
1108                                 else 
1109                                         throw(std::invalid_argument("Expression is a Clifford multi-vector"));
1110                 if (ind < e.nops()) {
1111                         ex S = 1;
1112                         bool same_value_index, found_dummy;
1113                         same_value_index = ( ex_to<varidx>(e.op(ind).op(1)).is_numeric()
1114                                                                  &&  (ival == ex_to<numeric>(ex_to<varidx>(e.op(ind).op(1)).get_value()).to_int()) );
1115                         found_dummy = same_value_index;
1116                         for(size_t j=0; j < e.nops(); j++)
1117                                 if (j != ind) 
1118                                         if (same_value_index) 
1119                                                 S = S * e.op(j);
1120                                         else {
1121                                                 exvector ind_vec = ex_to<indexed>(e.op(j)).get_dummy_indices(ex_to<indexed>(e.op(ind)));
1122                                                 if (ind_vec.size() > 0) {
1123                                                         found_dummy = true;
1124                                                         exvector::const_iterator it = ind_vec.begin(), itend = ind_vec.end();
1125                                                         while (it != itend) {
1126                                                                 S = S * e.op(j).subs(lst(ex_to<varidx>(*it) == ival, ex_to<varidx>(*it).toggle_variance() == ival), subs_options::no_pattern);
1127                                                                 ++it;
1128                                                         }
1129                                                 } else
1130                                                         S = S * e.op(j);
1131                                         }
1132                         return (found_dummy ? S : 0);
1133                 } else
1134                         throw(std::invalid_argument("Expression is not a Clifford vector to the given units"));
1135         } else if (e.is_zero()) 
1136                 return e;
1137         else if (is_a<clifford>(e) && ex_to<clifford>(e).same_metric(c))
1138                 if ( ex_to<varidx>(e.op(1)).is_numeric() &&
1139                          (ival != ex_to<numeric>(ex_to<varidx>(e.op(1)).get_value()).to_int()) )
1140                         return 0;
1141                 else 
1142                         return 1;
1143         else
1144                 throw(std::invalid_argument("Expression is not usable as a Clifford vector"));
1145 }
1146
1147
1148 lst clifford_to_lst(const ex & e, const ex & c, bool algebraic)
1149 {
1150         GINAC_ASSERT(is_a<clifford>(c));
1151         varidx mu = ex_to<varidx>(c.op(1));
1152         if (! mu.is_dim_numeric())
1153                 throw(std::invalid_argument("Index should have a numeric dimension"));
1154         unsigned int D = ex_to<numeric>(mu.get_dim()).to_int();
1155
1156         if (algebraic) // check if algebraic method is applicable
1157                 for (unsigned int i = 0; i < D; i++) 
1158                         if (pow(c.subs(mu == i), 2).is_zero() 
1159                                 or (not is_a<numeric>(pow(c.subs(mu == i), 2))))
1160                                 algebraic = false;
1161         lst V; 
1162         if (algebraic) 
1163                 for (unsigned int i = 0; i < D; i++) 
1164                         V.append(remove_dirac_ONE(
1165                                                 simplify_indexed(canonicalize_clifford(e * c.subs(mu == i) +  c.subs(mu == i) * e))
1166                                                 / (2*pow(c.subs(mu == i), 2))));
1167         else {
1168                 ex e1 = canonicalize_clifford(e);
1169                 for (unsigned int i = 0; i < D; i++) 
1170                         V.append(get_clifford_comp(e1, c.subs(c.op(1) == i)));
1171         }
1172         return V;
1173 }
1174
1175
1176 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)
1177 {
1178         ex x, D, cu;
1179         
1180         if (! is_a<matrix>(v) && ! is_a<lst>(v))
1181                 throw(std::invalid_argument("parameter v should be either vector or list"));
1182         
1183         if (is_a<clifford>(G)) {
1184                 cu = G;
1185         } else {
1186                 if (is_a<indexed>(G)) 
1187                         D = ex_to<varidx>(G.op(1)).get_dim();
1188                 else if (is_a<matrix>(G)) 
1189                         D = ex_to<matrix>(G).rows(); 
1190                 else throw(std::invalid_argument("metric should be an indexed object, matrix, or a Clifford unit"));
1191                 
1192                 varidx mu((new symbol)->setflag(status_flags::dynallocated), D);
1193                 cu = clifford_unit(mu, G, rl);
1194         }
1195
1196         x = lst_to_clifford(v, cu); 
1197         ex e = simplify_indexed(canonicalize_clifford((a * x + b) * clifford_inverse(c * x + d)));
1198         return clifford_to_lst(e, cu, false);
1199 }
1200
1201 ex clifford_moebius_map(const ex & M, const ex & v, const ex & G, unsigned char rl)
1202 {
1203         if (is_a<matrix>(M)) 
1204                 return clifford_moebius_map(ex_to<matrix>(M)(0,0), ex_to<matrix>(M)(0,1), 
1205                                                 ex_to<matrix>(M)(1,0), ex_to<matrix>(M)(1,1), v, G, rl);
1206         else
1207                 throw(std::invalid_argument("parameter M should be a matrix"));
1208 }
1209
1210 } // namespace GiNaC