3 * Implementation of GiNaC's special tensors. */
6 * GiNaC Copyright (C) 1999-2005 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(TINFO_tensor)
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 = TINFO_minkmetric;
80 spinmetric::spinmetric()
82 tinfo_key = TINFO_spinmetric;
85 minkmetric::minkmetric(bool ps) : pos_sig(ps)
87 tinfo_key = TINFO_minkmetric;
90 tensepsilon::tensepsilon() : minkowski(false), pos_sig(false)
92 tinfo_key = TINFO_tensepsilon;
95 tensepsilon::tensepsilon(bool mink, bool ps) : minkowski(mink), pos_sig(ps)
97 tinfo_key = TINFO_tensepsilon;
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 if (! is_a<idx>(other->op(i)))
360 const idx &other_idx = ex_to<idx>(other->op(i));
361 if (is_dummy_pair(*self_idx, other_idx)) {
363 // Contraction found, remove this tensor and substitute the
364 // index in the second object
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
371 } catch (std::exception &e) {
378 if (!first_index_tried) {
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;
390 /** Contraction of an indexed delta tensor with something else. */
391 bool tensdelta::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
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)));
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);
403 /** Contraction of an indexed metric tensor with something else. */
404 bool tensmetric::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
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)));
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)))
416 // Replace the dummy index with this tensor's other index and remove
418 return replace_contr_index(self, other);
421 /** Contraction of an indexed spinor metric with something else. */
422 bool spinmetric::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
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)));
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));
436 if (is_dummy_pair(self_i1, other_i1)) {
437 if (is_dummy_pair(self_i2, other_i2))
440 *self = delta_tensor(self_i2, other_i2);
443 } else if (is_dummy_pair(self_i1, other_i2)) {
444 if (is_dummy_pair(self_i2, other_i1))
447 *self = -delta_tensor(self_i2, other_i1);
450 } else if (is_dummy_pair(self_i2, other_i1)) {
451 *self = -delta_tensor(self_i1, other_i2);
454 } else if (is_dummy_pair(self_i2, other_i2)) {
455 *self = delta_tensor(self_i1, other_i1);
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)))
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;
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)) {
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);
488 if (!first_index_tried) {
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;
501 /** Contraction of epsilon tensor with something else. */
502 bool tensepsilon::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
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;
509 if (is_exactly_a<tensepsilon>(other->op(0)) && num+1 == other->nops()) {
511 // Contraction of two epsilon tensors is a determinant
512 bool variance = is_a<varidx>(self->op(1));
514 for (size_t i=0; i<num; i++) {
515 for (size_t j=0; j<num; j++) {
517 M(i, j) = lorentz_g(self->op(i+1), other->op(j+1), pos_sig);
519 M(i, j) = metric_tensor(self->op(i+1), other->op(j+1));
521 M(i, j) = delta_tensor(self->op(i+1), other->op(j+1));
524 int sign = minkowski ? -1 : 1;
525 *self = sign * M.determinant().simplify_indexed();
537 ex delta_tensor(const ex & i1, const ex & i2)
539 static ex delta = (new tensdelta)->setflag(status_flags::dynallocated);
541 if (!is_a<idx>(i1) || !is_a<idx>(i2))
542 throw(std::invalid_argument("indices of delta tensor must be of type idx"));
544 return indexed(delta, symmetric2(), i1, i2);
547 ex metric_tensor(const ex & i1, const ex & i2)
549 static ex metric = (new tensmetric)->setflag(status_flags::dynallocated);
551 if (!is_a<varidx>(i1) || !is_a<varidx>(i2))
552 throw(std::invalid_argument("indices of metric tensor must be of type varidx"));
554 return indexed(metric, symmetric2(), i1, i2);
557 ex lorentz_g(const ex & i1, const ex & i2, bool pos_sig)
559 static ex metric_neg = (new minkmetric(false))->setflag(status_flags::dynallocated);
560 static ex metric_pos = (new minkmetric(true))->setflag(status_flags::dynallocated);
562 if (!is_a<varidx>(i1) || !is_a<varidx>(i2))
563 throw(std::invalid_argument("indices of metric tensor must be of type varidx"));
565 return indexed(pos_sig ? metric_pos : metric_neg, symmetric2(), i1, i2);
568 ex spinor_metric(const ex & i1, const ex & i2)
570 static ex metric = (new spinmetric)->setflag(status_flags::dynallocated);
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"));
577 return indexed(metric, antisymmetric2(), i1, i2);
580 ex epsilon_tensor(const ex & i1, const ex & i2)
582 static ex epsilon = (new tensepsilon)->setflag(status_flags::dynallocated);
584 if (!is_a<idx>(i1) || !is_a<idx>(i2))
585 throw(std::invalid_argument("indices of epsilon tensor must be of type idx"));
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"));
593 return indexed(epsilon, antisymmetric2(), i1, i2);
596 ex epsilon_tensor(const ex & i1, const ex & i2, const ex & i3)
598 static ex epsilon = (new tensepsilon)->setflag(status_flags::dynallocated);
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"));
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"));
609 return indexed(epsilon, antisymmetric3(), i1, i2, i3);
612 ex lorentz_eps(const ex & i1, const ex & i2, const ex & i3, const ex & i4, bool pos_sig)
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);
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"));
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"));
626 return indexed(pos_sig ? epsilon_pos : epsilon_neg, antisymmetric4(), i1, i2, i3, i4);