]> www.ginac.de Git - ginac.git/blobdiff - ginac/clifford.cpp
This patch fixes a bug on machines where char is unsigned by default, by
[ginac.git] / ginac / clifford.cpp
index af54d77bbcb665ef7478c6dfd300c2d4aa4dd109..301d40c19640a605842079de30e8281293000918 100644 (file)
@@ -3,7 +3,7 @@
  *  Implementation of GiNaC's clifford algebra (Dirac gamma) objects. */
 
 /*
- *  GiNaC Copyright (C) 1999-2006 Johannes Gutenberg University Mainz, Germany
+ *  GiNaC Copyright (C) 1999-2011 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))
 
-const tinfo_static_t clifford::return_type_tinfo_static[256] = {{}};
-
 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(diracone, tensor,
   print_func<print_dflt>(&diracone::do_print).
   print_func<print_latex>(&diracone::do_print_latex))
@@ -78,7 +76,6 @@ GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(diracgammaR, tensor,
 
 clifford::clifford() : representation_label(0), metric(0), commutator_sign(-1)
 {
-       tinfo_key = &clifford::tinfo_static;
 }
 
 DEFAULT_CTOR(diracone)
@@ -97,7 +94,6 @@ DEFAULT_CTOR(diracgammaR)
  *  @see dirac_ONE */
 clifford::clifford(const ex & b, unsigned char rl) : inherited(b), representation_label(rl), metric(0), commutator_sign(-1)
 {
-       tinfo_key = &clifford::tinfo_static;
 }
 
 /** Construct object with one Lorentz index. This constructor is for internal
@@ -107,25 +103,28 @@ clifford::clifford(const ex & b, unsigned char rl) : inherited(b), representatio
 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));
-       tinfo_key = &clifford::tinfo_static;
 }
 
 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)
 {
-       tinfo_key = &clifford::tinfo_static;
 }
 
 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)
 {
-       tinfo_key = &clifford::tinfo_static;
+}
+
+return_type_t clifford::return_type_tinfo() const
+{
+       return make_return_type_t<clifford>(representation_label);
 }
 
 //////////
 // archiving
 //////////
 
-clifford::clifford(const archive_node & n, lst & sym_lst) : inherited(n, sym_lst)
+void clifford::read_archive(const archive_node& n, lst& sym_lst)
 {
+       inherited::read_archive(n, sym_lst);
        unsigned rl;
        n.find_unsigned("label", rl);
        representation_label = rl;
@@ -142,13 +141,12 @@ void clifford::archive(archive_node & n) const
        n.add_unsigned("commutator_sign+1", commutator_sign+1);
 }
 
-DEFAULT_UNARCHIVE(clifford)
-DEFAULT_ARCHIVING(diracone)
-DEFAULT_ARCHIVING(cliffordunit)
-DEFAULT_ARCHIVING(diracgamma)
-DEFAULT_ARCHIVING(diracgamma5)
-DEFAULT_ARCHIVING(diracgammaL)
-DEFAULT_ARCHIVING(diracgammaR)
+GINAC_BIND_UNARCHIVER(clifford);
+GINAC_BIND_UNARCHIVER(diracone);
+GINAC_BIND_UNARCHIVER(diracgamma);
+GINAC_BIND_UNARCHIVER(diracgamma5);
+GINAC_BIND_UNARCHIVER(diracgammaL);
+GINAC_BIND_UNARCHIVER(diracgammaR);
 
 
 ex clifford::get_metric(const ex & i, const ex & j, bool symmetrised) const
@@ -265,8 +263,21 @@ void clifford::do_print_dflt(const print_dflt & c, unsigned level) const
        if (is_dirac_slash(seq[0])) {
                seq[0].print(c, precedence());
                c.s << "\\";
-       } else
-               this->print_dispatch<inherited>(c, level);
+       } else { // We do not print representation label if it is 0
+               if (representation_label == 0) {
+                       this->print_dispatch<inherited>(c, level);
+               } else { // otherwise we put it before indices in square brackets; the code is borrowed from indexed.cpp 
+                       if (precedence() <= level) {
+                               c.s << '(';
+                       }
+                       seq[0].print(c, precedence());
+                       c.s << '[' << int(representation_label) << ']';
+                       printindices(c, level);
+                       if (precedence() <= level) {
+                               c.s << ')';
+                       }
+               }
+       }
 }
 
 void clifford::do_print_latex(const print_latex & c, unsigned level) const
@@ -703,28 +714,42 @@ ex dirac_ONE(unsigned char rl)
        return clifford(ONE, rl);
 }
 
+static unsigned get_dim_uint(const ex& e)
+{
+       if (!is_a<idx>(e))
+               throw std::invalid_argument("get_dim_uint: argument is not an index");
+       ex dim = ex_to<idx>(e).get_dim();
+       if (!dim.info(info_flags::posint))
+               throw std::invalid_argument("get_dim_uint: dimension of index should be a positive integer");
+       unsigned d = ex_to<numeric>(dim).to_int();
+       return d;
+}
+
 ex clifford_unit(const ex & mu, const ex & metr, unsigned char rl)
 {
-       static ex unit = (new cliffordunit)->setflag(status_flags::dynallocated);
+       //static ex unit = (new cliffordunit)->setflag(status_flags::dynallocated);
+       ex unit = (new cliffordunit)->setflag(status_flags::dynallocated);
 
        if (!is_a<idx>(mu))
                throw(std::invalid_argument("clifford_unit(): index of Clifford unit must be of type idx or varidx"));
 
        exvector indices = metr.get_free_indices();
 
-       if ((indices.size() == 2) && is_a<varidx>(indices[0]) && is_a<varidx>(indices[1])) {
+       if (indices.size() == 2) {
                return clifford(unit, mu, metr, rl);
        } else if (is_a<matrix>(metr)) {
                matrix M = ex_to<matrix>(metr);
                unsigned n = M.rows();
                bool symmetric = true;
 
-               static varidx xi((new symbol)->setflag(status_flags::dynallocated), n),
+               //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);
-               if ((n ==  M.cols()) && (n == ex_to<idx>(mu).get_dim())) {
+               if ((n ==  M.cols()) && (n == get_dim_uint(mu))) {
                        for (unsigned i = 0; i < n; i++) {
                                for (unsigned j = i+1; j < n; j++) {
-                                       if (M(i, j) != M(j, i)) {
+                                       if (!M(i, j).is_equal(M(j, i))) {
                                                symmetric = false;
                                        }
                                }
@@ -734,8 +759,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<varidx>(mu).get_dim()),
-                       chi((new symbol)->setflag(status_flags::dynallocated), ex_to<varidx>(mu).get_dim());
+               //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());
                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"));
@@ -782,19 +809,11 @@ ex dirac_slash(const ex & e, const ex & dim, unsigned char rl)
    return clifford(e, varidx(0, dim), indexed((new minkmetric)->setflag(status_flags::dynallocated), symmetric2(), xi, chi), rl);
 }
 
-/** Check whether a given tinfo key (as returned by return_type_tinfo()
- *  is that of a clifford object (with an arbitrary representation label). */
-bool is_clifford_tinfo(tinfo_t ti)
-{
-       p_int start_loc=(p_int)&clifford::return_type_tinfo_static;
-       return (p_int)ti>=start_loc && (p_int)ti<start_loc+256;
-}
-
 /** Extract representation label from tinfo key (as returned by
  *  return_type_tinfo()). */
