]> www.ginac.de Git - ginac.git/blobdiff - ginac/clifford.cpp
[BUGFIX] Fix crash in parser.
[ginac.git] / ginac / clifford.cpp
index f332c64edd034ce06987e1cffb24373ebd42e160..2cf0facf015ab00375fa3e334fbb80573bcf1f5b 100644 (file)
@@ -3,7 +3,7 @@
  *  Implementation of GiNaC's clifford algebra (Dirac gamma) objects. */
 
 /*
- *  GiNaC Copyright (C) 1999-2008 Johannes Gutenberg University Mainz, Germany
+ *  GiNaC Copyright (C) 1999-2024 Johannes Gutenberg University Mainz, Germany
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -20,8 +20,6 @@
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
-#include <stdexcept>
-
 #include "clifford.h"
 
 #include "ex.h"
 #include "archive.h"
 #include "utils.h"
 
+#include <stdexcept>
+
 namespace GiNaC {
 
 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(clifford, indexed,
   print_func<print_dflt>(&clifford::do_print_dflt).
-  print_func<print_latex>(&clifford::do_print_latex))
+  print_func<print_latex>(&clifford::do_print_latex).
+  print_func<print_tree>(&clifford::do_print_tree))
 
 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(diracone, tensor,
   print_func<print_dflt>(&diracone::do_print).
@@ -102,14 +103,14 @@ clifford::clifford(const ex & b, unsigned char rl) : inherited(b), representatio
  *  @see dirac_gamma */
 clifford::clifford(const ex & b, const ex & mu, const ex & metr, unsigned char rl, int comm_sign) : inherited(b, mu), representation_label(rl), metric(metr), commutator_sign(comm_sign)
 {
-       GINAC_ASSERT(is_a<varidx>(mu));
+       GINAC_ASSERT(is_a<idx>(mu));
 }
 
-clifford::clifford(unsigned char rl, const ex & metr, int comm_sign, const exvector & v, bool discardable) : inherited(not_symmetric(), v, discardable), representation_label(rl), metric(metr), commutator_sign(comm_sign)
+clifford::clifford(unsigned char rl, const ex & metr, int comm_sign, const exvector & v) : inherited(not_symmetric(), v), representation_label(rl), metric(metr), commutator_sign(comm_sign)
 {
 }
 
-clifford::clifford(unsigned char rl, const ex & metr, int comm_sign, std::auto_ptr<exvector> vp) : inherited(not_symmetric(), vp), representation_label(rl), metric(metr), commutator_sign(comm_sign)
+clifford::clifford(unsigned char rl, const ex & metr, int comm_sign, exvector && v) : inherited(not_symmetric(), std::move(v)), representation_label(rl), metric(metr), commutator_sign(comm_sign)
 {
 }
 
@@ -142,6 +143,7 @@ void clifford::archive(archive_node & n) const
 }
 
 GINAC_BIND_UNARCHIVER(clifford);
+GINAC_BIND_UNARCHIVER(cliffordunit);
 GINAC_BIND_UNARCHIVER(diracone);
 GINAC_BIND_UNARCHIVER(diracgamma);
 GINAC_BIND_UNARCHIVER(diracgamma5);
@@ -160,15 +162,15 @@ ex clifford::get_metric(const ex & i, const ex & j, bool symmetrised) const
                                return simplify_indexed(indexed(metric.op(0)*_ex1_2, i, j) + indexed(metric.op(0)*_ex1_2, j, i));
                        }
                } else {
-                       return metric.subs(lst(metric.op(1) == i, metric.op(2) == j), subs_options::no_pattern);
+                       return metric.subs(lst{metric.op(1) == i, metric.op(2) == j}, subs_options::no_pattern);
                }
        } else {
                exvector indices = metric.get_free_indices();
                if (symmetrised)
-                       return _ex1_2*simplify_indexed(metric.subs(lst(indices[0] == i, indices[1] == j), subs_options::no_pattern)
-                                                                       + metric.subs(lst(indices[0] == j, indices[1] == i), subs_options::no_pattern));
+                       return _ex1_2*simplify_indexed(metric.subs(lst{indices[0] == i, indices[1] == j}, subs_options::no_pattern)
+                                                    + metric.subs(lst{indices[0] == j, indices[1] == i}, subs_options::no_pattern));
                else
-                       return metric.subs(lst(indices[0] == i, indices[1] == j), subs_options::no_pattern);
+                       return metric.subs(lst{indices[0] == i, indices[1] == j}, subs_options::no_pattern);
        }
 }
 
@@ -207,7 +209,7 @@ ex & clifford::let_op(size_t i)
         GINAC_ASSERT(i<nops());
 
        static ex rl = numeric(representation_label);
