- added find() (like has(), but returns list of all occurrences)
[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(is_of_type(other, 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 bool color::match_same_type(const basic & other) const
144 {
145         GINAC_ASSERT(is_of_type(other, color));
146         const color &o = static_cast<const color &>(other);
147
148         return representation_label == o.representation_label;
149 }
150
151 DEFAULT_COMPARE(su3one)
152 DEFAULT_COMPARE(su3t)
153 DEFAULT_COMPARE(su3f)
154 DEFAULT_COMPARE(su3d)
155
156 DEFAULT_PRINT_LATEX(su3one, "ONE", "\\mathbb{1}")
157 DEFAULT_PRINT(su3t, "T")
158 DEFAULT_PRINT(su3f, "f")
159 DEFAULT_PRINT(su3d, "d")
160
161 /** Perform automatic simplification on noncommutative product of color
162  *  objects. This removes superfluous ONEs. */
163 ex color::simplify_ncmul(const exvector & v) const
164 {
165         exvector s;
166         s.reserve(v.size());
167
168         // Remove superfluous ONEs
169         exvector::const_iterator it = v.begin(), itend = v.end();
170         while (it != itend) {
171                 if (!is_ex_of_type(it->op(0), su3one))
172                         s.push_back(*it);
173                 it++;
174         }
175
176         if (s.empty())
177                 return color(su3one(), representation_label);
178         else
179                 return simplified_ncmul(s);
180 }
181
182 ex color::thisexprseq(const exvector & v) const
183 {
184         return color(representation_label, v);
185 }
186
187 ex color::thisexprseq(exvector * vp) const
188 {
189         return color(representation_label, vp);
190 }
191
192 /** Given a vector iv3 of three indices and a vector iv2 of two indices that
193  *  is a subset of iv3, return the (free) index that is in iv3 but not in
194  *  iv2 and the sign introduced by permuting that index to the front.
195  *
196  *  @param iv3 Vector of 3 indices
197  *  @param iv2 Vector of 2 indices, must be a subset of iv3
198  *  @param sig Returs sign introduced by index permutation
199  *  @return the free index (the one that is in iv3 but not in iv2) */
200 static ex permute_free_index_to_front(const exvector & iv3, const exvector & iv2, int & sig)
201 {
202         GINAC_ASSERT(iv3.size() == 3);
203         GINAC_ASSERT(iv2.size() == 2);
204
205         sig = 1;
206
207 #define TEST_PERMUTATION(A,B,C,P) \
208         if (iv3[B].is_equal(iv2[0]) && iv3[C].is_equal(iv2[1])) { \
209                 sig = P; \
210                 return iv3[A]; \
211         }
212         
213         TEST_PERMUTATION(0,1,2,  1);
214         TEST_PERMUTATION(0,2,1, -1);
215         TEST_PERMUTATION(1,0,2, -1);
216         TEST_PERMUTATION(1,2,0,  1);
217         TEST_PERMUTATION(2,0,1,  1);
218         TEST_PERMUTATION(2,1,0, -1);
219
220         throw(std::logic_error("permute_free_index_to_front(): no valid permutation found"));
221 }
222
223 /** Automatic symbolic evaluation of indexed symmetric structure constant. */
224 ex su3d::eval_indexed(const basic & i) const
225 {
226         GINAC_ASSERT(is_of_type(i, indexed));
227         GINAC_ASSERT(i.nops() == 4);
228         GINAC_ASSERT(is_ex_of_type(i.op(0), su3d));
229
230         // Convolutions are zero
231         if (!(static_cast<const indexed &>(i).get_dummy_indices().empty()))
232                 return _ex0();
233
234         // Numeric evaluation
235         if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
236
237                 // Sort indices
238                 int v[3];
239                 for (unsigned j=0; j<3; j++)
240                         v[j] = ex_to<numeric>(ex_to<idx>(i.op(j + 1)).get_value()).to_int();
241                 if (v[0] > v[1]) std::swap(v[0], v[1]);
242                 if (v[0] > v[2]) std::swap(v[0], v[2]);
243                 if (v[1] > v[2]) std::swap(v[1], v[2]);
244
245 #define CMPINDICES(A,B,C) ((v[0] == (A)) && (v[1] == (B)) && (v[2] == (C)))
246
247                 // Check for non-zero elements
248                 if (CMPINDICES(1,4,6) || CMPINDICES(1,5,7) || CMPINDICES(2,5,6)
249                  || CMPINDICES(3,4,4) || CMPINDICES(3,5,5))
250                         return _ex1_2();
251                 else if (CMPINDICES(2,4,7) || CMPINDICES(3,6,6) || CMPINDICES(3,7,7))
252                         return _ex_1_2();
253                 else if (CMPINDICES(1,1,8) || CMPINDICES(2,2,8) || CMPINDICES(3,3,8))
254                         return sqrt(_ex3())/3;
255                 else if (CMPINDICES(8,8,8))
256                         return -sqrt(_ex3())/3;
257                 else if (CMPINDICES(4,4,8) || CMPINDICES(5,5,8)
258                       || CMPINDICES(6,6,8) || CMPINDICES(7,7,8))
259                         return -sqrt(_ex3())/6;
260                 else
261                         return _ex0();
262         }
263
264         // No further simplifications
265         return i.hold();
266 }
267
268 /** Automatic symbolic evaluation of indexed antisymmetric structure constant. */
269 ex su3f::eval_indexed(const basic & i) const
270 {
271         GINAC_ASSERT(is_of_type(i, indexed));
272         GINAC_ASSERT(i.nops() == 4);
273         GINAC_ASSERT(is_ex_of_type(i.op(0), su3f));
274
275         // Numeric evaluation
276         if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
277
278                 // Sort indices, remember permutation sign
279                 int v[3];
280                 for (unsigned j=0; j<3; j++)
281                         v[j] = ex_to<numeric>(ex_to<idx>(i.op(j + 1)).get_value()).to_int();
282                 int sign = 1;
283                 if (v[0] > v[1]) { std::swap(v[0], v[1]); sign = -sign; }
284                 if (v[0] > v[2]) { std::swap(v[0], v[2]); sign = -sign; }
285                 if (v[1] > v[2]) { std::swap(v[1], v[2]); sign = -sign; }
286
287                 // Check for non-zero elements
288                 if (CMPINDICES(1,2,3))
289                         return sign;
290                 else if (CMPINDICES(1,4,7) || CMPINDICES(2,4,6)
291                       || CMPINDICES(2,5,7) || CMPINDICES(3,4,5))
292                         return _ex1_2() * sign;
293                 else if (CMPINDICES(1,5,6) || CMPINDICES(3,6,7))
294                         return _ex_1_2() * sign;
295                 else if (CMPINDICES(4,5,8) || CMPINDICES(6,7,8))
296                         return sqrt(_ex3())/2 * sign;
297                 else
298                         return _ex0();
299         }
300
301         // No further simplifications
302         return i.hold();
303 }
304
305
306 /** Contraction of generator with something else. */
307 bool su3t::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
308 {
309         GINAC_ASSERT(is_ex_of_type(*self, indexed));
310         GINAC_ASSERT(is_ex_of_type(*other, indexed));
311         GINAC_ASSERT(self->nops() == 2);
312         GINAC_ASSERT(is_ex_of_type(self->op(0), su3t));
313         unsigned char rl = ex_to<color>(*self).get_representation_label();
314
315         if (is_ex_exactly_of_type(other->op(0), su3t)) {
316
317                 // T.a T.a = 4/3 ONE
318                 if (other - self == 1) {
319                         *self = numeric(4, 3);
320                         *other = color_ONE(rl);
321                         return true;
322
323                 // T.a T.b T.a = -1/6 T.b
324                 } else if (other - self == 2
325                         && is_ex_of_type(self[1], color)) {
326                         *self = numeric(-1, 6);
327                         *other = _ex1();
328                         return true;
329
330                 // T.a S T.a = 1/2 Tr(S) - 1/6 S
331                 } else {
332                         exvector::iterator it = self + 1;
333                         while (it != other) {
334                                 if (!is_ex_of_type(*it, color)) {
335                                         return false;
336                                 }
337                                 it++;
338                         }
339
340                         it = self + 1;
341                         ex S = _ex1();
342                         while (it != other) {
343                                 S *= *it;
344                                 *it++ = _ex1();
345                         }
346
347                         *self = color_trace(S, rl) * color_ONE(rl) / 2 - S / 6;
348                         *other = _ex1();
349                         return true;
350                 }
351         }
352
353         return false;
354 }
355
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
358 {
359         GINAC_ASSERT(is_ex_of_type(*self, indexed));
360         GINAC_ASSERT(is_ex_of_type(*other, indexed));
361         GINAC_ASSERT(self->nops() == 4);
362         GINAC_ASSERT(is_ex_of_type(self->op(0), su3d));
363
364         if (is_ex_exactly_of_type(other->op(0), su3d)) {
365
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);
373
374                 // d.abc d.abc = 40/3
375                 if (dummy_indices.size() == 3) {
376                         *self = numeric(40, 3);
377                         *other = _ex1();
378                         return true;
379
380                 // d.akl d.bkl = 5/3 delta.ab
381                 } else if (dummy_indices.size() == 2) {
382                         exvector a;
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]);
388                         *other = _ex1();
389                         return true;
390                 }
391
392         } else if (is_ex_exactly_of_type(other->op(0), su3t)) {
393
394                 // d.abc T.b T.c = 5/6 T.a
395                 if (other+1 != v.end()
396                  && is_ex_exactly_of_type(other[1].op(0), su3t)
397                  && ex_to<indexed>(*self).has_dummy_index_for(other[1].op(1))) {
398
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));
403                         int sig;
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());
407                         other[1] = _ex1();
408                         return true;
409                 }
410         }
411
412         return false;
413 }
414
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
417 {
418         GINAC_ASSERT(is_ex_of_type(*self, indexed));
419         GINAC_ASSERT(is_ex_of_type(*other, indexed));
420         GINAC_ASSERT(self->nops() == 4);
421         GINAC_ASSERT(is_ex_of_type(self->op(0), su3f));
422
423         if (is_ex_exactly_of_type(other->op(0), su3f)) { // f*d is handled by su3d class
424
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));
428
429                 // f.abc f.abc = 24
430                 if (dummy_indices.size() == 3) {
431                         *self = 24;
432                         *other = _ex1();
433                         return true;
434
435                 // f.akl f.bkl = 3 delta.ab
436                 } else if (dummy_indices.size() == 2) {
437                         int sign1, sign2;
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);
441                         *other = _ex1();
442                         return true;
443                 }
444
445         } else if (is_ex_exactly_of_type(other->op(0), su3t)) {
446
447                 // f.abc T.b T.c = 3/2 I T.a
448                 if (other+1 != v.end()
449                  && is_ex_exactly_of_type(other[1].op(0), su3t)
450                  && ex_to<indexed>(*self).has_dummy_index_for(other[1].op(1))) {
451
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));
456                         int sig;
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());
460                         other[1] = _ex1();
461                         return true;
462                 }
463         }
464
465         return false;
466 }
467
468 //////////
469 // global functions
470 //////////
471
472 ex color_ONE(unsigned char rl)
473 {
474         return color(su3one(), rl);
475 }
476
477 ex color_T(const ex & a, unsigned char rl)
478 {
479         if (!is_ex_of_type(a, idx))
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"));
483
484         return color(su3t(), a, rl);
485 }
486
487 ex color_f(const ex & a, const ex & b, const ex & c)
488 {
489         if (!is_ex_of_type(a, idx) || !is_ex_of_type(b, idx) || !is_ex_of_type(c, idx))
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"));
493
494         return indexed(su3f(), sy_anti(), a, b, c);
495 }
496
497 ex color_d(const ex & a, const ex & b, const ex & c)
498 {
499         if (!is_ex_of_type(a, idx) || !is_ex_of_type(b, idx) || !is_ex_of_type(c, idx))
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"));
503
504         return indexed(su3d(), sy_symm(), a, b, c);
505 }
506
507 ex color_h(const ex & a, const ex & b, const ex & c)
508 {
509         return color_d(a, b, c) + I * color_f(a, b, c);
510 }
511
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)
515 {
516         return ti == (TINFO_color + rl);
517 }
518
519 ex color_trace(const ex & e, unsigned char rl)
520 {
521         if (is_ex_of_type(e, color)) {
522
523                 if (ex_to<color>(e).get_representation_label() == rl
524                  && is_ex_of_type(e.op(0), su3one))
525                         return _ex3();
526                 else
527                         return _ex0();
528
529         } else if (is_ex_exactly_of_type(e, mul)) {
530
531                 // Trace of product: pull out non-color factors
532                 ex prod = _ex1();
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);
537                         else
538                                 prod *= o;
539                 }
540                 return prod;
541
542         } else if (is_ex_exactly_of_type(e, ncmul)) {
543
544                 if (!is_color_tinfo(e.return_type_tinfo(), rl))
545                         return _ex0();
546
547                 // Expand product, if necessary
548                 ex e_expanded = e.expand();
549                 if (!is_ex_of_type(e_expanded, ncmul))
550                         return color_trace(e_expanded, rl);
551
552                 unsigned num = e.nops();
553
554                 if (num == 2) {
555
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;
558
559                 } else if (num == 3) {
560
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;
563
564                 } else {
565
566                         // Traces of 4 or more generators are computed recursively:
567                         // Tr T_a1 .. T_an =
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);
573
574                         exvector v1;
575                         v1.reserve(num - 2);
576                         for (int i=0; i<num-2; i++)
577                                 v1.push_back(e.op(i));
578
579                         exvector v2 = v1;
580                         v2.push_back(color_T(summation_index, rl));
581
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;
584                 }
585
586         } else if (e.nops() > 0) {
587
588                 // Trace maps to all other container classes (this includes sums)
589                 pointer_to_map_function_1arg<unsigned char> fcn(color_trace, rl);
590                 return e.map(fcn);
591
592         } else
593                 return _ex0();
594 }
595
596 } // namespace GiNaC