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)
43 GINAC_IMPLEMENT_REGISTERED_CLASS(tensdelta, tensor)
44 GINAC_IMPLEMENT_REGISTERED_CLASS(tensmetric, tensor)
45 GINAC_IMPLEMENT_REGISTERED_CLASS(minkmetric, tensmetric)
46 GINAC_IMPLEMENT_REGISTERED_CLASS(spinmetric, tensmetric)
47 GINAC_IMPLEMENT_REGISTERED_CLASS(tensepsilon, tensor)
50 // default ctor, dtor, copy ctor, assignment operator and helpers
54 DEFAULT_CTORS(tensdelta)
55 DEFAULT_CTORS(tensmetric)
56 DEFAULT_COPY(spinmetric)
57 DEFAULT_DESTROY(spinmetric)
58 DEFAULT_DESTROY(minkmetric)
59 DEFAULT_DESTROY(tensepsilon)
61 minkmetric::minkmetric() : pos_sig(false)
63 tinfo_key = TINFO_minkmetric;
66 spinmetric::spinmetric()
68 tinfo_key = TINFO_spinmetric;
71 minkmetric::minkmetric(bool ps) : pos_sig(ps)
73 tinfo_key = TINFO_minkmetric;
76 void minkmetric::copy(const minkmetric & other)
78 inherited::copy(other);
79 pos_sig = other.pos_sig;
82 tensepsilon::tensepsilon() : minkowski(false), pos_sig(false)
84 tinfo_key = TINFO_tensepsilon;
87 tensepsilon::tensepsilon(bool mink, bool ps) : minkowski(mink), pos_sig(ps)
89 tinfo_key = TINFO_tensepsilon;
92 void tensepsilon::copy(const tensepsilon & other)
94 inherited::copy(other);
95 minkowski = other.minkowski;
96 pos_sig = other.pos_sig;
103 DEFAULT_ARCHIVING(tensor)
104 DEFAULT_ARCHIVING(tensdelta)
105 DEFAULT_ARCHIVING(tensmetric)
106 DEFAULT_ARCHIVING(spinmetric)
107 DEFAULT_UNARCHIVE(minkmetric)
108 DEFAULT_UNARCHIVE(tensepsilon)
110 minkmetric::minkmetric(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
112 n.find_bool("pos_sig", pos_sig);
115 void minkmetric::archive(archive_node &n) const
117 inherited::archive(n);
118 n.add_bool("pos_sig", pos_sig);
121 tensepsilon::tensepsilon(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
123 n.find_bool("minkowski", minkowski);
124 n.find_bool("pos_sig", pos_sig);
127 void tensepsilon::archive(archive_node &n) const
129 inherited::archive(n);
130 n.add_bool("minkowski", minkowski);
131 n.add_bool("pos_sig", pos_sig);
135 // functions overriding virtual functions from base classes
138 DEFAULT_COMPARE(tensor)
139 DEFAULT_COMPARE(tensdelta)
140 DEFAULT_COMPARE(tensmetric)
141 DEFAULT_COMPARE(spinmetric)
143 int minkmetric::compare_same_type(const basic & other) const
145 GINAC_ASSERT(is_a<minkmetric>(other));
146 const minkmetric &o = static_cast<const minkmetric &>(other);
148 if (pos_sig != o.pos_sig)
149 return pos_sig ? -1 : 1;
151 return inherited::compare_same_type(other);
154 int tensepsilon::compare_same_type(const basic & other) const
156 GINAC_ASSERT(is_a<tensepsilon>(other));
157 const tensepsilon &o = static_cast<const tensepsilon &>(other);
159 if (minkowski != o.minkowski)
160 return minkowski ? -1 : 1;
161 else if (pos_sig != o.pos_sig)
162 return pos_sig ? -1 : 1;
164 return inherited::compare_same_type(other);
167 DEFAULT_PRINT_LATEX(tensdelta, "delta", "\\delta")
168 DEFAULT_PRINT(tensmetric, "g")
169 DEFAULT_PRINT_LATEX(minkmetric, "eta", "\\eta")
170 DEFAULT_PRINT_LATEX(spinmetric, "eps", "\\varepsilon")
171 DEFAULT_PRINT_LATEX(tensepsilon, "eps", "\\varepsilon")
173 /** Automatic symbolic evaluation of an indexed delta tensor. */
174 ex tensdelta::eval_indexed(const basic & i) const
176 GINAC_ASSERT(is_a<indexed>(i));
177 GINAC_ASSERT(i.nops() == 3);
178 GINAC_ASSERT(is_a<tensdelta>(i.op(0)));
180 const idx & i1 = ex_to<idx>(i.op(1));
181 const idx & i2 = ex_to<idx>(i.op(2));
183 // The dimension of the indices must be equal, otherwise we use the minimal
185 if (!i1.get_dim().is_equal(i2.get_dim())) {
186 ex min_dim = i1.minimal_dim(i2);
187 return i.subs(lst(i1 == i1.replace_dim(min_dim), i2 == i2.replace_dim(min_dim)));
190 // Trace of delta tensor is the (effective) dimension of the space
191 if (is_dummy_pair(i1, i2)) {
193 return i1.minimal_dim(i2);
194 } catch (std::exception &e) {
199 // Numeric evaluation
200 if (static_cast<const indexed &>(i).all_index_values_are(info_flags::integer)) {
201 int n1 = ex_to<numeric>(i1.get_value()).to_int(), n2 = ex_to<numeric>(i2.get_value()).to_int();
208 // No further simplifications
212 /** Automatic symbolic evaluation of an indexed metric tensor. */
213 ex tensmetric::eval_indexed(const basic & i) const
215 GINAC_ASSERT(is_a<indexed>(i));
216 GINAC_ASSERT(i.nops() == 3);
217 GINAC_ASSERT(is_a<tensmetric>(i.op(0)));
218 GINAC_ASSERT(is_a<varidx>(i.op(1)));
219 GINAC_ASSERT(is_a<varidx>(i.op(2)));
221 const varidx & i1 = ex_to<varidx>(i.op(1));
222 const varidx & i2 = ex_to<varidx>(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);
228 return i.subs(lst(i1 == i1.replace_dim(min_dim), i2 == i2.replace_dim(min_dim)));
231 // A metric tensor with one covariant and one contravariant index gets
232 // replaced by a delta tensor
233 if (i1.is_covariant() != i2.is_covariant())
234 return delta_tensor(i1, i2);
236 // No further simplifications
240 /** Automatic symbolic evaluation of an indexed Lorentz metric tensor. */
241 ex minkmetric::eval_indexed(const basic & i) const
243 GINAC_ASSERT(is_a<indexed>(i));
244 GINAC_ASSERT(i.nops() == 3);
245 GINAC_ASSERT(is_a<minkmetric>(i.op(0)));
246 GINAC_ASSERT(is_a<varidx>(i.op(1)));
247 GINAC_ASSERT(is_a<varidx>(i.op(2)));
249 const varidx & i1 = ex_to<varidx>(i.op(1));
250 const varidx & i2 = ex_to<varidx>(i.op(2));
252 // Numeric evaluation
253 if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
254 int n1 = ex_to<numeric>(i1.get_value()).to_int(), n2 = ex_to<numeric>(i2.get_value()).to_int();
258 return pos_sig ? _ex_1 : _ex1;
260 return pos_sig ? _ex1 : _ex_1;
263 // Perform the usual evaluations of a metric tensor
264 return inherited::eval_indexed(i);
267 /** Automatic symbolic evaluation of an indexed metric tensor. */
268 ex spinmetric::eval_indexed(const basic & i) const
270 GINAC_ASSERT(is_a<indexed>(i));
271 GINAC_ASSERT(i.nops() == 3);
272 GINAC_ASSERT(is_a<spinmetric>(i.op(0)));
273 GINAC_ASSERT(is_a<spinidx>(i.op(1)));
274 GINAC_ASSERT(is_a<spinidx>(i.op(2)));
276 const spinidx & i1 = ex_to<spinidx>(i.op(1));
277 const spinidx & i2 = ex_to<spinidx>(i.op(2));
279 // Convolutions are zero
280 if (!(static_cast<const indexed &>(i).get_dummy_indices().empty()))
283 // Numeric evaluation
284 if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
285 int n1 = ex_to<numeric>(i1.get_value()).to_int(), n2 = ex_to<numeric>(i2.get_value()).to_int();
294 // No further simplifications
298 /** Automatic symbolic evaluation of an indexed epsilon tensor. */
299 ex tensepsilon::eval_indexed(const basic & i) const
301 GINAC_ASSERT(is_a<indexed>(i));
302 GINAC_ASSERT(i.nops() > 1);
303 GINAC_ASSERT(is_a<tensepsilon>(i.op(0)));
305 // Convolutions are zero
306 if (!(static_cast<const indexed &>(i).get_dummy_indices().empty()))
309 // Numeric evaluation
310 if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
312 // Get sign of index permutation (the indices should already be in
313 // a canonic order but we can't assume what exactly that order is)
315 v.reserve(i.nops() - 1);
316 for (size_t j=1; j<i.nops(); j++)
317 v.push_back(ex_to<numeric>(ex_to<idx>(i.op(j)).get_value()).to_int());
318 int sign = permutation_sign(v.begin(), v.end());
320 // In a Minkowski space, check for covariant indices
322 for (size_t j=1; j<i.nops(); j++) {
323 const ex & x = i.op(j);
324 if (!is_a<varidx>(x))
325 throw(std::runtime_error("indices of epsilon tensor in Minkowski space must be of type varidx"));
326 if (ex_to<varidx>(x).is_covariant())
327 if (ex_to<idx>(x).get_value().is_zero())
328 sign = (pos_sig ? -sign : sign);
330 sign = (pos_sig ? sign : -sign);
337 // No further simplifications
341 bool tensor::replace_contr_index(exvector::iterator self, exvector::iterator other) const
343 // Try to contract the first index
344 const idx *self_idx = &ex_to<idx>(self->op(1));
345 const idx *free_idx = &ex_to<idx>(self->op(2));
346 bool first_index_tried = false;
349 if (self_idx->is_symbolic()) {
350 for (size_t i=1; i<other->nops(); i++) {
351 const idx &other_idx = ex_to<idx>(other->op(i));
352 if (is_dummy_pair(*self_idx, other_idx)) {
354 // Contraction found, remove this tensor and substitute the
355 // index in the second object
357 // minimal_dim() throws an exception when index dimensions are not comparable
358 ex min_dim = self_idx->minimal_dim(other_idx);
359 *other = other->subs(other_idx == free_idx->replace_dim(min_dim));
360 *self = _ex1; // *other is assigned first because assigning *self invalidates free_idx
362 } catch (std::exception &e) {
369 if (!first_index_tried) {
371 // No contraction with the first index found, try the second index
372 self_idx = &ex_to<idx>(self->op(2));
373 free_idx = &ex_to<idx>(self->op(1));
374 first_index_tried = true;
381 /** Contraction of an indexed delta tensor with something else. */
382 bool tensdelta::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
384 GINAC_ASSERT(is_a<indexed>(*self));
385 GINAC_ASSERT(is_a<indexed>(*other));
386 GINAC_ASSERT(self->nops() == 3);
387 GINAC_ASSERT(is_a<tensdelta>(self->op(0)));
389 // Replace the dummy index with this tensor's other index and remove
390 // the tensor (this is valid for contractions with all other tensors)
391 return replace_contr_index(self, other);
394 /** Contraction of an indexed metric tensor with something else. */
395 bool tensmetric::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
397 GINAC_ASSERT(is_a<indexed>(*self));
398 GINAC_ASSERT(is_a<indexed>(*other));
399 GINAC_ASSERT(self->nops() == 3);
400 GINAC_ASSERT(is_a<tensmetric>(self->op(0)));
402 // If contracting with the delta tensor, let the delta do it
403 // (don't raise/lower delta indices)
404 if (is_a<tensdelta>(other->op(0)))
407 // Replace the dummy index with this tensor's other index and remove
409 return replace_contr_index(self, other);
412 /** Contraction of an indexed spinor metric with something else. */
413 bool spinmetric::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
415 GINAC_ASSERT(is_a<indexed>(*self));
416 GINAC_ASSERT(is_a<indexed>(*other));
417 GINAC_ASSERT(self->nops() == 3);
418 GINAC_ASSERT(is_a<spinmetric>(self->op(0)));
420 // Contractions between spinor metrics
421 if (is_a<spinmetric>(other->op(0))) {
422 const idx &self_i1 = ex_to<idx>(self->op(1));
423 const idx &self_i2 = ex_to<idx>(self->op(2));
424 const idx &other_i1 = ex_to<idx>(other->op(1));
425 const idx &other_i2 = ex_to<idx>(other->op(2));
427 if (is_dummy_pair(self_i1, other_i1)) {
428 if (is_dummy_pair(self_i2, other_i2))
431 *self = delta_tensor(self_i2, other_i2);
434 } else if (is_dummy_pair(self_i1, other_i2)) {
435 if (is_dummy_pair(self_i2, other_i1))
438 *self = -delta_tensor(self_i2, other_i1);
441 } else if (is_dummy_pair(self_i2, other_i1)) {
442 *self = -delta_tensor(self_i1, other_i2);
445 } else if (is_dummy_pair(self_i2, other_i2)) {
446 *self = delta_tensor(self_i1, other_i1);
452 // If contracting with the delta tensor, let the delta do it
453 // (don't raise/lower delta indices)
454 if (is_a<tensdelta>(other->op(0)))
457 // Try to contract first index
458 const idx *self_idx = &ex_to<idx>(self->op(1));
459 const idx *free_idx = &ex_to<idx>(self->op(2));
460 bool first_index_tried = false;
464 if (self_idx->is_symbolic()) {
465 for (size_t i=1; i<other->nops(); i++) {
466 const idx &other_idx = ex_to<idx>(other->op(i));
467 if (is_dummy_pair(*self_idx, other_idx)) {
469 // Contraction found, remove metric tensor and substitute
470 // index in second object (assign *self last because this
471 // invalidates free_idx)
472 *other = other->subs(other_idx == *free_idx);
473 *self = (static_cast<const spinidx *>(self_idx)->is_covariant() ? sign : -sign);
479 if (!first_index_tried) {
481 // No contraction with first index found, try second index
482 self_idx = &ex_to<idx>(self->op(2));
483 free_idx = &ex_to<idx>(self->op(1));
484 first_index_tried = true;
492 /** Contraction of epsilon tensor with something else. */
493 bool tensepsilon::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
495 GINAC_ASSERT(is_a<indexed>(*self));
496 GINAC_ASSERT(is_a<indexed>(*other));
497 GINAC_ASSERT(is_a<tensepsilon>(self->op(0)));
498 size_t num = self->nops() - 1;
500 if (is_exactly_a<tensepsilon>(other->op(0)) && num+1 == other->nops()) {
502 // Contraction of two epsilon tensors is a determinant
503 bool variance = is_a<varidx>(self->op(1));
505 for (size_t i=0; i<num; i++) {
506 for (size_t j=0; j<num; j++) {
508 M(i, j) = lorentz_g(self->op(i+1), other->op(j+1), pos_sig);
510 M(i, j) = metric_tensor(self->op(i+1), other->op(j+1));
512 M(i, j) = delta_tensor(self->op(i+1), other->op(j+1));
515 int sign = minkowski ? -1 : 1;
516 *self = sign * M.determinant().simplify_indexed();
528 ex delta_tensor(const ex & i1, const ex & i2)
530 if (!is_a<idx>(i1) || !is_a<idx>(i2))
531 throw(std::invalid_argument("indices of delta tensor must be of type idx"));
533 return indexed(tensdelta(), sy_symm(), i1, i2);
536 ex metric_tensor(const ex & i1, const ex & i2)
538 if (!is_a<varidx>(i1) || !is_a<varidx>(i2))
539 throw(std::invalid_argument("indices of metric tensor must be of type varidx"));
541 return indexed(tensmetric(), sy_symm(), i1, i2);
544 ex lorentz_g(const ex & i1, const ex & i2, bool pos_sig)
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(minkmetric(pos_sig), sy_symm(), i1, i2);
552 ex spinor_metric(const ex & i1, const ex & i2)
554 if (!is_a<spinidx>(i1) || !is_a<spinidx>(i2))
555 throw(std::invalid_argument("indices of spinor metric must be of type spinidx"));
556 if (!ex_to<idx>(i1).get_dim().is_equal(2) || !ex_to<idx>(i2).get_dim().is_equal(2))
557 throw(std::runtime_error("index dimension for spinor metric must be 2"));
559 return indexed(spinmetric(), sy_anti(), i1, i2);
562 ex epsilon_tensor(const ex & i1, const ex & i2)
564 if (!is_a<idx>(i1) || !is_a<idx>(i2))
565 throw(std::invalid_argument("indices of epsilon tensor must be of type idx"));
567 ex dim = ex_to<idx>(i1).get_dim();
568 if (!dim.is_equal(ex_to<idx>(i2).get_dim()))
569 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
570 if (!ex_to<idx>(i1).get_dim().is_equal(_ex2))
571 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
573 return indexed(tensepsilon(), sy_anti(), i1, i2);
576 ex epsilon_tensor(const ex & i1, const ex & i2, const ex & i3)
578 if (!is_a<idx>(i1) || !is_a<idx>(i2) || !is_a<idx>(i3))
579 throw(std::invalid_argument("indices of epsilon tensor must be of type idx"));
581 ex dim = ex_to<idx>(i1).get_dim();
582 if (!dim.is_equal(ex_to<idx>(i2).get_dim()) || !dim.is_equal(ex_to<idx>(i3).get_dim()))
583 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
584 if (!ex_to<idx>(i1).get_dim().is_equal(_ex3))
585 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
587 return indexed(tensepsilon(), sy_anti(), i1, i2, i3);
590 ex lorentz_eps(const ex & i1, const ex & i2, const ex & i3, const ex & i4, bool pos_sig)
592 if (!is_a<varidx>(i1) || !is_a<varidx>(i2) || !is_a<varidx>(i3) || !is_a<varidx>(i4))
593 throw(std::invalid_argument("indices of Lorentz epsilon tensor must be of type varidx"));
595 ex dim = ex_to<idx>(i1).get_dim();
596 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()))
597 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
598 if (!ex_to<idx>(i1).get_dim().is_equal(_ex4))
599 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
601 return indexed(tensepsilon(true, pos_sig), sy_anti(), i1, i2, i3, i4);