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