-static unsigned char get_representation_label(tinfo_t ti)
+static unsigned char get_representation_label(const return_type_t& ti)
 {
-       return (unsigned char)((p_int)ti-(p_int)&clifford::return_type_tinfo_static);
+       return (unsigned char)ti.rl;
 }
 
 /** Take trace of a string of an even number of Dirac gammas given a vector
@@ -1113,7 +1132,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)))
@@ -1121,7 +1140,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;
@@ -1157,7 +1176,7 @@ ex lst_to_clifford(const ex & v, const ex & e) {
                ex mu = e.op(1);
                ex mu_toggle
                        = is_a<varidx>(mu) ? ex_to<varidx>(mu).toggle_variance() : mu;
-               unsigned dim = (ex_to<numeric>(ex_to<idx>(mu).get_dim())).to_int();
+               unsigned dim = get_dim_uint(mu);
 
                if (is_a<matrix>(v)) {
                        if (ex_to<matrix>(v).cols() > ex_to<matrix>(v).rows()) {
@@ -1170,13 +1189,20 @@ ex lst_to_clifford(const ex & v, const ex & e) {
                        if (min == 1) {
                                if (dim == max)
                                        return indexed(v, mu_toggle) * e;
-                               else
+                               else if (max - dim == 1) {
+                                       if (ex_to<matrix>(v).cols() > ex_to<matrix>(v).rows())
+                                               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
                                        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)"));
                } else if (v.info(info_flags::list)) {
                        if (dim == ex_to<lst>(v).nops())
                                return indexed(matrix(dim, 1, ex_to<lst>(v)), mu_toggle) * e;
+                       else if (ex_to<lst>(v).nops() - dim == 1)
+                               return v.op(0) * dirac_ONE(ex_to<clifford>(e).get_representation_label()) + indexed(sub_matrix(matrix(dim+1, 1, ex_to<lst>(v)), 1, dim, 0, 1), mu_toggle) * e;
                        else
                                throw(std::invalid_argument("lst_to_clifford(): list length and dimension of clifford unit mismatch"));
                } else
@@ -1198,22 +1224,27 @@ static ex get_clifford_comp(const ex & e, const ex & c)
        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()) 
+               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 
+                               }
+                               else {
                                        throw(std::invalid_argument("get_clifford_comp(): expression is a Clifford multi-vector"));
+                               }
+                       }
+               }
                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) 
+                       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) {
@@ -1231,6 +1262,8 @@ static ex get_clifford_comp(const ex & e, const ex & c)
                                                } else
                                                        S = S * e.op(j);
                                        }
+                               }
+                       }
                        return (found_dummy ? S : 0);
                } else
                        throw(std::invalid_argument("get_clifford_comp(): expression is not a Clifford vector to the given units"));
@@ -1258,22 +1291,31 @@ 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 (! v0.is_zero())
+               V.append(v0);
+       ex e1 = canonicalize_clifford(e - v0 * dirac_ONE(ex_to<clifford>(c).get_representation_label())); 
        if (algebraic) {
                for (unsigned int i = 0; i < D; i++) 
                        V.append(remove_dirac_ONE(
-                                               simplify_indexed(canonicalize_clifford(e * c.subs(mu == i, subs_options::no_pattern) +  c.subs(mu == i, subs_options::no_pattern) * e))
+                                               simplify_indexed(canonicalize_clifford(e1 * c.subs(mu == i, subs_options::no_pattern) +  c.subs(mu == i, subs_options::no_pattern) * e1))
                                                / (2*pow(c.subs(mu == i, subs_options::no_pattern), 2))));
        } else {
-               ex e1 = canonicalize_clifford(e);
                try {
                        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(e1, true));
+                       e1 = canonicalize_clifford(expand_dummy_sum(e, true));
+                       V.remove_all();
+                       v0 = remove_dirac_ONE(canonicalize_clifford(e1+clifford_prime(e1)).normal())/2;
+                       if (! v0.is_zero()) {
+                               V.append(v0);
+                               e1 = canonicalize_clifford(e1 - v0 * dirac_ONE(ex_to<clifford>(c).get_representation_label())); 
+                       }
                        for (unsigned int i = 0; i < D; i++) 
                                V.append(get_clifford_comp(e1, c.subs(c.op(1) == i, subs_options::no_pattern)));
                }
@@ -1292,14 +1334,16 @@ ex clifford_moebius_map(const ex & a, const ex & b, const ex & c, const ex & d,
        if (is_a<clifford>(G)) {
                cu = G;
        } else {
-               if (is_a<indexed>(G)) 
-                       D = ex_to<varidx>(G.op(1)).get_dim();
-               else if (is_a<matrix>(G)) 
+               if (is_a<indexed>(G)) {
+                       D = ex_to<idx>(G.op(1)).get_dim();
+                       varidx mu((new symbol)->setflag(status_flags::dynallocated), D);
+                       cu = clifford_unit(mu, G, rl);
+               } else if (is_a<matrix>(G)) {
                        D = ex_to<matrix>(G).rows(); 
-               else throw(std::invalid_argument("clifford_moebius_map(): metric should be an indexed object, matrix, or a Clifford unit"));
+                       idx mu((new symbol)->setflag(status_flags::dynallocated), 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"));
                
-               varidx mu((new symbol)->setflag(status_flags::dynallocated), D);
-               cu = clifford_unit(mu, G, rl);
        }
        
        x = lst_to_clifford(v, cu); 
@@ -1309,11 +1353,10 @@ ex clifford_moebius_map(const ex & a, const ex & b, const ex & c, const ex & d,
 
 ex clifford_moebius_map(const ex & M, const ex & v, const ex & G, unsigned char rl)
 {
-       if (is_a<matrix>(M)) 
-               return clifford_moebius_map(ex_to<matrix>(M)(0,0), ex_to<matrix>(M)(0,1),
-                                           ex_to<matrix>(M)(1,0), ex_to<matrix>(M)(1,1), v, G, rl);
+       if (is_a<matrix>(M) && (ex_to<matrix>(M).rows() == 2) && (ex_to<matrix>(M).cols() == 2)) 
+               return clifford_moebius_map(M.op(0), M.op(1), M.op(2), M.op(3), v, G, rl);
        else
-               throw(std::invalid_argument("clifford_moebius_map(): parameter M should be a matrix"));
+               throw(std::invalid_argument("clifford_moebius_map(): parameter M should be a 2x2 matrix"));
 }
 
 } // namespace GiNaC