-        ensure_if_modifiable();
+       ensure_if_modifiable();
        if (nops()-i == 1)
                return rl;
        else 
@@ -293,6 +295,17 @@ void clifford::do_print_latex(const print_latex & c, unsigned level) const
        }
 }
 
+void clifford::do_print_tree(const print_tree & c, unsigned level) const
+{
+       c.s << std::string(level, ' ') << class_name() << " @" << this
+           << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
+           << ", " << seq.size()-1 << " indices"
+           << ", symmetry=" << symtree << std::endl;
+       metric.print(c, level + c.delta_indent);
+       seq[0].print(c, level + c.delta_indent);
+       printindices(c, level + c.delta_indent);
+}
+
 DEFAULT_COMPARE(diracone)
 DEFAULT_COMPARE(cliffordunit)
 DEFAULT_COMPARE(diracgamma)
@@ -320,14 +333,14 @@ static void base_and_index(const ex & c, ex & b, ex & i)
                i = _ex0;
                b = _ex1;
        } else { // slash object, generate new dummy index
-               varidx ix((new symbol)->setflag(status_flags::dynallocated), ex_to<idx>(c.op(1)).get_dim());
+               varidx ix(dynallocate<symbol>(), ex_to<idx>(c.op(1)).get_dim());
                b = indexed(c.op(0), ix.toggle_variance());
                i = ix;
        }
 }
 
 /** Predicate for finding non-clifford objects. */
