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