3 * Implementation of GiNaC's special tensors. */
6 * GiNaC Copyright (C) 1999-2007 Johannes Gutenberg University Mainz, Germany
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.
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.
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
31 #include "relational.h"
32 #include "operators.h"
41 GINAC_IMPLEMENT_REGISTERED_CLASS(tensor, basic)
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))
47 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(tensmetric, tensor,
48 print_func<print_dflt>(&tensmetric::do_print).
49 print_func<print_latex>(&tensmetric::do_print))
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))
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))
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))
67 tensor::tensor() : inherited(&tensor::tinfo_static)
69 setflag(status_flags::evaluated | status_flags::expanded);
72 DEFAULT_CTOR(tensdelta)
73 DEFAULT_CTOR(tensmetric)
75 minkmetric::minkmetric() : pos_sig(false)
77 tinfo_key = &minkmetric::tinfo_static;
80 spinmetric::spinmetric()
82 tinfo_key = &spinmetric::tinfo_static;
85 minkmetric::minkmetric(bool ps) : pos_sig(ps)
87 tinfo_key = &minkmetric::tinfo_static;
90 tensepsilon::tensepsilon() : minkowski(false), pos_sig(false)
92 tinfo_key = &tensepsilon::tinfo_static;
95 tensepsilon::tensepsilon(bool mink, bool ps) : minkowski(mink), pos_sig(ps)
97 tinfo_key = &tensepsilon::tinfo_static;
104 DEFAULT_ARCHIVING(tensor)
105 DEFAULT_ARCHIVING(tensdelta)
106 DEFAULT_ARCHIVING(tensmetric)
107 DEFAULT_ARCHIVING(spinmetric)
108 DEFAULT_UNARCHIVE(minkmetric)
109 DEFAULT_UNARCHIVE(tensepsilon)
111 minkmetric::minkmetric(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
113 n.find_bool("pos_sig", pos_sig);
116 void minkmetric::archive(archive_node &n) const
118 inherited::archive(n);
119 n.add_bool("pos_sig", pos_sig);
122 tensepsilon::tensepsilon(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
124 n.find_bool("minkowski", minkowski);
125 n.find_bool("pos_sig", pos_sig);
128 void tensepsilon::archive(archive_node &n) const
130 inherited::archive(n);
131 n.add_bool("minkowski", minkowski);
132 n.add_bool("pos_sig", pos_sig);
136 // functions overriding virtual functions from base classes
139 DEFAULT_COMPARE(tensor)
140 DEFAULT_COMPARE(tensdelta)
141 DEFAULT_COMPARE(tensmetric)
142 DEFAULT_COMPARE(spinmetric)
144 bool tensdelta::info(unsigned inf) const
146 if(inf == info_flags::real)
152 bool tensmetric::info(unsigned inf) const
154 if(inf == info_flags::real)
160 int minkmetric::compare_same_type(const basic & other) const
162 GINAC_ASSERT(is_a<minkmetric>(other));
163 const minkmetric &o = static_cast<const minkmetric &>(other);
165 if (pos_sig != o.pos_sig)
166 return pos_sig ? -1 : 1;
168 return inherited::compare_same_type(other);
171 bool minkmetric::info(unsigned inf) const
173 if(inf == info_flags::real)
179 int tensepsilon::compare_same_type(const basic & other) const
181 GINAC_ASSERT(is_a<tensepsilon>(other));
182 const tensepsilon &o = static_cast<const tensepsilon &>(other);
184 if (minkowski != o.minkowski)
185 return minkowski ? -1 : 1;
186 else if (pos_sig != o.pos_sig)
187 return pos_sig ? -1 : 1;
189 return inherited::compare_same_type(other);
192 bool tensepsilon::info(unsigned inf) const
194 if(inf == info_flags::real)
200 bool spinmetric::info(unsigned inf) const
202 if(inf == info_flags::real)
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")
214 /** Automatic symbolic evaluation of an indexed delta tensor. */
215 ex tensdelta::eval_indexed(const basic & i) const
217 GINAC_ASSERT(is_a<indexed>(i));
218 GINAC_ASSERT(i.nops() == 3);
219 GINAC_ASSERT(is_a<tensdelta>(i.op(0)));
221 const idx & i1 = ex_to<idx>(i.op(1));
222 const idx & i2 = ex_to<idx>(i.op(2));
224 // The dimension of the indices must be equal, otherwise we use the minimal
226 if (!i1.get_dim().is_equal(i2.get_dim())) {
227 ex min_dim = i1.minimal_dim(i2);
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);
234 // Trace of delta tensor is the (effective) dimension of the space
235 if (is_dummy_pair(i1, i2)) {
237 return i1.minimal_dim(i2);
238 } catch (std::exception &e) {
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();
252 // No further simplifications
256 /** Automatic symbolic evaluation of an indexed metric tensor. */
257 ex tensmetric::eval_indexed(const basic & i) const
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)));
265 const varidx & i1 = ex_to<varidx>(i.op(1));
266 const varidx & i2 = ex_to<varidx>(i.op(2));
268 // The dimension of the indices must be equal, otherwise we use the minimal
270 if (!i1.get_dim().is_equal(i2.get_dim())) {
271 ex min_dim = i1.minimal_dim(i2);
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);
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);
283 // No further simplifications
287 /** Automatic symbolic evaluation of an indexed Lorentz metric tensor. */
288 ex minkmetric::eval_indexed(const basic & i) const
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)));
296 const varidx & i1 = ex_to<varidx>(i.op(1));
297 const varidx & i2 = ex_to<varidx>(i.op(2));
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();
305 return pos_sig ? _ex_1 : _ex1;
307 return pos_sig ? _ex1 : _ex_1;
310 // Perform the usual evaluations of a metric tensor
311 return inherited::eval_indexed(i);
314 /** Automatic symbolic evaluation of an indexed metric tensor. */
315 ex spinmetric::eval_indexed(const basic & i) const
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)));
323 const spinidx & i1 = ex_to<spinidx>(i.op(1));
324 const spinidx & i2 = ex_to<spinidx>(i.op(2));
326 // Convolutions are zero
327 if (!(static_cast<const indexed &>(i).get_dummy_indices().empty()))
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();
341 // No further simplifications
345 /** Automatic symbolic evaluation of an indexed epsilon tensor. */
346 ex tensepsilon::eval_indexed(const basic & i) const
348 GINAC_ASSERT(is_a<indexed>(i));
349 GINAC_ASSERT(i.nops() > 1);
350 GINAC_ASSERT(is_a<tensepsilon>(i.op(0)));
352 // Convolutions are zero
353 if (!(static_cast<const indexed &>(i).get_dummy_indices().empty()))
356 // Numeric evaluation
357 if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
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)
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());
367 // In a Minkowski space, check for covariant indices
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);
377 sign = (pos_sig ? sign : -sign);
384 // No further simplifications
388 bool tensor::replace_contr_index(exvector::iterator self, exvector::iterator other) const
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;
396 if (self_idx->is_symbolic()) {
397 for (size_t i=1; i<other->nops(); i++) {
398 if (! is_a<idx>(other->op(i)))
400 const idx &other_idx = ex_to<idx>(other->op(i));
401 if (is_dummy_pair(*self_idx, other_idx)) {
403 // Contraction found, remove this tensor and substitute the
404 // index in the second object
406 // minimal_dim() throws an exception when index dimensions are not comparable
407 ex min_dim = self_idx->minimal_dim(other_idx);
408 *other = other->subs(other_idx == free_idx->replace_dim(min_dim));
409 *self = _ex1; // *other is assigned first because assigning *self invalidates free_idx
411 } catch (std::exception &e) {
418 if (!first_index_tried) {
420 // No contraction with the first index found, try the second index
421 self_idx = &ex_to<idx>(self->op(2));
422 free_idx = &ex_to<idx>(self->op(1));
423 first_index_tried = true;
430 /** Contraction of an indexed delta tensor with something else. */
431 bool tensdelta::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
433 GINAC_ASSERT(is_a<indexed>(*self));
434 GINAC_ASSERT(is_a<indexed>(*other));
435 GINAC_ASSERT(self->nops() == 3);
436 GINAC_ASSERT(is_a<tensdelta>(self->op(0)));
438 // Replace the dummy index with this tensor's other index and remove
439 // the tensor (this is valid for contractions with all other tensors)
440 return replace_contr_index(self, other);
443 /** Contraction of an indexed metric tensor with something else. */
444 bool tensmetric::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
446 GINAC_ASSERT(is_a<indexed>(*self));
447 GINAC_ASSERT(is_a<indexed>(*other));
448 GINAC_ASSERT(self->nops() == 3);
449 GINAC_ASSERT(is_a<tensmetric>(self->op(0)));
451 // If contracting with the delta tensor, let the delta do it
452 // (don't raise/lower delta indices)
453 if (is_a<tensdelta>(other->op(0)))
456 // Replace the dummy index with this tensor's other index and remove
458 return replace_contr_index(self, other);
461 /** Contraction of an indexed spinor metric with something else. */
462 bool spinmetric::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
464 GINAC_ASSERT(is_a<indexed>(*self));
465 GINAC_ASSERT(is_a<indexed>(*other));
466 GINAC_ASSERT(self->nops() == 3);
467 GINAC_ASSERT(is_a<spinmetric>(self->op(0)));
469 // Contractions between spinor metrics
470 if (is_a<spinmetric>(other->op(0))) {
471 const idx &self_i1 = ex_to<idx>(self->op(1));
472 const idx &self_i2 = ex_to<idx>(self->op(2));
473 const idx &other_i1 = ex_to<idx>(other->op(1));
474 const idx &other_i2 = ex_to<idx>(other->op(2));
476 if (is_dummy_pair(self_i1, other_i1)) {
477 if (is_dummy_pair(self_i2, other_i2))
480 *self = delta_tensor(self_i2, other_i2);
483 } else if (is_dummy_pair(self_i1, other_i2)) {
484 if (is_dummy_pair(self_i2, other_i1))
487 *self = -delta_tensor(self_i2, other_i1);
490 } else if (is_dummy_pair(self_i2, other_i1)) {
491 *self = -delta_tensor(self_i1, other_i2);
494 } else if (is_dummy_pair(self_i2, other_i2)) {
495 *self = delta_tensor(self_i1, other_i1);
501 // If contracting with the delta tensor, let the delta do it
502 // (don't raise/lower delta indices)
503 if (is_a<tensdelta>(other->op(0)))
506 // Try to contract first index
507 const idx *self_idx = &ex_to<idx>(self->op(1));
508 const idx *free_idx = &ex_to<idx>(self->op(2));
509 bool first_index_tried = false;
513 if (self_idx->is_symbolic()) {
514 for (size_t i=1; i<other->nops(); i++) {
515 const idx &other_idx = ex_to<idx>(other->op(i));
516 if (is_dummy_pair(*self_idx, other_idx)) {
518 // Contraction found, remove metric tensor and substitute
519 // index in second object (assign *self last because this
520 // invalidates free_idx)
521 *other = other->subs(other_idx == *free_idx);
522 *self = (static_cast<const spinidx *>(self_idx)->is_covariant() ? sign : -sign);
528 if (!first_index_tried) {
530 // No contraction with first index found, try second index
531 self_idx = &ex_to<idx>(self->op(2));
532 free_idx = &ex_to<idx>(self->op(1));
533 first_index_tried = true;
541 /** Contraction of epsilon tensor with something else. */
542 bool tensepsilon::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
544 GINAC_ASSERT(is_a<indexed>(*self));
545 GINAC_ASSERT(is_a<indexed>(*other));
546 GINAC_ASSERT(is_a<tensepsilon>(self->op(0)));
547 size_t num = self->nops() - 1;
549 if (is_exactly_a<tensepsilon>(other->op(0)) && num+1 == other->nops()) {
551 // Contraction of two epsilon tensors is a determinant
552 bool variance = is_a<varidx>(self->op(1));
554 for (size_t i=0; i<num; i++) {
555 for (size_t j=0; j<num; j++) {
557 M(i, j) = lorentz_g(self->op(i+1), other->op(j+1), pos_sig);
559 M(i, j) = metric_tensor(self->op(i+1), other->op(j+1));
561 M(i, j) = delta_tensor(self->op(i+1), other->op(j+1));
564 int sign = minkowski ? -1 : 1;
565 *self = sign * M.determinant().simplify_indexed();
577 ex delta_tensor(const ex & i1, const ex & i2)
579 static ex delta = (new tensdelta)->setflag(status_flags::dynallocated);
581 if (!is_a<idx>(i1) || !is_a<idx>(i2))
582 throw(std::invalid_argument("indices of delta tensor must be of type idx"));
584 return indexed(delta, symmetric2(), i1, i2);
587 ex metric_tensor(const ex & i1, const ex & i2)
589 static ex metric = (new tensmetric)->setflag(status_flags::dynallocated);
591 if (!is_a<varidx>(i1) || !is_a<varidx>(i2))
592 throw(std::invalid_argument("indices of metric tensor must be of type varidx"));
594 return indexed(metric, symmetric2(), i1, i2);
597 ex lorentz_g(const ex & i1, const ex & i2, bool pos_sig)
599 static ex metric_neg = (new minkmetric(false))->setflag(status_flags::dynallocated);
600 static ex metric_pos = (new minkmetric(true))->setflag(status_flags::dynallocated);
602 if (!is_a<varidx>(i1) || !is_a<varidx>(i2))
603 throw(std::invalid_argument("indices of metric tensor must be of type varidx"));
605 return indexed(pos_sig ? metric_pos : metric_neg, symmetric2(), i1, i2);
608 ex spinor_metric(const ex & i1, const ex & i2)
610 static ex metric = (new spinmetric)->setflag(status_flags::dynallocated);
612 if (!is_a<spinidx>(i1) || !is_a<spinidx>(i2))
613 throw(std::invalid_argument("indices of spinor metric must be of type spinidx"));
614 if (!ex_to<idx>(i1).get_dim().is_equal(2) || !ex_to<idx>(i2).get_dim().is_equal(2))
615 throw(std::runtime_error("index dimension for spinor metric must be 2"));
617 return indexed(metric, antisymmetric2(), i1, i2);
620 ex epsilon_tensor(const ex & i1, const ex & i2)
622 static ex epsilon = (new tensepsilon)->setflag(status_flags::dynallocated);
624 if (!is_a<idx>(i1) || !is_a<idx>(i2))
625 throw(std::invalid_argument("indices of epsilon tensor must be of type idx"));
627 ex dim = ex_to<idx>(i1).get_dim();
628 if (!dim.is_equal(ex_to<idx>(i2).get_dim()))
629 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
630 if (!ex_to<idx>(i1).get_dim().is_equal(_ex2))
631 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
633 if(is_a<wildcard>(i1.op(0))||is_a<wildcard>(i2.op(0)))
634 return indexed(epsilon, antisymmetric2(), i1, i2).hold();
636 return indexed(epsilon, antisymmetric2(), i1, i2);
639 ex epsilon_tensor(const ex & i1, const ex & i2, const ex & i3)
641 static ex epsilon = (new tensepsilon)->setflag(status_flags::dynallocated);
643 if (!is_a<idx>(i1) || !is_a<idx>(i2) || !is_a<idx>(i3))
644 throw(std::invalid_argument("indices of epsilon tensor must be of type idx"));
646 ex dim = ex_to<idx>(i1).get_dim();
647 if (!dim.is_equal(ex_to<idx>(i2).get_dim()) || !dim.is_equal(ex_to<idx>(i3).get_dim()))
648 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
649 if (!ex_to<idx>(i1).get_dim().is_equal(_ex3))
650 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
652 if(is_a<wildcard>(i1.op(0))||is_a<wildcard>(i2.op(0))||is_a<wildcard>(i3.op(0)))
653 return indexed(epsilon, antisymmetric3(), i1, i2, i3).hold();
655 return indexed(epsilon, antisymmetric3(), i1, i2, i3);
658 ex lorentz_eps(const ex & i1, const ex & i2, const ex & i3, const ex & i4, bool pos_sig)
660 static ex epsilon_neg = (new tensepsilon(true, false))->setflag(status_flags::dynallocated);
661 static ex epsilon_pos = (new tensepsilon(true, true))->setflag(status_flags::dynallocated);
663 if (!is_a<varidx>(i1) || !is_a<varidx>(i2) || !is_a<varidx>(i3) || !is_a<varidx>(i4))
664 throw(std::invalid_argument("indices of Lorentz epsilon tensor must be of type varidx"));
666 ex dim = ex_to<idx>(i1).get_dim();
667 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()))
668 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
669 if (!ex_to<idx>(i1).get_dim().is_equal(_ex4))
670 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
672 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)))
673 return indexed(pos_sig ? epsilon_pos : epsilon_neg, antisymmetric4(), i1, i2, i3, i4).hold();
675 return indexed(pos_sig ? epsilon_pos : epsilon_neg, antisymmetric4(), i1, i2, i3, i4);