-struct is_not_a_clifford : public std::unary_function<ex, bool> {
+struct is_not_a_clifford {
        bool operator()(const ex & e)
        {
                return !is_a<clifford>(e);
@@ -348,7 +361,7 @@ bool diracgamma::contract_with(exvector::iterator self, exvector::iterator other
 
        if (is_a<clifford>(*other)) {
 
-               // Contraction only makes sense if the represenation labels are equal
+               // Contraction only makes sense if the representation labels are equal
                if (ex_to<clifford>(*other).get_representation_label() != rl)
                        return false;
 
@@ -398,7 +411,7 @@ bool diracgamma::contract_with(exvector::iterator self, exvector::iterator other
                        if (std::find_if(self + 1, other, is_not_a_clifford()) != other)
                                return false;
 
-                       *self = ncmul(exvector(std::reverse_iterator<exvector::const_iterator>(other), std::reverse_iterator<exvector::const_iterator>(self + 1)), true);
+                       *self = ncmul(exvector(std::reverse_iterator<exvector::const_iterator>(other), std::reverse_iterator<exvector::const_iterator>(self + 1)));
                        std::fill(self + 1, other, _ex1);
                        *other = _ex_2;
                        return true;
@@ -410,9 +423,9 @@ bool diracgamma::contract_with(exvector::iterator self, exvector::iterator other
                        if (std::find_if(self + 1, other, is_not_a_clifford()) != other)
                                return false;
 
-                       exvector::iterator next_to_last = other - 1;
-                       ex S = ncmul(exvector(self + 1, next_to_last), true);
-                       ex SR = ncmul(exvector(std::reverse_iterator<exvector::const_iterator>(next_to_last), std::reverse_iterator<exvector::const_iterator>(self + 1)), true);
+                       auto next_to_last = other - 1;
+                       ex S = ncmul(exvector(self + 1, next_to_last));
+                       ex SR = ncmul(exvector(std::reverse_iterator<exvector::const_iterator>(next_to_last), std::reverse_iterator<exvector::const_iterator>(self + 1)));
 
                        *self = (*next_to_last) * S + SR * (*next_to_last);
                        std::fill(self + 1, other, _ex1);
@@ -426,8 +439,8 @@ bool diracgamma::contract_with(exvector::iterator self, exvector::iterator other
                        if (std::find_if(self + 1, other, is_not_a_clifford()) != other)
                                return false;
 
-                       exvector::iterator next_to_last = other - 1;
-                       ex S = ncmul(exvector(self + 1, next_to_last), true);
+                       auto next_to_last = other - 1;
+                       ex S = ncmul(exvector(self + 1, next_to_last));
 
                        *self = 2 * (*next_to_last) * S - (*self) * S * (*other) * (*next_to_last);
                        std::fill(self + 1, other + 1, _ex1);
@@ -455,13 +468,13 @@ bool cliffordunit::contract_with(exvector::iterator self, exvector::iterator oth
        unsigned char rl = unit.get_representation_label();
 
        if (is_a<clifford>(*other)) {
-               // Contraction only makes sense if the represenation labels are equal
+               // Contraction only makes sense if the representation labels are equal
                // and the metrics are the same
                if ((ex_to<clifford>(*other).get_representation_label() != rl) 
                    && unit.same_metric(*other))
                        return false;
 
-               exvector::iterator before_other = other - 1;
+               auto before_other = other - 1;
                ex mu = self->op(1);
                ex mu_toggle = other->op(1);
                ex alpha = before_other->op(1);
@@ -494,7 +507,7 @@ bool cliffordunit::contract_with(exvector::iterator self, exvector::iterator oth
                                return false;
                        }
                        
-                       ex S = ncmul(exvector(self + 1, before_other), true);
+                       ex S = ncmul(exvector(self + 1, before_other));
 
                        if (is_a<clifford>(*before_other) && ex_to<clifford>(*before_other).get_representation_label() == rl) {
                                *self = 2 * (*self) * S * unit.get_metric(alpha, mu_toggle, true) - (*self) * S * (*other) * (*before_other);
@@ -519,11 +532,9 @@ ex clifford::eval_ncmul(const exvector & v) const
        s.reserve(v.size());
 
        // Remove superfluous ONEs
-       exvector::const_iterator cit = v.begin(), citend = v.end();
-       while (cit != citend) {
-               if (!is_a<clifford>(*cit) || !is_a<diracone>(cit->op(0)))
-                       s.push_back(*cit);
-               cit++;
+       for (auto & it : v) {
+               if (!is_a<clifford>(it) || !is_a<diracone>(it.op(0)))
+                       s.push_back(it);
        }
 
        bool something_changed = false;
@@ -531,11 +542,11 @@ ex clifford::eval_ncmul(const exvector & v) const
 
        // Anticommutate gamma5/L/R's to the front
        if (s.size() >= 2) {
-               exvector::iterator first = s.begin(), next_to_last = s.end() - 2;
+               auto first = s.begin(), next_to_last = s.end() - 2;
                while (true) {
-                       exvector::iterator it = next_to_last;
+                       auto it = next_to_last;
                        while (true) {
-                               exvector::iterator it2 = it + 1;
+                               auto it2 = it + 1;
                                if (is_a<clifford>(*it) && is_a<clifford>(*it2)) {
                                        ex e1 = it->op(0), e2 = it2->op(0);
 
@@ -662,7 +673,7 @@ ex clifford::eval_ncmul(const exvector & v) const
                        } else if (!a_is_cliffordunit && !b_is_cliffordunit && ag.is_equal(bg)) {
 
                                // a\ a\ -> a^2
-                               varidx ix((new symbol)->setflag(status_flags::dynallocated), ex_to<idx>(a.op(1)).minimal_dim(ex_to<idx>(b.op(1))));
+                               varidx ix(dynallocate<symbol>(), ex_to<idx>(a.op(1)).minimal_dim(ex_to<idx>(b.op(1))));
                                
                                a = indexed(ag, ix) * indexed(ag, ix.toggle_variance());
                                b = dirac_ONE(representation_label);
@@ -684,9 +695,9 @@ ex clifford::thiscontainer(const exvector & v) const
        return clifford(representation_label, metric, commutator_sign, v);
 }
 
-ex clifford::thiscontainer(std::auto_ptr<exvector> vp) const
+ex clifford::thiscontainer(exvector && v) const
 {
-       return clifford(representation_label, metric, commutator_sign, vp);
+       return clifford(representation_label, metric, commutator_sign, std::move(v));
 }
 
 ex diracgamma5::conjugate() const
@@ -696,12 +707,12 @@ ex diracgamma5::conjugate() const
 
 ex diracgammaL::conjugate() const
 {
-       return (new diracgammaR)->setflag(status_flags::dynallocated);
+       return dynallocate<diracgammaR>();
 }
 
 ex diracgammaR::conjugate() const
 {
-       return (new diracgammaL)->setflag(status_flags::dynallocated);
+       return dynallocate<diracgammaL>();
 }
 
 //////////
@@ -710,7 +721,7 @@ ex diracgammaR::conjugate() const
 
 ex dirac_ONE(unsigned char rl)
 {
-       static ex ONE = (new diracone)->setflag(status_flags::dynallocated);
+       static ex ONE = dynallocate<diracone>();
        return clifford(ONE, rl);
 }
 
@@ -727,8 +738,7 @@ static unsigned get_dim_uint(const ex& e)
 
 ex clifford_unit(const ex & mu, const ex & metr, unsigned char rl)
 {
-       //static ex unit = (new cliffordunit)->setflag(status_flags::dynallocated);
-       ex unit = (new cliffordunit)->setflag(status_flags::dynallocated);
+       ex unit = dynallocate<cliffordunit>();
 
        if (!is_a<idx>(mu))
                throw(std::invalid_argument("clifford_unit(): index of Clifford unit must be of type idx or varidx"));
@@ -742,10 +752,10 @@ ex clifford_unit(const ex & mu, const ex & metr, unsigned char rl)
                unsigned n = M.rows();
                bool symmetric = true;
 
-               //static idx xi((new symbol)->setflag(status_flags::dynallocated), n),
-               //      chi((new symbol)->setflag(status_flags::dynallocated), n);
-               idx xi((new symbol)->setflag(status_flags::dynallocated), n),
-                       chi((new symbol)->setflag(status_flags::dynallocated), n);
+               //static idx xi(dynallocate<symbol>(), n),
+               //           chi(dynallocate<symbol>(), n);
+               idx xi(dynallocate<symbol>(), n),
+                   chi(dynallocate<symbol>(), n);
                if ((n ==  M.cols()) && (n == get_dim_uint(mu))) {
                        for (unsigned i = 0; i < n; i++) {
                                for (unsigned j = i+1; j < n; j++) {
@@ -759,10 +769,10 @@ ex clifford_unit(const ex & mu, const ex & metr, unsigned char rl)
                        throw(std::invalid_argument("clifford_unit(): metric for Clifford unit must be a square matrix with the same dimensions as index"));
                }
        } else if (indices.size() == 0) { // a tensor or other expression without indices
-               //static varidx xi((new symbol)->setflag(status_flags::dynallocated), ex_to<idx>(mu).get_dim()),
-               //      chi((new symbol)->setflag(status_flags::dynallocated), ex_to<idx>(mu).get_dim());
-               varidx xi((new symbol)->setflag(status_flags::dynallocated), ex_to<idx>(mu).get_dim()),
-                       chi((new symbol)->setflag(status_flags::dynallocated), ex_to<idx>(mu).get_dim());
+               //static varidx xi(dynallocate<symbol>(), ex_to<idx>(mu).get_dim()),
+               //              chi(dynallocate<symbol>(), ex_to<idx>(mu).get_dim());
+               varidx xi(dynallocate<symbol>(), ex_to<idx>(mu).get_dim()),
+                      chi(dynallocate<symbol>(), ex_to<idx>(mu).get_dim());
                return clifford(unit, mu, indexed(metr, xi, chi), rl);
        }  else 
                throw(std::invalid_argument("clifford_unit(): metric for Clifford unit must be of type tensor, matrix or an expression with two free indices"));
@@ -770,31 +780,31 @@ ex clifford_unit(const ex & mu, const ex & metr, unsigned char rl)
 
 ex dirac_gamma(const ex & mu, unsigned char rl)
 {
-       static ex gamma = (new diracgamma)->setflag(status_flags::dynallocated);
+       static ex gamma = dynallocate<diracgamma>();
 
        if (!is_a<varidx>(mu))
                throw(std::invalid_argument("dirac_gamma(): index of Dirac gamma must be of type varidx"));
 
-       static varidx xi((new symbol)->setflag(status_flags::dynallocated), ex_to<varidx>(mu).get_dim()),
-               chi((new symbol)->setflag(status_flags::dynallocated), ex_to<varidx>(mu).get_dim());
-       return clifford(gamma, mu, indexed((new minkmetric)->setflag(status_flags::dynallocated), symmetric2(), xi, chi), rl);
+       static varidx xi(dynallocate<symbol>(), ex_to<varidx>(mu).get_dim()),
+                     chi(dynallocate<symbol>(), ex_to<varidx>(mu).get_dim());
+       return clifford(gamma, mu, indexed(dynallocate<minkmetric>(), symmetric2(), xi, chi), rl);
 }
 
 ex dirac_gamma5(unsigned char rl)
 {
-       static ex gamma5 = (new diracgamma5)->setflag(status_flags::dynallocated);
+       static ex gamma5 = dynallocate<diracgamma5>();
        return clifford(gamma5, rl);
 }
 
 ex dirac_gammaL(unsigned char rl)
 {
-       static ex gammaL = (new diracgammaL)->setflag(status_flags::dynallocated);
+       static ex gammaL = dynallocate<diracgammaL>();
        return clifford(gammaL, rl);
 }
 
 ex dirac_gammaR(unsigned char rl)
 {
-       static ex gammaR = (new diracgammaR)->setflag(status_flags::dynallocated);
+       static ex gammaR = dynallocate<diracgammaR>();
        return clifford(gammaR, rl);
 }
 
@@ -804,9 +814,9 @@ ex dirac_slash(const ex & e, const ex & dim, unsigned char rl)
        // vector as its base expression and a (dummy) index that just serves
        // for storing the space dimensionality
 
-       static varidx xi((new symbol)->setflag(status_flags::dynallocated), dim),
-               chi((new symbol)->setflag(status_flags::dynallocated), dim);
-   return clifford(e, varidx(0, dim), indexed((new minkmetric)->setflag(status_flags::dynallocated), symmetric2(), xi, chi), rl);
+       static varidx xi(dynallocate<symbol>(), dim),
+                     chi(dynallocate<symbol>(), dim);
+       return clifford(e, varidx(0, dim), indexed(dynallocate<minkmetric>(), symmetric2(), xi, chi), rl);
 }
 
 /** Extract representation label from tinfo key (as returned by
@@ -893,10 +903,10 @@ ex dirac_trace(const ex & e, const std::set<unsigned char> & rls, const ex & trO
                        return e;
 
                // Substitute gammaL/R and expand product, if necessary
-               ex e_expanded = e.subs(lst(
+               ex e_expanded = e.subs(lst{
                        dirac_gammaL(rl) == (dirac_ONE(rl)-dirac_gamma5(rl))/2,
                        dirac_gammaR(rl) == (dirac_ONE(rl)+dirac_gamma5(rl))/2
-               ), subs_options::no_pattern).expand();
+               }, subs_options::no_pattern).expand();
                if (!is_a<ncmul>(e_expanded))
                        return dirac_trace(e_expanded, rls, trONE);
 
@@ -922,7 +932,7 @@ ex dirac_trace(const ex & e, const std::set<unsigned char> & rls, const ex & trO
                                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();
                        }
 
-                       // Tr gamma5 S_2k =
+                       // Tr gamma5 S_2k =
                        //   I/4! * epsilon0123.mu1.mu2.mu3.mu4 * Tr gamma.mu1 gamma.mu2 gamma.mu3 gamma.mu4 S_2k
                        // (the epsilon is always 4-dimensional)
                        exvector ix(num-1), bv(num-1);
@@ -993,9 +1003,9 @@ ex dirac_trace(const ex & e, const lst & rll, const ex & trONE)
 {
        // Convert list to set
        std::set<unsigned char> rls;
-       for (lst::const_iterator i = rll.begin(); i != rll.end(); ++i) {
-               if (i->info(info_flags::nonnegint))
-                       rls.insert(ex_to<numeric>(*i).to_int());
+       for (const auto & i : rll) {
+               if (i.info(info_flags::nonnegint))
+                       rls.insert(ex_to<numeric>(i).to_int());
        }
 
        return dirac_trace(e, rls, trONE);
@@ -1023,10 +1033,10 @@ ex canonicalize_clifford(const ex & e_)
                // Scan for any ncmul objects
                exmap srl;
                ex aux = e.to_rational(srl);
-               for (exmap::iterator i = srl.begin(); i != srl.end(); ++i) {
+               for (auto & i : srl) {
 
-                       ex lhs = i->first;
-                       ex rhs = i->second;
+                       ex lhs = i.first;
+                       ex rhs = i.second;
 
                        if (is_exactly_a<ncmul>(rhs)
                                        && rhs.return_type() == return_types::noncommutative
@@ -1035,7 +1045,7 @@ ex canonicalize_clifford(const ex & e_)
                                // Expand product, if necessary
                                ex rhs_expanded = rhs.expand();
                                if (!is_a<ncmul>(rhs_expanded)) {
-                                       i->second = canonicalize_clifford(rhs_expanded);
+                                       i.second = canonicalize_clifford(rhs_expanded);
                                        continue;
 
                                } else if (!is_a<clifford>(rhs.op(0)))
@@ -1047,7 +1057,7 @@ ex canonicalize_clifford(const ex & e_)
                                        v.push_back(rhs.op(j));
 
                                // Stupid recursive bubble sort because we only want to swap adjacent gammas
-                               exvector::iterator it = v.begin(), next_to_last = v.end() - 1;
+                               auto it = v.begin(), next_to_last = v.end() - 1;
                                if (is_a<diracgamma5>(it->op(0)) || is_a<diracgammaL>(it->op(0)) || is_a<diracgammaR>(it->op(0)))
                                        ++it;
 
@@ -1064,8 +1074,8 @@ ex canonicalize_clifford(const ex & e_)
                                                ex sum = ncmul(v);
                                                it[0] = save1;
                                                it[1] = save0;
-                                               sum += ex_to<clifford>(save0).get_commutator_sign() * ncmul(v, true);
-                                               i->second = canonicalize_clifford(sum);
+                                               sum += ex_to<clifford>(save0).get_commutator_sign() * ncmul(std::move(v));
+                                               i.second = canonicalize_clifford(sum);
                                                goto next_sym;
                                        }
                                        ++it;
@@ -1077,6 +1087,47 @@ next_sym:        ;
        }
 }
 
+ex clifford_star_bar(const ex & e, bool do_bar, unsigned options)
+{
+       pointer_to_map_function_2args<bool, unsigned> fcn(clifford_star_bar, do_bar, options | 1);
+
+       // is a child, no need to expand
+       ex e1= (options & 1 ? e : e.expand());
+
+       if (is_a<ncmul>(e1) ) { // reversing order of clifford units
+               exvector ev, cv;
+               ev.reserve(e1.nops());
+               cv.reserve(e1.nops());
+               // separate clifford and non-clifford entries
+               for (size_t i= 0; i < e1.nops(); ++i) {
+                       if (is_a<clifford>(e1.op(i)) && is_a<cliffordunit>(e1.op(i).op(0)))
+                               cv.push_back(e1.op(i));
+                       else
+                               ev.push_back(e1.op(i));
+               }
+               for (auto i=cv.rbegin(); i!=cv.rend(); ++i) { // reverse order of Clifford units
+                       ev.push_back(i->conjugate());
+               }
+               // For clifford_bar an odd number of clifford units reverts the sign
+               if (do_bar && (cv.size() % 2 == 1))
+                       return -dynallocate<ncmul>(std::move(ev));
+               else
+                       return dynallocate<ncmul>(std::move(ev));
+       } else if (is_a<clifford>(e1) && is_a<cliffordunit>(e1.op(0))) {
+               if (do_bar)
+                       return -e;
+               else
+                       return e;
+       } else if (is_a<power>(e1)) {
+               // apply the procedure to the base of a power
+               return pow(clifford_star_bar(e1.op(0), do_bar, 0), e1.op(1));
+       } else if (is_a<add>(e1) || is_a<mul>(e1) || e.info(info_flags::list)) {
+               // recurse into subexpressions
+               return e1.map(fcn);
+       } else  // nothing meaningful can be done
+               return e;
+}
+
 ex clifford_prime(const ex & e)
 {
        pointer_to_map_function fcn(clifford_prime);
@@ -1132,7 +1183,7 @@ ex remove_dirac_ONE(const ex & e, unsigned char rl, unsigned options)
        return e1;
 }
 
-char clifford_max_label(const ex & e, bool ignore_ONE)
+int clifford_max_label(const ex & e, bool ignore_ONE)
 {
        if (is_a<clifford>(e))
                if (ignore_ONE && is_a<diracone>(e.op(0)))
@@ -1140,7 +1191,7 @@ char clifford_max_label(const ex & e, bool ignore_ONE)
                else
                        return ex_to<clifford>(e).get_representation_label();
        else {
-               char rl = -1;
+               int rl = -1;
                for (size_t i=0; i < e.nops(); i++) 
                        rl = (rl > clifford_max_label(e.op(i), ignore_ONE)) ? rl : clifford_max_label(e.op(i), ignore_ONE);
                return rl;
@@ -1194,7 +1245,7 @@ ex lst_to_clifford(const ex & v, const ex & e) {
                                                return v.op(0) * dirac_ONE(ex_to<clifford>(e).get_representation_label()) + indexed(sub_matrix(ex_to<matrix>(v), 0, 1, 1, dim), mu_toggle) * e;
                                        else 
                                                return v.op(0) * dirac_ONE(ex_to<clifford>(e).get_representation_label()) + indexed(sub_matrix(ex_to<matrix>(v), 1, dim, 0, 1), mu_toggle) * e;
-                               } else
+                               } else
                                        throw(std::invalid_argument("lst_to_clifford(): dimensions of vector and clifford unit mismatch"));
                        } else
                                throw(std::invalid_argument("lst_to_clifford(): first argument should be a vector (nx1 or 1xn matrix)"));
@@ -1210,76 +1261,72 @@ ex lst_to_clifford(const ex & v, const ex & e) {
        } else
                throw(std::invalid_argument("lst_to_clifford(): the second argument should be a Clifford unit"));
 }
+
 /** Auxiliary structure to define a function for striping one Clifford unit
  * from vectors. Used in  clifford_to_lst(). */
-static ex get_clifford_comp(const ex & e, const ex & c
+static ex get_clifford_comp(const ex & e, const ex & c, bool root=true)
 {
-       pointer_to_map_function_1arg<const ex &> fcn(get_clifford_comp, c);
+       // make expansion on the top-level call only
+       ex e1=(root? e.expand() : e);
+
+       pointer_to_map_function_2args<const ex &, bool> fcn(get_clifford_comp, c, false);
        int ival = ex_to<numeric>(ex_to<idx>(c.op(1)).get_value()).to_int();
-               
-       if (is_a<add>(e) || e.info(info_flags::list) // || is_a<pseries>(e) || is_a<integral>(e)
-               || is_a<matrix>(e)) 
-               return e.map(fcn);
-       else if (is_a<ncmul>(e) || is_a<mul>(e)) {
-               // find a Clifford unit with the same metric, delete it and substitute its index
-               size_t ind = e.nops() + 1;
-               for (size_t j = 0; j < e.nops(); j++) {
-                       if (is_a<clifford>(e.op(j)) && ex_to<clifford>(c).same_metric(e.op(j))) {
-                               if (ind > e.nops()) {
-                                       ind = j;
-                               }
-                               else {
+       int rl=ex_to<clifford>(c).get_representation_label();
+
+       if ( (is_a<add>(e1) || e1.info(info_flags::list) || is_a<matrix>(e1))) {
+               return e1.map(fcn);
+       } else if (is_a<ncmul>(e1) || is_a<mul>(e1)) {
+               // searches are done within products only
+               exvector ev, all_dummy=get_all_dummy_indices(e1);
+               bool found=false, same_value_found=false;
+               ex dummy_ind=0;
+               ev.reserve(e1.nops());
+               for (size_t i=0; i < e1.nops(); ++i) {
+                       // look for a Clifford unit with the same metric and representation label,
+                       // if found remember its index
+                       if (is_a<clifford>(e1.op(i)) && ex_to<clifford>(e1.op(i)).get_representation_label() == rl
+                               && is_a<cliffordunit>(e1.op(i).op(0)) && ex_to<clifford>(e1.op(i)).same_metric(c)) { // same Clifford unit
+                               if (found)
                                        throw(std::invalid_argument("get_clifford_comp(): expression is a Clifford multi-vector"));
-                               }
-                       }
+                               found=true;
+                               if (ex_to<idx>(e1.op(i).op(1)).is_numeric() &&
+                                   (ival == ex_to<numeric>(ex_to<idx>(e1.op(i).op(1)).get_value()).to_int())) {
+                                       same_value_found = true; // desired index value is found
+                               } else if ((std::find(all_dummy.begin(), all_dummy.end(), e1.op(i).op(1)) != all_dummy.end())
+                                          || (is_a<varidx>(e1.op(i).op(1))
+                                              && std::find(all_dummy.begin(), all_dummy.end(),
+                                                           ex_to<varidx>(e1.op(i).op(1)).toggle_variance()) != all_dummy.end())) {
+                                       dummy_ind=(e1.op(i).op(1)); // suitable dummy index found
+                               } else
+                                       ev.push_back(e.op(i)); // another index value
+                       } else
+                               ev.push_back(e1.op(i));
                }
-               if (ind < e.nops()) {
-                       ex S = 1;
-                       bool same_value_index, found_dummy;
-                       same_value_index = ( ex_to<idx>(e.op(ind).op(1)).is_numeric()
-                                                                &&  (ival == ex_to<numeric>(ex_to<idx>(e.op(ind).op(1)).get_value()).to_int()) );
-                       found_dummy = same_value_index;
-                       for (size_t j=0; j < e.nops(); j++) {
-                               if (j != ind) {
-                                       if (same_value_index) {
-                                               S = S * e.op(j);
-                                       }
-                                       else {
-                                               exvector ind_vec = ex_to<indexed>(e.op(j)).get_dummy_indices(ex_to<indexed>(e.op(ind)));
-                                               if (ind_vec.size() > 0) {
-                                                       found_dummy = true;
-                                                       exvector::const_iterator it = ind_vec.begin(), itend = ind_vec.end();
-                                                       while (it != itend) {
-                                                               ex curridx = *it;
-                                                               ex curridx_toggle = is_a<varidx>(curridx)
-                                                                       ? ex_to<varidx>(curridx).toggle_variance()
-                                                                       : curridx;
-                                                               S = S * e.op(j).subs(lst(curridx == ival,
-                                                                       curridx_toggle == ival), subs_options::no_pattern);
-                                                               ++it;
-                                                       }
-                                               } else
-                                                       S = S * e.op(j);
-                                       }
-                               }
-                       }
-                       return (found_dummy ? S : 0);
-               } else
+
+               if (! found) // no Clifford units found at all
                        throw(std::invalid_argument("get_clifford_comp(): expression is not a Clifford vector to the given units"));
-       } else if (e.is_zero()) 
-               return e;
-       else if (is_a<clifford>(e) && ex_to<clifford>(e).same_metric(c))
-               if ( ex_to<idx>(e.op(1)).is_numeric() &&
-                        (ival != ex_to<numeric>(ex_to<idx>(e.op(1)).get_value()).to_int()) )
+
+               ex res=dynallocate<ncmul>(std::move(ev));
+               if (same_value_found) {
+                       return  res;
+               } else if (! dummy_ind.is_zero()) { // a dummy index was found
+                       if (is_a<varidx>(dummy_ind))
+                               dummy_ind = ex_to<varidx>(dummy_ind).toggle_variance();
+                       return res.subs(dummy_ind==ival, subs_options::no_pattern);
+               } else // found a Clifford unit with another index
                        return 0;
-               else 
+       } else if (e1.is_zero()) {
+               return 0;
+       } else if (is_a<clifford>(e1) && is_a<cliffordunit>(e1.op(0)) && ex_to<clifford>(e1).same_metric(c)) {
+               if (ex_to<idx>(e1.op(1)).is_numeric() &&
+                   (ival == ex_to<numeric>(ex_to<idx>(e1.op(1)).get_value()).to_int()) )
                        return 1;
-       else
+               else
+                       return 0;
+       } else
                throw(std::invalid_argument("get_clifford_comp(): expression is not usable as a Clifford vector"));
 }
 
-
 lst clifford_to_lst(const ex & e, const ex & c, bool algebraic)
 {
        GINAC_ASSERT(is_a<clifford>(c));
@@ -1291,11 +1338,11 @@ lst clifford_to_lst(const ex & e, const ex & c, bool algebraic)
        if (algebraic) // check if algebraic method is applicable
                for (unsigned int i = 0; i < D; i++) 
                        if (pow(c.subs(mu == i, subs_options::no_pattern), 2).is_zero() 
-                               or (not is_a<numeric>(pow(c.subs(mu == i, subs_options::no_pattern), 2))))
+                               || (! is_a<numeric>(pow(c.subs(mu == i, subs_options::no_pattern), 2))))
                                algebraic = false;
        lst V; 
-       ex v0 = remove_dirac_ONE(canonicalize_clifford(e+clifford_prime(e)).normal())/2;
-       if (not v0.is_zero())
+       ex v0 = remove_dirac_ONE(canonicalize_clifford(e+clifford_prime(e)))/2;
+       if (! v0.is_zero())
                V.append(v0);
        ex e1 = canonicalize_clifford(e - v0 * dirac_ONE(ex_to<clifford>(c).get_representation_label())); 
        if (algebraic) {
@@ -1305,14 +1352,14 @@ lst clifford_to_lst(const ex & e, const ex & c, bool algebraic)
                                                / (2*pow(c.subs(mu == i, subs_options::no_pattern), 2))));
        } else {
                try {
-                       for (unsigned int i = 0; i < D; i++) 
+                       for (unsigned int i = 0; i < D; i++)
                                V.append(get_clifford_comp(e1, c.subs(c.op(1) == i, subs_options::no_pattern)));
                } catch  (std::exception &p) {
                        /* Try to expand dummy summations to simplify the expression*/
                        e1 = canonicalize_clifford(expand_dummy_sum(e, true));
                        V.remove_all();
-                       v0 = remove_dirac_ONE(canonicalize_clifford(e1+clifford_prime(e1)).normal())/2;
-                       if (not v0.is_zero()) {
+                       v0 = remove_dirac_ONE(canonicalize_clifford(e1+clifford_prime(e1)))/2;
+                       if (! v0.is_zero()) {
                                V.append(v0);
                                e1 = canonicalize_clifford(e1 - v0 * dirac_ONE(ex_to<clifford>(c).get_representation_label())); 
                        }
@@ -1336,11 +1383,11 @@ ex clifford_moebius_map(const ex & a, const ex & b, const ex & c, const ex & d,
        } else {
                if (is_a<indexed>(G)) {
                        D = ex_to<idx>(G.op(1)).get_dim();
-                       varidx mu((new symbol)->setflag(status_flags::dynallocated), D);
+                       varidx mu(dynallocate<symbol>(), D);
                        cu = clifford_unit(mu, G, rl);
                } else if (is_a<matrix>(G)) {
                        D = ex_to<matrix>(G).rows(); 
-                       idx mu((new symbol)->setflag(status_flags::dynallocated), D);
+                       idx mu(dynallocate<symbol>(), D);
                        cu = clifford_unit(mu, G, rl);
                } else throw(std::invalid_argument("clifford_moebius_map(): metric should be an indexed object, matrix, or a Clifford unit"));