]> www.ginac.de Git - ginac.git/blob - ginac/color.cpp
e36c1cbe9796effef77d085d228bb4f5be614894
[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 "ex.h"
28 #include "idx.h"
29 #include "ncmul.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(indexed::unknown, 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(indexed::unknown, 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(su3one, "ONE")
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         //!! TODO: sort by representation label
158         exvector s;
159         s.reserve(v.size());
160
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());
170         else if (s.size() == v.size())
171                 return simplified_ncmul(v);
172         else
173                 return simplified_ncmul(s);
174 }
175
176 ex color::thisexprseq(const exvector & v) const
177 {
178         return color(representation_label, v);
179 }
180
181 ex color::thisexprseq(exvector * vp) const
182 {
183         return color(representation_label, vp);
184 }
185
186 /** Given a vector iv3 of three indices and a vector iv2 of two indices that
187  *  is a subset of iv3, return the (free) index that is in iv3 but not in
188  *  iv2 and the sign introduced by permuting that index to the front.
189  *
190  *  @param iv3 Vector of 3 indices
191  *  @param iv2 Vector of 2 indices, must be a subset of iv3
192  *  @param sig Returs sign introduced by index permutation
193  *  @return the free index (the one that is in iv3 but not in iv2) */
194 static ex permute_free_index_to_front(const exvector & iv3, const exvector & iv2, int & sig)
195 {
196         GINAC_ASSERT(iv3.size() == 3);
197         GINAC_ASSERT(iv2.size() == 2);
198
199         sig = 1;
200
201 #define TEST_PERMUTATION(A,B,C,P) \
202         if (iv3[B].is_equal(iv2[0]) && iv3[C].is_equal(iv2[1])) { \
203                 sig = P; \
204                 return iv3[A]; \
205         }
206         
207         TEST_PERMUTATION(0,1,2,  1);
208         TEST_PERMUTATION(0,2,1, -1);
209         TEST_PERMUTATION(1,0,2, -1);
210         TEST_PERMUTATION(1,2,0,  1);
211         TEST_PERMUTATION(2,0,1,  1);
212         TEST_PERMUTATION(2,1,0, -1);
213
214         throw(std::logic_error("permute_free_index_to_front(): no valid permutation found"));
215 }
216
217 /** Automatic symbolic evaluation of indexed symmetric structure constant. */
218 ex su3d::eval_indexed(const basic & i) const
219 {
220         GINAC_ASSERT(is_of_type(i, indexed));
221         GINAC_ASSERT(i.nops() == 4);
222         GINAC_ASSERT(is_ex_of_type(i.op(0), su3d));
223
224         // Convolutions are zero
225         if (static_cast<const indexed &>(i).get_dummy_indices().size() != 0)
226                 return _ex0();
227
228         // Numeric evaluation
229         if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
230
231                 // Sort indices
232                 int v[3];
233                 for (unsigned j=0; j<3; j++)
234                         v[j] = ex_to_numeric(ex_to_idx(i.op(j + 1)).get_value()).to_int();
235                 if (v[0] > v[1]) std::swap(v[0], v[1]);
236                 if (v[0] > v[2]) std::swap(v[0], v[2]);
237                 if (v[1] > v[2]) std::swap(v[1], v[2]);
238
239 #define CMPINDICES(A,B,C) ((v[0] == (A)) && (v[1] == (B)) && (v[2] == (C)))
240
241                 // Check for non-zero elements
242                 if (CMPINDICES(1,4,6) || CMPINDICES(1,5,7) || CMPINDICES(2,5,6)
243                  || CMPINDICES(3,4,4) || CMPINDICES(3,5,5))
244                         return _ex1_2();
245                 else if (CMPINDICES(2,4,7) || CMPINDICES(3,6,6) || CMPINDICES(3,7,7))
246                         return _ex_1_2();
247                 else if (CMPINDICES(1,1,8) || CMPINDICES(2,2,8) || CMPINDICES(3,3,8))
248                         return sqrt(_ex3())/3;
249                 else if (CMPINDICES(8,8,8))
250                         return -sqrt(_ex3())/3;
251                 else if (CMPINDICES(4,4,8) || CMPINDICES(5,5,8)
252                       || CMPINDICES(6,6,8) || CMPINDICES(7,7,8))
253                         return -sqrt(_ex3())/6;
254                 else
255                         return _ex0();
256         }
257
258         // No further simplifications
259         return i.hold();
260 }
261
262 /** Automatic symbolic evaluation of indexed antisymmetric structure constant. */
263 ex su3f::eval_indexed(const basic & i) const
264 {
265         GINAC_ASSERT(is_of_type(i, indexed));
266         GINAC_ASSERT(i.nops() == 4);
267         GINAC_ASSERT(is_ex_of_type(i.op(0), su3f));
268
269         // Numeric evaluation
270         if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
271
272                 // Sort indices, remember permutation sign
273                 int v[3];
274                 for (unsigned j=0; j<3; j++)
275                         v[j] = ex_to_numeric(ex_to_idx(i.op(j + 1)).get_value()).to_int();
276                 int sign = 1;
277                 if (v[0] > v[1]) { std::swap(v[0], v[1]); sign = -sign; }
278                 if (v[0] > v[2]) { std::swap(v[0], v[2]); sign = -sign; }
279                 if (v[1] > v[2]) { std::swap(v[1], v[2]); sign = -sign; }
280
281                 // Check for non-zero elements
282                 if (CMPINDICES(1,2,3))
283                         return sign;
284                 else if (CMPINDICES(1,4,7) || CMPINDICES(2,4,6)
285                       || CMPINDICES(2,5,7) || CMPINDICES(3,4,5))
286                         return _ex1_2() * sign;
287                 else if (CMPINDICES(1,5,6) || CMPINDICES(3,6,7))
288                         return _ex_1_2() * sign;
289                 else if (CMPINDICES(4,5,8) || CMPINDICES(6,7,8))
290                         return sqrt(_ex3())/2 * sign;
291                 else
292                         return _ex0();
293         }
294
295         // No further simplifications
296         return i.hold();
297 }
298
299
300 /** Contraction of an indexed symmetric structure constant with something else. */
301 bool su3d::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
302 {
303         GINAC_ASSERT(is_ex_of_type(*self, indexed));
304         GINAC_ASSERT(is_ex_of_type(*other, indexed));
305         GINAC_ASSERT(self->nops() == 4);
306         GINAC_ASSERT(is_ex_of_type(self->op(0), su3d));
307
308         if (is_ex_exactly_of_type(other->op(0), su3d)) {
309
310                 // Find the dummy indices of the contraction
311                 exvector dummy_indices;
312                 dummy_indices = ex_to_indexed(*self).get_dummy_indices(ex_to_indexed(*other));
313
314                 // d.abc*d.abc=40/3
315                 if (dummy_indices.size() == 3) {
316                         *self = numeric(40, 3);
317                         *other = _ex1();
318                         return true;
319
320                 // d.akl*d.bkl=5/3*delta.ab
321                 } else if (dummy_indices.size() == 2) {
322                         exvector a = index_set_difference(ex_to_indexed(*self).get_indices(), dummy_indices);
323                         exvector b = index_set_difference(ex_to_indexed(*other).get_indices(), dummy_indices);
324                         GINAC_ASSERT(a.size() > 0);
325                         GINAC_ASSERT(b.size() > 0);
326                         *self = numeric(5, 3) * delta_tensor(a[0], b[0]);
327                         *other = _ex1();
328                         return true;
329                 }
330         }
331
332         return false;
333 }
334
335 /** Contraction of an indexed antisymmetric structure constant with something else. */
336 bool su3f::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
337 {
338         GINAC_ASSERT(is_ex_of_type(*self, indexed));
339         GINAC_ASSERT(is_ex_of_type(*other, indexed));
340         GINAC_ASSERT(self->nops() == 4);
341         GINAC_ASSERT(is_ex_of_type(self->op(0), su3f));
342
343         if (is_ex_exactly_of_type(other->op(0), su3f)) { // f*d is handled by su3d class
344
345                 // Find the dummy indices of the contraction
346                 exvector dummy_indices;
347                 dummy_indices = ex_to_indexed(*self).get_dummy_indices(ex_to_indexed(*other));
348
349                 // f.abc*f.abc=24
350                 if (dummy_indices.size() == 3) {
351                         *self = 24;
352                         *other = _ex1();
353                         return true;
354
355                 // f.akl*f.bkl=3*delta.ab
356                 } else if (dummy_indices.size() == 2) {
357                         int sign1, sign2;
358                         ex a = permute_free_index_to_front(ex_to_indexed(*self).get_indices(), dummy_indices, sign1);
359                         ex b = permute_free_index_to_front(ex_to_indexed(*other).get_indices(), dummy_indices, sign2);
360                         *self = sign1 * sign2 * 3 * delta_tensor(a, b);
361                         *other = _ex1();
362                         return true;
363                 }
364         }
365
366         return false;
367 }
368
369 //////////
370 // global functions
371 //////////
372
373 ex color_ONE(unsigned char rl)
374 {
375         return color(su3one(), rl);
376 }
377
378 ex color_T(const ex & a, unsigned char rl)
379 {
380         if (!is_ex_of_type(a, idx))
381                 throw(std::invalid_argument("indices of color_T must be of type idx"));
382         if (!ex_to_idx(a).get_dim().is_equal(8))
383                 throw(std::invalid_argument("index dimension for color_T must be 8"));
384
385         return color(su3t(), a, rl);
386 }
387
388 ex color_f(const ex & a, const ex & b, const ex & c)
389 {
390         if (!is_ex_of_type(a, idx) || !is_ex_of_type(b, idx) || !is_ex_of_type(c, idx))
391                 throw(std::invalid_argument("indices of color_f must be of type idx"));
392         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))
393                 throw(std::invalid_argument("index dimension for color_f must be 8"));
394
395         return indexed(su3f(), indexed::antisymmetric, a, b, c);
396 }
397
398 ex color_d(const ex & a, const ex & b, const ex & c)
399 {
400         if (!is_ex_of_type(a, idx) || !is_ex_of_type(b, idx) || !is_ex_of_type(c, idx))
401                 throw(std::invalid_argument("indices of color_d must be of type idx"));
402         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))
403                 throw(std::invalid_argument("index dimension for color_d must be 8"));
404
405         return indexed(su3d(), indexed::symmetric, a, b, c);
406 }
407
408 ex color_h(const ex & a, const ex & b, const ex & c)
409 {
410         return color_d(a, b, c) + I * color_f(a, b, c);
411 }
412
413 /** Check whether a given tinfo key (as returned by return_type_tinfo()
414  *  is that of a color object with the specified representation label. */
415 static bool is_color_tinfo(unsigned ti, unsigned char rl)
416 {
417         return ti == (TINFO_color + rl);
418 }
419
420 ex color_trace(const ex & e, unsigned char rl)
421 {
422         if (is_ex_of_type(e, color)) {
423
424                 if (ex_to_color(e).get_representation_label() == rl
425                  && is_ex_of_type(e.op(0), su3one))
426                         return _ex3();
427                 else
428                         return _ex0();
429
430         } else if (is_ex_exactly_of_type(e, add)) {
431
432                 // Trace of sum = sum of traces
433                 ex sum = _ex0();
434                 for (unsigned i=0; i<e.nops(); i++)
435                         sum += color_trace(e.op(i), rl);
436                 return sum;
437
438         } else if (is_ex_exactly_of_type(e, mul)) {
439
440                 // Trace of product: pull out non-color factors
441                 ex prod = _ex1();
442                 for (unsigned i=0; i<e.nops(); i++) {
443                         const ex &o = e.op(i);
444                         if (is_color_tinfo(o.return_type_tinfo(), rl))
445                                 prod *= color_trace(o, rl);
446                         else
447                                 prod *= o;
448                 }
449                 return prod;
450
451         } else if (is_ex_exactly_of_type(e, ncmul)) {
452
453                 if (!is_color_tinfo(e.return_type_tinfo(), rl))
454                         return _ex0();
455
456                 // Expand product, if necessary
457                 ex e_expanded = e.expand();
458                 if (!is_ex_of_type(e_expanded, ncmul))
459                         return color_trace(e_expanded, rl);
460
461                 unsigned num = e.nops();
462
463                 if (num == 2) {
464
465                         // Tr T_a T_b = 1/2 delta_a_b
466                         return delta_tensor(e.op(0).op(1), e.op(1).op(1)) / 2;
467
468                 } else if (num == 3) {
469
470                         // Tr T_a T_b T_c = 1/4 h_a_b_c
471                         return color_h(e.op(0).op(1), e.op(1).op(1), e.op(2).op(1)) / 4;
472
473                 } else {
474
475                         // Traces of 4 or more generators are computed recursively:
476                         // Tr T_a1 .. T_an =
477                         //     1/6 delta_a(n-1)_an Tr T_a1 .. T_a(n-2)
478                         //   + 1/2 h_a(n-1)_an_k Tr T_a1 .. T_a(n-2) T_k
479                         const ex &last_index = e.op(num - 1).op(1);
480                         const ex &next_to_last_index = e.op(num - 2).op(1);
481                         idx summation_index((new symbol)->setflag(status_flags::dynallocated), 8);
482
483                         exvector v1;
484                         v1.reserve(num - 2);
485                         for (int i=0; i<num-2; i++)
486                                 v1.push_back(e.op(i));
487
488                         exvector v2 = v1;
489                         v2.push_back(color_T(summation_index, rl));
490
491                         return delta_tensor(next_to_last_index, last_index) * color_trace(ncmul(v1), rl) / 6
492                                + color_h(next_to_last_index, last_index, summation_index) * color_trace(ncmul(v2), rl) / 2;
493                 }
494         }
495
496         return _ex0();
497 }
498
499 } // namespace GiNaC