3 * Implementation of GiNaC's special tensors. */
6 * GiNaC Copyright (C) 1999-2006 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 int minkmetric::compare_same_type(const basic & other) const
146 GINAC_ASSERT(is_a<minkmetric>(other));
147 const minkmetric &o = static_cast<const minkmetric &>(other);
149 if (pos_sig != o.pos_sig)
150 return pos_sig ? -1 : 1;
152 return inherited::compare_same_type(other);
155 int tensepsilon::compare_same_type(const basic & other) const
157 GINAC_ASSERT(is_a<tensepsilon>(other));
158 const tensepsilon &o = static_cast<const tensepsilon &>(other);
160 if (minkowski != o.minkowski)
161 return minkowski ? -1 : 1;
162 else if (pos_sig != o.pos_sig)
163 return pos_sig ? -1 : 1;
165 return inherited::compare_same_type(other);
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")
174 /** Automatic symbolic evaluation of an indexed delta tensor. */
175 ex tensdelta::eval_indexed(const basic & i) const
177 GINAC_ASSERT(is_a<indexed>(i));
178 GINAC_ASSERT(i.nops() == 3);
179 GINAC_ASSERT(is_a<tensdelta>(i.op(0)));
181 const idx & i1 = ex_to<idx>(i.op(1));
182 const idx & i2 = ex_to<idx>(i.op(2));
184 // The dimension of the indices must be equal, otherwise we use the minimal
186 if (!i1.get_dim().is_equal(i2.get_dim())) {
187 ex min_dim = i1.minimal_dim(i2);
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);
194 // Trace of delta tensor is the (effective) dimension of the space
195 if (is_dummy_pair(i1, i2)) {
197 return i1.minimal_dim(i2);
198 } catch (std::exception &e) {
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();
212 // No further simplifications
216 /** Automatic symbolic evaluation of an indexed metric tensor. */
217 ex tensmetric::eval_indexed(const basic & i) const
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)));
225 const varidx & i1 = ex_to<varidx>(i.op(1));
226 const varidx & i2 = ex_to<varidx>(i.op(2));
228 // The dimension of the indices must be equal, otherwise we use the minimal
230 if (!i1.get_dim().is_equal(i2.get_dim())) {
231 ex min_dim = i1.minimal_dim(i2);
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);
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);
243 // No further simplifications
247 /** Automatic symbolic evaluation of an indexed Lorentz metric tensor. */
248 ex minkmetric::eval_indexed(const basic & i) const
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)));
256 const varidx & i1 = ex_to<varidx>(i.op(1));
257 const varidx & i2 = ex_to<varidx>(i.op(2));
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();
265 return pos_sig ? _ex_1 : _ex1;
267 return pos_sig ? _ex1 : _ex_1;
270 // Perform the usual evaluations of a metric tensor
271 return inherited::eval_indexed(i);
274 /** Automatic symbolic evaluation of an indexed metric tensor. */
275 ex spinmetric::eval_indexed(const basic & i) const
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)));
283 const spinidx & i1 = ex_to<spinidx>(i.op(1));
284 const spinidx & i2 = ex_to<spinidx>(i.op(2));
286 // Convolutions are zero
287 if (!(static_cast<const indexed &>(i).get_dummy_indices().empty()))
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();
301 // No further simplifications
305 /** Automatic symbolic evaluation of an indexed epsilon tensor. */
306 ex tensepsilon::eval_indexed(const basic & i) const
308 GINAC_ASSERT(is_a<indexed>(i));
309 GINAC_ASSERT(i.nops() > 1);
310 GINAC_ASSERT(is_a<tensepsilon>(i.op(0)));
312 // Convolutions are zero
313 if (!(static_cast<const indexed &>(i).get_dummy_indices().empty()))
316 // Numeric evaluation
317 if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
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)
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());
327 // In a Minkowski space, check for covariant indices
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);
337 sign = (pos_sig ? sign : -sign);
344 // No further simplifications
348 bool tensor::replace_contr_index(exvector::iterator self, exvector::iterator other) const
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;
356 if (self_idx->is_symbolic()) {
357 for (size_t i=1; i<other->nops(); i++) {
358 const idx &other_idx = ex_to<idx>(other->op(i));
359 if (is_dummy_pair(*self_idx, other_idx)) {
361 // Contraction found, remove this tensor and substitute the
362 // index in the second object
364 // minimal_dim() throws an exception when index dimensions are not comparable
365 ex min_dim = self_idx->minimal_dim(other_idx);
366 *other = other->subs(other_idx == free_idx->replace_dim(min_dim));
367 *self = _ex1; // *other is assigned first because assigning *self invalidates free_idx
369 } catch (std::exception &e) {
376 if (!first_index_tried) {
378 // No contraction with the first index found, try the second index
379 self_idx = &ex_to<idx>(self->op(2));
380 free_idx = &ex_to<idx>(self->op(1));
381 first_index_tried = true;
388 /** Contraction of an indexed delta tensor with something else. */
389 bool tensdelta::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
391 GINAC_ASSERT(is_a<indexed>(*self));
392 GINAC_ASSERT(is_a<indexed>(*other));
393 GINAC_ASSERT(self->nops() == 3);
394 GINAC_ASSERT(is_a<tensdelta>(self->op(0)));
396 // Replace the dummy index with this tensor's other index and remove
397 // the tensor (this is valid for contractions with all other tensors)
398 return replace_contr_index(self, other);
401 /** Contraction of an indexed metric tensor with something else. */
402 bool tensmetric::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
404 GINAC_ASSERT(is_a<indexed>(*self));
405 GINAC_ASSERT(is_a<indexed>(*other));
406 GINAC_ASSERT(self->nops() == 3);
407 GINAC_ASSERT(is_a<tensmetric>(self->op(0)));
409 // If contracting with the delta tensor, let the delta do it
410 // (don't raise/lower delta indices)
411 if (is_a<tensdelta>(other->op(0)))
414 // Replace the dummy index with this tensor's other index and remove
416 return replace_contr_index(self, other);
419 /** Contraction of an indexed spinor metric with something else. */
420 bool spinmetric::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
422 GINAC_ASSERT(is_a<indexed>(*self));
423 GINAC_ASSERT(is_a<indexed>(*other));
424 GINAC_ASSERT(self->nops() == 3);
425 GINAC_ASSERT(is_a<spinmetric>(self->op(0)));
427 // Contractions between spinor metrics
428 if (is_a<spinmetric>(other->op(0))) {
429 const idx &self_i1 = ex_to<idx>(self->op(1));
430 const idx &self_i2 = ex_to<idx>(self->op(2));
431 const idx &other_i1 = ex_to<idx>(other->op(1));
432 const idx &other_i2 = ex_to<idx>(other->op(2));
434 if (is_dummy_pair(self_i1, other_i1)) {
435 if (is_dummy_pair(self_i2, other_i2))
438 *self = delta_tensor(self_i2, other_i2);
441 } else if (is_dummy_pair(self_i1, other_i2)) {
442 if (is_dummy_pair(self_i2, other_i1))
445 *self = -delta_tensor(self_i2, other_i1);
448 } else if (is_dummy_pair(self_i2, other_i1)) {
449 *self = -delta_tensor(self_i1, other_i2);
452 } else if (is_dummy_pair(self_i2, other_i2)) {
453 *self = delta_tensor(self_i1, other_i1);
459 // If contracting with the delta tensor, let the delta do it
460 // (don't raise/lower delta indices)
461 if (is_a<tensdelta>(other->op(0)))
464 // Try to contract first index
465 const idx *self_idx = &ex_to<idx>(self->op(1));
466 const idx *free_idx = &ex_to<idx>(self->op(2));
467 bool first_index_tried = false;
471 if (self_idx->is_symbolic()) {
472 for (size_t i=1; i<other->nops(); i++) {
473 const idx &other_idx = ex_to<idx>(other->op(i));
474 if (is_dummy_pair(*self_idx, other_idx)) {
476 // Contraction found, remove metric tensor and substitute
477 // index in second object (assign *self last because this
478 // invalidates free_idx)
479 *other = other->subs(other_idx == *free_idx);
480 *self = (static_cast<const spinidx *>(self_idx)->is_covariant() ? sign : -sign);
486 if (!first_index_tried) {
488 // No contraction with first index found, try second index
489 self_idx = &ex_to<idx>(self->op(2));
490 free_idx = &ex_to<idx>(self->op(1));
491 first_index_tried = true;
499 /** Contraction of epsilon tensor with something else. */
500 bool tensepsilon::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
502 GINAC_ASSERT(is_a<indexed>(*self));
503 GINAC_ASSERT(is_a<indexed>(*other));
504 GINAC_ASSERT(is_a<tensepsilon>(self->op(0)));
505 size_t num = self->nops() - 1;
507 if (is_exactly_a<tensepsilon>(other->op(0)) && num+1 == other->nops()) {
509 // Contraction of two epsilon tensors is a determinant
510 bool variance = is_a<varidx>(self->op(1));
512 for (size_t i=0; i<num; i++) {
513 for (size_t j=0; j<num; j++) {
515 M(i, j) = lorentz_g(self->op(i+1), other->op(j+1), pos_sig);
517 M(i, j) = metric_tensor(self->op(i+1), other->op(j+1));
519 M(i, j) = delta_tensor(self->op(i+1), other->op(j+1));
522 int sign = minkowski ? -1 : 1;
523 *self = sign * M.determinant().simplify_indexed();
535 ex delta_tensor(const ex & i1, const ex & i2)
537 static ex delta = (new tensdelta)->setflag(status_flags::dynallocated);
539 if (!is_a<idx>(i1) || !is_a<idx>(i2))
540 throw(std::invalid_argument("indices of delta tensor must be of type idx"));
542 return indexed(delta, symmetric2(), i1, i2);
545 ex metric_tensor(const ex & i1, const ex & i2)
547 static ex metric = (new tensmetric)->setflag(status_flags::dynallocated);
549 if (!is_a<varidx>(i1) || !is_a<varidx>(i2))
550 throw(std::invalid_argument("indices of metric tensor must be of type varidx"));
552 return indexed(metric, symmetric2(), i1, i2);
555 ex lorentz_g(const ex & i1, const ex & i2, bool pos_sig)
557 static ex metric_neg = (new minkmetric(false))->setflag(status_flags::dynallocated);
558 static ex metric_pos = (new minkmetric(true))->setflag(status_flags::dynallocated);
560 if (!is_a<varidx>(i1) || !is_a<varidx>(i2))
561 throw(std::invalid_argument("indices of metric tensor must be of type varidx"));
563 return indexed(pos_sig ? metric_pos : metric_neg, symmetric2(), i1, i2);
566 ex spinor_metric(const ex & i1, const ex & i2)
568 static ex metric = (new spinmetric)->setflag(status_flags::dynallocated);
570 if (!is_a<spinidx>(i1) || !is_a<spinidx>(i2))
571 throw(std::invalid_argument("indices of spinor metric must be of type spinidx"));
572 if (!ex_to<idx>(i1).get_dim().is_equal(2) || !ex_to<idx>(i2).get_dim().is_equal(2))
573 throw(std::runtime_error("index dimension for spinor metric must be 2"));
575 return indexed(metric, antisymmetric2(), i1, i2);
578 ex epsilon_tensor(const ex & i1, const ex & i2)
580 static ex epsilon = (new tensepsilon)->setflag(status_flags::dynallocated);
582 if (!is_a<idx>(i1) || !is_a<idx>(i2))
583 throw(std::invalid_argument("indices of epsilon tensor must be of type idx"));
585 ex dim = ex_to<idx>(i1).get_dim();
586 if (!dim.is_equal(ex_to<idx>(i2).get_dim()))
587 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
588 if (!ex_to<idx>(i1).get_dim().is_equal(_ex2))
589 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
591 if(is_a<wildcard>(i1.op(0))||is_a<wildcard>(i2.op(0)))
592 return indexed(epsilon, antisymmetric2(), i1, i2).hold();
594 return indexed(epsilon, antisymmetric2(), i1, i2);
597 ex epsilon_tensor(const ex & i1, const ex & i2, const ex & i3)
599 static ex epsilon = (new tensepsilon)->setflag(status_flags::dynallocated);
601 if (!is_a<idx>(i1) || !is_a<idx>(i2) || !is_a<idx>(i3))
602 throw(std::invalid_argument("indices of epsilon tensor must be of type idx"));
604 ex dim = ex_to<idx>(i1).get_dim();
605 if (!dim.is_equal(ex_to<idx>(i2).get_dim()) || !dim.is_equal(ex_to<idx>(i3).get_dim()))
606 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
607 if (!ex_to<idx>(i1).get_dim().is_equal(_ex3))
608 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
610 if(is_a<wildcard>(i1.op(0))||is_a<wildcard>(i2.op(0))||is_a<wildcard>(i3.op(0)))
611 return indexed(epsilon, antisymmetric3(), i1, i2, i3).hold();
613 return indexed(epsilon, antisymmetric3(), i1, i2, i3);
616 ex lorentz_eps(const ex & i1, const ex & i2, const ex & i3, const ex & i4, bool pos_sig)
618 static ex epsilon_neg = (new tensepsilon(true, false))->setflag(status_flags::dynallocated);
619 static ex epsilon_pos = (new tensepsilon(true, true))->setflag(status_flags::dynallocated);
621 if (!is_a<varidx>(i1) || !is_a<varidx>(i2) || !is_a<varidx>(i3) || !is_a<varidx>(i4))
622 throw(std::invalid_argument("indices of Lorentz epsilon tensor must be of type varidx"));
624 ex dim = ex_to<idx>(i1).get_dim();
625 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()))
626 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
627 if (!ex_to<idx>(i1).get_dim().is_equal(_ex4))
628 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
630 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)))
631 return indexed(pos_sig ? epsilon_pos : epsilon_neg, antisymmetric4(), i1, i2, i3, i4).hold();
633 return indexed(pos_sig ? epsilon_pos : epsilon_neg, antisymmetric4(), i1, i2, i3, i4);