3 * Implementation of GiNaC's special tensors. */
6 * GiNaC Copyright (C) 1999-2001 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
30 #include "relational.h"
40 GINAC_IMPLEMENT_REGISTERED_CLASS(tensor, basic)
41 GINAC_IMPLEMENT_REGISTERED_CLASS(tensdelta, tensor)
42 GINAC_IMPLEMENT_REGISTERED_CLASS(tensmetric, tensor)
43 GINAC_IMPLEMENT_REGISTERED_CLASS(minkmetric, tensmetric)
44 GINAC_IMPLEMENT_REGISTERED_CLASS(spinmetric, tensmetric)
45 GINAC_IMPLEMENT_REGISTERED_CLASS(tensepsilon, tensor)
48 // default ctor, dtor, copy ctor, assignment operator and helpers
52 DEFAULT_CTORS(tensdelta)
53 DEFAULT_CTORS(tensmetric)
54 DEFAULT_COPY(spinmetric)
55 DEFAULT_DESTROY(spinmetric)
56 DEFAULT_DESTROY(minkmetric)
57 DEFAULT_DESTROY(tensepsilon)
59 minkmetric::minkmetric() : pos_sig(false)
61 tinfo_key = TINFO_minkmetric;
64 spinmetric::spinmetric()
66 tinfo_key = TINFO_spinmetric;
69 minkmetric::minkmetric(bool ps) : pos_sig(ps)
71 tinfo_key = TINFO_minkmetric;
74 void minkmetric::copy(const minkmetric & other)
76 inherited::copy(other);
77 pos_sig = other.pos_sig;
80 tensepsilon::tensepsilon() : minkowski(false), pos_sig(false)
82 tinfo_key = TINFO_tensepsilon;
85 tensepsilon::tensepsilon(bool mink, bool ps) : minkowski(mink), pos_sig(ps)
87 tinfo_key = TINFO_tensepsilon;
90 void tensepsilon::copy(const tensepsilon & other)
92 inherited::copy(other);
93 minkowski = other.minkowski;
94 pos_sig = other.pos_sig;
101 DEFAULT_ARCHIVING(tensor)
102 DEFAULT_ARCHIVING(tensdelta)
103 DEFAULT_ARCHIVING(tensmetric)
104 DEFAULT_ARCHIVING(spinmetric)
105 DEFAULT_UNARCHIVE(minkmetric)
106 DEFAULT_UNARCHIVE(tensepsilon)
108 minkmetric::minkmetric(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
110 n.find_bool("pos_sig", pos_sig);
113 void minkmetric::archive(archive_node &n) const
115 inherited::archive(n);
116 n.add_bool("pos_sig", pos_sig);
119 tensepsilon::tensepsilon(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
121 n.find_bool("minkowski", minkowski);
122 n.find_bool("pos_sig", pos_sig);
125 void tensepsilon::archive(archive_node &n) const
127 inherited::archive(n);
128 n.add_bool("minkowski", minkowski);
129 n.add_bool("pos_sig", pos_sig);
133 // functions overriding virtual functions from base classes
136 DEFAULT_COMPARE(tensor)
137 DEFAULT_COMPARE(tensdelta)
138 DEFAULT_COMPARE(tensmetric)
139 DEFAULT_COMPARE(spinmetric)
141 int minkmetric::compare_same_type(const basic & other) const
143 GINAC_ASSERT(is_a<minkmetric>(other));
144 const minkmetric &o = static_cast<const minkmetric &>(other);
146 if (pos_sig != o.pos_sig)
147 return pos_sig ? -1 : 1;
149 return inherited::compare_same_type(other);
152 int tensepsilon::compare_same_type(const basic & other) const
154 GINAC_ASSERT(is_a<tensepsilon>(other));
155 const tensepsilon &o = static_cast<const tensepsilon &>(other);
157 if (minkowski != o.minkowski)
158 return minkowski ? -1 : 1;
159 else if (pos_sig != o.pos_sig)
160 return pos_sig ? -1 : 1;
162 return inherited::compare_same_type(other);
165 DEFAULT_PRINT_LATEX(tensdelta, "delta", "\\delta")
166 DEFAULT_PRINT(tensmetric, "g")
167 DEFAULT_PRINT_LATEX(minkmetric, "eta", "\\eta")
168 DEFAULT_PRINT_LATEX(spinmetric, "eps", "\\varepsilon")
169 DEFAULT_PRINT_LATEX(tensepsilon, "eps", "\\varepsilon")
171 /** Automatic symbolic evaluation of an indexed delta tensor. */
172 ex tensdelta::eval_indexed(const basic & i) const
174 GINAC_ASSERT(is_a<indexed>(i));
175 GINAC_ASSERT(i.nops() == 3);
176 GINAC_ASSERT(is_a<tensdelta>(i.op(0)));
178 const idx & i1 = ex_to<idx>(i.op(1));
179 const idx & i2 = ex_to<idx>(i.op(2));
181 // Trace of delta tensor is the dimension of the space
182 if (is_dummy_pair(i1, i2))
185 // Numeric evaluation
186 if (static_cast<const indexed &>(i).all_index_values_are(info_flags::integer)) {
187 int n1 = ex_to<numeric>(i1.get_value()).to_int(), n2 = ex_to<numeric>(i2.get_value()).to_int();
194 // No further simplifications
198 /** Automatic symbolic evaluation of an indexed metric tensor. */
199 ex tensmetric::eval_indexed(const basic & i) const
201 GINAC_ASSERT(is_a<indexed>(i));
202 GINAC_ASSERT(i.nops() == 3);
203 GINAC_ASSERT(is_a<tensmetric>(i.op(0)));
204 GINAC_ASSERT(is_a<varidx>(i.op(1)));
205 GINAC_ASSERT(is_a<varidx>(i.op(2)));
207 const varidx & i1 = ex_to<varidx>(i.op(1));
208 const varidx & i2 = ex_to<varidx>(i.op(2));
210 // A metric tensor with one covariant and one contravariant index gets
211 // replaced by a delta tensor
212 if (i1.is_covariant() != i2.is_covariant())
213 return delta_tensor(i1, i2);
215 // No further simplifications
219 /** Automatic symbolic evaluation of an indexed Lorentz metric tensor. */
220 ex minkmetric::eval_indexed(const basic & i) const
222 GINAC_ASSERT(is_a<indexed>(i));
223 GINAC_ASSERT(i.nops() == 3);
224 GINAC_ASSERT(is_a<minkmetric>(i.op(0)));
225 GINAC_ASSERT(is_a<varidx>(i.op(1)));
226 GINAC_ASSERT(is_a<varidx>(i.op(2)));
228 const varidx & i1 = ex_to<varidx>(i.op(1));
229 const varidx & i2 = ex_to<varidx>(i.op(2));
231 // Numeric evaluation
232 if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
233 int n1 = ex_to<numeric>(i1.get_value()).to_int(), n2 = ex_to<numeric>(i2.get_value()).to_int();
237 return pos_sig ? _ex_1 : _ex1;
239 return pos_sig ? _ex1 : _ex_1;
242 // Perform the usual evaluations of a metric tensor
243 return inherited::eval_indexed(i);
246 /** Automatic symbolic evaluation of an indexed metric tensor. */
247 ex spinmetric::eval_indexed(const basic & i) const
249 GINAC_ASSERT(is_a<indexed>(i));
250 GINAC_ASSERT(i.nops() == 3);
251 GINAC_ASSERT(is_a<spinmetric>(i.op(0)));
252 GINAC_ASSERT(is_a<spinidx>(i.op(1)));
253 GINAC_ASSERT(is_a<spinidx>(i.op(2)));
255 const spinidx & i1 = ex_to<spinidx>(i.op(1));
256 const spinidx & i2 = ex_to<spinidx>(i.op(2));
258 // Convolutions are zero
259 if (!(static_cast<const indexed &>(i).get_dummy_indices().empty()))
262 // Numeric evaluation
263 if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
264 int n1 = ex_to<numeric>(i1.get_value()).to_int(), n2 = ex_to<numeric>(i2.get_value()).to_int();
273 // No further simplifications
277 /** Automatic symbolic evaluation of an indexed epsilon tensor. */
278 ex tensepsilon::eval_indexed(const basic & i) const
280 GINAC_ASSERT(is_a<indexed>(i));
281 GINAC_ASSERT(i.nops() > 1);
282 GINAC_ASSERT(is_a<tensepsilon>(i.op(0)));
284 // Convolutions are zero
285 if (!(static_cast<const indexed &>(i).get_dummy_indices().empty()))
288 // Numeric evaluation
289 if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
291 // Get sign of index permutation (the indices should already be in
292 // a canonic order but we can't assume what exactly that order is)
294 v.reserve(i.nops() - 1);
295 for (unsigned j=1; j<i.nops(); j++)
296 v.push_back(ex_to<numeric>(ex_to<idx>(i.op(j)).get_value()).to_int());
297 int sign = permutation_sign(v.begin(), v.end());
299 // In a Minkowski space, check for covariant indices
301 for (unsigned j=1; j<i.nops(); j++) {
302 const ex & x = i.op(j);
303 if (!is_ex_of_type(x, varidx))
304 throw(std::runtime_error("indices of epsilon tensor in Minkowski space must be of type varidx"));
305 if (ex_to<varidx>(x).is_covariant())
306 if (ex_to<idx>(x).get_value().is_zero())
307 sign = (pos_sig ? -sign : sign);
309 sign = (pos_sig ? sign : -sign);
316 // No further simplifications
320 /** Contraction of an indexed delta tensor with something else. */
321 bool tensdelta::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
323 GINAC_ASSERT(is_a<indexed>(*self));
324 GINAC_ASSERT(is_a<indexed>(*other));
325 GINAC_ASSERT(self->nops() == 3);
326 GINAC_ASSERT(is_a<tensdelta>(self->op(0)));
328 // Try to contract first index
329 const idx *self_idx = &ex_to<idx>(self->op(1));
330 const idx *free_idx = &ex_to<idx>(self->op(2));
331 bool first_index_tried = false;
334 if (self_idx->is_symbolic()) {
335 for (unsigned i=1; i<other->nops(); i++) {
336 const idx &other_idx = ex_to<idx>(other->op(i));
337 if (is_dummy_pair(*self_idx, other_idx)) {
339 // Contraction found, remove delta tensor and substitute
340 // index in second object
342 *other = other->subs(other_idx == *free_idx);
348 if (!first_index_tried) {
350 // No contraction with first index found, try second index
351 self_idx = &ex_to<idx>(self->op(2));
352 free_idx = &ex_to<idx>(self->op(1));
353 first_index_tried = true;
360 /** Contraction of an indexed metric tensor with something else. */
361 bool tensmetric::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
363 GINAC_ASSERT(is_a<indexed>(*self));
364 GINAC_ASSERT(is_a<indexed>(*other));
365 GINAC_ASSERT(self->nops() == 3);
366 GINAC_ASSERT(is_a<tensmetric>(self->op(0)));
368 // If contracting with the delta tensor, let the delta do it
369 // (don't raise/lower delta indices)
370 if (is_ex_of_type(other->op(0), tensdelta))
373 // Try to contract first index
374 const idx *self_idx = &ex_to<idx>(self->op(1));
375 const idx *free_idx = &ex_to<idx>(self->op(2));
376 bool first_index_tried = false;
379 if (self_idx->is_symbolic()) {
380 for (unsigned i=1; i<other->nops(); i++) {
381 const idx &other_idx = ex_to<idx>(other->op(i));
382 if (is_dummy_pair(*self_idx, other_idx)) {
384 // Contraction found, remove metric tensor and substitute
385 // index in second object
387 *other = other->subs(other_idx == *free_idx);
393 if (!first_index_tried) {
395 // No contraction with first index found, try second index
396 self_idx = &ex_to<idx>(self->op(2));
397 free_idx = &ex_to<idx>(self->op(1));
398 first_index_tried = true;
405 /** Contraction of an indexed spinor metric with something else. */
406 bool spinmetric::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
408 GINAC_ASSERT(is_a<indexed>(*self));
409 GINAC_ASSERT(is_a<indexed>(*other));
410 GINAC_ASSERT(self->nops() == 3);
411 GINAC_ASSERT(is_a<spinmetric>(self->op(0)));
413 // Contractions between spinor metrics
414 if (is_ex_of_type(other->op(0), spinmetric)) {
415 const idx &self_i1 = ex_to<idx>(self->op(1));
416 const idx &self_i2 = ex_to<idx>(self->op(2));
417 const idx &other_i1 = ex_to<idx>(other->op(1));
418 const idx &other_i2 = ex_to<idx>(other->op(2));
420 if (is_dummy_pair(self_i1, other_i1)) {
421 if (is_dummy_pair(self_i2, other_i2))
424 *self = delta_tensor(self_i2, other_i2);
427 } else if (is_dummy_pair(self_i1, other_i2)) {
428 if (is_dummy_pair(self_i2, other_i1))
431 *self = -delta_tensor(self_i2, other_i1);
434 } else if (is_dummy_pair(self_i2, other_i1)) {
435 *self = -delta_tensor(self_i1, other_i2);
438 } else if (is_dummy_pair(self_i2, other_i2)) {
439 *self = delta_tensor(self_i1, other_i1);
445 // If contracting with the delta tensor, let the delta do it
446 // (don't raise/lower delta indices)
447 if (is_ex_of_type(other->op(0), tensdelta))
450 // Try to contract first index
451 const idx *self_idx = &ex_to<idx>(self->op(1));
452 const idx *free_idx = &ex_to<idx>(self->op(2));
453 bool first_index_tried = false;
457 if (self_idx->is_symbolic()) {
458 for (unsigned i=1; i<other->nops(); i++) {
459 const idx &other_idx = ex_to<idx>(other->op(i));
460 if (is_dummy_pair(*self_idx, other_idx)) {
462 // Contraction found, remove metric tensor and substitute
463 // index in second object
464 *self = (static_cast<const spinidx *>(self_idx)->is_covariant() ? sign : -sign);
465 *other = other->subs(other_idx == *free_idx);
471 if (!first_index_tried) {
473 // No contraction with first index found, try second index
474 self_idx = &ex_to<idx>(self->op(2));
475 free_idx = &ex_to<idx>(self->op(1));
476 first_index_tried = true;
484 /** Contraction of epsilon tensor with something else. */
485 bool tensepsilon::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
487 GINAC_ASSERT(is_a<indexed>(*self));
488 GINAC_ASSERT(is_a<indexed>(*other));
489 GINAC_ASSERT(is_a<tensepsilon>(self->op(0)));
490 unsigned num = self->nops() - 1;
492 if (is_ex_exactly_of_type(other->op(0), tensepsilon) && num+1 == other->nops()) {
494 // Contraction of two epsilon tensors is a determinant
495 ex dim = ex_to<idx>(self->op(1)).get_dim();
497 for (int i=0; i<num; i++) {
498 for (int j=0; j<num; j++) {
500 M(i, j) = lorentz_g(self->op(i+1), other->op(j+1), pos_sig);
502 M(i, j) = metric_tensor(self->op(i+1), other->op(j+1));
505 int sign = minkowski ? -1 : 1;
506 *self = sign * M.determinant().simplify_indexed();
510 } else if (other->return_type() == return_types::commutative) {
513 // This handles eps.i.j.k * p.j * p.k = 0
514 // Maybe something like this should go to simplify_indexed() because
515 // such relations are true for any antisymmetric tensors...
518 // Handle all indices of the epsilon tensor
519 for (int i=0; i<num; i++) {
520 ex idx = self->op(i+1);
522 // Look whether there's a contraction with this index
523 exvector::const_iterator ait, aitend = v.end();
524 for (ait = v.begin(); ait != aitend; ait++) {
527 if (is_a<indexed>(*ait) && ait->return_type() == return_types::commutative && ex_to<indexed>(*ait).has_dummy_index_for(idx) && ait->nops() == 2) {
529 // Yes, did we already have another contraction with the same base expression?
530 ex base = ait->op(0);
531 if (std::find_if(c.begin(), c.end(), bind2nd(ex_is_equal(), base)) == c.end()) {
533 // No, add the base expression to the list
538 // Yes, the contraction is zero
556 ex delta_tensor(const ex & i1, const ex & i2)
558 if (!is_ex_of_type(i1, idx) || !is_ex_of_type(i2, idx))
559 throw(std::invalid_argument("indices of delta tensor must be of type idx"));
561 return indexed(tensdelta(), sy_symm(), i1, i2);
564 ex metric_tensor(const ex & i1, const ex & i2)
566 if (!is_ex_of_type(i1, varidx) || !is_ex_of_type(i2, varidx))
567 throw(std::invalid_argument("indices of metric tensor must be of type varidx"));
569 return indexed(tensmetric(), sy_symm(), i1, i2);
572 ex lorentz_g(const ex & i1, const ex & i2, bool pos_sig)
574 if (!is_ex_of_type(i1, varidx) || !is_ex_of_type(i2, varidx))
575 throw(std::invalid_argument("indices of metric tensor must be of type varidx"));
577 return indexed(minkmetric(pos_sig), sy_symm(), i1, i2);
580 ex spinor_metric(const ex & i1, const ex & i2)
582 if (!is_ex_of_type(i1, spinidx) || !is_ex_of_type(i2, spinidx))
583 throw(std::invalid_argument("indices of spinor metric must be of type spinidx"));
584 if (!ex_to<idx>(i1).get_dim().is_equal(2) || !ex_to<idx>(i2).get_dim().is_equal(2))
585 throw(std::runtime_error("index dimension for spinor metric must be 2"));
587 return indexed(spinmetric(), sy_anti(), i1, i2);
590 ex epsilon_tensor(const ex & i1, const ex & i2)
592 if (!is_ex_of_type(i1, idx) || !is_ex_of_type(i2, idx))
593 throw(std::invalid_argument("indices of epsilon tensor must be of type idx"));
595 ex dim = ex_to<idx>(i1).get_dim();
596 if (!dim.is_equal(ex_to<idx>(i2).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(_ex2))
599 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
601 return indexed(tensepsilon(), sy_anti(), i1, i2);
604 ex epsilon_tensor(const ex & i1, const ex & i2, const ex & i3)
606 if (!is_ex_of_type(i1, idx) || !is_ex_of_type(i2, idx) || !is_ex_of_type(i3, idx))
607 throw(std::invalid_argument("indices of epsilon tensor must be of type idx"));
609 ex dim = ex_to<idx>(i1).get_dim();
610 if (!dim.is_equal(ex_to<idx>(i2).get_dim()) || !dim.is_equal(ex_to<idx>(i3).get_dim()))
611 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
612 if (!ex_to<idx>(i1).get_dim().is_equal(_ex3))
613 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
615 return indexed(tensepsilon(), sy_anti(), i1, i2, i3);
618 ex lorentz_eps(const ex & i1, const ex & i2, const ex & i3, const ex & i4, bool pos_sig)
620 if (!is_ex_of_type(i1, varidx) || !is_ex_of_type(i2, varidx) || !is_ex_of_type(i3, varidx) || !is_ex_of_type(i4, varidx))
621 throw(std::invalid_argument("indices of Lorentz epsilon tensor must be of type varidx"));
623 ex dim = ex_to<idx>(i1).get_dim();
624 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()))
625 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
626 if (!ex_to<idx>(i1).get_dim().is_equal(_ex4))
627 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
629 return indexed(tensepsilon(true, pos_sig), sy_anti(), i1, i2, i3, i4);
632 ex eps0123(const ex & i1, const ex & i2, const ex & i3, const ex & i4, bool pos_sig)
634 if (!is_ex_of_type(i1, varidx) || !is_ex_of_type(i2, varidx) || !is_ex_of_type(i3, varidx) || !is_ex_of_type(i4, varidx))
635 throw(std::invalid_argument("indices of epsilon tensor must be of type varidx"));
637 ex dim = ex_to<idx>(i1).get_dim();
639 return lorentz_eps(i1, i2, i3, i4, pos_sig);
641 return indexed(tensepsilon(true, pos_sig), sy_anti(), i1, i2, i3, i4);