]> www.ginac.de Git - ginac.git/blob - ginac/tensor.cpp
- added symmetrize() and antisymmetrize() functions
[ginac.git] / ginac / tensor.cpp
1 /** @file tensor.cpp
2  *
3  *  Implementation of GiNaC's special tensors. */
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 <stdexcept>
24 #include <vector>
25
26 #include "tensor.h"
27 #include "idx.h"
28 #include "indexed.h"
29 #include "relational.h"
30 #include "lst.h"
31 #include "numeric.h"
32 #include "print.h"
33 #include "archive.h"
34 #include "utils.h"
35 #include "debugmsg.h"
36
37 namespace GiNaC {
38
39 GINAC_IMPLEMENT_REGISTERED_CLASS(tensor, basic)
40 GINAC_IMPLEMENT_REGISTERED_CLASS(tensdelta, tensor)
41 GINAC_IMPLEMENT_REGISTERED_CLASS(tensmetric, tensor)
42 GINAC_IMPLEMENT_REGISTERED_CLASS(minkmetric, tensmetric)
43 GINAC_IMPLEMENT_REGISTERED_CLASS(spinmetric, tensmetric)
44 GINAC_IMPLEMENT_REGISTERED_CLASS(tensepsilon, tensor)
45
46 //////////
47 // default constructor, destructor, copy constructor assignment operator and helpers
48 //////////
49
50 tensor::tensor(unsigned ti) : inherited(ti)
51 {
52         debugmsg("tensor constructor from unsigned", LOGLEVEL_CONSTRUCT); \
53 }
54
55 DEFAULT_CTORS(tensor)
56 DEFAULT_CTORS(tensdelta)
57 DEFAULT_CTORS(tensmetric)
58 DEFAULT_COPY(spinmetric)
59 DEFAULT_DESTROY(spinmetric)
60 DEFAULT_DESTROY(minkmetric)
61 DEFAULT_DESTROY(tensepsilon)
62
63 minkmetric::minkmetric() : pos_sig(false)
64 {
65         debugmsg("minkmetric default constructor", LOGLEVEL_CONSTRUCT);
66         tinfo_key = TINFO_minkmetric;
67 }
68
69 spinmetric::spinmetric()
70 {
71         debugmsg("spinmetric default constructor", LOGLEVEL_CONSTRUCT);
72         tinfo_key = TINFO_spinmetric;
73 }
74
75 minkmetric::minkmetric(bool ps) : pos_sig(ps)
76 {
77         debugmsg("minkmetric constructor from bool", LOGLEVEL_CONSTRUCT);
78         tinfo_key = TINFO_minkmetric;
79 }
80
81 void minkmetric::copy(const minkmetric & other)
82 {
83         inherited::copy(other);
84         pos_sig = other.pos_sig;
85 }
86
87 tensepsilon::tensepsilon() : minkowski(false), pos_sig(false)
88 {
89         debugmsg("tensepsilon default constructor", LOGLEVEL_CONSTRUCT);
90         tinfo_key = TINFO_tensepsilon;
91 }
92
93 tensepsilon::tensepsilon(bool mink, bool ps) : minkowski(mink), pos_sig(ps)
94 {
95         debugmsg("tensepsilon constructor from bool,bool", LOGLEVEL_CONSTRUCT);
96         tinfo_key = TINFO_tensepsilon;
97 }
98
99 void tensepsilon::copy(const tensepsilon & other)
100 {
101         inherited::copy(other);
102         minkowski = other.minkowski;
103         pos_sig = other.pos_sig;
104 }
105
106 //////////
107 // archiving
108 //////////
109
110 DEFAULT_ARCHIVING(tensor)
111 DEFAULT_ARCHIVING(tensdelta)
112 DEFAULT_ARCHIVING(tensmetric)
113 DEFAULT_ARCHIVING(spinmetric)
114 DEFAULT_UNARCHIVE(minkmetric)
115 DEFAULT_UNARCHIVE(tensepsilon)
116
117 minkmetric::minkmetric(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
118 {
119         debugmsg("minkmetric constructor from archive_node", LOGLEVEL_CONSTRUCT);
120         n.find_bool("pos_sig", pos_sig);
121 }
122
123 void minkmetric::archive(archive_node &n) const
124 {
125         inherited::archive(n);
126         n.add_bool("pos_sig", pos_sig);
127 }
128
129 tensepsilon::tensepsilon(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
130 {
131         debugmsg("tensepsilon constructor from archive_node", LOGLEVEL_CONSTRUCT);
132         n.find_bool("minkowski", minkowski);
133         n.find_bool("pos_sig", pos_sig);
134 }
135
136 void tensepsilon::archive(archive_node &n) const
137 {
138         inherited::archive(n);
139         n.add_bool("minkowski", minkowski);
140         n.add_bool("pos_sig", pos_sig);
141 }
142
143 //////////
144 // functions overriding virtual functions from bases classes
145 //////////
146
147 DEFAULT_COMPARE(tensor)
148 DEFAULT_COMPARE(tensdelta)
149 DEFAULT_COMPARE(tensmetric)
150 DEFAULT_COMPARE(spinmetric)
151
152 int minkmetric::compare_same_type(const basic & other) const
153 {
154         GINAC_ASSERT(is_of_type(other, minkmetric));
155         const minkmetric &o = static_cast<const minkmetric &>(other);
156
157         if (pos_sig != o.pos_sig)
158                 return pos_sig ? -1 : 1;
159         else
160                 return inherited::compare_same_type(other);
161 }
162
163 int tensepsilon::compare_same_type(const basic & other) const
164 {
165         GINAC_ASSERT(is_of_type(other, tensepsilon));
166         const tensepsilon &o = static_cast<const tensepsilon &>(other);
167
168         if (minkowski != o.minkowski)
169                 return minkowski ? -1 : 1;
170         else if (pos_sig != o.pos_sig)
171                 return pos_sig ? -1 : 1;
172         else
173                 return inherited::compare_same_type(other);
174 }
175
176 DEFAULT_PRINT_LATEX(tensdelta, "delta", "\\delta")
177 DEFAULT_PRINT(tensmetric, "g")
178 DEFAULT_PRINT_LATEX(minkmetric, "eta", "\\eta")
179 DEFAULT_PRINT_LATEX(spinmetric, "eps", "\\varepsilon")
180 DEFAULT_PRINT_LATEX(tensepsilon, "eps", "\\varepsilon")
181
182 /** Automatic symbolic evaluation of an indexed delta tensor. */
183 ex tensdelta::eval_indexed(const basic & i) const
184 {
185         GINAC_ASSERT(is_of_type(i, indexed));
186         GINAC_ASSERT(i.nops() == 3);
187         GINAC_ASSERT(is_ex_of_type(i.op(0), tensdelta));
188
189         const idx & i1 = ex_to_idx(i.op(1));
190         const idx & i2 = ex_to_idx(i.op(2));
191
192         // Trace of delta tensor is the dimension of the space
193         if (is_dummy_pair(i1, i2))
194                 return i1.get_dim();
195
196         // Numeric evaluation
197         if (static_cast<const indexed &>(i).all_index_values_are(info_flags::integer)) {
198                 int n1 = ex_to_numeric(i1.get_value()).to_int(), n2 = ex_to_numeric(i2.get_value()).to_int();
199                 if (n1 == n2)
200                         return _ex1();
201                 else
202                         return _ex0();
203         }
204
205         // No further simplifications
206         return i.hold();
207 }
208
209 /** Automatic symbolic evaluation of an indexed metric tensor. */
210 ex tensmetric::eval_indexed(const basic & i) const
211 {
212         GINAC_ASSERT(is_of_type(i, indexed));
213         GINAC_ASSERT(i.nops() == 3);
214         GINAC_ASSERT(is_ex_of_type(i.op(0), tensmetric));
215         GINAC_ASSERT(is_ex_of_type(i.op(1), varidx));
216         GINAC_ASSERT(is_ex_of_type(i.op(2), varidx));
217
218         const varidx & i1 = ex_to_varidx(i.op(1));
219         const varidx & i2 = ex_to_varidx(i.op(2));
220
221         // A metric tensor with one covariant and one contravariant index gets
222         // replaced by a delta tensor
223         if (i1.is_covariant() != i2.is_covariant())
224                 return delta_tensor(i1, i2);
225
226         // No further simplifications
227         return i.hold();
228 }
229
230 /** Automatic symbolic evaluation of an indexed Lorentz metric tensor. */
231 ex minkmetric::eval_indexed(const basic & i) const
232 {
233         GINAC_ASSERT(is_of_type(i, indexed));
234         GINAC_ASSERT(i.nops() == 3);
235         GINAC_ASSERT(is_ex_of_type(i.op(0), minkmetric));
236         GINAC_ASSERT(is_ex_of_type(i.op(1), varidx));
237         GINAC_ASSERT(is_ex_of_type(i.op(2), varidx));
238
239         const varidx & i1 = ex_to_varidx(i.op(1));
240         const varidx & i2 = ex_to_varidx(i.op(2));
241
242         // Numeric evaluation
243         if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
244                 int n1 = ex_to_numeric(i1.get_value()).to_int(), n2 = ex_to_numeric(i2.get_value()).to_int();
245                 if (n1 != n2)
246                         return _ex0();
247                 else if (n1 == 0)
248                         return pos_sig ? _ex_1() : _ex1();
249                 else
250                         return pos_sig ? _ex1() : _ex_1();
251         }
252
253         // Perform the usual evaluations of a metric tensor
254         return inherited::eval_indexed(i);
255 }
256
257 /** Automatic symbolic evaluation of an indexed metric tensor. */
258 ex spinmetric::eval_indexed(const basic & i) const
259 {
260         GINAC_ASSERT(is_of_type(i, indexed));
261         GINAC_ASSERT(i.nops() == 3);
262         GINAC_ASSERT(is_ex_of_type(i.op(0), spinmetric));
263         GINAC_ASSERT(is_ex_of_type(i.op(1), spinidx));
264         GINAC_ASSERT(is_ex_of_type(i.op(2), spinidx));
265
266         const spinidx & i1 = ex_to_spinidx(i.op(1));
267         const spinidx & i2 = ex_to_spinidx(i.op(2));
268
269         // Convolutions are zero
270         if (static_cast<const indexed &>(i).get_dummy_indices().size() != 0)
271                 return _ex0();
272
273         // Numeric evaluation
274         if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
275                 int n1 = ex_to_numeric(i1.get_value()).to_int(), n2 = ex_to_numeric(i2.get_value()).to_int();
276                 if (n1 == n2)
277                         return _ex0();
278                 else if (n1 < n2)
279                         return _ex1();
280                 else
281                         return _ex_1();
282         }
283
284         // No further simplifications
285         return i.hold();
286 }
287
288 /** Automatic symbolic evaluation of an indexed epsilon tensor. */
289 ex tensepsilon::eval_indexed(const basic & i) const
290 {
291         GINAC_ASSERT(is_of_type(i, indexed));
292         GINAC_ASSERT(i.nops() > 1);
293         GINAC_ASSERT(is_ex_of_type(i.op(0), tensepsilon));
294
295         // Convolutions are zero
296         if (static_cast<const indexed &>(i).get_dummy_indices().size() != 0)
297                 return _ex0();
298
299         // Numeric evaluation
300         if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
301
302                 // Get sign of index permutation (the indices should already be in
303                 // a canonic order but we can't assume what exactly that order is)
304                 std::vector<int> v;
305                 v.reserve(i.nops() - 1);
306                 for (unsigned j=1; j<i.nops(); j++)
307                         v.push_back(ex_to_numeric(ex_to_idx(i.op(j)).get_value()).to_int());
308                 int sign = permutation_sign(v.begin(), v.end());
309
310                 // In a Minkowski space, check for covariant indices
311                 if (minkowski) {
312                         for (unsigned j=1; j<i.nops(); j++) {
313                                 const ex & x = i.op(j);
314                                 if (!is_ex_of_type(x, varidx))
315                                         throw(std::runtime_error("indices of epsilon tensor in Minkowski space must be of type varidx"));
316                                 if (ex_to_varidx(x).is_covariant())
317                                         if (ex_to_idx(x).get_value().is_zero())
318                                                 sign = (pos_sig ? -sign : sign);
319                                         else
320                                                 sign = (pos_sig ? sign : -sign);
321                         }
322                 }
323
324                 return sign;
325         }
326
327         // No further simplifications
328         return i.hold();
329 }
330
331 /** Contraction of an indexed delta tensor with something else. */
332 bool tensdelta::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() == 3);
337         GINAC_ASSERT(is_ex_of_type(self->op(0), tensdelta));
338
339         // Try to contract first index
340         const idx *self_idx = &ex_to_idx(self->op(1));
341         const idx *free_idx = &ex_to_idx(self->op(2));
342         bool first_index_tried = false;
343
344 again:
345         if (self_idx->is_symbolic()) {
346                 for (int i=1; i<other->nops(); i++) {
347                         const idx &other_idx = ex_to_idx(other->op(i));
348                         if (is_dummy_pair(*self_idx, other_idx)) {
349
350                                 // Contraction found, remove delta tensor and substitute
351                                 // index in second object
352                                 *self = _ex1();
353                                 *other = other->subs(other_idx == *free_idx);
354                                 return true;
355                         }
356                 }
357         }
358
359         if (!first_index_tried) {
360
361                 // No contraction with first index found, try second index
362                 self_idx = &ex_to_idx(self->op(2));
363                 free_idx = &ex_to_idx(self->op(1));
364                 first_index_tried = true;
365                 goto again;
366         }
367
368         return false;
369 }
370
371 /** Contraction of an indexed metric tensor with something else. */
372 bool tensmetric::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
373 {
374         GINAC_ASSERT(is_ex_of_type(*self, indexed));
375         GINAC_ASSERT(is_ex_of_type(*other, indexed));
376         GINAC_ASSERT(self->nops() == 3);
377         GINAC_ASSERT(is_ex_of_type(self->op(0), tensmetric));
378
379         // If contracting with the delta tensor, let the delta do it
380         // (don't raise/lower delta indices)
381         if (is_ex_of_type(other->op(0), tensdelta))
382                 return false;
383
384         // Try to contract first index
385         const idx *self_idx = &ex_to_idx(self->op(1));
386         const idx *free_idx = &ex_to_idx(self->op(2));
387         bool first_index_tried = false;
388
389 again:
390         if (self_idx->is_symbolic()) {
391                 for (int i=1; i<other->nops(); i++) {
392                         const idx &other_idx = ex_to_idx(other->op(i));
393                         if (is_dummy_pair(*self_idx, other_idx)) {
394
395                                 // Contraction found, remove metric tensor and substitute
396                                 // index in second object
397                                 *self = _ex1();
398                                 *other = other->subs(other_idx == *free_idx);
399                                 return true;
400                         }
401                 }
402         }
403
404         if (!first_index_tried) {
405
406                 // No contraction with first index found, try second index
407                 self_idx = &ex_to_idx(self->op(2));
408                 free_idx = &ex_to_idx(self->op(1));
409                 first_index_tried = true;
410                 goto again;
411         }
412
413         return false;
414 }
415
416 /** Contraction of an indexed spinor metric with something else. */
417 bool spinmetric::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
418 {
419         GINAC_ASSERT(is_ex_of_type(*self, indexed));
420         GINAC_ASSERT(is_ex_of_type(*other, indexed));
421         GINAC_ASSERT(self->nops() == 3);
422         GINAC_ASSERT(is_ex_of_type(self->op(0), spinmetric));
423
424         // Contractions between spinor metrics
425         if (is_ex_of_type(other->op(0), spinmetric)) {
426                 const idx &self_i1 = ex_to_idx(self->op(1));
427                 const idx &self_i2 = ex_to_idx(self->op(2));
428                 const idx &other_i1 = ex_to_idx(other->op(1));
429                 const idx &other_i2 = ex_to_idx(other->op(2));
430
431                 if (is_dummy_pair(self_i1, other_i1)) {
432                         if (is_dummy_pair(self_i2, other_i2))
433                                 *self = _ex2();
434                         else
435                                 *self = delta_tensor(self_i2, other_i2);
436                         *other = _ex1();
437                         return true;
438                 } else if (is_dummy_pair(self_i1, other_i2)) {
439                         if (is_dummy_pair(self_i2, other_i1))
440                                 *self = _ex_2();
441                         else
442                                 *self = -delta_tensor(self_i2, other_i1);
443                         *other = _ex1();
444                         return true;
445                 } else if (is_dummy_pair(self_i2, other_i1)) {
446                         *self = -delta_tensor(self_i1, other_i2);
447                         *other = _ex1();
448                         return true;
449                 } else if (is_dummy_pair(self_i2, other_i2)) {
450                         *self = delta_tensor(self_i1, other_i1);
451                         *other = _ex1();
452                         return true;
453                 }
454         }
455
456         // If contracting with the delta tensor, let the delta do it
457         // (don't raise/lower delta indices)
458         if (is_ex_of_type(other->op(0), tensdelta))
459                 return false;
460
461         // Try to contract first index
462         const idx *self_idx = &ex_to_idx(self->op(1));
463         const idx *free_idx = &ex_to_idx(self->op(2));
464         bool first_index_tried = false;
465         int sign = 1;
466
467 again:
468         if (self_idx->is_symbolic()) {
469                 for (int i=1; i<other->nops(); i++) {
470                         const idx &other_idx = ex_to_idx(other->op(i));
471                         if (is_dummy_pair(*self_idx, other_idx)) {
472
473                                 // Contraction found, remove metric tensor and substitute
474                                 // index in second object
475                                 *self = (static_cast<const spinidx *>(self_idx)->is_covariant() ? sign : -sign);
476                                 *other = other->subs(other_idx == *free_idx);
477                                 return true;
478                         }
479                 }
480         }
481
482         if (!first_index_tried) {
483
484                 // No contraction with first index found, try second index
485                 self_idx = &ex_to_idx(self->op(2));
486                 free_idx = &ex_to_idx(self->op(1));
487                 first_index_tried = true;
488                 sign = -sign;
489                 goto again;
490         }
491
492         return false;
493 }
494
495 //////////
496 // global functions
497 //////////
498
499 ex delta_tensor(const ex & i1, const ex & i2)
500 {
501         if (!is_ex_of_type(i1, idx) || !is_ex_of_type(i2, idx))
502                 throw(std::invalid_argument("indices of delta tensor must be of type idx"));
503
504         return indexed(tensdelta(), indexed::symmetric, i1, i2);
505 }
506
507 ex metric_tensor(const ex & i1, const ex & i2)
508 {
509         if (!is_ex_of_type(i1, varidx) || !is_ex_of_type(i2, varidx))
510                 throw(std::invalid_argument("indices of metric tensor must be of type varidx"));
511
512         return indexed(tensmetric(), indexed::symmetric, i1, i2);
513 }
514
515 ex lorentz_g(const ex & i1, const ex & i2, bool pos_sig)
516 {
517         if (!is_ex_of_type(i1, varidx) || !is_ex_of_type(i2, varidx))
518                 throw(std::invalid_argument("indices of metric tensor must be of type varidx"));
519
520         return indexed(minkmetric(pos_sig), indexed::symmetric, i1, i2);
521 }
522
523 ex spinor_metric(const ex & i1, const ex & i2)
524 {
525         if (!is_ex_of_type(i1, spinidx) || !is_ex_of_type(i2, spinidx))
526                 throw(std::invalid_argument("indices of spinor metric must be of type spinidx"));
527         if (!ex_to_idx(i1).get_dim().is_equal(2) || !ex_to_idx(i2).get_dim().is_equal(2))
528                 throw(std::runtime_error("index dimension for spinor metric must be 2"));
529
530         return indexed(spinmetric(), indexed::antisymmetric, i1, i2);
531 }
532
533 ex epsilon_tensor(const ex & i1, const ex & i2)
534 {
535         if (!is_ex_of_type(i1, idx) || !is_ex_of_type(i2, idx))
536                 throw(std::invalid_argument("indices of epsilon tensor must be of type idx"));
537
538         ex dim = ex_to_idx(i1).get_dim();
539         if (!dim.is_equal(ex_to_idx(i2).get_dim()))
540                 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
541         if (!ex_to_idx(i1).get_dim().is_equal(_ex2()))
542                 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
543
544         return indexed(tensepsilon(), indexed::antisymmetric, i1, i2);
545 }
546
547 ex epsilon_tensor(const ex & i1, const ex & i2, const ex & i3)
548 {
549         if (!is_ex_of_type(i1, idx) || !is_ex_of_type(i2, idx) || !is_ex_of_type(i3, idx))
550                 throw(std::invalid_argument("indices of epsilon tensor must be of type idx"));
551
552         ex dim = ex_to_idx(i1).get_dim();
553         if (!dim.is_equal(ex_to_idx(i2).get_dim()) || !dim.is_equal(ex_to_idx(i3).get_dim()))
554                 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
555         if (!ex_to_idx(i1).get_dim().is_equal(_ex3()))
556                 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
557
558         return indexed(tensepsilon(), indexed::antisymmetric, i1, i2, i3);
559 }
560
561 ex lorentz_eps(const ex & i1, const ex & i2, const ex & i3, const ex & i4, bool pos_sig)
562 {
563         if (!is_ex_of_type(i1, varidx) || !is_ex_of_type(i2, varidx) || !is_ex_of_type(i3, varidx) || !is_ex_of_type(i4, varidx))
564                 throw(std::invalid_argument("indices of Lorentz epsilon tensor must be of type varidx"));
565
566         ex dim = ex_to_idx(i1).get_dim();
567         if (!dim.is_equal(ex_to_idx(i2).get_dim()) || !dim.is_equal(ex_to_idx(i3).get_dim()) || !dim.is_equal(ex_to_idx(i4).get_dim()))
568                 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
569         if (!ex_to_idx(i1).get_dim().is_equal(_ex4()))
570                 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
571
572         return indexed(tensepsilon(true, pos_sig), indexed::antisymmetric, i1, i2, i3, i4);
573 }
574
575 ex eps0123(const ex & i1, const ex & i2, const ex & i3, const ex & i4, bool pos_sig)
576 {
577         if (!is_ex_of_type(i1, varidx) || !is_ex_of_type(i2, varidx) || !is_ex_of_type(i3, varidx) || !is_ex_of_type(i4, varidx))
578                 throw(std::invalid_argument("indices of epsilon tensor must be of type varidx"));
579
580         ex dim = ex_to_idx(i1).get_dim();
581         if (dim.is_equal(4))
582                 return lorentz_eps(i1, i2, i3, i4, pos_sig);
583         else
584                 return indexed(tensepsilon(true, pos_sig), indexed::antisymmetric, i1, i2, i3, i4);
585 }
586
587 } // namespace GiNaC