Added methods for taking real and imaginary parts.
[ginac.git] / ginac / tensor.cpp
1 /** @file tensor.cpp
2  *
3  *  Implementation of GiNaC's special tensors. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2006 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(&tensor::tinfo_static)
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 = &minkmetric::tinfo_static;
78 }
79
80 spinmetric::spinmetric()
81 {
82         tinfo_key = &spinmetric::tinfo_static;
83 }
84
85 minkmetric::minkmetric(bool ps) : pos_sig(ps)
86 {
87         tinfo_key = &minkmetric::tinfo_static;
88 }
89
90 tensepsilon::tensepsilon() : minkowski(false), pos_sig(false)
91 {
92         tinfo_key = &tensepsilon::tinfo_static;
93 }
94
95 tensepsilon::tensepsilon(bool mink, bool ps) : minkowski(mink), pos_sig(ps)
96 {
97         tinfo_key = &tensepsilon::tinfo_static;
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 bool tensdelta::info(unsigned inf) const
145 {
146         if(inf == info_flags::real)
147                 return true;
148
149         return false;
150 }
151
152 bool tensmetric::info(unsigned inf) const
153 {
154         if(inf == info_flags::real)
155                 return true;
156
157         return false;
158 }
159
160 int minkmetric::compare_same_type(const basic & other) const
161 {
162         GINAC_ASSERT(is_a<minkmetric>(other));
163         const minkmetric &o = static_cast<const minkmetric &>(other);
164
165         if (pos_sig != o.pos_sig)
166                 return pos_sig ? -1 : 1;
167         else
168                 return inherited::compare_same_type(other);
169 }
170
171 bool minkmetric::info(unsigned inf) const
172 {
173         if(inf == info_flags::real)
174                 return true;
175
176         return false;
177 }
178
179 int tensepsilon::compare_same_type(const basic & other) const
180 {
181         GINAC_ASSERT(is_a<tensepsilon>(other));
182         const tensepsilon &o = static_cast<const tensepsilon &>(other);
183
184         if (minkowski != o.minkowski)
185                 return minkowski ? -1 : 1;
186         else if (pos_sig != o.pos_sig)
187                 return pos_sig ? -1 : 1;
188         else
189                 return inherited::compare_same_type(other);
190 }
191
192 bool tensepsilon::info(unsigned inf) const
193 {
194         if(inf == info_flags::real)
195                 return true;
196
197         return false;
198 }
199
200 bool spinmetric::info(unsigned inf) const
201 {
202         if(inf == info_flags::real)
203                 return true;
204
205         return false;
206 }
207
208 DEFAULT_PRINT_LATEX(tensdelta, "delta", "\\delta")
209 DEFAULT_PRINT(tensmetric, "g")
210 DEFAULT_PRINT_LATEX(minkmetric, "eta", "\\eta")
211 DEFAULT_PRINT_LATEX(spinmetric, "eps", "\\varepsilon")
212 DEFAULT_PRINT_LATEX(tensepsilon, "eps", "\\varepsilon")
213
214 /** Automatic symbolic evaluation of an indexed delta tensor. */
215 ex tensdelta::eval_indexed(const basic & i) const
216 {
217         GINAC_ASSERT(is_a<indexed>(i));
218         GINAC_ASSERT(i.nops() == 3);
219         GINAC_ASSERT(is_a<tensdelta>(i.op(0)));
220
221         const idx & i1 = ex_to<idx>(i.op(1));
222         const idx & i2 = ex_to<idx>(i.op(2));
223
224         // The dimension of the indices must be equal, otherwise we use the minimal
225         // dimension
226         if (!i1.get_dim().is_equal(i2.get_dim())) {
227                 ex min_dim = i1.minimal_dim(i2);
228                 exmap m;
229                 m[i1] = i1.replace_dim(min_dim);
230                 m[i2] = i2.replace_dim(min_dim);
231                 return i.subs(m, subs_options::no_pattern);
232         }
233
234         // Trace of delta tensor is the (effective) dimension of the space
235         if (is_dummy_pair(i1, i2)) {
236                 try {
237                         return i1.minimal_dim(i2);
238                 } catch (std::exception &e) {
239                         return i.hold();
240                 }
241         }
242
243         // Numeric evaluation
244         if (static_cast<const indexed &>(i).all_index_values_are(info_flags::integer)) {
245                 int n1 = ex_to<numeric>(i1.get_value()).to_int(), n2 = ex_to<numeric>(i2.get_value()).to_int();
246                 if (n1 == n2)
247                         return _ex1;
248                 else
249                         return _ex0;
250         }
251
252         // No further simplifications
253         return i.hold();
254 }
255
256 /** Automatic symbolic evaluation of an indexed metric tensor. */
257 ex tensmetric::eval_indexed(const basic & i) const
258 {
259         GINAC_ASSERT(is_a<indexed>(i));
260         GINAC_ASSERT(i.nops() == 3);
261         GINAC_ASSERT(is_a<tensmetric>(i.op(0)));
262         GINAC_ASSERT(is_a<varidx>(i.op(1)));
263         GINAC_ASSERT(is_a<varidx>(i.op(2)));
264
265         const varidx & i1 = ex_to<varidx>(i.op(1));
266         const varidx & i2 = ex_to<varidx>(i.op(2));
267
268         // The dimension of the indices must be equal, otherwise we use the minimal
269         // dimension
270         if (!i1.get_dim().is_equal(i2.get_dim())) {
271                 ex min_dim = i1.minimal_dim(i2);
272                 exmap m;
273                 m[i1] = i1.replace_dim(min_dim);
274                 m[i2] = i2.replace_dim(min_dim);
275                 return i.subs(m, subs_options::no_pattern);
276         }
277
278         // A metric tensor with one covariant and one contravariant index gets
279         // replaced by a delta tensor
280         if (i1.is_covariant() != i2.is_covariant())
281                 return delta_tensor(i1, i2);
282
283         // No further simplifications
284         return i.hold();
285 }
286
287 /** Automatic symbolic evaluation of an indexed Lorentz metric tensor. */
288 ex minkmetric::eval_indexed(const basic & i) const
289 {
290         GINAC_ASSERT(is_a<indexed>(i));
291         GINAC_ASSERT(i.nops() == 3);
292         GINAC_ASSERT(is_a<minkmetric>(i.op(0)));
293         GINAC_ASSERT(is_a<varidx>(i.op(1)));
294         GINAC_ASSERT(is_a<varidx>(i.op(2)));
295
296         const varidx & i1 = ex_to<varidx>(i.op(1));
297         const varidx & i2 = ex_to<varidx>(i.op(2));
298
299         // Numeric evaluation
300         if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
301                 int n1 = ex_to<numeric>(i1.get_value()).to_int(), n2 = ex_to<numeric>(i2.get_value()).to_int();
302                 if (n1 != n2)
303                         return _ex0;
304                 else if (n1 == 0)
305                         return pos_sig ? _ex_1 : _ex1;
306                 else
307                         return pos_sig ? _ex1 : _ex_1;
308         }
309
310         // Perform the usual evaluations of a metric tensor
311         return inherited::eval_indexed(i);
312 }
313
314 /** Automatic symbolic evaluation of an indexed metric tensor. */
315 ex spinmetric::eval_indexed(const basic & i) const
316 {
317         GINAC_ASSERT(is_a<indexed>(i));
318         GINAC_ASSERT(i.nops() == 3);
319         GINAC_ASSERT(is_a<spinmetric>(i.op(0)));
320         GINAC_ASSERT(is_a<spinidx>(i.op(1)));
321         GINAC_ASSERT(is_a<spinidx>(i.op(2)));
322
323         const spinidx & i1 = ex_to<spinidx>(i.op(1));
324         const spinidx & i2 = ex_to<spinidx>(i.op(2));
325
326         // Convolutions are zero
327         if (!(static_cast<const indexed &>(i).get_dummy_indices().empty()))
328                 return _ex0;
329
330         // Numeric evaluation
331         if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
332                 int n1 = ex_to<numeric>(i1.get_value()).to_int(), n2 = ex_to<numeric>(i2.get_value()).to_int();
333                 if (n1 == n2)
334                         return _ex0;
335                 else if (n1 < n2)
336                         return _ex1;
337                 else
338                         return _ex_1;
339         }
340
341         // No further simplifications
342         return i.hold();
343 }
344
345 /** Automatic symbolic evaluation of an indexed epsilon tensor. */
346 ex tensepsilon::eval_indexed(const basic & i) const
347 {
348         GINAC_ASSERT(is_a<indexed>(i));
349         GINAC_ASSERT(i.nops() > 1);
350         GINAC_ASSERT(is_a<tensepsilon>(i.op(0)));
351
352         // Convolutions are zero
353         if (!(static_cast<const indexed &>(i).get_dummy_indices().empty()))
354                 return _ex0;
355
356         // Numeric evaluation
357         if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
358
359                 // Get sign of index permutation (the indices should already be in
360                 // a canonic order but we can't assume what exactly that order is)
361                 std::vector<int> v;
362                 v.reserve(i.nops() - 1);
363                 for (size_t j=1; j<i.nops(); j++)
364                         v.push_back(ex_to<numeric>(ex_to<idx>(i.op(j)).get_value()).to_int());
365                 int sign = permutation_sign(v.begin(), v.end());
366
367                 // In a Minkowski space, check for covariant indices
368                 if (minkowski) {
369                         for (size_t j=1; j<i.nops(); j++) {
370                                 const ex & x = i.op(j);
371                                 if (!is_a<varidx>(x))
372                                         throw(std::runtime_error("indices of epsilon tensor in Minkowski space must be of type varidx"));
373                                 if (ex_to<varidx>(x).is_covariant())
374                                         if (ex_to<idx>(x).get_value().is_zero())
375                                                 sign = (pos_sig ? -sign : sign);
376                                         else
377                                                 sign = (pos_sig ? sign : -sign);
378                         }
379                 }
380
381                 return sign;
382         }
383
384         // No further simplifications
385         return i.hold();
386 }
387
388 bool tensor::replace_contr_index(exvector::iterator self, exvector::iterator other) const
389 {
390         // Try to contract the first index
391         const idx *self_idx = &ex_to<idx>(self->op(1));
392         const idx *free_idx = &ex_to<idx>(self->op(2));
393         bool first_index_tried = false;
394
395 again:
396         if (self_idx->is_symbolic()) {
397                 for (size_t i=1; i<other->nops(); i++) {
398                         const idx &other_idx = ex_to<idx>(other->op(i));
399                         if (is_dummy_pair(*self_idx, other_idx)) {
400
401                                 // Contraction found, remove this tensor and substitute the
402                                 // index in the second object
403                                 try {
404                                         // minimal_dim() throws an exception when index dimensions are not comparable
405                                         ex min_dim = self_idx->minimal_dim(other_idx);
406                                         *other = other->subs(other_idx == free_idx->replace_dim(min_dim));
407                                         *self = _ex1; // *other is assigned first because assigning *self invalidates free_idx
408                                         return true;
409                                 } catch (std::exception &e) {
410                                         return false;
411                                 }
412                         }
413                 }
414         }
415
416         if (!first_index_tried) {
417
418                 // No contraction with the first index found, try the second index
419                 self_idx = &ex_to<idx>(self->op(2));
420                 free_idx = &ex_to<idx>(self->op(1));
421                 first_index_tried = true;
422                 goto again;
423         }
424
425         return false;
426 }
427
428 /** Contraction of an indexed delta tensor with something else. */
429 bool tensdelta::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
430 {
431         GINAC_ASSERT(is_a<indexed>(*self));
432         GINAC_ASSERT(is_a<indexed>(*other));
433         GINAC_ASSERT(self->nops() == 3);
434         GINAC_ASSERT(is_a<tensdelta>(self->op(0)));
435
436         // Replace the dummy index with this tensor's other index and remove
437         // the tensor (this is valid for contractions with all other tensors)
438         return replace_contr_index(self, other);
439 }
440
441 /** Contraction of an indexed metric tensor with something else. */
442 bool tensmetric::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
443 {
444         GINAC_ASSERT(is_a<indexed>(*self));
445         GINAC_ASSERT(is_a<indexed>(*other));
446         GINAC_ASSERT(self->nops() == 3);
447         GINAC_ASSERT(is_a<tensmetric>(self->op(0)));
448
449         // If contracting with the delta tensor, let the delta do it
450         // (don't raise/lower delta indices)
451         if (is_a<tensdelta>(other->op(0)))
452                 return false;
453
454         // Replace the dummy index with this tensor's other index and remove
455         // the tensor
456         return replace_contr_index(self, other);
457 }
458
459 /** Contraction of an indexed spinor metric with something else. */
460 bool spinmetric::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
461 {
462         GINAC_ASSERT(is_a<indexed>(*self));
463         GINAC_ASSERT(is_a<indexed>(*other));
464         GINAC_ASSERT(self->nops() == 3);
465         GINAC_ASSERT(is_a<spinmetric>(self->op(0)));
466
467         // Contractions between spinor metrics
468         if (is_a<spinmetric>(other->op(0))) {
469                 const idx &self_i1 = ex_to<idx>(self->op(1));
470                 const idx &self_i2 = ex_to<idx>(self->op(2));
471                 const idx &other_i1 = ex_to<idx>(other->op(1));
472                 const idx &other_i2 = ex_to<idx>(other->op(2));
473
474                 if (is_dummy_pair(self_i1, other_i1)) {
475                         if (is_dummy_pair(self_i2, other_i2))
476                                 *self = _ex2;
477                         else
478                                 *self = delta_tensor(self_i2, other_i2);
479                         *other = _ex1;
480                         return true;
481                 } else if (is_dummy_pair(self_i1, other_i2)) {
482                         if (is_dummy_pair(self_i2, other_i1))
483                                 *self = _ex_2;
484                         else
485                                 *self = -delta_tensor(self_i2, other_i1);
486                         *other = _ex1;
487                         return true;
488                 } else if (is_dummy_pair(self_i2, other_i1)) {
489                         *self = -delta_tensor(self_i1, other_i2);
490                         *other = _ex1;
491                         return true;
492                 } else if (is_dummy_pair(self_i2, other_i2)) {
493                         *self = delta_tensor(self_i1, other_i1);
494                         *other = _ex1;
495                         return true;
496                 }
497         }
498
499         // If contracting with the delta tensor, let the delta do it
500         // (don't raise/lower delta indices)
501         if (is_a<tensdelta>(other->op(0)))
502                 return false;
503
504         // Try to contract first index
505         const idx *self_idx = &ex_to<idx>(self->op(1));
506         const idx *free_idx = &ex_to<idx>(self->op(2));
507         bool first_index_tried = false;
508         int sign = 1;
509
510 again:
511         if (self_idx->is_symbolic()) {
512                 for (size_t i=1; i<other->nops(); i++) {
513                         const idx &other_idx = ex_to<idx>(other->op(i));
514                         if (is_dummy_pair(*self_idx, other_idx)) {
515
516                                 // Contraction found, remove metric tensor and substitute
517                                 // index in second object (assign *self last because this
518                                 // invalidates free_idx)
519                                 *other = other->subs(other_idx == *free_idx);
520                                 *self = (static_cast<const spinidx *>(self_idx)->is_covariant() ? sign : -sign);
521                                 return true;
522                         }
523                 }
524         }
525
526         if (!first_index_tried) {
527
528                 // No contraction with first index found, try second index
529                 self_idx = &ex_to<idx>(self->op(2));
530                 free_idx = &ex_to<idx>(self->op(1));
531                 first_index_tried = true;
532                 sign = -sign;
533                 goto again;
534         }
535
536         return false;
537 }
538
539 /** Contraction of epsilon tensor with something else. */
540 bool tensepsilon::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
541 {
542         GINAC_ASSERT(is_a<indexed>(*self));
543         GINAC_ASSERT(is_a<indexed>(*other));
544         GINAC_ASSERT(is_a<tensepsilon>(self->op(0)));
545         size_t num = self->nops() - 1;
546
547         if (is_exactly_a<tensepsilon>(other->op(0)) && num+1 == other->nops()) {
548
549                 // Contraction of two epsilon tensors is a determinant
550                 bool variance = is_a<varidx>(self->op(1));
551                 matrix M(num, num);
552                 for (size_t i=0; i<num; i++) {
553                         for (size_t j=0; j<num; j++) {
554                                 if (minkowski)
555                                         M(i, j) = lorentz_g(self->op(i+1), other->op(j+1), pos_sig);
556                                 else if (variance)
557                                         M(i, j) = metric_tensor(self->op(i+1), other->op(j+1));
558                                 else
559                                         M(i, j) = delta_tensor(self->op(i+1), other->op(j+1));
560                         }
561                 }
562                 int sign = minkowski ? -1 : 1;
563                 *self = sign * M.determinant().simplify_indexed();
564                 *other = _ex1;
565                 return true;
566         }
567
568         return false;
569 }
570
571 //////////
572 // global functions
573 //////////
574
575 ex delta_tensor(const ex & i1, const ex & i2)
576 {
577         static ex delta = (new tensdelta)->setflag(status_flags::dynallocated);
578
579         if (!is_a<idx>(i1) || !is_a<idx>(i2))
580                 throw(std::invalid_argument("indices of delta tensor must be of type idx"));
581
582         return indexed(delta, symmetric2(), i1, i2);
583 }
584
585 ex metric_tensor(const ex & i1, const ex & i2)
586 {
587         static ex metric = (new tensmetric)->setflag(status_flags::dynallocated);
588
589         if (!is_a<varidx>(i1) || !is_a<varidx>(i2))
590                 throw(std::invalid_argument("indices of metric tensor must be of type varidx"));
591
592         return indexed(metric, symmetric2(), i1, i2);
593 }
594
595 ex lorentz_g(const ex & i1, const ex & i2, bool pos_sig)
596 {
597         static ex metric_neg = (new minkmetric(false))->setflag(status_flags::dynallocated);
598         static ex metric_pos = (new minkmetric(true))->setflag(status_flags::dynallocated);
599
600         if (!is_a<varidx>(i1) || !is_a<varidx>(i2))
601                 throw(std::invalid_argument("indices of metric tensor must be of type varidx"));
602
603         return indexed(pos_sig ? metric_pos : metric_neg, symmetric2(), i1, i2);
604 }
605
606 ex spinor_metric(const ex & i1, const ex & i2)
607 {
608         static ex metric = (new spinmetric)->setflag(status_flags::dynallocated);
609
610         if (!is_a<spinidx>(i1) || !is_a<spinidx>(i2))
611                 throw(std::invalid_argument("indices of spinor metric must be of type spinidx"));
612         if (!ex_to<idx>(i1).get_dim().is_equal(2) || !ex_to<idx>(i2).get_dim().is_equal(2))
613                 throw(std::runtime_error("index dimension for spinor metric must be 2"));
614
615         return indexed(metric, antisymmetric2(), i1, i2);
616 }
617
618 ex epsilon_tensor(const ex & i1, const ex & i2)
619 {
620         static ex epsilon = (new tensepsilon)->setflag(status_flags::dynallocated);
621
622         if (!is_a<idx>(i1) || !is_a<idx>(i2))
623                 throw(std::invalid_argument("indices of epsilon tensor must be of type idx"));
624
625         ex dim = ex_to<idx>(i1).get_dim();
626         if (!dim.is_equal(ex_to<idx>(i2).get_dim()))
627                 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
628         if (!ex_to<idx>(i1).get_dim().is_equal(_ex2))
629                 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
630
631         if(is_a<wildcard>(i1.op(0))||is_a<wildcard>(i2.op(0)))
632                 return indexed(epsilon, antisymmetric2(), i1, i2).hold();
633
634         return indexed(epsilon, antisymmetric2(), i1, i2);
635 }
636
637 ex epsilon_tensor(const ex & i1, const ex & i2, const ex & i3)
638 {
639         static ex epsilon = (new tensepsilon)->setflag(status_flags::dynallocated);
640
641         if (!is_a<idx>(i1) || !is_a<idx>(i2) || !is_a<idx>(i3))
642                 throw(std::invalid_argument("indices of epsilon tensor must be of type idx"));
643
644         ex dim = ex_to<idx>(i1).get_dim();
645         if (!dim.is_equal(ex_to<idx>(i2).get_dim()) || !dim.is_equal(ex_to<idx>(i3).get_dim()))
646                 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
647         if (!ex_to<idx>(i1).get_dim().is_equal(_ex3))
648                 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
649
650         if(is_a<wildcard>(i1.op(0))||is_a<wildcard>(i2.op(0))||is_a<wildcard>(i3.op(0)))
651                 return indexed(epsilon, antisymmetric3(), i1, i2, i3).hold();
652
653         return indexed(epsilon, antisymmetric3(), i1, i2, i3);
654 }
655
656 ex lorentz_eps(const ex & i1, const ex & i2, const ex & i3, const ex & i4, bool pos_sig)
657 {
658         static ex epsilon_neg = (new tensepsilon(true, false))->setflag(status_flags::dynallocated);
659         static ex epsilon_pos = (new tensepsilon(true, true))->setflag(status_flags::dynallocated);
660
661         if (!is_a<varidx>(i1) || !is_a<varidx>(i2) || !is_a<varidx>(i3) || !is_a<varidx>(i4))
662                 throw(std::invalid_argument("indices of Lorentz epsilon tensor must be of type varidx"));
663
664         ex dim = ex_to<idx>(i1).get_dim();
665         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()))
666                 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
667         if (!ex_to<idx>(i1).get_dim().is_equal(_ex4))
668                 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
669
670         if(is_a<wildcard>(i1.op(0))||is_a<wildcard>(i2.op(0))||is_a<wildcard>(i3.op(0))||is_a<wildcard>(i4.op(0)))
671                 return indexed(pos_sig ? epsilon_pos : epsilon_neg, antisymmetric4(), i1, i2, i3, i4).hold();
672
673         return indexed(pos_sig ? epsilon_pos : epsilon_neg, antisymmetric4(), i1, i2, i3, i4);
674 }
675
676 } // namespace GiNaC