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