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