3 * Implementation of GiNaC's color (SU(3) Lie algebra) objects. */
6 * GiNaC Copyright (C) 1999-2002 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
30 #include "operators.h"
33 #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 ctor, dtor, copy ctor, assignment operator and helpers
51 color::color() : representation_label(0)
53 tinfo_key = TINFO_color;
56 void color::copy(const color & other)
58 inherited::copy(other);
59 representation_label = other.representation_label;
62 DEFAULT_DESTROY(color)
72 /** Construct object without any color index. This constructor is for
73 * internal use only. Use the color_ONE() function instead.
75 color::color(const ex & b, unsigned char rl) : inherited(b), representation_label(rl)
77 tinfo_key = TINFO_color;
80 /** Construct object with one color index. This constructor is for internal
81 * use only. Use the color_T() function instead.
83 color::color(const ex & b, const ex & i1, unsigned char rl) : inherited(b, i1), representation_label(rl)
85 tinfo_key = TINFO_color;
88 color::color(unsigned char rl, const exvector & v, bool discardable) : inherited(sy_none(), v, discardable), representation_label(rl)
90 tinfo_key = TINFO_color;
93 color::color(unsigned char rl, exvector * vp) : inherited(sy_none(), vp), representation_label(rl)
95 tinfo_key = TINFO_color;
102 color::color(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
105 n.find_unsigned("label", rl);
106 representation_label = rl;
109 void color::archive(archive_node &n) const
111 inherited::archive(n);
112 n.add_unsigned("label", representation_label);
115 DEFAULT_UNARCHIVE(color)
116 DEFAULT_ARCHIVING(su3one)
117 DEFAULT_ARCHIVING(su3t)
118 DEFAULT_ARCHIVING(su3f)
119 DEFAULT_ARCHIVING(su3d)
122 // functions overriding virtual functions from base classes
125 int color::compare_same_type(const basic & other) const
127 GINAC_ASSERT(is_a<color>(other));
128 const color &o = static_cast<const color &>(other);
130 if (representation_label != o.representation_label) {
131 // different representation label
132 return representation_label < o.representation_label ? -1 : 1;
135 return inherited::compare_same_type(other);
138 bool color::match_same_type(const basic & other) const
140 GINAC_ASSERT(is_a<color>(other));
141 const color &o = static_cast<const color &>(other);
143 return representation_label == o.representation_label;
146 DEFAULT_COMPARE(su3one)
147 DEFAULT_COMPARE(su3t)
148 DEFAULT_COMPARE(su3f)
149 DEFAULT_COMPARE(su3d)
151 DEFAULT_PRINT_LATEX(su3one, "ONE", "\\mathbb{1}")
152 DEFAULT_PRINT(su3t, "T")
153 DEFAULT_PRINT(su3f, "f")
154 DEFAULT_PRINT(su3d, "d")
156 /** Perform automatic simplification on noncommutative product of color
157 * objects. This removes superfluous ONEs. */
158 ex color::simplify_ncmul(const exvector & v) const
163 // Remove superfluous ONEs
164 exvector::const_iterator it = v.begin(), itend = v.end();
165 while (it != itend) {
166 if (!is_a<su3one>(it->op(0)))
172 return color(su3one(), representation_label);
174 return simplified_ncmul(s);
177 ex color::thisexprseq(const exvector & v) const
179 return color(representation_label, v);
182 ex color::thisexprseq(exvector * vp) const
184 return color(representation_label, vp);
187 /** Given a vector iv3 of three indices and a vector iv2 of two indices that
188 * is a subset of iv3, return the (free) index that is in iv3 but not in
189 * iv2 and the sign introduced by permuting that index to the front.
191 * @param iv3 Vector of 3 indices
192 * @param iv2 Vector of 2 indices, must be a subset of iv3
193 * @param sig Returs sign introduced by index permutation
194 * @return the free index (the one that is in iv3 but not in iv2) */
195 static ex permute_free_index_to_front(const exvector & iv3, const exvector & iv2, int & sig)
197 GINAC_ASSERT(iv3.size() == 3);
198 GINAC_ASSERT(iv2.size() == 2);
202 #define TEST_PERMUTATION(A,B,C,P) \
203 if (iv3[B].is_equal(iv2[0]) && iv3[C].is_equal(iv2[1])) { \
208 TEST_PERMUTATION(0,1,2, 1);
209 TEST_PERMUTATION(0,2,1, -1);
210 TEST_PERMUTATION(1,0,2, -1);
211 TEST_PERMUTATION(1,2,0, 1);
212 TEST_PERMUTATION(2,0,1, 1);
213 TEST_PERMUTATION(2,1,0, -1);
215 throw(std::logic_error("permute_free_index_to_front(): no valid permutation found"));
218 /** Automatic symbolic evaluation of indexed symmetric structure constant. */
219 ex su3d::eval_indexed(const basic & i) const
221 GINAC_ASSERT(is_a<indexed>(i));
222 GINAC_ASSERT(i.nops() == 4);
223 GINAC_ASSERT(is_a<su3d>(i.op(0)));
225 // Convolutions are zero
226 if (!(static_cast<const indexed &>(i).get_dummy_indices().empty()))
229 // Numeric evaluation
230 if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
234 for (unsigned j=0; j<3; j++)
235 v[j] = ex_to<numeric>(ex_to<idx>(i.op(j + 1)).get_value()).to_int();
236 if (v[0] > v[1]) std::swap(v[0], v[1]);
237 if (v[0] > v[2]) std::swap(v[0], v[2]);
238 if (v[1] > v[2]) std::swap(v[1], v[2]);
240 #define CMPINDICES(A,B,C) ((v[0] == (A)) && (v[1] == (B)) && (v[2] == (C)))
242 // Check for non-zero elements
243 if (CMPINDICES(1,4,6) || CMPINDICES(1,5,7) || CMPINDICES(2,5,6)
244 || CMPINDICES(3,4,4) || CMPINDICES(3,5,5))
246 else if (CMPINDICES(2,4,7) || CMPINDICES(3,6,6) || CMPINDICES(3,7,7))
248 else if (CMPINDICES(1,1,8) || CMPINDICES(2,2,8) || CMPINDICES(3,3,8))
249 return sqrt(_ex3)*_ex1_3;
250 else if (CMPINDICES(8,8,8))
251 return sqrt(_ex3)*_ex_1_3;
252 else if (CMPINDICES(4,4,8) || CMPINDICES(5,5,8)
253 || CMPINDICES(6,6,8) || CMPINDICES(7,7,8))
254 return sqrt(_ex3)/_ex_6;
259 // No further simplifications
263 /** Automatic symbolic evaluation of indexed antisymmetric structure constant. */
264 ex su3f::eval_indexed(const basic & i) const
266 GINAC_ASSERT(is_a<indexed>(i));
267 GINAC_ASSERT(i.nops() == 4);
268 GINAC_ASSERT(is_a<su3f>(i.op(0)));
270 // Numeric evaluation
271 if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
273 // Sort indices, remember permutation sign
275 for (unsigned j=0; j<3; j++)
276 v[j] = ex_to<numeric>(ex_to<idx>(i.op(j + 1)).get_value()).to_int();
278 if (v[0] > v[1]) { std::swap(v[0], v[1]); sign = -sign; }
279 if (v[0] > v[2]) { std::swap(v[0], v[2]); sign = -sign; }
280 if (v[1] > v[2]) { std::swap(v[1], v[2]); sign = -sign; }
282 // Check for non-zero elements
283 if (CMPINDICES(1,2,3))
285 else if (CMPINDICES(1,4,7) || CMPINDICES(2,4,6)
286 || CMPINDICES(2,5,7) || CMPINDICES(3,4,5))
287 return _ex1_2 * sign;
288 else if (CMPINDICES(1,5,6) || CMPINDICES(3,6,7))
289 return _ex_1_2 * sign;
290 else if (CMPINDICES(4,5,8) || CMPINDICES(6,7,8))
291 return sqrt(_ex3)/2 * sign;
296 // No further simplifications
301 /** Contraction of generator with something else. */
302 bool su3t::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
304 GINAC_ASSERT(is_a<indexed>(*self));
305 GINAC_ASSERT(is_a<indexed>(*other));
306 GINAC_ASSERT(self->nops() == 2);
307 GINAC_ASSERT(is_a<su3t>(self->op(0)));
308 unsigned char rl = ex_to<color>(*self).get_representation_label();
310 if (is_exactly_a<su3t>(other->op(0))) {
312 // Contraction only makes sense if the represenation labels are equal
313 GINAC_ASSERT(is_a<color>(*other));
314 if (ex_to<color>(*other).get_representation_label() != rl)
318 if (other - self == 1) {
319 *self = numeric(4, 3);
320 *other = color_ONE(rl);
323 // T.a T.b T.a = -1/6 T.b
324 } else if (other - self == 2
325 && is_a<color>(self[1])) {
326 *self = numeric(-1, 6);
330 // T.a S T.a = 1/2 Tr(S) - 1/6 S
332 exvector::iterator it = self + 1;
333 while (it != other) {
334 if (!is_a<color>(*it)) {
342 while (it != other) {
347 *self = color_trace(S, rl) * color_ONE(rl) / 2 - S / 6;
356 /** Contraction of an indexed symmetric structure constant with something else. */
357 bool su3d::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
359 GINAC_ASSERT(is_a<indexed>(*self));
360 GINAC_ASSERT(is_a<indexed>(*other));
361 GINAC_ASSERT(self->nops() == 4);
362 GINAC_ASSERT(is_a<su3d>(self->op(0)));
364 if (is_exactly_a<su3d>(other->op(0))) {
366 // Find the dummy indices of the contraction
367 exvector self_indices = ex_to<indexed>(*self).get_indices();
368 exvector other_indices = ex_to<indexed>(*other).get_indices();
369 exvector all_indices = self_indices;
370 all_indices.insert(all_indices.end(), other_indices.begin(), other_indices.end());
371 exvector free_indices, dummy_indices;
372 find_free_and_dummy(all_indices, free_indices, dummy_indices);
374 // d.abc d.abc = 40/3
375 if (dummy_indices.size() == 3) {
376 *self = numeric(40, 3);
380 // d.akl d.bkl = 5/3 delta.ab
381 } else if (dummy_indices.size() == 2) {
383 std::back_insert_iterator<exvector> ita(a);
384 ita = set_difference(self_indices.begin(), self_indices.end(), dummy_indices.begin(), dummy_indices.end(), ita, ex_is_less());
385 ita = set_difference(other_indices.begin(), other_indices.end(), dummy_indices.begin(), dummy_indices.end(), ita, ex_is_less());
386 GINAC_ASSERT(a.size() == 2);
387 *self = numeric(5, 3) * delta_tensor(a[0], a[1]);
392 } else if (is_exactly_a<su3t>(other->op(0))) {
394 // d.abc T.b T.c = 5/6 T.a
395 if (other+1 != v.end()
396 && is_exactly_a<su3t>(other[1].op(0))
397 && ex_to<indexed>(*self).has_dummy_index_for(other[1].op(1))) {
399 exvector self_indices = ex_to<indexed>(*self).get_indices();
400 exvector dummy_indices;
401 dummy_indices.push_back(other[0].op(1));
402 dummy_indices.push_back(other[1].op(1));
404 ex a = permute_free_index_to_front(self_indices, dummy_indices, sig);
405 *self = numeric(5, 6);
406 other[0] = color_T(a, ex_to<color>(other[0]).get_representation_label());
415 /** Contraction of an indexed antisymmetric structure constant with something else. */
416 bool su3f::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
418 GINAC_ASSERT(is_a<indexed>(*self));
419 GINAC_ASSERT(is_a<indexed>(*other));
420 GINAC_ASSERT(self->nops() == 4);
421 GINAC_ASSERT(is_a<su3f>(self->op(0)));
423 if (is_exactly_a<su3f>(other->op(0))) { // f*d is handled by su3d class
425 // Find the dummy indices of the contraction
426 exvector dummy_indices;
427 dummy_indices = ex_to<indexed>(*self).get_dummy_indices(ex_to<indexed>(*other));
430 if (dummy_indices.size() == 3) {
435 // f.akl f.bkl = 3 delta.ab
436 } else if (dummy_indices.size() == 2) {
438 ex a = permute_free_index_to_front(ex_to<indexed>(*self).get_indices(), dummy_indices, sign1);
439 ex b = permute_free_index_to_front(ex_to<indexed>(*other).get_indices(), dummy_indices, sign2);
440 *self = sign1 * sign2 * 3 * delta_tensor(a, b);
445 } else if (is_exactly_a<su3t>(other->op(0))) {
447 // f.abc T.b T.c = 3/2 I T.a
448 if (other+1 != v.end()
449 && is_exactly_a<su3t>(other[1].op(0))
450 && ex_to<indexed>(*self).has_dummy_index_for(other[1].op(1))) {
452 exvector self_indices = ex_to<indexed>(*self).get_indices();
453 exvector dummy_indices;
454 dummy_indices.push_back(other[0].op(1));
455 dummy_indices.push_back(other[1].op(1));
457 ex a = permute_free_index_to_front(self_indices, dummy_indices, sig);
458 *self = numeric(3, 2) * sig * I;
459 other[0] = color_T(a, ex_to<color>(other[0]).get_representation_label());
472 ex color_ONE(unsigned char rl)
474 return color(su3one(), rl);
477 ex color_T(const ex & a, unsigned char rl)
480 throw(std::invalid_argument("indices of color_T must be of type idx"));
481 if (!ex_to<idx>(a).get_dim().is_equal(8))
482 throw(std::invalid_argument("index dimension for color_T must be 8"));
484 return color(su3t(), a, rl);
487 ex color_f(const ex & a, const ex & b, const ex & c)
489 if (!is_a<idx>(a) || !is_a<idx>(b) || !is_a<idx>(c))
490 throw(std::invalid_argument("indices of color_f must be of type idx"));
491 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))
492 throw(std::invalid_argument("index dimension for color_f must be 8"));
494 return indexed(su3f(), sy_anti(), a, b, c);
497 ex color_d(const ex & a, const ex & b, const ex & c)
499 if (!is_a<idx>(a) || !is_a<idx>(b) || !is_a<idx>(c))
500 throw(std::invalid_argument("indices of color_d must be of type idx"));
501 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))
502 throw(std::invalid_argument("index dimension for color_d must be 8"));
504 return indexed(su3d(), sy_symm(), a, b, c);
507 ex color_h(const ex & a, const ex & b, const ex & c)
509 return color_d(a, b, c) + I * color_f(a, b, c);
512 /** Check whether a given tinfo key (as returned by return_type_tinfo()
513 * is that of a color object with the specified representation label. */
514 static bool is_color_tinfo(unsigned ti, unsigned char rl)
516 return ti == (TINFO_color + rl);
519 ex color_trace(const ex & e, unsigned char rl)
521 if (is_a<color>(e)) {
523 if (ex_to<color>(e).get_representation_label() == rl
524 && is_a<su3one>(e.op(0)))
529 } else if (is_exactly_a<mul>(e)) {
531 // Trace of product: pull out non-color factors
533 for (unsigned i=0; i<e.nops(); i++) {
534 const ex &o = e.op(i);
535 if (is_color_tinfo(o.return_type_tinfo(), rl))
536 prod *= color_trace(o, rl);
542 } else if (is_exactly_a<ncmul>(e)) {
544 if (!is_color_tinfo(e.return_type_tinfo(), rl))
547 // Expand product, if necessary
548 ex e_expanded = e.expand();
549 if (!is_a<ncmul>(e_expanded))
550 return color_trace(e_expanded, rl);
552 unsigned num = e.nops();
556 // Tr T_a T_b = 1/2 delta_a_b
557 return delta_tensor(e.op(0).op(1), e.op(1).op(1)) / 2;
559 } else if (num == 3) {
561 // Tr T_a T_b T_c = 1/4 h_a_b_c
562 return color_h(e.op(0).op(1), e.op(1).op(1), e.op(2).op(1)) / 4;
566 // Traces of 4 or more generators are computed recursively:
568 // 1/6 delta_a(n-1)_an Tr T_a1 .. T_a(n-2)
569 // + 1/2 h_a(n-1)_an_k Tr T_a1 .. T_a(n-2) T_k
570 const ex &last_index = e.op(num - 1).op(1);
571 const ex &next_to_last_index = e.op(num - 2).op(1);
572 idx summation_index((new symbol)->setflag(status_flags::dynallocated), 8);
576 for (unsigned i=0; i<num-2; i++)
577 v1.push_back(e.op(i));
580 v2.push_back(color_T(summation_index, rl));
582 return delta_tensor(next_to_last_index, last_index) * color_trace(ncmul(v1), rl) / 6
583 + color_h(next_to_last_index, last_index, summation_index) * color_trace(ncmul(v2), rl) / 2;
586 } else if (e.nops() > 0) {
588 // Trace maps to all other container classes (this includes sums)
589 pointer_to_map_function_1arg<unsigned char> fcn(color_trace, rl);