]> www.ginac.de Git - ginac.git/blob - ginac/color.cpp
- color/clifford objects have representation label to distinguish elements
[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 "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(su3one, "ONE")
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         //!! TODO: sort by representation label
157         exvector s;
158         s.reserve(v.size());
159
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());
169         else if (s.size() == v.size())
170                 return simplified_ncmul(v);
171         else
172                 return simplified_ncmul(s);
173 }
174
175 ex color::thisexprseq(const exvector & v) const
176 {
177         return color(representation_label, v);
178 }
179
180 ex color::thisexprseq(exvector * vp) const
181 {
182         return color(representation_label, vp);
183 }
184
185 /** Given a vector iv3 of three indices and a vector iv2 of two indices that
186  *  is a subset of iv3, return the (free) index that is in iv3 but not in
187  *  iv2 and the sign introduced by permuting that index to the front.
188  *
189  *  @param iv3 Vector of 3 indices
190  *  @param iv2 Vector of 2 indices, must be a subset of iv3
191  *  @param sig Returs sign introduced by index permutation
192  *  @return the free index (the one that is in iv3 but not in iv2) */
193 static ex permute_free_index_to_front(const exvector & iv3, const exvector & iv2, int & sig)
194 {
195         GINAC_ASSERT(iv3.size() == 3);
196         GINAC_ASSERT(iv2.size() == 2);
197
198         sig = 1;
199
200 #define TEST_PERMUTATION(A,B,C,P) \
201         if (iv3[B].is_equal(iv2[0]) && iv3[C].is_equal(iv2[1])) { \
202                 sig = P; \
203                 return iv3[A]; \
204         }
205         
206         TEST_PERMUTATION(0,1,2,  1);
207         TEST_PERMUTATION(0,2,1, -1);
208         TEST_PERMUTATION(1,0,2, -1);
209         TEST_PERMUTATION(1,2,0,  1);
210         TEST_PERMUTATION(2,0,1,  1);
211         TEST_PERMUTATION(2,1,0, -1);
212
213         throw(std::logic_error("permute_free_index_to_front(): no valid permutation found"));
214 }
215
216 /** Automatic symbolic evaluation of indexed symmetric structure constant. */
217 ex su3d::eval_indexed(const basic & i) const
218 {
219         GINAC_ASSERT(is_of_type(i, indexed));
220         GINAC_ASSERT(i.nops() == 4);
221         GINAC_ASSERT(is_ex_of_type(i.op(0), su3d));
222
223         // Convolutions are zero
224         if (static_cast<const indexed &>(i).get_dummy_indices().size() != 0)
225                 return _ex0();
226
227         // Numeric evaluation
228         if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
229
230                 // Sort indices
231                 int v[3];
232                 for (unsigned j=0; j<3; j++)
233                         v[j] = ex_to_numeric(ex_to_idx(i.op(j + 1)).get_value()).to_int();
234                 if (v[0] > v[1]) std::swap(v[0], v[1]);
235                 if (v[0] > v[2]) std::swap(v[0], v[2]);
236                 if (v[1] > v[2]) std::swap(v[1], v[2]);
237
238 #define CMPINDICES(A,B,C) ((v[0] == (A)) && (v[1] == (B)) && (v[2] == (C)))
239
240                 // Check for non-zero elements
241                 if (CMPINDICES(1,4,6) || CMPINDICES(1,5,7) || CMPINDICES(2,5,6)
242                  || CMPINDICES(3,4,4) || CMPINDICES(3,5,5))
243                         return _ex1_2();
244                 else if (CMPINDICES(2,4,7) || CMPINDICES(3,6,6) || CMPINDICES(3,7,7))
245                         return _ex_1_2();
246                 else if (CMPINDICES(1,1,8) || CMPINDICES(2,2,8) || CMPINDICES(3,3,8))
247                         return sqrt(_ex3())/3;
248                 else if (CMPINDICES(8,8,8))
249                         return -sqrt(_ex3())/3;
250                 else if (CMPINDICES(4,4,8) || CMPINDICES(5,5,8)
251                       || CMPINDICES(6,6,8) || CMPINDICES(7,7,8))
252                         return -sqrt(_ex3())/6;
253                 else
254                         return _ex0();
255         }
256
257         // No further simplifications
258         return i.hold();
259 }
260
261 /** Automatic symbolic evaluation of indexed antisymmetric structure constant. */
262 ex su3f::eval_indexed(const basic & i) const
263 {
264         GINAC_ASSERT(is_of_type(i, indexed));
265         GINAC_ASSERT(i.nops() == 4);
266         GINAC_ASSERT(is_ex_of_type(i.op(0), su3f));
267
268         // Numeric evaluation
269         if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
270
271                 // Sort indices, remember permutation sign
272                 int v[3];
273                 for (unsigned j=0; j<3; j++)
274                         v[j] = ex_to_numeric(ex_to_idx(i.op(j + 1)).get_value()).to_int();
275                 int sign = 1;
276                 if (v[0] > v[1]) { std::swap(v[0], v[1]); sign = -sign; }
277                 if (v[0] > v[2]) { std::swap(v[0], v[2]); sign = -sign; }
278                 if (v[1] > v[2]) { std::swap(v[1], v[2]); sign = -sign; }
279
280                 // Check for non-zero elements
281                 if (CMPINDICES(1,2,3))
282                         return sign;
283                 else if (CMPINDICES(1,4,7) || CMPINDICES(2,4,6)
284                       || CMPINDICES(2,5,7) || CMPINDICES(3,4,5))
285                         return _ex1_2() * sign;
286                 else if (CMPINDICES(1,5,6) || CMPINDICES(3,6,7))
287                         return _ex_1_2() * sign;
288                 else if (CMPINDICES(4,5,8) || CMPINDICES(6,7,8))
289                         return sqrt(_ex3())/2 * sign;
290                 else
291                         return _ex0();
292         }
293
294         // No further simplifications
295         return i.hold();
296 }
297
298
299 /** Contraction of an indexed symmetric structure constant with something else. */
300 bool su3d::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
301 {
302         GINAC_ASSERT(is_ex_of_type(*self, indexed));
303         GINAC_ASSERT(is_ex_of_type(*other, indexed));
304         GINAC_ASSERT(self->nops() == 4);
305         GINAC_ASSERT(is_ex_of_type(self->op(0), su3d));
306
307         if (is_ex_exactly_of_type(other->op(0), su3d)) {
308
309                 // Find the dummy indices of the contraction
310                 exvector dummy_indices;
311                 dummy_indices = ex_to_indexed(*self).get_dummy_indices(ex_to_indexed(*other));
312
313                 // d.abc*d.abc=40/3
314                 if (dummy_indices.size() == 3) {
315                         *self = numeric(40, 3);
316                         *other = _ex1();
317                         return true;
318
319                 // d.akl*d.bkl=5/3*delta.ab
320                 } else if (dummy_indices.size() == 2) {
321                         exvector a = index_set_difference(ex_to_indexed(*self).get_indices(), dummy_indices);
322                         exvector b = index_set_difference(ex_to_indexed(*other).get_indices(), dummy_indices);
323                         GINAC_ASSERT(a.size() > 0);
324                         GINAC_ASSERT(b.size() > 0);
325                         *self = numeric(5, 3) * delta_tensor(a[0], b[0]);
326                         *other = _ex1();
327                         return true;
328                 }
329         }
330
331         return false;
332 }
333
334 /** Contraction of an indexed antisymmetric structure constant with something else. */
335 bool su3f::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
336 {
337         GINAC_ASSERT(is_ex_of_type(*self, indexed));
338         GINAC_ASSERT(is_ex_of_type(*other, indexed));
339         GINAC_ASSERT(self->nops() == 4);
340         GINAC_ASSERT(is_ex_of_type(self->op(0), su3f));
341
342         if (is_ex_exactly_of_type(other->op(0), su3f)) { // f*d is handled by su3d class
343
344                 // Find the dummy indices of the contraction
345                 exvector dummy_indices;
346                 dummy_indices = ex_to_indexed(*self).get_dummy_indices(ex_to_indexed(*other));
347
348                 // f.abc*f.abc=24
349                 if (dummy_indices.size() == 3) {
350                         *self = 24;
351                         *other = _ex1();
352                         return true;
353
354                 // f.akl*f.bkl=3*delta.ab
355                 } else if (dummy_indices.size() == 2) {
356                         int sign1, sign2;
357                         ex a = permute_free_index_to_front(ex_to_indexed(*self).get_indices(), dummy_indices, sign1);
358                         ex b = permute_free_index_to_front(ex_to_indexed(*other).get_indices(), dummy_indices, sign2);
359                         *self = sign1 * sign2 * 3 * delta_tensor(a, b);
360                         *other = _ex1();
361                         return true;
362                 }
363         }
364
365         return false;
366 }
367
368 //////////
369 // global functions
370 //////////
371
372 ex color_ONE(unsigned char rl)
373 {
374         return color(su3one(), rl);
375 }
376
377 ex color_T(const ex & a, unsigned char rl)
378 {
379         if (!is_ex_of_type(a, idx))
380                 throw(std::invalid_argument("indices of color_T must be of type idx"));
381         if (!ex_to_idx(a).get_dim().is_equal(8))
382                 throw(std::invalid_argument("index dimension for color_T must be 8"));
383
384         return color(su3t(), a, rl);
385 }
386
387 ex color_f(const ex & a, const ex & b, const ex & c)
388 {
389         if (!is_ex_of_type(a, idx) || !is_ex_of_type(b, idx) || !is_ex_of_type(c, idx))
390                 throw(std::invalid_argument("indices of color_f must be of type idx"));
391         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))
392                 throw(std::invalid_argument("index dimension for color_f must be 8"));
393
394         return indexed(su3f(), indexed::antisymmetric, a, b, c);
395 }
396
397 ex color_d(const ex & a, const ex & b, const ex & c)
398 {
399         if (!is_ex_of_type(a, idx) || !is_ex_of_type(b, idx) || !is_ex_of_type(c, idx))
400                 throw(std::invalid_argument("indices of color_d must be of type idx"));
401         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))
402                 throw(std::invalid_argument("index dimension for color_d must be 8"));
403
404         return indexed(su3d(), indexed::symmetric, a, b, c);
405 }
406
407 ex color_h(const ex & a, const ex & b, const ex & c)
408 {
409         return color_d(a, b, c) + I * color_f(a, b, c);
410 }
411
412 } // namespace GiNaC