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