3 * Implementation of GiNaC's color (SU(3) Lie algebra) objects. */
6 * GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include "power.h" // for sqrt()
41 GINAC_IMPLEMENT_REGISTERED_CLASS(color, indexed)
42 GINAC_IMPLEMENT_REGISTERED_CLASS(su3one, tensor)
43 GINAC_IMPLEMENT_REGISTERED_CLASS(su3t, tensor)
44 GINAC_IMPLEMENT_REGISTERED_CLASS(su3f, tensor)
45 GINAC_IMPLEMENT_REGISTERED_CLASS(su3d, tensor)
48 // default constructor, destructor, copy constructor assignment operator and helpers
51 color::color() : representation_label(0)
53 debugmsg("color default constructor", LOGLEVEL_CONSTRUCT);
54 tinfo_key = TINFO_color;
57 void color::copy(const color & other)
59 inherited::copy(other);
60 representation_label = other.representation_label;
63 DEFAULT_DESTROY(color)
73 /** Construct object without any color index. This constructor is for
74 * internal use only. Use the color_ONE() function instead.
76 color::color(const ex & b, unsigned char rl) : inherited(b), representation_label(rl)
78 debugmsg("color constructor from ex,unsigned char", LOGLEVEL_CONSTRUCT);
79 tinfo_key = TINFO_color;
82 /** Construct object with one color index. This constructor is for internal
83 * use only. Use the color_T() function instead.
85 color::color(const ex & b, const ex & i1, unsigned char rl) : inherited(b, i1), representation_label(rl)
87 debugmsg("color constructor from ex,ex,unsigned char", LOGLEVEL_CONSTRUCT);
88 tinfo_key = TINFO_color;
91 color::color(unsigned char rl, const exvector & v, bool discardable) : inherited(sy_none(), v, discardable), representation_label(rl)
93 debugmsg("color constructor from unsigned char,exvector", LOGLEVEL_CONSTRUCT);
94 tinfo_key = TINFO_color;
97 color::color(unsigned char rl, exvector * vp) : inherited(sy_none(), vp), representation_label(rl)
99 debugmsg("color constructor from unsigned char,exvector *", LOGLEVEL_CONSTRUCT);
100 tinfo_key = TINFO_color;
107 color::color(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
109 debugmsg("color constructor from archive_node", LOGLEVEL_CONSTRUCT);
111 n.find_unsigned("label", rl);
112 representation_label = rl;
115 void color::archive(archive_node &n) const
117 inherited::archive(n);
118 n.add_unsigned("label", representation_label);
121 DEFAULT_UNARCHIVE(color)
122 DEFAULT_ARCHIVING(su3one)
123 DEFAULT_ARCHIVING(su3t)
124 DEFAULT_ARCHIVING(su3f)
125 DEFAULT_ARCHIVING(su3d)
128 // functions overriding virtual functions from base classes
131 int color::compare_same_type(const basic & other) const
133 GINAC_ASSERT(is_a<color>(other));
134 const color &o = static_cast<const color &>(other);
136 if (representation_label != o.representation_label) {
137 // different representation label
138 return representation_label < o.representation_label ? -1 : 1;
141 return inherited::compare_same_type(other);
144 bool color::match_same_type(const basic & other) const
146 GINAC_ASSERT(is_a<color>(other));
147 const color &o = static_cast<const color &>(other);
149 return representation_label == o.representation_label;
152 DEFAULT_COMPARE(su3one)
153 DEFAULT_COMPARE(su3t)
154 DEFAULT_COMPARE(su3f)
155 DEFAULT_COMPARE(su3d)
157 DEFAULT_PRINT_LATEX(su3one, "ONE", "\\mathbb{1}")
158 DEFAULT_PRINT(su3t, "T")
159 DEFAULT_PRINT(su3f, "f")
160 DEFAULT_PRINT(su3d, "d")
162 /** Perform automatic simplification on noncommutative product of color
163 * objects. This removes superfluous ONEs. */
164 ex color::simplify_ncmul(const exvector & v) const
169 // Remove superfluous ONEs
170 exvector::const_iterator it = v.begin(), itend = v.end();
171 while (it != itend) {
172 if (!is_ex_of_type(it->op(0), su3one))
178 return color(su3one(), representation_label);
180 return simplified_ncmul(s);
183 ex color::thisexprseq(const exvector & v) const
185 return color(representation_label, v);
188 ex color::thisexprseq(exvector * vp) const
190 return color(representation_label, vp);
193 /** Given a vector iv3 of three indices and a vector iv2 of two indices that
194 * is a subset of iv3, return the (free) index that is in iv3 but not in
195 * iv2 and the sign introduced by permuting that index to the front.
197 * @param iv3 Vector of 3 indices
198 * @param iv2 Vector of 2 indices, must be a subset of iv3
199 * @param sig Returs sign introduced by index permutation
200 * @return the free index (the one that is in iv3 but not in iv2) */
201 static ex permute_free_index_to_front(const exvector & iv3, const exvector & iv2, int & sig)
203 GINAC_ASSERT(iv3.size() == 3);
204 GINAC_ASSERT(iv2.size() == 2);
208 #define TEST_PERMUTATION(A,B,C,P) \
209 if (iv3[B].is_equal(iv2[0]) && iv3[C].is_equal(iv2[1])) { \
214 TEST_PERMUTATION(0,1,2, 1);
215 TEST_PERMUTATION(0,2,1, -1);
216 TEST_PERMUTATION(1,0,2, -1);
217 TEST_PERMUTATION(1,2,0, 1);
218 TEST_PERMUTATION(2,0,1, 1);
219 TEST_PERMUTATION(2,1,0, -1);
221 throw(std::logic_error("permute_free_index_to_front(): no valid permutation found"));
224 /** Automatic symbolic evaluation of indexed symmetric structure constant. */
225 ex su3d::eval_indexed(const basic & i) const
227 GINAC_ASSERT(is_a<indexed>(i));
228 GINAC_ASSERT(i.nops() == 4);
229 GINAC_ASSERT(is_a<su3d>(i.op(0)));
231 // Convolutions are zero
232 if (!(static_cast<const indexed &>(i).get_dummy_indices().empty()))
235 // Numeric evaluation
236 if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
240 for (unsigned j=0; j<3; j++)
241 v[j] = ex_to<numeric>(ex_to<idx>(i.op(j + 1)).get_value()).to_int();
242 if (v[0] > v[1]) std::swap(v[0], v[1]);
243 if (v[0] > v[2]) std::swap(v[0], v[2]);
244 if (v[1] > v[2]) std::swap(v[1], v[2]);
246 #define CMPINDICES(A,B,C) ((v[0] == (A)) && (v[1] == (B)) && (v[2] == (C)))
248 // Check for non-zero elements
249 if (CMPINDICES(1,4,6) || CMPINDICES(1,5,7) || CMPINDICES(2,5,6)
250 || CMPINDICES(3,4,4) || CMPINDICES(3,5,5))
252 else if (CMPINDICES(2,4,7) || CMPINDICES(3,6,6) || CMPINDICES(3,7,7))
254 else if (CMPINDICES(1,1,8) || CMPINDICES(2,2,8) || CMPINDICES(3,3,8))
255 return sqrt(_ex3)*_ex1_3;
256 else if (CMPINDICES(8,8,8))
257 return sqrt(_ex3)*_ex_1_3;
258 else if (CMPINDICES(4,4,8) || CMPINDICES(5,5,8)
259 || CMPINDICES(6,6,8) || CMPINDICES(7,7,8))
260 return sqrt(_ex3)/_ex_6;
265 // No further simplifications
269 /** Automatic symbolic evaluation of indexed antisymmetric structure constant. */
270 ex su3f::eval_indexed(const basic & i) const
272 GINAC_ASSERT(is_a<indexed>(i));
273 GINAC_ASSERT(i.nops() == 4);
274 GINAC_ASSERT(is_a<su3f>(i.op(0)));
276 // Numeric evaluation
277 if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
279 // Sort indices, remember permutation sign
281 for (unsigned j=0; j<3; j++)
282 v[j] = ex_to<numeric>(ex_to<idx>(i.op(j + 1)).get_value()).to_int();
284 if (v[0] > v[1]) { std::swap(v[0], v[1]); sign = -sign; }
285 if (v[0] > v[2]) { std::swap(v[0], v[2]); sign = -sign; }
286 if (v[1] > v[2]) { std::swap(v[1], v[2]); sign = -sign; }
288 // Check for non-zero elements
289 if (CMPINDICES(1,2,3))
291 else if (CMPINDICES(1,4,7) || CMPINDICES(2,4,6)
292 || CMPINDICES(2,5,7) || CMPINDICES(3,4,5))
293 return _ex1_2 * sign;
294 else if (CMPINDICES(1,5,6) || CMPINDICES(3,6,7))
295 return _ex_1_2 * sign;
296 else if (CMPINDICES(4,5,8) || CMPINDICES(6,7,8))
297 return sqrt(_ex3)/2 * sign;
302 // No further simplifications
307 /** Contraction of generator with something else. */
308 bool su3t::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
310 GINAC_ASSERT(is_a<indexed>(*self));
311 GINAC_ASSERT(is_a<indexed>(*other));
312 GINAC_ASSERT(self->nops() == 2);
313 GINAC_ASSERT(is_a<su3t>(self->op(0)));
314 unsigned char rl = ex_to<color>(*self).get_representation_label();
316 if (is_ex_exactly_of_type(other->op(0), su3t)) {
319 if (other - self == 1) {
320 *self = numeric(4, 3);
321 *other = color_ONE(rl);
324 // T.a T.b T.a = -1/6 T.b
325 } else if (other - self == 2
326 && is_ex_of_type(self[1], color)) {
327 *self = numeric(-1, 6);
331 // T.a S T.a = 1/2 Tr(S) - 1/6 S
333 exvector::iterator it = self + 1;
334 while (it != other) {
335 if (!is_ex_of_type(*it, color)) {
343 while (it != other) {
348 *self = color_trace(S, rl) * color_ONE(rl) / 2 - S / 6;
357 /** Contraction of an indexed symmetric structure constant with something else. */
358 bool su3d::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
360 GINAC_ASSERT(is_a<indexed>(*self));
361 GINAC_ASSERT(is_a<indexed>(*other));
362 GINAC_ASSERT(self->nops() == 4);
363 GINAC_ASSERT(is_a<su3d>(self->op(0)));
365 if (is_ex_exactly_of_type(other->op(0), su3d)) {
367 // Find the dummy indices of the contraction
368 exvector self_indices = ex_to<indexed>(*self).get_indices();
369 exvector other_indices = ex_to<indexed>(*other).get_indices();
370 exvector all_indices = self_indices;
371 all_indices.insert(all_indices.end(), other_indices.begin(), other_indices.end());
372 exvector free_indices, dummy_indices;
373 find_free_and_dummy(all_indices, free_indices, dummy_indices);
375 // d.abc d.abc = 40/3
376 if (dummy_indices.size() == 3) {
377 *self = numeric(40, 3);
381 // d.akl d.bkl = 5/3 delta.ab
382 } else if (dummy_indices.size() == 2) {
384 std::back_insert_iterator<exvector> ita(a);
385 ita = set_difference(self_indices.begin(), self_indices.end(), dummy_indices.begin(), dummy_indices.end(), ita, ex_is_less());
386 ita = set_difference(other_indices.begin(), other_indices.end(), dummy_indices.begin(), dummy_indices.end(), ita, ex_is_less());
387 GINAC_ASSERT(a.size() == 2);
388 *self = numeric(5, 3) * delta_tensor(a[0], a[1]);
393 } else if (is_ex_exactly_of_type(other->op(0), su3t)) {
395 // d.abc T.b T.c = 5/6 T.a
396 if (other+1 != v.end()
397 && is_ex_exactly_of_type(other[1].op(0), su3t)
398 && ex_to<indexed>(*self).has_dummy_index_for(other[1].op(1))) {
400 exvector self_indices = ex_to<indexed>(*self).get_indices();
401 exvector dummy_indices;
402 dummy_indices.push_back(other[0].op(1));
403 dummy_indices.push_back(other[1].op(1));
405 ex a = permute_free_index_to_front(self_indices, dummy_indices, sig);
406 *self = numeric(5, 6);
407 other[0] = color_T(a, ex_to<color>(other[0]).get_representation_label());
416 /** Contraction of an indexed antisymmetric structure constant with something else. */
417 bool su3f::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
419 GINAC_ASSERT(is_a<indexed>(*self));
420 GINAC_ASSERT(is_a<indexed>(*other));
421 GINAC_ASSERT(self->nops() == 4);
422 GINAC_ASSERT(is_a<su3f>(self->op(0)));
424 if (is_ex_exactly_of_type(other->op(0), su3f)) { // f*d is handled by su3d class
426 // Find the dummy indices of the contraction
427 exvector dummy_indices;
428 dummy_indices = ex_to<indexed>(*self).get_dummy_indices(ex_to<indexed>(*other));
431 if (dummy_indices.size() == 3) {
436 // f.akl f.bkl = 3 delta.ab
437 } else if (dummy_indices.size() == 2) {
439 ex a = permute_free_index_to_front(ex_to<indexed>(*self).get_indices(), dummy_indices, sign1);
440 ex b = permute_free_index_to_front(ex_to<indexed>(*other).get_indices(), dummy_indices, sign2);
441 *self = sign1 * sign2 * 3 * delta_tensor(a, b);
446 } else if (is_ex_exactly_of_type(other->op(0), su3t)) {
448 // f.abc T.b T.c = 3/2 I T.a
449 if (other+1 != v.end()
450 && is_ex_exactly_of_type(other[1].op(0), su3t)
451 && ex_to<indexed>(*self).has_dummy_index_for(other[1].op(1))) {
453 exvector self_indices = ex_to<indexed>(*self).get_indices();
454 exvector dummy_indices;
455 dummy_indices.push_back(other[0].op(1));
456 dummy_indices.push_back(other[1].op(1));
458 ex a = permute_free_index_to_front(self_indices, dummy_indices, sig);
459 *self = numeric(3, 2) * sig * I;
460 other[0] = color_T(a, ex_to<color>(other[0]).get_representation_label());
473 ex color_ONE(unsigned char rl)
475 return color(su3one(), rl);
478 ex color_T(const ex & a, unsigned char rl)
480 if (!is_ex_of_type(a, idx))
481 throw(std::invalid_argument("indices of color_T must be of type idx"));
482 if (!ex_to<idx>(a).get_dim().is_equal(8))
483 throw(std::invalid_argument("index dimension for color_T must be 8"));
485 return color(su3t(), a, rl);
488 ex color_f(const ex & a, const ex & b, const ex & c)
490 if (!is_ex_of_type(a, idx) || !is_ex_of_type(b, idx) || !is_ex_of_type(c, idx))
491 throw(std::invalid_argument("indices of color_f must be of type idx"));
492 if (!ex_to<idx>(a).get_dim().is_equal(8) || !ex_to<idx>(b).get_dim().is_equal(8) || !ex_to<idx>(c).get_dim().is_equal(8))
493 throw(std::invalid_argument("index dimension for color_f must be 8"));
495 return indexed(su3f(), sy_anti(), a, b, c);
498 ex color_d(const ex & a, const ex & b, const ex & c)
500 if (!is_ex_of_type(a, idx) || !is_ex_of_type(b, idx) || !is_ex_of_type(c, idx))
501 throw(std::invalid_argument("indices of color_d must be of type idx"));
502 if (!ex_to<idx>(a).get_dim().is_equal(8) || !ex_to<idx>(b).get_dim().is_equal(8) || !ex_to<idx>(c).get_dim().is_equal(8))
503 throw(std::invalid_argument("index dimension for color_d must be 8"));
505 return indexed(su3d(), sy_symm(), a, b, c);
508 ex color_h(const ex & a, const ex & b, const ex & c)
510 return color_d(a, b, c) + I * color_f(a, b, c);
513 /** Check whether a given tinfo key (as returned by return_type_tinfo()
514 * is that of a color object with the specified representation label. */
515 static bool is_color_tinfo(unsigned ti, unsigned char rl)
517 return ti == (TINFO_color + rl);
520 ex color_trace(const ex & e, unsigned char rl)
522 if (is_ex_of_type(e, color)) {
524 if (ex_to<color>(e).get_representation_label() == rl
525 && is_ex_of_type(e.op(0), su3one))
530 } else if (is_ex_exactly_of_type(e, mul)) {
532 // Trace of product: pull out non-color factors
534 for (unsigned i=0; i<e.nops(); i++) {
535 const ex &o = e.op(i);
536 if (is_color_tinfo(o.return_type_tinfo(), rl))
537 prod *= color_trace(o, rl);
543 } else if (is_ex_exactly_of_type(e, ncmul)) {
545 if (!is_color_tinfo(e.return_type_tinfo(), rl))
548 // Expand product, if necessary
549 ex e_expanded = e.expand();
550 if (!is_ex_of_type(e_expanded, ncmul))
551 return color_trace(e_expanded, rl);
553 unsigned num = e.nops();
557 // Tr T_a T_b = 1/2 delta_a_b
558 return delta_tensor(e.op(0).op(1), e.op(1).op(1)) / 2;
560 } else if (num == 3) {
562 // Tr T_a T_b T_c = 1/4 h_a_b_c
563 return color_h(e.op(0).op(1), e.op(1).op(1), e.op(2).op(1)) / 4;
567 // Traces of 4 or more generators are computed recursively:
569 // 1/6 delta_a(n-1)_an Tr T_a1 .. T_a(n-2)
570 // + 1/2 h_a(n-1)_an_k Tr T_a1 .. T_a(n-2) T_k
571 const ex &last_index = e.op(num - 1).op(1);
572 const ex &next_to_last_index = e.op(num - 2).op(1);
573 idx summation_index((new symbol)->setflag(status_flags::dynallocated), 8);
577 for (unsigned i=0; i<num-2; i++)
578 v1.push_back(e.op(i));
581 v2.push_back(color_T(summation_index, rl));
583 return delta_tensor(next_to_last_index, last_index) * color_trace(ncmul(v1), rl) / 6
584 + color_h(next_to_last_index, last_index, summation_index) * color_trace(ncmul(v2), rl) / 2;
587 } else if (e.nops() > 0) {
589 // Trace maps to all other container classes (this includes sums)
590 pointer_to_map_function_1arg<unsigned char> fcn(color_trace, rl);