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