3 * Implementation of GiNaC's special tensors. */
6 * GiNaC Copyright (C) 1999-2003 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include "relational.h"
32 #include "operators.h"
42 GINAC_IMPLEMENT_REGISTERED_CLASS(tensor, basic)
44 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(tensdelta, tensor,
45 print_func<print_dflt>(&tensdelta::do_print).
46 print_func<print_latex>(&tensdelta::do_print_latex))
48 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(tensmetric, tensor,
49 print_func<print_dflt>(&tensmetric::do_print).
50 print_func<print_latex>(&tensmetric::do_print))
52 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(minkmetric, tensmetric,
53 print_func<print_dflt>(&minkmetric::do_print).
54 print_func<print_latex>(&minkmetric::do_print_latex))
56 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(spinmetric, tensmetric,
57 print_func<print_dflt>(&spinmetric::do_print).
58 print_func<print_latex>(&spinmetric::do_print_latex))
60 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(tensepsilon, tensor,
61 print_func<print_dflt>(&tensepsilon::do_print).
62 print_func<print_latex>(&tensepsilon::do_print_latex))
68 tensor::tensor() : inherited(TINFO_tensor)
70 setflag(status_flags::evaluated | status_flags::expanded);
73 DEFAULT_CTOR(tensdelta)
74 DEFAULT_CTOR(tensmetric)
76 minkmetric::minkmetric() : pos_sig(false)
78 tinfo_key = TINFO_minkmetric;
81 spinmetric::spinmetric()
83 tinfo_key = TINFO_spinmetric;
86 minkmetric::minkmetric(bool ps) : pos_sig(ps)
88 tinfo_key = TINFO_minkmetric;
91 tensepsilon::tensepsilon() : minkowski(false), pos_sig(false)
93 tinfo_key = TINFO_tensepsilon;
96 tensepsilon::tensepsilon(bool mink, bool ps) : minkowski(mink), pos_sig(ps)
98 tinfo_key = TINFO_tensepsilon;
105 DEFAULT_ARCHIVING(tensor)
106 DEFAULT_ARCHIVING(tensdelta)
107 DEFAULT_ARCHIVING(tensmetric)
108 DEFAULT_ARCHIVING(spinmetric)
109 DEFAULT_UNARCHIVE(minkmetric)
110 DEFAULT_UNARCHIVE(tensepsilon)
112 minkmetric::minkmetric(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
114 n.find_bool("pos_sig", pos_sig);
117 void minkmetric::archive(archive_node &n) const
119 inherited::archive(n);
120 n.add_bool("pos_sig", pos_sig);
123 tensepsilon::tensepsilon(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
125 n.find_bool("minkowski", minkowski);
126 n.find_bool("pos_sig", pos_sig);
129 void tensepsilon::archive(archive_node &n) const
131 inherited::archive(n);
132 n.add_bool("minkowski", minkowski);
133 n.add_bool("pos_sig", pos_sig);
137 // functions overriding virtual functions from base classes
140 DEFAULT_COMPARE(tensor)
141 DEFAULT_COMPARE(tensdelta)
142 DEFAULT_COMPARE(tensmetric)
143 DEFAULT_COMPARE(spinmetric)
145 int minkmetric::compare_same_type(const basic & other) const
147 GINAC_ASSERT(is_a<minkmetric>(other));
148 const minkmetric &o = static_cast<const minkmetric &>(other);
150 if (pos_sig != o.pos_sig)
151 return pos_sig ? -1 : 1;
153 return inherited::compare_same_type(other);
156 int tensepsilon::compare_same_type(const basic & other) const
158 GINAC_ASSERT(is_a<tensepsilon>(other));
159 const tensepsilon &o = static_cast<const tensepsilon &>(other);
161 if (minkowski != o.minkowski)
162 return minkowski ? -1 : 1;
163 else if (pos_sig != o.pos_sig)
164 return pos_sig ? -1 : 1;
166 return inherited::compare_same_type(other);
169 DEFAULT_PRINT_LATEX(tensdelta, "delta", "\\delta")
170 DEFAULT_PRINT(tensmetric, "g")
171 DEFAULT_PRINT_LATEX(minkmetric, "eta", "\\eta")
172 DEFAULT_PRINT_LATEX(spinmetric, "eps", "\\varepsilon")
173 DEFAULT_PRINT_LATEX(tensepsilon, "eps", "\\varepsilon")
175 /** Automatic symbolic evaluation of an indexed delta tensor. */
176 ex tensdelta::eval_indexed(const basic & i) const
178 GINAC_ASSERT(is_a<indexed>(i));
179 GINAC_ASSERT(i.nops() == 3);
180 GINAC_ASSERT(is_a<tensdelta>(i.op(0)));
182 const idx & i1 = ex_to<idx>(i.op(1));
183 const idx & i2 = ex_to<idx>(i.op(2));
185 // The dimension of the indices must be equal, otherwise we use the minimal
187 if (!i1.get_dim().is_equal(i2.get_dim())) {
188 ex min_dim = i1.minimal_dim(i2);
190 m[i1] = i1.replace_dim(min_dim);
191 m[i2] = i2.replace_dim(min_dim);
192 return i.subs(m, subs_options::no_pattern);
195 // Trace of delta tensor is the (effective) dimension of the space
196 if (is_dummy_pair(i1, i2)) {
198 return i1.minimal_dim(i2);
199 } catch (std::exception &e) {
204 // Numeric evaluation
205 if (static_cast<const indexed &>(i).all_index_values_are(info_flags::integer)) {
206 int n1 = ex_to<numeric>(i1.get_value()).to_int(), n2 = ex_to<numeric>(i2.get_value()).to_int();
213 // No further simplifications
217 /** Automatic symbolic evaluation of an indexed metric tensor. */
218 ex tensmetric::eval_indexed(const basic & i) const
220 GINAC_ASSERT(is_a<indexed>(i));
221 GINAC_ASSERT(i.nops() == 3);
222 GINAC_ASSERT(is_a<tensmetric>(i.op(0)));
223 GINAC_ASSERT(is_a<varidx>(i.op(1)));
224 GINAC_ASSERT(is_a<varidx>(i.op(2)));
226 const varidx & i1 = ex_to<varidx>(i.op(1));
227 const varidx & i2 = ex_to<varidx>(i.op(2));
229 // The dimension of the indices must be equal, otherwise we use the minimal
231 if (!i1.get_dim().is_equal(i2.get_dim())) {
232 ex min_dim = i1.minimal_dim(i2);
234 m[i1] = i1.replace_dim(min_dim);
235 m[i2] = i2.replace_dim(min_dim);
236 return i.subs(m, subs_options::no_pattern);
239 // A metric tensor with one covariant and one contravariant index gets
240 // replaced by a delta tensor
241 if (i1.is_covariant() != i2.is_covariant())
242 return delta_tensor(i1, i2);
244 // No further simplifications
248 /** Automatic symbolic evaluation of an indexed Lorentz metric tensor. */
249 ex minkmetric::eval_indexed(const basic & i) const
251 GINAC_ASSERT(is_a<indexed>(i));
252 GINAC_ASSERT(i.nops() == 3);
253 GINAC_ASSERT(is_a<minkmetric>(i.op(0)));
254 GINAC_ASSERT(is_a<varidx>(i.op(1)));
255 GINAC_ASSERT(is_a<varidx>(i.op(2)));
257 const varidx & i1 = ex_to<varidx>(i.op(1));
258 const varidx & i2 = ex_to<varidx>(i.op(2));
260 // Numeric evaluation
261 if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
262 int n1 = ex_to<numeric>(i1.get_value()).to_int(), n2 = ex_to<numeric>(i2.get_value()).to_int();
266 return pos_sig ? _ex_1 : _ex1;
268 return pos_sig ? _ex1 : _ex_1;
271 // Perform the usual evaluations of a metric tensor
272 return inherited::eval_indexed(i);
275 /** Automatic symbolic evaluation of an indexed metric tensor. */
276 ex spinmetric::eval_indexed(const basic & i) const
278 GINAC_ASSERT(is_a<indexed>(i));
279 GINAC_ASSERT(i.nops() == 3);
280 GINAC_ASSERT(is_a<spinmetric>(i.op(0)));
281 GINAC_ASSERT(is_a<spinidx>(i.op(1)));
282 GINAC_ASSERT(is_a<spinidx>(i.op(2)));
284 const spinidx & i1 = ex_to<spinidx>(i.op(1));
285 const spinidx & i2 = ex_to<spinidx>(i.op(2));
287 // Convolutions are zero
288 if (!(static_cast<const indexed &>(i).get_dummy_indices().empty()))
291 // Numeric evaluation
292 if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
293 int n1 = ex_to<numeric>(i1.get_value()).to_int(), n2 = ex_to<numeric>(i2.get_value()).to_int();
302 // No further simplifications
306 /** Automatic symbolic evaluation of an indexed epsilon tensor. */
307 ex tensepsilon::eval_indexed(const basic & i) const
309 GINAC_ASSERT(is_a<indexed>(i));
310 GINAC_ASSERT(i.nops() > 1);
311 GINAC_ASSERT(is_a<tensepsilon>(i.op(0)));
313 // Convolutions are zero
314 if (!(static_cast<const indexed &>(i).get_dummy_indices().empty()))
317 // Numeric evaluation
318 if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
320 // Get sign of index permutation (the indices should already be in
321 // a canonic order but we can't assume what exactly that order is)
323 v.reserve(i.nops() - 1);
324 for (size_t j=1; j<i.nops(); j++)
325 v.push_back(ex_to<numeric>(ex_to<idx>(i.op(j)).get_value()).to_int());
326 int sign = permutation_sign(v.begin(), v.end());
328 // In a Minkowski space, check for covariant indices
330 for (size_t j=1; j<i.nops(); j++) {
331 const ex & x = i.op(j);
332 if (!is_a<varidx>(x))
333 throw(std::runtime_error("indices of epsilon tensor in Minkowski space must be of type varidx"));
334 if (ex_to<varidx>(x).is_covariant())
335 if (ex_to<idx>(x).get_value().is_zero())
336 sign = (pos_sig ? -sign : sign);
338 sign = (pos_sig ? sign : -sign);
345 // No further simplifications
349 bool tensor::replace_contr_index(exvector::iterator self, exvector::iterator other) const
351 // Try to contract the first index
352 const idx *self_idx = &ex_to<idx>(self->op(1));
353 const idx *free_idx = &ex_to<idx>(self->op(2));
354 bool first_index_tried = false;
357 if (self_idx->is_symbolic()) {
358 for (size_t i=1; i<other->nops(); i++) {
359 const idx &other_idx = ex_to<idx>(other->op(i));
360 if (is_dummy_pair(*self_idx, other_idx)) {
362 // Contraction found, remove this tensor and substitute the
363 // index in the second object
365 // minimal_dim() throws an exception when index dimensions are not comparable
366 ex min_dim = self_idx->minimal_dim(other_idx);
367 *other = other->subs(other_idx == free_idx->replace_dim(min_dim));
368 *self = _ex1; // *other is assigned first because assigning *self invalidates free_idx
370 } catch (std::exception &e) {
377 if (!first_index_tried) {
379 // No contraction with the first index found, try the second index
380 self_idx = &ex_to<idx>(self->op(2));
381 free_idx = &ex_to<idx>(self->op(1));
382 first_index_tried = true;
389 /** Contraction of an indexed delta tensor with something else. */
390 bool tensdelta::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
392 GINAC_ASSERT(is_a<indexed>(*self));
393 GINAC_ASSERT(is_a<indexed>(*other));
394 GINAC_ASSERT(self->nops() == 3);
395 GINAC_ASSERT(is_a<tensdelta>(self->op(0)));
397 // Replace the dummy index with this tensor's other index and remove
398 // the tensor (this is valid for contractions with all other tensors)
399 return replace_contr_index(self, other);
402 /** Contraction of an indexed metric tensor with something else. */
403 bool tensmetric::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
405 GINAC_ASSERT(is_a<indexed>(*self));
406 GINAC_ASSERT(is_a<indexed>(*other));
407 GINAC_ASSERT(self->nops() == 3);
408 GINAC_ASSERT(is_a<tensmetric>(self->op(0)));
410 // If contracting with the delta tensor, let the delta do it
411 // (don't raise/lower delta indices)
412 if (is_a<tensdelta>(other->op(0)))
415 // Replace the dummy index with this tensor's other index and remove
417 return replace_contr_index(self, other);
420 /** Contraction of an indexed spinor metric with something else. */
421 bool spinmetric::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
423 GINAC_ASSERT(is_a<indexed>(*self));
424 GINAC_ASSERT(is_a<indexed>(*other));
425 GINAC_ASSERT(self->nops() == 3);
426 GINAC_ASSERT(is_a<spinmetric>(self->op(0)));
428 // Contractions between spinor metrics
429 if (is_a<spinmetric>(other->op(0))) {
430 const idx &self_i1 = ex_to<idx>(self->op(1));
431 const idx &self_i2 = ex_to<idx>(self->op(2));
432 const idx &other_i1 = ex_to<idx>(other->op(1));
433 const idx &other_i2 = ex_to<idx>(other->op(2));
435 if (is_dummy_pair(self_i1, other_i1)) {
436 if (is_dummy_pair(self_i2, other_i2))
439 *self = delta_tensor(self_i2, other_i2);
442 } else if (is_dummy_pair(self_i1, other_i2)) {
443 if (is_dummy_pair(self_i2, other_i1))
446 *self = -delta_tensor(self_i2, other_i1);
449 } else if (is_dummy_pair(self_i2, other_i1)) {
450 *self = -delta_tensor(self_i1, other_i2);
453 } else if (is_dummy_pair(self_i2, other_i2)) {
454 *self = delta_tensor(self_i1, other_i1);
460 // If contracting with the delta tensor, let the delta do it
461 // (don't raise/lower delta indices)
462 if (is_a<tensdelta>(other->op(0)))
465 // Try to contract first index
466 const idx *self_idx = &ex_to<idx>(self->op(1));
467 const idx *free_idx = &ex_to<idx>(self->op(2));
468 bool first_index_tried = false;
472 if (self_idx->is_symbolic()) {
473 for (size_t i=1; i<other->nops(); i++) {
474 const idx &other_idx = ex_to<idx>(other->op(i));
475 if (is_dummy_pair(*self_idx, other_idx)) {
477 // Contraction found, remove metric tensor and substitute
478 // index in second object (assign *self last because this
479 // invalidates free_idx)
480 *other = other->subs(other_idx == *free_idx);
481 *self = (static_cast<const spinidx *>(self_idx)->is_covariant() ? sign : -sign);
487 if (!first_index_tried) {
489 // No contraction with first index found, try second index
490 self_idx = &ex_to<idx>(self->op(2));
491 free_idx = &ex_to<idx>(self->op(1));
492 first_index_tried = true;
500 /** Contraction of epsilon tensor with something else. */
501 bool tensepsilon::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
503 GINAC_ASSERT(is_a<indexed>(*self));
504 GINAC_ASSERT(is_a<indexed>(*other));
505 GINAC_ASSERT(is_a<tensepsilon>(self->op(0)));
506 size_t num = self->nops() - 1;
508 if (is_exactly_a<tensepsilon>(other->op(0)) && num+1 == other->nops()) {
510 // Contraction of two epsilon tensors is a determinant
511 bool variance = is_a<varidx>(self->op(1));
513 for (size_t i=0; i<num; i++) {
514 for (size_t j=0; j<num; j++) {
516 M(i, j) = lorentz_g(self->op(i+1), other->op(j+1), pos_sig);
518 M(i, j) = metric_tensor(self->op(i+1), other->op(j+1));
520 M(i, j) = delta_tensor(self->op(i+1), other->op(j+1));
523 int sign = minkowski ? -1 : 1;
524 *self = sign * M.determinant().simplify_indexed();
536 ex delta_tensor(const ex & i1, const ex & i2)
538 if (!is_a<idx>(i1) || !is_a<idx>(i2))
539 throw(std::invalid_argument("indices of delta tensor must be of type idx"));
541 return indexed(tensdelta(), sy_symm(), i1, i2);
544 ex metric_tensor(const ex & i1, const ex & i2)
546 if (!is_a<varidx>(i1) || !is_a<varidx>(i2))
547 throw(std::invalid_argument("indices of metric tensor must be of type varidx"));
549 return indexed(tensmetric(), sy_symm(), i1, i2);
552 ex lorentz_g(const ex & i1, const ex & i2, bool pos_sig)
554 if (!is_a<varidx>(i1) || !is_a<varidx>(i2))
555 throw(std::invalid_argument("indices of metric tensor must be of type varidx"));
557 return indexed(minkmetric(pos_sig), sy_symm(), i1, i2);
560 ex spinor_metric(const ex & i1, const ex & i2)
562 if (!is_a<spinidx>(i1) || !is_a<spinidx>(i2))
563 throw(std::invalid_argument("indices of spinor metric must be of type spinidx"));
564 if (!ex_to<idx>(i1).get_dim().is_equal(2) || !ex_to<idx>(i2).get_dim().is_equal(2))
565 throw(std::runtime_error("index dimension for spinor metric must be 2"));
567 return indexed(spinmetric(), sy_anti(), i1, i2);
570 ex epsilon_tensor(const ex & i1, const ex & i2)
572 if (!is_a<idx>(i1) || !is_a<idx>(i2))
573 throw(std::invalid_argument("indices of epsilon tensor must be of type idx"));
575 ex dim = ex_to<idx>(i1).get_dim();
576 if (!dim.is_equal(ex_to<idx>(i2).get_dim()))
577 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
578 if (!ex_to<idx>(i1).get_dim().is_equal(_ex2))
579 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
581 return indexed(tensepsilon(), sy_anti(), i1, i2);
584 ex epsilon_tensor(const ex & i1, const ex & i2, const ex & i3)
586 if (!is_a<idx>(i1) || !is_a<idx>(i2) || !is_a<idx>(i3))
587 throw(std::invalid_argument("indices of epsilon tensor must be of type idx"));
589 ex dim = ex_to<idx>(i1).get_dim();
590 if (!dim.is_equal(ex_to<idx>(i2).get_dim()) || !dim.is_equal(ex_to<idx>(i3).get_dim()))
591 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
592 if (!ex_to<idx>(i1).get_dim().is_equal(_ex3))
593 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
595 return indexed(tensepsilon(), sy_anti(), i1, i2, i3);
598 ex lorentz_eps(const ex & i1, const ex & i2, const ex & i3, const ex & i4, bool pos_sig)
600 if (!is_a<varidx>(i1) || !is_a<varidx>(i2) || !is_a<varidx>(i3) || !is_a<varidx>(i4))
601 throw(std::invalid_argument("indices of Lorentz epsilon tensor must be of type varidx"));
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()) || !dim.is_equal(ex_to<idx>(i4).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(_ex4))
607 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
609 return indexed(tensepsilon(true, pos_sig), sy_anti(), i1, i2, i3, i4);