+/** 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)
+{
+ pointer_to_map_function_1arg<const ex &> fcn(get_clifford_comp, c);
+
+ if (is_a<add>(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
+ int ival = ex_to<numeric>(ex_to<varidx>(c.op(1)).get_value()).to_int();
+ 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
+ throw(std::invalid_argument("Expression is a Clifford multi-vector"));
+ if (ind < e.nops()) {
+ ex S = 1;
+ for(size_t j=0; j < e.nops(); j++)
+ if (j != ind) {
+ exvector ind_vec = ex_to<indexed>(e.op(j)).get_dummy_indices(ex_to<indexed>(e.op(ind)));
+ if (ind_vec.size() > 0) {
+ exvector::const_iterator it = ind_vec.begin(), itend = ind_vec.end();
+ while (it != itend) {
+ S = S * e.op(j).subs(lst(ex_to<varidx>(*it) == ival, ex_to<varidx>(*it).toggle_variance() == ival), subs_options::no_pattern);
+ it++;
+ }
+ } else
+ S = S * e.op(j);
+ }
+ return S;
+ } else
+ throw(std::invalid_argument("Expression is not a Clifford vector to the given units"));
+ } else if (e.is_zero())
+ return e;
+ else
+ throw(std::invalid_argument("Expression is not handlable as a Clifford vector"));
+
+}
+
+
+lst clifford_to_lst (const ex & e, const ex & c, bool algebraic)
+{
+ GINAC_ASSERT(is_a<clifford>(c));
+ varidx mu = ex_to<varidx>(c.op(1));
+ if (! mu.is_dim_numeric())
+ throw(std::invalid_argument("Index should have a numeric dimension"));
+ unsigned int D = ex_to<numeric>(mu.get_dim()).to_int();
+
+ if (algebraic) // check if algebraic method is applicable
+ for (unsigned int i = 0; i < D; i++)
+ if (pow(c.subs(mu == i), 2) == 0)
+ algebraic = false;
+ lst V;
+ if (algebraic)
+ for (unsigned int i = 0; i < D; i++)
+ V.append(remove_dirac_ONE(
+ simplify_indexed(canonicalize_clifford(e * c.subs(mu == i) + c.subs(mu == i) * e))
+ / (2*pow(c.subs(mu == i), 2))));
+ else {
+ ex e1 = canonicalize_clifford(e);
+ for (unsigned int i = 0; i < D; i++)
+ V.append(get_clifford_comp(e1, c.subs(c.op(1) == i)));
+ }
+ return V;
+}
+
+
+ex clifford_moebius_map(const ex & a, const ex & b, const ex & c, const ex & d, const ex & v, const ex & G)
+{
+ ex x, D;
+ if (is_a<indexed>(G))
+ D = ex_to<varidx>(G.op(1));
+ else
+ throw(std::invalid_argument("metric should be an indexed object"));
+
+ varidx mu ((new symbol)->setflag(status_flags::dynallocated), ex_to<varidx>(D).get_dim());
+
+ if (! is_a<matrix>(v) && ! is_a<lst>(v))
+ throw(std::invalid_argument("parameter v should be either vector or list"));
+
+ x = lst_to_clifford(v, mu, G);
+ ex e = simplify_indexed(canonicalize_clifford((a * x + b) * clifford_inverse(c * x + d)));
+ ex cu = clifford_unit(mu, G);
+ return clifford_to_lst(e, cu, false);
+}