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