- dummy index recognition in products was flawed: A.i.i*B.j.j would be
[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 "archive.h"
33 #include "debugmsg.h"
34 #include "utils.h"
35
36 namespace GiNaC {
37
38 GINAC_IMPLEMENT_REGISTERED_CLASS(color, indexed)
39 GINAC_IMPLEMENT_REGISTERED_CLASS(su3one, tensor)
40 GINAC_IMPLEMENT_REGISTERED_CLASS(su3t, tensor)
41 GINAC_IMPLEMENT_REGISTERED_CLASS(su3f, tensor)
42 GINAC_IMPLEMENT_REGISTERED_CLASS(su3d, tensor)
43
44 //////////
45 // default constructor, destructor, copy constructor assignment operator and helpers
46 //////////
47
48 color::color() : representation_label(0)
49 {
50         debugmsg("color default constructor", LOGLEVEL_CONSTRUCT);
51         tinfo_key = TINFO_color;
52 }
53
54 void color::copy(const color & other)
55 {
56         inherited::copy(other);
57         representation_label = other.representation_label;
58 }
59
60 DEFAULT_DESTROY(color)
61 DEFAULT_CTORS(su3one)
62 DEFAULT_CTORS(su3t)
63 DEFAULT_CTORS(su3f)
64 DEFAULT_CTORS(su3d)
65
66 //////////
67 // other constructors
68 //////////
69
70 /** Construct object without any color index. This constructor is for
71  *  internal use only. Use the color_ONE() function instead.
72  *  @see color_ONE */
73 color::color(const ex & b, unsigned rl = 0) : inherited(b), representation_label(rl)
74 {
75         debugmsg("color constructor from ex,unsigned", LOGLEVEL_CONSTRUCT);
76         tinfo_key = TINFO_color;
77 }
78
79 /** Construct object with one color index. This constructor is for internal
80  *  use only. Use the color_T() function instead.
81  *  @see color_T */
82 color::color(const ex & b, const ex & i1, unsigned rl = 0) : inherited(b, i1), representation_label(rl)
83 {
84         debugmsg("color constructor from ex,ex,unsigned", LOGLEVEL_CONSTRUCT);
85         tinfo_key = TINFO_color;
86 }
87
88 color::color(unsigned rl, const exvector & v, bool discardable) : inherited(indexed::unknown, v, discardable), representation_label(rl)
89 {
90         debugmsg("color constructor from unsigned,exvector", LOGLEVEL_CONSTRUCT);
91         tinfo_key = TINFO_color;
92 }
93
94 color::color(unsigned rl, exvector * vp) : inherited(indexed::unknown, vp), representation_label(rl)
95 {
96         debugmsg("color constructor from unsigned,exvector *", LOGLEVEL_CONSTRUCT);
97         tinfo_key = TINFO_color;
98 }
99
100 //////////
101 // archiving
102 //////////
103
104 color::color(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
105 {
106         debugmsg("color constructor from archive_node", LOGLEVEL_CONSTRUCT);
107         n.find_unsigned("representation", representation_label);
108 }
109
110 void color::archive(archive_node &n) const
111 {
112         inherited::archive(n);
113         n.add_unsigned("representation", representation_label);
114 }
115
116 DEFAULT_UNARCHIVE(color)
117 DEFAULT_ARCHIVING(su3one)
118 DEFAULT_ARCHIVING(su3t)
119 DEFAULT_ARCHIVING(su3f)
120 DEFAULT_ARCHIVING(su3d)
121
122 //////////
123 // functions overriding virtual functions from bases classes
124 //////////
125
126 int color::compare_same_type(const basic & other) const
127 {
128         GINAC_ASSERT(other.tinfo() == TINFO_color);
129         const color &o = static_cast<const color &>(other);
130
131         if (representation_label != o.representation_label) {
132                 // different representation label
133                 return representation_label < o.representation_label ? -1 : 1;
134         }
135
136         return inherited::compare_same_type(other);
137 }
138
139 DEFAULT_COMPARE(su3one)
140 DEFAULT_COMPARE(su3t)
141 DEFAULT_COMPARE(su3f)
142 DEFAULT_COMPARE(su3d)
143
144 DEFAULT_PRINT(su3one, "ONE")
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::simplify_ncmul(const exvector & v) const
152 {
153         //!! TODO: sort by representation label
154         exvector s;
155         s.reserve(v.size());
156
157         exvector::const_iterator it = v.begin(), itend = v.end();
158         while (it != itend) {
159                 if (!is_ex_of_type(it->op(0), su3one))
160                         s.push_back(*it);
161                 it++;
162         }
163
164         if (s.size() == 0)
165                 return color(su3one());
166         else if (s.size() == v.size())
167                 return simplified_ncmul(v);
168         else
169                 return simplified_ncmul(s);
170 }
171
172 ex color::thisexprseq(const exvector & v) const
173 {
174         return color(representation_label, v);
175 }
176
177 ex color::thisexprseq(exvector * vp) const
178 {
179         return color(representation_label, vp);
180 }
181
182 /** Given a vector iv3 of three indices and a vector iv2 of two indices that
183  *  is a subset of iv3, return the (free) index that is in iv3 but not in
184  *  iv2 and the sign introduced by permuting that index to the front.
185  *
186  *  @param iv3 Vector of 3 indices
187  *  @param iv2 Vector of 2 indices, must be a subset of iv3
188  *  @param sig Returs sign introduced by index permutation
189  *  @return the free index (the one that is in iv3 but not in iv2) */
190 static ex permute_free_index_to_front(const exvector & iv3, const exvector & iv2, int & sig)
191 {
192         GINAC_ASSERT(iv3.size() == 3);
193         GINAC_ASSERT(iv2.size() == 2);
194
195         sig = 1;
196
197 #define TEST_PERMUTATION(A,B,C,P) \
198         if (iv3[B].is_equal(iv2[0]) && iv3[C].is_equal(iv2[1])) { \
199                 sig = P; \
200                 return iv3[A]; \
201         }
202         
203         TEST_PERMUTATION(0,1,2,  1);
204         TEST_PERMUTATION(0,2,1, -1);
205         TEST_PERMUTATION(1,0,2, -1);
206         TEST_PERMUTATION(1,2,0,  1);
207         TEST_PERMUTATION(2,0,1,  1);
208         TEST_PERMUTATION(2,1,0, -1);
209
210         throw(std::logic_error("permute_free_index_to_front(): no valid permutation found"));
211 }
212
213 /** Automatic symbolic evaluation of indexed symmetric structure constant. */
214 ex su3d::eval_indexed(const basic & i) const
215 {
216         GINAC_ASSERT(is_of_type(i, indexed));
217         GINAC_ASSERT(i.nops() == 4);
218         GINAC_ASSERT(is_ex_of_type(i.op(0), su3d));
219
220         // Convolutions are zero
221         if (static_cast<const indexed &>(i).get_dummy_indices().size() != 0)
222                 return _ex0();
223
224         // Numeric evaluation
225         if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
226
227                 // Sort indices
228                 int v[3];
229                 for (unsigned j=0; j<3; j++)
230                         v[j] = ex_to_numeric(ex_to_idx(i.op(j + 1)).get_value()).to_int();
231                 if (v[0] > v[1]) std::swap(v[0], v[1]);
232                 if (v[0] > v[2]) std::swap(v[0], v[2]);
233                 if (v[1] > v[2]) std::swap(v[1], v[2]);
234
235 #define CMPINDICES(A,B,C) ((v[0] == (A)) && (v[1] == (B)) && (v[2] == (C)))
236
237                 // Check for non-zero elements
238                 if (CMPINDICES(1,4,6) || CMPINDICES(1,5,7) || CMPINDICES(2,5,6)
239                  || CMPINDICES(3,4,4) || CMPINDICES(3,5,5))
240                         return _ex1_2();
241                 else if (CMPINDICES(2,4,7) || CMPINDICES(3,6,6) || CMPINDICES(3,7,7))
242                         return _ex_1_2();
243                 else if (CMPINDICES(1,1,8) || CMPINDICES(2,2,8) || CMPINDICES(3,3,8))
244                         return sqrt(_ex3())/3;
245                 else if (CMPINDICES(8,8,8))
246                         return -sqrt(_ex3())/3;
247                 else if (CMPINDICES(4,4,8) || CMPINDICES(5,5,8)
248                       || CMPINDICES(6,6,8) || CMPINDICES(7,7,8))
249                         return -sqrt(_ex3())/6;
250                 else
251                         return _ex0();
252         }
253
254         // No further simplifications
255         return i.hold();
256 }
257
258 /** Automatic symbolic evaluation of indexed antisymmetric structure constant. */
259 ex su3f::eval_indexed(const basic & i) const
260 {
261         GINAC_ASSERT(is_of_type(i, indexed));
262         GINAC_ASSERT(i.nops() == 4);
263         GINAC_ASSERT(is_ex_of_type(i.op(0), su3f));
264
265         // Numeric evaluation
266         if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
267
268                 // Sort indices, remember permutation sign
269                 int v[3];
270                 for (unsigned j=0; j<3; j++)
271                         v[j] = ex_to_numeric(ex_to_idx(i.op(j + 1)).get_value()).to_int();
272                 int sign = 1;
273                 if (v[0] > v[1]) { std::swap(v[0], v[1]); sign = -sign; }
274                 if (v[0] > v[2]) { std::swap(v[0], v[2]); sign = -sign; }
275                 if (v[1] > v[2]) { std::swap(v[1], v[2]); sign = -sign; }
276
277                 // Check for non-zero elements
278                 if (CMPINDICES(1,2,3))
279                         return sign;
280                 else if (CMPINDICES(1,4,7) || CMPINDICES(2,4,6)
281                       || CMPINDICES(2,5,7) || CMPINDICES(3,4,5))
282                         return _ex1_2() * sign;
283                 else if (CMPINDICES(1,5,6) || CMPINDICES(3,6,7))
284                         return _ex_1_2() * sign;
285                 else if (CMPINDICES(4,5,8) || CMPINDICES(6,7,8))
286                         return sqrt(_ex3())/2 * sign;
287                 else
288                         return _ex0();
289         }
290
291         // No further simplifications
292         return i.hold();
293 }
294
295
296 /** Contraction of an indexed symmetric structure constant with something else. */
297 bool su3d::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
298 {
299         GINAC_ASSERT(is_ex_of_type(*self, indexed));
300         GINAC_ASSERT(is_ex_of_type(*other, indexed));
301         GINAC_ASSERT(self->nops() == 4);
302         GINAC_ASSERT(is_ex_of_type(self->op(0), su3d));
303
304         if (is_ex_exactly_of_type(other->op(0), su3d)) {
305
306                 // Find the dummy indices of the contraction
307                 exvector dummy_indices;
308                 dummy_indices = ex_to_indexed(*self).get_dummy_indices(ex_to_indexed(*other));
309
310                 // d.abc*d.abc=40/3
311                 if (dummy_indices.size() == 3) {
312                         *self = numeric(40, 3);
313                         *other = _ex1();
314                         return true;
315
316                 // d.akl*d.bkl=5/3*delta.ab
317                 } else if (dummy_indices.size() == 2) {
318                         exvector a = index_set_difference(ex_to_indexed(*self).get_indices(), dummy_indices);
319                         exvector b = index_set_difference(ex_to_indexed(*other).get_indices(), dummy_indices);
320                         GINAC_ASSERT(a.size() > 0);
321                         GINAC_ASSERT(b.size() > 0);
322                         *self = numeric(5, 3) * delta_tensor(a[0], b[0]);
323                         *other = _ex1();
324                         return true;
325                 }
326         }
327
328         return false;
329 }
330
331 /** Contraction of an indexed antisymmetric structure constant with something else. */
332 bool su3f::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
333 {
334         GINAC_ASSERT(is_ex_of_type(*self, indexed));
335         GINAC_ASSERT(is_ex_of_type(*other, indexed));
336         GINAC_ASSERT(self->nops() == 4);
337         GINAC_ASSERT(is_ex_of_type(self->op(0), su3f));
338
339         if (is_ex_exactly_of_type(other->op(0), su3f)) { // f*d is handled by su3d class
340
341                 // Find the dummy indices of the contraction
342                 exvector dummy_indices;
343                 dummy_indices = ex_to_indexed(*self).get_dummy_indices(ex_to_indexed(*other));
344
345                 // f.abc*f.abc=24
346                 if (dummy_indices.size() == 3) {
347                         *self = 24;
348                         *other = _ex1();
349                         return true;
350
351                 // f.akl*f.bkl=3*delta.ab
352                 } else if (dummy_indices.size() == 2) {
353                         int sign1, sign2;
354                         ex a = permute_free_index_to_front(ex_to_indexed(*self).get_indices(), dummy_indices, sign1);
355                         ex b = permute_free_index_to_front(ex_to_indexed(*other).get_indices(), dummy_indices, sign2);
356                         *self = sign1 * sign2 * 3 * delta_tensor(a, b);
357                         *other = _ex1();
358                         return true;
359                 }
360         }
361
362         return false;
363 }
364
365 //////////
366 // global functions
367 //////////
368
369 ex color_ONE(unsigned rl)
370 {
371         return color(su3one(), rl);
372 }
373
374 ex color_T(const ex & a, unsigned rl)
375 {
376         if (!is_ex_of_type(a, idx))
377                 throw(std::invalid_argument("indices of color_T must be of type idx"));
378         if (!ex_to_idx(a).get_dim().is_equal(8))
379                 throw(std::invalid_argument("index dimension for color_T must be 8"));
380
381         return color(su3t(), a, rl);
382 }
383
384 ex color_f(const ex & a, const ex & b, const ex & c)
385 {
386         if (!is_ex_of_type(a, idx) || !is_ex_of_type(b, idx) || !is_ex_of_type(c, idx))
387                 throw(std::invalid_argument("indices of color_f must be of type idx"));
388         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))
389                 throw(std::invalid_argument("index dimension for color_f must be 8"));
390
391         return indexed(su3f(), indexed::antisymmetric, a, b, c);
392 }
393
394 ex color_d(const ex & a, const ex & b, const ex & c)
395 {
396         if (!is_ex_of_type(a, idx) || !is_ex_of_type(b, idx) || !is_ex_of_type(c, idx))
397                 throw(std::invalid_argument("indices of color_d must be of type idx"));
398         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))
399                 throw(std::invalid_argument("index dimension for color_d must be 8"));
400
401         return indexed(su3d(), indexed::symmetric, a, b, c);
402 }
403
404 ex color_h(const ex & a, const ex & b, const ex & c)
405 {
406         return color_d(a, b, c) + I * color_f(a, b, c);
407 }
408
409 } // namespace GiNaC