]> www.ginac.de Git - ginac.git/blobdiff - ginac/color.cpp
- dirac_trace() handles arbitrary expressions (even unexpanded), including
[ginac.git] / ginac / color.cpp
index 4e7d6dd3038bdfb92226352617738f9bc61addc7..e36c1cbe9796effef77d085d228bb4f5be614894 100644 (file)
@@ -29,6 +29,7 @@
 #include "ncmul.h"
 #include "numeric.h"
 #include "power.h" // for sqrt()
+#include "symbol.h"
 #include "print.h"
 #include "archive.h"
 #include "debugmsg.h"
@@ -71,30 +72,30 @@ DEFAULT_CTORS(su3d)
 /** Construct object without any color index. This constructor is for
  *  internal use only. Use the color_ONE() function instead.
  *  @see color_ONE */
-color::color(const ex & b, unsigned rl) : inherited(b), representation_label(rl)
+color::color(const ex & b, unsigned char rl) : inherited(b), representation_label(rl)
 {
-       debugmsg("color constructor from ex,unsigned", LOGLEVEL_CONSTRUCT);
+       debugmsg("color constructor from ex,unsigned char", LOGLEVEL_CONSTRUCT);
        tinfo_key = TINFO_color;
 }
 
 /** Construct object with one color index. This constructor is for internal
  *  use only. Use the color_T() function instead.
  *  @see color_T */
-color::color(const ex & b, const ex & i1, unsigned rl) : inherited(b, i1), representation_label(rl)
+color::color(const ex & b, const ex & i1, unsigned char rl) : inherited(b, i1), representation_label(rl)
 {
-       debugmsg("color constructor from ex,ex,unsigned", LOGLEVEL_CONSTRUCT);
+       debugmsg("color constructor from ex,ex,unsigned char", LOGLEVEL_CONSTRUCT);
        tinfo_key = TINFO_color;
 }
 
-color::color(unsigned rl, const exvector & v, bool discardable) : inherited(indexed::unknown, v, discardable), representation_label(rl)
+color::color(unsigned char rl, const exvector & v, bool discardable) : inherited(indexed::unknown, v, discardable), representation_label(rl)
 {
-       debugmsg("color constructor from unsigned,exvector", LOGLEVEL_CONSTRUCT);
+       debugmsg("color constructor from unsigned char,exvector", LOGLEVEL_CONSTRUCT);
        tinfo_key = TINFO_color;
 }
 
-color::color(unsigned rl, exvector * vp) : inherited(indexed::unknown, vp), representation_label(rl)
+color::color(unsigned char rl, exvector * vp) : inherited(indexed::unknown, vp), representation_label(rl)
 {
-       debugmsg("color constructor from unsigned,exvector *", LOGLEVEL_CONSTRUCT);
+       debugmsg("color constructor from unsigned char,exvector *", LOGLEVEL_CONSTRUCT);
        tinfo_key = TINFO_color;
 }
 
@@ -105,13 +106,15 @@ color::color(unsigned rl, exvector * vp) : inherited(indexed::unknown, vp), repr
 color::color(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
 {
        debugmsg("color constructor from archive_node", LOGLEVEL_CONSTRUCT);
-       n.find_unsigned("representation", representation_label);
+       unsigned rl;
+       n.find_unsigned("label", rl);
+       representation_label = rl;
 }
 
 void color::archive(archive_node &n) const
 {
        inherited::archive(n);
-       n.add_unsigned("representation", representation_label);
+       n.add_unsigned("label", representation_label);
 }
 
 DEFAULT_UNARCHIVE(color)
@@ -367,12 +370,12 @@ bool su3f::contract_with(exvector::iterator self, exvector::iterator other, exve
 // global functions
 //////////
 
-ex color_ONE(unsigned rl)
+ex color_ONE(unsigned char rl)
 {
        return color(su3one(), rl);
 }
 
-ex color_T(const ex & a, unsigned rl)
+ex color_T(const ex & a, unsigned char rl)
 {
        if (!is_ex_of_type(a, idx))
                throw(std::invalid_argument("indices of color_T must be of type idx"));
@@ -407,4 +410,90 @@ ex color_h(const ex & a, const ex & b, const ex & c)
        return color_d(a, b, c) + I * color_f(a, b, c);
 }
 
+/** Check whether a given tinfo key (as returned by return_type_tinfo()
+ *  is that of a color object with the specified representation label. */
+static bool is_color_tinfo(unsigned ti, unsigned char rl)
+{
+       return ti == (TINFO_color + rl);
+}
+
+ex color_trace(const ex & e, unsigned char rl)
+{
+       if (is_ex_of_type(e, color)) {
+
+               if (ex_to_color(e).get_representation_label() == rl
+                && is_ex_of_type(e.op(0), su3one))
+                       return _ex3();
+               else
+                       return _ex0();
+
+       } else if (is_ex_exactly_of_type(e, add)) {
+
+               // Trace of sum = sum of traces
+               ex sum = _ex0();
+               for (unsigned i=0; i<e.nops(); i++)
+                       sum += color_trace(e.op(i), rl);
+               return sum;
+
+       } else if (is_ex_exactly_of_type(e, mul)) {
+
+               // Trace of product: pull out non-color factors
+               ex prod = _ex1();
+               for (unsigned i=0; i<e.nops(); i++) {
+                       const ex &o = e.op(i);
+                       if (is_color_tinfo(o.return_type_tinfo(), rl))
+                               prod *= color_trace(o, rl);
+                       else
+                               prod *= o;
+               }
+               return prod;
+
+       } else if (is_ex_exactly_of_type(e, ncmul)) {
+
+               if (!is_color_tinfo(e.return_type_tinfo(), rl))
+                       return _ex0();
+
+               // Expand product, if necessary
+               ex e_expanded = e.expand();
+               if (!is_ex_of_type(e_expanded, ncmul))
+                       return color_trace(e_expanded, rl);
+
+               unsigned num = e.nops();
+
+               if (num == 2) {
+
+                       // Tr T_a T_b = 1/2 delta_a_b
+                       return delta_tensor(e.op(0).op(1), e.op(1).op(1)) / 2;
+
+               } else if (num == 3) {
+
+                       // Tr T_a T_b T_c = 1/4 h_a_b_c
+                       return color_h(e.op(0).op(1), e.op(1).op(1), e.op(2).op(1)) / 4;
+
+               } else {
+
+                       // Traces of 4 or more generators are computed recursively:
+                       // Tr T_a1 .. T_an =
+                       //     1/6 delta_a(n-1)_an Tr T_a1 .. T_a(n-2)
+                       //   + 1/2 h_a(n-1)_an_k Tr T_a1 .. T_a(n-2) T_k
+                       const ex &last_index = e.op(num - 1).op(1);
+                       const ex &next_to_last_index = e.op(num - 2).op(1);
+                       idx summation_index((new symbol)->setflag(status_flags::dynallocated), 8);
+
+                       exvector v1;
+                       v1.reserve(num - 2);
+                       for (int i=0; i<num-2; i++)
+                               v1.push_back(e.op(i));
+
+                       exvector v2 = v1;
+                       v2.push_back(color_T(summation_index, rl));
+
+                       return delta_tensor(next_to_last_index, last_index) * color_trace(ncmul(v1), rl) / 6
+                              + color_h(next_to_last_index, last_index, summation_index) * color_trace(ncmul(v2), rl) / 2;
+               }
+       }
+
+       return _ex0();
+}
+
 } // namespace GiNaC