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
29 #include "relational.h"
39 GINAC_IMPLEMENT_REGISTERED_CLASS(tensor, basic)
40 GINAC_IMPLEMENT_REGISTERED_CLASS(tensdelta, tensor)
41 GINAC_IMPLEMENT_REGISTERED_CLASS(tensmetric, tensor)
42 GINAC_IMPLEMENT_REGISTERED_CLASS(minkmetric, tensmetric)
43 GINAC_IMPLEMENT_REGISTERED_CLASS(spinmetric, tensmetric)
44 GINAC_IMPLEMENT_REGISTERED_CLASS(tensepsilon, tensor)
47 // default constructor, destructor, copy constructor assignment operator and helpers
50 tensor::tensor(unsigned ti) : inherited(ti)
52 debugmsg("tensor constructor from unsigned", LOGLEVEL_CONSTRUCT); \
56 DEFAULT_CTORS(tensdelta)
57 DEFAULT_CTORS(tensmetric)
58 DEFAULT_COPY(spinmetric)
59 DEFAULT_DESTROY(spinmetric)
60 DEFAULT_DESTROY(minkmetric)
61 DEFAULT_DESTROY(tensepsilon)
63 minkmetric::minkmetric() : pos_sig(false)
65 debugmsg("minkmetric default constructor", LOGLEVEL_CONSTRUCT);
66 tinfo_key = TINFO_minkmetric;
69 spinmetric::spinmetric()
71 debugmsg("spinmetric default constructor", LOGLEVEL_CONSTRUCT);
72 tinfo_key = TINFO_spinmetric;
75 minkmetric::minkmetric(bool ps) : pos_sig(ps)
77 debugmsg("minkmetric constructor from bool", LOGLEVEL_CONSTRUCT);
78 tinfo_key = TINFO_minkmetric;
81 void minkmetric::copy(const minkmetric & other)
83 inherited::copy(other);
84 pos_sig = other.pos_sig;
87 tensepsilon::tensepsilon() : minkowski(false), pos_sig(false)
89 debugmsg("tensepsilon default constructor", LOGLEVEL_CONSTRUCT);
90 tinfo_key = TINFO_tensepsilon;
93 tensepsilon::tensepsilon(bool mink, bool ps) : minkowski(mink), pos_sig(ps)
95 debugmsg("tensepsilon constructor from bool,bool", LOGLEVEL_CONSTRUCT);
96 tinfo_key = TINFO_tensepsilon;
99 void tensepsilon::copy(const tensepsilon & other)
101 inherited::copy(other);
102 minkowski = other.minkowski;
103 pos_sig = other.pos_sig;
110 DEFAULT_ARCHIVING(tensor)
111 DEFAULT_ARCHIVING(tensdelta)
112 DEFAULT_ARCHIVING(tensmetric)
113 DEFAULT_ARCHIVING(spinmetric)
114 DEFAULT_UNARCHIVE(minkmetric)
115 DEFAULT_UNARCHIVE(tensepsilon)
117 minkmetric::minkmetric(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
119 debugmsg("minkmetric constructor from archive_node", LOGLEVEL_CONSTRUCT);
120 n.find_bool("pos_sig", pos_sig);
123 void minkmetric::archive(archive_node &n) const
125 inherited::archive(n);
126 n.add_bool("pos_sig", pos_sig);
129 tensepsilon::tensepsilon(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
131 debugmsg("tensepsilon constructor from archive_node", LOGLEVEL_CONSTRUCT);
132 n.find_bool("minkowski", minkowski);
133 n.find_bool("pos_sig", pos_sig);
136 void tensepsilon::archive(archive_node &n) const
138 inherited::archive(n);
139 n.add_bool("minkowski", minkowski);
140 n.add_bool("pos_sig", pos_sig);
144 // functions overriding virtual functions from bases classes
147 DEFAULT_COMPARE(tensor)
148 DEFAULT_COMPARE(tensdelta)
149 DEFAULT_COMPARE(tensmetric)
150 DEFAULT_COMPARE(spinmetric)
152 int minkmetric::compare_same_type(const basic & other) const
154 GINAC_ASSERT(is_of_type(other, minkmetric));
155 const minkmetric &o = static_cast<const minkmetric &>(other);
157 if (pos_sig != o.pos_sig)
158 return pos_sig ? -1 : 1;
160 return inherited::compare_same_type(other);
163 int tensepsilon::compare_same_type(const basic & other) const
165 GINAC_ASSERT(is_of_type(other, tensepsilon));
166 const tensepsilon &o = static_cast<const tensepsilon &>(other);
168 if (minkowski != o.minkowski)
169 return minkowski ? -1 : 1;
170 else if (pos_sig != o.pos_sig)
171 return pos_sig ? -1 : 1;
173 return inherited::compare_same_type(other);
176 DEFAULT_PRINT_LATEX(tensdelta, "delta", "\\delta")
177 DEFAULT_PRINT(tensmetric, "g")
178 DEFAULT_PRINT_LATEX(minkmetric, "eta", "\\eta")
179 DEFAULT_PRINT_LATEX(spinmetric, "eps", "\\varepsilon")
180 DEFAULT_PRINT_LATEX(tensepsilon, "eps", "\\varepsilon")
182 /** Automatic symbolic evaluation of an indexed delta tensor. */
183 ex tensdelta::eval_indexed(const basic & i) const
185 GINAC_ASSERT(is_of_type(i, indexed));
186 GINAC_ASSERT(i.nops() == 3);
187 GINAC_ASSERT(is_ex_of_type(i.op(0), tensdelta));
189 const idx & i1 = ex_to_idx(i.op(1));
190 const idx & i2 = ex_to_idx(i.op(2));
192 // Trace of delta tensor is the dimension of the space
193 if (is_dummy_pair(i1, i2))
196 // Numeric evaluation
197 if (static_cast<const indexed &>(i).all_index_values_are(info_flags::integer)) {
198 int n1 = ex_to_numeric(i1.get_value()).to_int(), n2 = ex_to_numeric(i2.get_value()).to_int();
205 // No further simplifications
209 /** Automatic symbolic evaluation of an indexed metric tensor. */
210 ex tensmetric::eval_indexed(const basic & i) const
212 GINAC_ASSERT(is_of_type(i, indexed));
213 GINAC_ASSERT(i.nops() == 3);
214 GINAC_ASSERT(is_ex_of_type(i.op(0), tensmetric));
215 GINAC_ASSERT(is_ex_of_type(i.op(1), varidx));
216 GINAC_ASSERT(is_ex_of_type(i.op(2), varidx));
218 const varidx & i1 = ex_to_varidx(i.op(1));
219 const varidx & i2 = ex_to_varidx(i.op(2));
221 // A metric tensor with one covariant and one contravariant index gets
222 // replaced by a delta tensor
223 if (i1.is_covariant() != i2.is_covariant())
224 return delta_tensor(i1, i2);
226 // No further simplifications
230 /** Automatic symbolic evaluation of an indexed Lorentz metric tensor. */
231 ex minkmetric::eval_indexed(const basic & i) const
233 GINAC_ASSERT(is_of_type(i, indexed));
234 GINAC_ASSERT(i.nops() == 3);
235 GINAC_ASSERT(is_ex_of_type(i.op(0), minkmetric));
236 GINAC_ASSERT(is_ex_of_type(i.op(1), varidx));
237 GINAC_ASSERT(is_ex_of_type(i.op(2), varidx));
239 const varidx & i1 = ex_to_varidx(i.op(1));
240 const varidx & i2 = ex_to_varidx(i.op(2));
242 // Numeric evaluation
243 if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
244 int n1 = ex_to_numeric(i1.get_value()).to_int(), n2 = ex_to_numeric(i2.get_value()).to_int();
248 return pos_sig ? _ex_1() : _ex1();
250 return pos_sig ? _ex1() : _ex_1();
253 // Perform the usual evaluations of a metric tensor
254 return inherited::eval_indexed(i);
257 /** Automatic symbolic evaluation of an indexed metric tensor. */
258 ex spinmetric::eval_indexed(const basic & i) const
260 GINAC_ASSERT(is_of_type(i, indexed));
261 GINAC_ASSERT(i.nops() == 3);
262 GINAC_ASSERT(is_ex_of_type(i.op(0), spinmetric));
263 GINAC_ASSERT(is_ex_of_type(i.op(1), spinidx));
264 GINAC_ASSERT(is_ex_of_type(i.op(2), spinidx));
266 const spinidx & i1 = ex_to_spinidx(i.op(1));
267 const spinidx & i2 = ex_to_spinidx(i.op(2));
269 // Convolutions are zero
270 if (static_cast<const indexed &>(i).get_dummy_indices().size() != 0)
273 // Numeric evaluation
274 if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
275 int n1 = ex_to_numeric(i1.get_value()).to_int(), n2 = ex_to_numeric(i2.get_value()).to_int();
284 // No further simplifications
288 /** Automatic symbolic evaluation of an indexed epsilon tensor. */
289 ex tensepsilon::eval_indexed(const basic & i) const
291 GINAC_ASSERT(is_of_type(i, indexed));
292 GINAC_ASSERT(i.nops() > 1);
293 GINAC_ASSERT(is_ex_of_type(i.op(0), tensepsilon));
295 // Convolutions are zero
296 if (static_cast<const indexed &>(i).get_dummy_indices().size() != 0)
299 // Numeric evaluation
300 if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
302 // Get sign of index permutation (the indices should already be in
303 // a canonic order but we can't assume what exactly that order is)
305 v.reserve(i.nops() - 1);
306 for (unsigned j=1; j<i.nops(); j++)
307 v.push_back(ex_to_numeric(ex_to_idx(i.op(j)).get_value()).to_int());
308 int sign = permutation_sign(v);
310 // In a Minkowski space, check for covariant indices
312 for (unsigned j=1; j<i.nops(); j++) {
313 const ex & x = i.op(j);
314 if (!is_ex_of_type(x, varidx))
315 throw(std::runtime_error("indices of epsilon tensor in Minkowski space must be of type varidx"));
316 if (ex_to_varidx(x).is_covariant())
317 if (ex_to_idx(x).get_value().is_zero())
318 sign = (pos_sig ? -sign : sign);
320 sign = (pos_sig ? sign : -sign);
327 // No further simplifications
331 /** Contraction of an indexed delta tensor with something else. */
332 bool tensdelta::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
334 GINAC_ASSERT(is_ex_of_type(*self, indexed));
335 GINAC_ASSERT(is_ex_of_type(*other, indexed));
336 GINAC_ASSERT(self->nops() == 3);
337 GINAC_ASSERT(is_ex_of_type(self->op(0), tensdelta));
339 // Try to contract first index
340 const idx *self_idx = &ex_to_idx(self->op(1));
341 const idx *free_idx = &ex_to_idx(self->op(2));
342 bool first_index_tried = false;
345 if (self_idx->is_symbolic()) {
346 for (int i=1; i<other->nops(); i++) {
347 const idx &other_idx = ex_to_idx(other->op(i));
348 if (is_dummy_pair(*self_idx, other_idx)) {
350 // Contraction found, remove delta tensor and substitute
351 // index in second object
353 *other = other->subs(other_idx == *free_idx);
359 if (!first_index_tried) {
361 // No contraction with first index found, try second index
362 self_idx = &ex_to_idx(self->op(2));
363 free_idx = &ex_to_idx(self->op(1));
364 first_index_tried = true;
371 /** Contraction of an indexed metric tensor with something else. */
372 bool tensmetric::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
374 GINAC_ASSERT(is_ex_of_type(*self, indexed));
375 GINAC_ASSERT(is_ex_of_type(*other, indexed));
376 GINAC_ASSERT(self->nops() == 3);
377 GINAC_ASSERT(is_ex_of_type(self->op(0), tensmetric));
379 // If contracting with the delta tensor, let the delta do it
380 // (don't raise/lower delta indices)
381 if (is_ex_of_type(other->op(0), tensdelta))
384 // Try to contract first index
385 const idx *self_idx = &ex_to_idx(self->op(1));
386 const idx *free_idx = &ex_to_idx(self->op(2));
387 bool first_index_tried = false;
390 if (self_idx->is_symbolic()) {
391 for (int i=1; i<other->nops(); i++) {
392 const idx &other_idx = ex_to_idx(other->op(i));
393 if (is_dummy_pair(*self_idx, other_idx)) {
395 // Contraction found, remove metric tensor and substitute
396 // index in second object
398 *other = other->subs(other_idx == *free_idx);
404 if (!first_index_tried) {
406 // No contraction with first index found, try second index
407 self_idx = &ex_to_idx(self->op(2));
408 free_idx = &ex_to_idx(self->op(1));
409 first_index_tried = true;
416 /** Contraction of an indexed spinor metric with something else. */
417 bool spinmetric::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
419 GINAC_ASSERT(is_ex_of_type(*self, indexed));
420 GINAC_ASSERT(is_ex_of_type(*other, indexed));
421 GINAC_ASSERT(self->nops() == 3);
422 GINAC_ASSERT(is_ex_of_type(self->op(0), spinmetric));
424 // Contractions between spinor metrics
425 if (is_ex_of_type(other->op(0), spinmetric)) {
426 const idx &self_i1 = ex_to_idx(self->op(1));
427 const idx &self_i2 = ex_to_idx(self->op(2));
428 const idx &other_i1 = ex_to_idx(other->op(1));
429 const idx &other_i2 = ex_to_idx(other->op(2));
431 if (is_dummy_pair(self_i1, other_i1)) {
432 if (is_dummy_pair(self_i2, other_i2))
435 *self = delta_tensor(self_i2, other_i2);
438 } else if (is_dummy_pair(self_i1, other_i2)) {
439 if (is_dummy_pair(self_i2, other_i1))
442 *self = -delta_tensor(self_i2, other_i1);
445 } else if (is_dummy_pair(self_i2, other_i1)) {
446 *self = -delta_tensor(self_i1, other_i2);
449 } else if (is_dummy_pair(self_i2, other_i2)) {
450 *self = delta_tensor(self_i1, other_i1);
456 // If contracting with the delta tensor, let the delta do it
457 // (don't raise/lower delta indices)
458 if (is_ex_of_type(other->op(0), tensdelta))
461 // Try to contract first index
462 const idx *self_idx = &ex_to_idx(self->op(1));
463 const idx *free_idx = &ex_to_idx(self->op(2));
464 bool first_index_tried = false;
468 if (self_idx->is_symbolic()) {
469 for (int i=1; i<other->nops(); i++) {
470 const idx &other_idx = ex_to_idx(other->op(i));
471 if (is_dummy_pair(*self_idx, other_idx)) {
473 // Contraction found, remove metric tensor and substitute
474 // index in second object
475 *self = (static_cast<const spinidx *>(self_idx)->is_covariant() ? sign : -sign);
476 *other = other->subs(other_idx == *free_idx);
482 if (!first_index_tried) {
484 // No contraction with first index found, try second index
485 self_idx = &ex_to_idx(self->op(2));
486 free_idx = &ex_to_idx(self->op(1));
487 first_index_tried = true;
499 ex delta_tensor(const ex & i1, const ex & i2)
501 if (!is_ex_of_type(i1, idx) || !is_ex_of_type(i2, idx))
502 throw(std::invalid_argument("indices of delta tensor must be of type idx"));
504 return indexed(tensdelta(), indexed::symmetric, i1, i2);
507 ex metric_tensor(const ex & i1, const ex & i2)
509 if (!is_ex_of_type(i1, varidx) || !is_ex_of_type(i2, varidx))
510 throw(std::invalid_argument("indices of metric tensor must be of type varidx"));
512 return indexed(tensmetric(), indexed::symmetric, i1, i2);
515 ex lorentz_g(const ex & i1, const ex & i2, bool pos_sig)
517 if (!is_ex_of_type(i1, varidx) || !is_ex_of_type(i2, varidx))
518 throw(std::invalid_argument("indices of metric tensor must be of type varidx"));
520 return indexed(minkmetric(pos_sig), indexed::symmetric, i1, i2);
523 ex spinor_metric(const ex & i1, const ex & i2)
525 if (!is_ex_of_type(i1, spinidx) || !is_ex_of_type(i2, spinidx))
526 throw(std::invalid_argument("indices of spinor metric must be of type spinidx"));
527 if (!ex_to_idx(i1).get_dim().is_equal(2) || !ex_to_idx(i2).get_dim().is_equal(2))
528 throw(std::runtime_error("index dimension for spinor metric must be 2"));
530 return indexed(spinmetric(), indexed::antisymmetric, i1, i2);
533 ex epsilon_tensor(const ex & i1, const ex & i2)
535 if (!is_ex_of_type(i1, idx) || !is_ex_of_type(i2, idx))
536 throw(std::invalid_argument("indices of epsilon tensor must be of type idx"));
538 ex dim = ex_to_idx(i1).get_dim();
539 if (!dim.is_equal(ex_to_idx(i2).get_dim()))
540 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
541 if (!ex_to_idx(i1).get_dim().is_equal(_ex2()))
542 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
544 return indexed(tensepsilon(), indexed::antisymmetric, i1, i2);
547 ex epsilon_tensor(const ex & i1, const ex & i2, const ex & i3)
549 if (!is_ex_of_type(i1, idx) || !is_ex_of_type(i2, idx) || !is_ex_of_type(i3, idx))
550 throw(std::invalid_argument("indices of epsilon tensor must be of type idx"));
552 ex dim = ex_to_idx(i1).get_dim();
553 if (!dim.is_equal(ex_to_idx(i2).get_dim()) || !dim.is_equal(ex_to_idx(i3).get_dim()))
554 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
555 if (!ex_to_idx(i1).get_dim().is_equal(_ex3()))
556 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
558 return indexed(tensepsilon(), indexed::antisymmetric, i1, i2, i3);
561 ex lorentz_eps(const ex & i1, const ex & i2, const ex & i3, const ex & i4, bool pos_sig)
563 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))
564 throw(std::invalid_argument("indices of Lorentz epsilon tensor must be of type varidx"));
566 ex dim = ex_to_idx(i1).get_dim();
567 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()))
568 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
569 if (!ex_to_idx(i1).get_dim().is_equal(_ex4()))
570 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
572 return indexed(tensepsilon(true, pos_sig), indexed::antisymmetric, i1, i2, i3, i4);
575 ex eps0123(const ex & i1, const ex & i2, const ex & i3, const ex & i4, bool pos_sig)
577 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))
578 throw(std::invalid_argument("indices of epsilon tensor must be of type varidx"));
580 ex dim = ex_to_idx(i1).get_dim();
582 return lorentz_eps(i1, i2, i3, i4, pos_sig);
584 return indexed(tensepsilon(true, pos_sig), indexed::antisymmetric, i1, i2, i3, i4);