clifford and color objects are not contracted if their representation labels
[ginac.git] / ginac / color.cpp
1 /** @file color.cpp
2  *
3  *  Implementation of GiNaC's color (SU(3) Lie algebra) objects. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2002 Johannes Gutenberg University Mainz, Germany
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include <iostream>
24 #include <stdexcept>
25
26 #include "color.h"
27 #include "idx.h"
28 #include "ncmul.h"
29 #include "symmetry.h"
30 #include "numeric.h"
31 #include "mul.h"
32 #include "power.h" // for sqrt()
33 #include "symbol.h"
34 #include "print.h"
35 #include "archive.h"
36 #include "utils.h"
37
38 namespace GiNaC {
39
40 GINAC_IMPLEMENT_REGISTERED_CLASS(color, indexed)
41 GINAC_IMPLEMENT_REGISTERED_CLASS(su3one, tensor)
42 GINAC_IMPLEMENT_REGISTERED_CLASS(su3t, tensor)
43 GINAC_IMPLEMENT_REGISTERED_CLASS(su3f, tensor)
44 GINAC_IMPLEMENT_REGISTERED_CLASS(su3d, tensor)
45
46 //////////
47 // default ctor, dtor, copy ctor, assignment operator and helpers
48 //////////
49
50 color::color() : representation_label(0)
51 {
52         tinfo_key = TINFO_color;
53 }
54
55 void color::copy(const color & other)
56 {
57         inherited::copy(other);
58         representation_label = other.representation_label;
59 }
60
61 DEFAULT_DESTROY(color)
62 DEFAULT_CTORS(su3one)
63 DEFAULT_CTORS(su3t)
64 DEFAULT_CTORS(su3f)
65 DEFAULT_CTORS(su3d)
66
67 //////////
68 // other constructors
69 //////////
70
71 /** Construct object without any color index. This constructor is for
72  *  internal use only. Use the color_ONE() function instead.
73  *  @see color_ONE */
74 color::color(const ex & b, unsigned char rl) : inherited(b), representation_label(rl)
75 {
76         tinfo_key = TINFO_color;
77 }
78
79 /** Construct object with one color index. This constructor is for internal
80  *  use only. Use the color_T() function instead.
81  *  @see color_T */
82 color::color(const ex & b, const ex & i1, unsigned char rl) : inherited(b, i1), representation_label(rl)
83 {
84         tinfo_key = TINFO_color;
85 }
86
87 color::color(unsigned char rl, const exvector & v, bool discardable) : inherited(sy_none(), v, discardable), representation_label(rl)
88 {
89         tinfo_key = TINFO_color;
90 }
91
92 color::color(unsigned char rl, exvector * vp) : inherited(sy_none(), vp), representation_label(rl)
93 {
94         tinfo_key = TINFO_color;
95 }
96
97 //////////
98 // archiving
99 //////////
100
101 color::color(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
102 {
103         unsigned rl;
104         n.find_unsigned("label", rl);
105         representation_label = rl;
106 }
107
108 void color::archive(archive_node &n) const
109 {
110         inherited::archive(n);
111         n.add_unsigned("label", representation_label);
112 }
113
114 DEFAULT_UNARCHIVE(color)
115 DEFAULT_ARCHIVING(su3one)
116 DEFAULT_ARCHIVING(su3t)
117 DEFAULT_ARCHIVING(su3f)
118 DEFAULT_ARCHIVING(su3d)
119
120 //////////
121 // functions overriding virtual functions from base classes
122 //////////
123
124 int color::compare_same_type(const basic & other) const
125 {
126         GINAC_ASSERT(is_a<color>(other));
127         const color &o = static_cast<const color &>(other);
128
129         if (representation_label != o.representation_label) {
130                 // different representation label
131                 return representation_label < o.representation_label ? -1 : 1;
132         }
133
134         return inherited::compare_same_type(other);
135 }
136
137 bool color::match_same_type(const basic & other) const
138 {
139         GINAC_ASSERT(is_a<color>(other));
140         const color &o = static_cast<const color &>(other);
141
142         return representation_label == o.representation_label;
143 }
144
145 DEFAULT_COMPARE(su3one)
146 DEFAULT_COMPARE(su3t)
147 DEFAULT_COMPARE(su3f)
148 DEFAULT_COMPARE(su3d)
149
150 DEFAULT_PRINT_LATEX(su3one, "ONE", "\\mathbb{1}")
151 DEFAULT_PRINT(su3t, "T")
152 DEFAULT_PRINT(su3f, "f")
153 DEFAULT_PRINT(su3d, "d")
154
155 /** Perform automatic simplification on noncommutative product of color
156  *  objects. This removes superfluous ONEs. */
157 ex color::simplify_ncmul(const exvector & v) const
158 {
159         exvector s;
160         s.reserve(v.size());
161
162         // Remove superfluous ONEs
163         exvector::const_iterator it = v.begin(), itend = v.end();
164         while (it != itend) {
165                 if (!is_ex_of_type(it->op(0), su3one))
166                         s.push_back(*it);
167                 it++;
168         }
169
170         if (s.empty())
171                 return color(su3one(), representation_label);
172         else
173                 return simplified_ncmul(s);
174 }
175
176 ex color::thisexprseq(const exvector & v) const
177 {
178         return color(representation_label, v);
179 }
180
181 ex color::thisexprseq(exvector * vp) const
182 {
183         return color(representation_label, vp);
184 }
185
186 /** Given a vector iv3 of three indices and a vector iv2 of two indices that
187  *  is a subset of iv3, return the (free) index that is in iv3 but not in
188  *  iv2 and the sign introduced by permuting that index to the front.
189  *
190  *  @param iv3 Vector of 3 indices
191  *  @param iv2 Vector of 2 indices, must be a subset of iv3
192  *  @param sig Returs sign introduced by index permutation
193  *  @return the free index (the one that is in iv3 but not in iv2) */
194 static ex permute_free_index_to_front(const exvector & iv3, const exvector & iv2, int & sig)
195 {
196         GINAC_ASSERT(iv3.size() == 3);
197         GINAC_ASSERT(iv2.size() == 2);
198
199         sig = 1;
200
201 #define TEST_PERMUTATION(A,B,C,P) \
202         if (iv3[B].is_equal(iv2[0]) && iv3[C].is_equal(iv2[1])) { \
203                 sig = P; \
204                 return iv3[A]; \
205         }
206         
207         TEST_PERMUTATION(0,1,2,  1);
208         TEST_PERMUTATION(0,2,1, -1);
209         TEST_PERMUTATION(1,0,2, -1);
210         TEST_PERMUTATION(1,2,0,  1);
211         TEST_PERMUTATION(2,0,1,  1);
212         TEST_PERMUTATION(2,1,0, -1);
213
214         throw(std::logic_error("permute_free_index_to_front(): no valid permutation found"));
215 }
216
217 /** Automatic symbolic evaluation of indexed symmetric structure constant. */
218 ex su3d::eval_indexed(const basic & i) const
219 {
220         GINAC_ASSERT(is_a<indexed>(i));
221         GINAC_ASSERT(i.nops() == 4);
222         GINAC_ASSERT(is_a<su3d>(i.op(0)));
223
224         // Convolutions are zero
225         if (!(static_cast<const indexed &>(i).get_dummy_indices().empty()))
226                 return _ex0;
227
228         // Numeric evaluation
229         if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
230
231                 // Sort indices
232                 int v[3];
233                 for (unsigned j=0; j<3; j++)
234                         v[j] = ex_to<numeric>(ex_to<idx>(i.op(j + 1)).get_value()).to_int();
235                 if (v[0] > v[1]) std::swap(v[0], v[1]);
236                 if (v[0] > v[2]) std::swap(v[0], v[2]);
237                 if (v[1] > v[2]) std::swap(v[1], v[2]);
238
239 #define CMPINDICES(A,B,C) ((v[0] == (A)) && (v[1] == (B)) && (v[2] == (C)))
240
241                 // Check for non-zero elements
242                 if (CMPINDICES(1,4,6) || CMPINDICES(1,5,7) || CMPINDICES(2,5,6)
243                  || CMPINDICES(3,4,4) || CMPINDICES(3,5,5))
244                         return _ex1_2;
245                 else if (CMPINDICES(2,4,7) || CMPINDICES(3,6,6) || CMPINDICES(3,7,7))
246                         return _ex_1_2;
247                 else if (CMPINDICES(1,1,8) || CMPINDICES(2,2,8) || CMPINDICES(3,3,8))
248                         return sqrt(_ex3)*_ex1_3;
249                 else if (CMPINDICES(8,8,8))
250                         return sqrt(_ex3)*_ex_1_3;
251                 else if (CMPINDICES(4,4,8) || CMPINDICES(5,5,8)
252                       || CMPINDICES(6,6,8) || CMPINDICES(7,7,8))
253                         return sqrt(_ex3)/_ex_6;
254                 else
255                         return _ex0;
256         }
257
258         // No further simplifications
259         return i.hold();
260 }
261
262 /** Automatic symbolic evaluation of indexed antisymmetric structure constant. */
263 ex su3f::eval_indexed(const basic & i) const
264 {
265         GINAC_ASSERT(is_a<indexed>(i));
266         GINAC_ASSERT(i.nops() == 4);
267         GINAC_ASSERT(is_a<su3f>(i.op(0)));
268
269         // Numeric evaluation
270         if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
271
272                 // Sort indices, remember permutation sign
273                 int v[3];
274                 for (unsigned j=0; j<3; j++)
275                         v[j] = ex_to<numeric>(ex_to<idx>(i.op(j + 1)).get_value()).to_int();
276                 int sign = 1;
277                 if (v[0] > v[1]) { std::swap(v[0], v[1]); sign = -sign; }
278                 if (v[0] > v[2]) { std::swap(v[0], v[2]); sign = -sign; }
279                 if (v[1] > v[2]) { std::swap(v[1], v[2]); sign = -sign; }
280
281                 // Check for non-zero elements
282                 if (CMPINDICES(1,2,3))
283                         return sign;
284                 else if (CMPINDICES(1,4,7) || CMPINDICES(2,4,6)
285                       || CMPINDICES(2,5,7) || CMPINDICES(3,4,5))
286                         return _ex1_2 * sign;
287                 else if (CMPINDICES(1,5,6) || CMPINDICES(3,6,7))
288                         return _ex_1_2 * sign;
289                 else if (CMPINDICES(4,5,8) || CMPINDICES(6,7,8))
290                         return sqrt(_ex3)/2 * sign;
291                 else
292                         return _ex0;
293         }
294
295         // No further simplifications
296         return i.hold();
297 }
298
299
300 /** Contraction of generator with something else. */
301 bool su3t::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
302 {
303         GINAC_ASSERT(is_a<indexed>(*self));
304         GINAC_ASSERT(is_a<indexed>(*other));
305         GINAC_ASSERT(self->nops() == 2);
306         GINAC_ASSERT(is_a<su3t>(self->op(0)));
307         unsigned char rl = ex_to<color>(*self).get_representation_label();
308
309         if (is_ex_exactly_of_type(other->op(0), su3t)) {
310
311                 // Contraction only makes sense if the represenation labels are equal
312                 GINAC_ASSERT(is_a<color>(*other));
313                 if (ex_to<color>(*other).get_representation_label() != rl)
314                         return false;
315
316                 // T.a T.a = 4/3 ONE
317                 if (other - self == 1) {
318                         *self = numeric(4, 3);
319                         *other = color_ONE(rl);
320                         return true;
321
322                 // T.a T.b T.a = -1/6 T.b
323                 } else if (other - self == 2
324                         && is_ex_of_type(self[1], color)) {
325                         *self = numeric(-1, 6);
326                         *other = _ex1;
327                         return true;
328
329                 // T.a S T.a = 1/2 Tr(S) - 1/6 S
330                 } else {
331                         exvector::iterator it = self + 1;
332                         while (it != other) {
333                                 if (!is_ex_of_type(*it, color)) {
334                                         return false;
335                                 }
336                                 it++;
337                         }
338
339                         it = self + 1;
340                         ex S = _ex1;
341                         while (it != other) {
342                                 S *= *it;
343                                 *it++ = _ex1;
344                         }
345
346                         *self = color_trace(S, rl) * color_ONE(rl) / 2 - S / 6;
347                         *other = _ex1;
348                         return true;
349                 }
350         }
351
352         return false;
353 }
354
355 /** Contraction of an indexed symmetric structure constant with something else. */
356 bool su3d::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
357 {
358         GINAC_ASSERT(is_a<indexed>(*self));
359         GINAC_ASSERT(is_a<indexed>(*other));
360         GINAC_ASSERT(self->nops() == 4);
361         GINAC_ASSERT(is_a<su3d>(self->op(0)));
362
363         if (is_ex_exactly_of_type(other->op(0), su3d)) {
364
365                 // Find the dummy indices of the contraction
366                 exvector self_indices = ex_to<indexed>(*self).get_indices();
367                 exvector other_indices = ex_to<indexed>(*other).get_indices();
368                 exvector all_indices = self_indices;
369                 all_indices.insert(all_indices.end(), other_indices.begin(), other_indices.end());
370                 exvector free_indices, dummy_indices;
371                 find_free_and_dummy(all_indices, free_indices, dummy_indices);
372
373                 // d.abc d.abc = 40/3
374                 if (dummy_indices.size() == 3) {
375                         *self = numeric(40, 3);
376                         *other = _ex1;
377                         return true;
378
379                 // d.akl d.bkl = 5/3 delta.ab
380                 } else if (dummy_indices.size() == 2) {
381                         exvector a;
382                         std::back_insert_iterator<exvector> ita(a);
383                         ita = set_difference(self_indices.begin(), self_indices.end(), dummy_indices.begin(), dummy_indices.end(), ita, ex_is_less());
384                         ita = set_difference(other_indices.begin(), other_indices.end(), dummy_indices.begin(), dummy_indices.end(), ita, ex_is_less());
385                         GINAC_ASSERT(a.size() == 2);
386                         *self = numeric(5, 3) * delta_tensor(a[0], a[1]);
387                         *other = _ex1;
388                         return true;
389                 }
390
391         } else if (is_ex_exactly_of_type(other->op(0), su3t)) {
392
393                 // d.abc T.b T.c = 5/6 T.a
394                 if (other+1 != v.end()
395                  && is_ex_exactly_of_type(other[1].op(0), su3t)
396                  && ex_to<indexed>(*self).has_dummy_index_for(other[1].op(1))) {
397
398                         exvector self_indices = ex_to<indexed>(*self).get_indices();
399                         exvector dummy_indices;
400                         dummy_indices.push_back(other[0].op(1));
401                         dummy_indices.push_back(other[1].op(1));
402                         int sig;
403                         ex a = permute_free_index_to_front(self_indices, dummy_indices, sig);
404                         *self = numeric(5, 6);
405                         other[0] = color_T(a, ex_to<color>(other[0]).get_representation_label());
406                         other[1] = _ex1;
407                         return true;
408                 }
409         }
410
411         return false;
412 }
413
414 /** Contraction of an indexed antisymmetric structure constant with something else. */
415 bool su3f::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
416 {
417         GINAC_ASSERT(is_a<indexed>(*self));
418         GINAC_ASSERT(is_a<indexed>(*other));
419         GINAC_ASSERT(self->nops() == 4);
420         GINAC_ASSERT(is_a<su3f>(self->op(0)));
421
422         if (is_ex_exactly_of_type(other->op(0), su3f)) { // f*d is handled by su3d class
423
424                 // Find the dummy indices of the contraction
425                 exvector dummy_indices;
426                 dummy_indices = ex_to<indexed>(*self).get_dummy_indices(ex_to<indexed>(*other));
427
428                 // f.abc f.abc = 24
429                 if (dummy_indices.size() == 3) {
430                         *self = 24;
431                         *other = _ex1;
432                         return true;
433
434                 // f.akl f.bkl = 3 delta.ab
435                 } else if (dummy_indices.size() == 2) {
436                         int sign1, sign2;
437                         ex a = permute_free_index_to_front(ex_to<indexed>(*self).get_indices(), dummy_indices, sign1);
438                         ex b = permute_free_index_to_front(ex_to<indexed>(*other).get_indices(), dummy_indices, sign2);
439                         *self = sign1 * sign2 * 3 * delta_tensor(a, b);
440                         *other = _ex1;
441                         return true;
442                 }
443
444         } else if (is_ex_exactly_of_type(other->op(0), su3t)) {
445
446                 // f.abc T.b T.c = 3/2 I T.a
447                 if (other+1 != v.end()
448                  && is_ex_exactly_of_type(other[1].op(0), su3t)
449                  && ex_to<indexed>(*self).has_dummy_index_for(other[1].op(1))) {
450
451                         exvector self_indices = ex_to<indexed>(*self).get_indices();
452                         exvector dummy_indices;
453                         dummy_indices.push_back(other[0].op(1));
454                         dummy_indices.push_back(other[1].op(1));
455                         int sig;
456                         ex a = permute_free_index_to_front(self_indices, dummy_indices, sig);
457                         *self = numeric(3, 2) * sig * I;
458                         other[0] = color_T(a, ex_to<color>(other[0]).get_representation_label());
459                         other[1] = _ex1;
460                         return true;
461                 }
462         }
463
464         return false;
465 }
466
467 //////////
468 // global functions
469 //////////
470
471 ex color_ONE(unsigned char rl)
472 {
473         return color(su3one(), rl);
474 }
475
476 ex color_T(const ex & a, unsigned char rl)
477 {
478         if (!is_ex_of_type(a, idx))
479                 throw(std::invalid_argument("indices of color_T must be of type idx"));
480         if (!ex_to<idx>(a).get_dim().is_equal(8))
481                 throw(std::invalid_argument("index dimension for color_T must be 8"));
482
483         return color(su3t(), a, rl);
484 }
485
486 ex color_f(const ex & a, const ex & b, const ex & c)
487 {
488         if (!is_ex_of_type(a, idx) || !is_ex_of_type(b, idx) || !is_ex_of_type(c, idx))
489                 throw(std::invalid_argument("indices of color_f must be of type idx"));
490         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))
491                 throw(std::invalid_argument("index dimension for color_f must be 8"));
492
493         return indexed(su3f(), sy_anti(), a, b, c);
494 }
495
496 ex color_d(const ex & a, const ex & b, const ex & c)
497 {
498         if (!is_ex_of_type(a, idx) || !is_ex_of_type(b, idx) || !is_ex_of_type(c, idx))
499                 throw(std::invalid_argument("indices of color_d must be of type idx"));
500         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))
501                 throw(std::invalid_argument("index dimension for color_d must be 8"));
502
503         return indexed(su3d(), sy_symm(), a, b, c);
504 }
505
506 ex color_h(const ex & a, const ex & b, const ex & c)
507 {
508         return color_d(a, b, c) + I * color_f(a, b, c);
509 }
510
511 /** Check whether a given tinfo key (as returned by return_type_tinfo()
512  *  is that of a color object with the specified representation label. */
513 static bool is_color_tinfo(unsigned ti, unsigned char rl)
514 {
515         return ti == (TINFO_color + rl);
516 }
517
518 ex color_trace(const ex & e, unsigned char rl)
519 {
520         if (is_ex_of_type(e, color)) {
521
522                 if (ex_to<color>(e).get_representation_label() == rl
523                  && is_ex_of_type(e.op(0), su3one))
524                         return _ex3;
525                 else
526                         return _ex0;
527
528         } else if (is_ex_exactly_of_type(e, mul)) {
529
530                 // Trace of product: pull out non-color factors
531                 ex prod = _ex1;
532                 for (unsigned i=0; i<e.nops(); i++) {
533                         const ex &o = e.op(i);
534                         if (is_color_tinfo(o.return_type_tinfo(), rl))
535                                 prod *= color_trace(o, rl);
536                         else
537                                 prod *= o;
538                 }
539                 return prod;
540
541         } else if (is_ex_exactly_of_type(e, ncmul)) {
542
543                 if (!is_color_tinfo(e.return_type_tinfo(), rl))
544                         return _ex0;
545
546                 // Expand product, if necessary
547                 ex e_expanded = e.expand();
548                 if (!is_ex_of_type(e_expanded, ncmul))
549                         return color_trace(e_expanded, rl);
550
551                 unsigned num = e.nops();
552
553                 if (num == 2) {
554
555                         // Tr T_a T_b = 1/2 delta_a_b
556                         return delta_tensor(e.op(0).op(1), e.op(1).op(1)) / 2;
557
558                 } else if (num == 3) {
559
560                         // Tr T_a T_b T_c = 1/4 h_a_b_c
561                         return color_h(e.op(0).op(1), e.op(1).op(1), e.op(2).op(1)) / 4;
562
563                 } else {
564
565                         // Traces of 4 or more generators are computed recursively:
566                         // Tr T_a1 .. T_an =
567                         //     1/6 delta_a(n-1)_an Tr T_a1 .. T_a(n-2)
568                         //   + 1/2 h_a(n-1)_an_k Tr T_a1 .. T_a(n-2) T_k
569                         const ex &last_index = e.op(num - 1).op(1);
570                         const ex &next_to_last_index = e.op(num - 2).op(1);
571                         idx summation_index((new symbol)->setflag(status_flags::dynallocated), 8);
572
573                         exvector v1;
574                         v1.reserve(num - 2);
575                         for (unsigned i=0; i<num-2; i++)
576                                 v1.push_back(e.op(i));
577
578                         exvector v2 = v1;
579                         v2.push_back(color_T(summation_index, rl));
580
581                         return delta_tensor(next_to_last_index, last_index) * color_trace(ncmul(v1), rl) / 6
582                                + color_h(next_to_last_index, last_index, summation_index) * color_trace(ncmul(v2), rl) / 2;
583                 }
584
585         } else if (e.nops() > 0) {
586
587                 // Trace maps to all other container classes (this includes sums)
588                 pointer_to_map_function_1arg<unsigned char> fcn(color_trace, rl);
589                 return e.map(fcn);
590
591         } else
592                 return _ex0;
593 }
594
595 } // namespace GiNaC