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 constructor, destructor, copy constructor assignment operator and helpers
51 tensor::tensor(unsigned ti) : inherited(ti)
53 debugmsg("tensor constructor from unsigned", LOGLEVEL_CONSTRUCT); \
57 DEFAULT_CTORS(tensdelta)
58 DEFAULT_CTORS(tensmetric)
59 DEFAULT_COPY(spinmetric)
60 DEFAULT_DESTROY(spinmetric)
61 DEFAULT_DESTROY(minkmetric)
62 DEFAULT_DESTROY(tensepsilon)
64 minkmetric::minkmetric() : pos_sig(false)
66 debugmsg("minkmetric default constructor", LOGLEVEL_CONSTRUCT);
67 tinfo_key = TINFO_minkmetric;
70 spinmetric::spinmetric()
72 debugmsg("spinmetric default constructor", LOGLEVEL_CONSTRUCT);
73 tinfo_key = TINFO_spinmetric;
76 minkmetric::minkmetric(bool ps) : pos_sig(ps)
78 debugmsg("minkmetric constructor from bool", LOGLEVEL_CONSTRUCT);
79 tinfo_key = TINFO_minkmetric;
82 void minkmetric::copy(const minkmetric & other)
84 inherited::copy(other);
85 pos_sig = other.pos_sig;
88 tensepsilon::tensepsilon() : minkowski(false), pos_sig(false)
90 debugmsg("tensepsilon default constructor", LOGLEVEL_CONSTRUCT);
91 tinfo_key = TINFO_tensepsilon;
94 tensepsilon::tensepsilon(bool mink, bool ps) : minkowski(mink), pos_sig(ps)
96 debugmsg("tensepsilon constructor from bool,bool", LOGLEVEL_CONSTRUCT);
97 tinfo_key = TINFO_tensepsilon;
100 void tensepsilon::copy(const tensepsilon & other)
102 inherited::copy(other);
103 minkowski = other.minkowski;
104 pos_sig = other.pos_sig;
111 DEFAULT_ARCHIVING(tensor)
112 DEFAULT_ARCHIVING(tensdelta)
113 DEFAULT_ARCHIVING(tensmetric)
114 DEFAULT_ARCHIVING(spinmetric)
115 DEFAULT_UNARCHIVE(minkmetric)
116 DEFAULT_UNARCHIVE(tensepsilon)
118 minkmetric::minkmetric(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
120 debugmsg("minkmetric constructor from archive_node", LOGLEVEL_CONSTRUCT);
121 n.find_bool("pos_sig", pos_sig);
124 void minkmetric::archive(archive_node &n) const
126 inherited::archive(n);
127 n.add_bool("pos_sig", pos_sig);
130 tensepsilon::tensepsilon(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
132 debugmsg("tensepsilon constructor from archive_node", LOGLEVEL_CONSTRUCT);
133 n.find_bool("minkowski", minkowski);
134 n.find_bool("pos_sig", pos_sig);
137 void tensepsilon::archive(archive_node &n) const
139 inherited::archive(n);
140 n.add_bool("minkowski", minkowski);
141 n.add_bool("pos_sig", pos_sig);
145 // functions overriding virtual functions from bases classes
148 DEFAULT_COMPARE(tensor)
149 DEFAULT_COMPARE(tensdelta)
150 DEFAULT_COMPARE(tensmetric)
151 DEFAULT_COMPARE(spinmetric)
153 int minkmetric::compare_same_type(const basic & other) const
155 GINAC_ASSERT(is_of_type(other, minkmetric));
156 const minkmetric &o = static_cast<const minkmetric &>(other);
158 if (pos_sig != o.pos_sig)
159 return pos_sig ? -1 : 1;
161 return inherited::compare_same_type(other);
164 int tensepsilon::compare_same_type(const basic & other) const
166 GINAC_ASSERT(is_of_type(other, tensepsilon));
167 const tensepsilon &o = static_cast<const tensepsilon &>(other);
169 if (minkowski != o.minkowski)
170 return minkowski ? -1 : 1;
171 else if (pos_sig != o.pos_sig)
172 return pos_sig ? -1 : 1;
174 return inherited::compare_same_type(other);
177 DEFAULT_PRINT_LATEX(tensdelta, "delta", "\\delta")
178 DEFAULT_PRINT(tensmetric, "g")
179 DEFAULT_PRINT_LATEX(minkmetric, "eta", "\\eta")
180 DEFAULT_PRINT_LATEX(spinmetric, "eps", "\\varepsilon")
181 DEFAULT_PRINT_LATEX(tensepsilon, "eps", "\\varepsilon")
183 /** Automatic symbolic evaluation of an indexed delta tensor. */
184 ex tensdelta::eval_indexed(const basic & i) const
186 GINAC_ASSERT(is_of_type(i, indexed));
187 GINAC_ASSERT(i.nops() == 3);
188 GINAC_ASSERT(is_ex_of_type(i.op(0), tensdelta));
190 const idx & i1 = ex_to_idx(i.op(1));
191 const idx & i2 = ex_to_idx(i.op(2));
193 // Trace of delta tensor is the dimension of the space
194 if (is_dummy_pair(i1, i2))
197 // Numeric evaluation
198 if (static_cast<const indexed &>(i).all_index_values_are(info_flags::integer)) {
199 int n1 = ex_to_numeric(i1.get_value()).to_int(), n2 = ex_to_numeric(i2.get_value()).to_int();
206 // No further simplifications
210 /** Automatic symbolic evaluation of an indexed metric tensor. */
211 ex tensmetric::eval_indexed(const basic & i) const
213 GINAC_ASSERT(is_of_type(i, indexed));
214 GINAC_ASSERT(i.nops() == 3);
215 GINAC_ASSERT(is_ex_of_type(i.op(0), tensmetric));
216 GINAC_ASSERT(is_ex_of_type(i.op(1), varidx));
217 GINAC_ASSERT(is_ex_of_type(i.op(2), varidx));
219 const varidx & i1 = ex_to_varidx(i.op(1));
220 const varidx & i2 = ex_to_varidx(i.op(2));
222 // A metric tensor with one covariant and one contravariant index gets
223 // replaced by a delta tensor
224 if (i1.is_covariant() != i2.is_covariant())
225 return delta_tensor(i1, i2);
227 // No further simplifications
231 /** Automatic symbolic evaluation of an indexed Lorentz metric tensor. */
232 ex minkmetric::eval_indexed(const basic & i) const
234 GINAC_ASSERT(is_of_type(i, indexed));
235 GINAC_ASSERT(i.nops() == 3);
236 GINAC_ASSERT(is_ex_of_type(i.op(0), minkmetric));
237 GINAC_ASSERT(is_ex_of_type(i.op(1), varidx));
238 GINAC_ASSERT(is_ex_of_type(i.op(2), varidx));
240 const varidx & i1 = ex_to_varidx(i.op(1));
241 const varidx & i2 = ex_to_varidx(i.op(2));
243 // Numeric evaluation
244 if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
245 int n1 = ex_to_numeric(i1.get_value()).to_int(), n2 = ex_to_numeric(i2.get_value()).to_int();
249 return pos_sig ? _ex_1() : _ex1();
251 return pos_sig ? _ex1() : _ex_1();
254 // Perform the usual evaluations of a metric tensor
255 return inherited::eval_indexed(i);
258 /** Automatic symbolic evaluation of an indexed metric tensor. */
259 ex spinmetric::eval_indexed(const basic & i) const
261 GINAC_ASSERT(is_of_type(i, indexed));
262 GINAC_ASSERT(i.nops() == 3);
263 GINAC_ASSERT(is_ex_of_type(i.op(0), spinmetric));
264 GINAC_ASSERT(is_ex_of_type(i.op(1), spinidx));
265 GINAC_ASSERT(is_ex_of_type(i.op(2), spinidx));
267 const spinidx & i1 = ex_to_spinidx(i.op(1));
268 const spinidx & i2 = ex_to_spinidx(i.op(2));
270 // Convolutions are zero
271 if (static_cast<const indexed &>(i).get_dummy_indices().size() != 0)
274 // Numeric evaluation
275 if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
276 int n1 = ex_to_numeric(i1.get_value()).to_int(), n2 = ex_to_numeric(i2.get_value()).to_int();
285 // No further simplifications
289 /** Automatic symbolic evaluation of an indexed epsilon tensor. */
290 ex tensepsilon::eval_indexed(const basic & i) const
292 GINAC_ASSERT(is_of_type(i, indexed));
293 GINAC_ASSERT(i.nops() > 1);
294 GINAC_ASSERT(is_ex_of_type(i.op(0), tensepsilon));
296 // Convolutions are zero
297 if (static_cast<const indexed &>(i).get_dummy_indices().size() != 0)
300 // Numeric evaluation
301 if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
303 // Get sign of index permutation (the indices should already be in
304 // a canonic order but we can't assume what exactly that order is)
306 v.reserve(i.nops() - 1);
307 for (unsigned j=1; j<i.nops(); j++)
308 v.push_back(ex_to_numeric(ex_to_idx(i.op(j)).get_value()).to_int());
309 int sign = permutation_sign(v.begin(), v.end());
311 // In a Minkowski space, check for covariant indices
313 for (unsigned j=1; j<i.nops(); j++) {
314 const ex & x = i.op(j);
315 if (!is_ex_of_type(x, varidx))
316 throw(std::runtime_error("indices of epsilon tensor in Minkowski space must be of type varidx"));
317 if (ex_to_varidx(x).is_covariant())
318 if (ex_to_idx(x).get_value().is_zero())
319 sign = (pos_sig ? -sign : sign);
321 sign = (pos_sig ? sign : -sign);
328 // No further simplifications
332 /** Contraction of an indexed delta tensor with something else. */
333 bool tensdelta::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
335 GINAC_ASSERT(is_ex_of_type(*self, indexed));
336 GINAC_ASSERT(is_ex_of_type(*other, indexed));
337 GINAC_ASSERT(self->nops() == 3);
338 GINAC_ASSERT(is_ex_of_type(self->op(0), tensdelta));
340 // Try to contract first index
341 const idx *self_idx = &ex_to_idx(self->op(1));
342 const idx *free_idx = &ex_to_idx(self->op(2));
343 bool first_index_tried = false;
346 if (self_idx->is_symbolic()) {
347 for (int i=1; i<other->nops(); i++) {
348 const idx &other_idx = ex_to_idx(other->op(i));
349 if (is_dummy_pair(*self_idx, other_idx)) {
351 // Contraction found, remove delta tensor and substitute
352 // index in second object
354 *other = other->subs(other_idx == *free_idx);
360 if (!first_index_tried) {
362 // No contraction with first index found, try second index
363 self_idx = &ex_to_idx(self->op(2));
364 free_idx = &ex_to_idx(self->op(1));
365 first_index_tried = true;
372 /** Contraction of an indexed metric tensor with something else. */
373 bool tensmetric::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
375 GINAC_ASSERT(is_ex_of_type(*self, indexed));
376 GINAC_ASSERT(is_ex_of_type(*other, indexed));
377 GINAC_ASSERT(self->nops() == 3);
378 GINAC_ASSERT(is_ex_of_type(self->op(0), tensmetric));
380 // If contracting with the delta tensor, let the delta do it
381 // (don't raise/lower delta indices)
382 if (is_ex_of_type(other->op(0), tensdelta))
385 // Try to contract first index
386 const idx *self_idx = &ex_to_idx(self->op(1));
387 const idx *free_idx = &ex_to_idx(self->op(2));
388 bool first_index_tried = false;
391 if (self_idx->is_symbolic()) {
392 for (int i=1; i<other->nops(); i++) {
393 const idx &other_idx = ex_to_idx(other->op(i));
394 if (is_dummy_pair(*self_idx, other_idx)) {
396 // Contraction found, remove metric tensor and substitute
397 // index in second object
399 *other = other->subs(other_idx == *free_idx);
405 if (!first_index_tried) {
407 // No contraction with first index found, try second index
408 self_idx = &ex_to_idx(self->op(2));
409 free_idx = &ex_to_idx(self->op(1));
410 first_index_tried = true;
417 /** Contraction of an indexed spinor metric with something else. */
418 bool spinmetric::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
420 GINAC_ASSERT(is_ex_of_type(*self, indexed));
421 GINAC_ASSERT(is_ex_of_type(*other, indexed));
422 GINAC_ASSERT(self->nops() == 3);
423 GINAC_ASSERT(is_ex_of_type(self->op(0), spinmetric));
425 // Contractions between spinor metrics
426 if (is_ex_of_type(other->op(0), spinmetric)) {
427 const idx &self_i1 = ex_to_idx(self->op(1));
428 const idx &self_i2 = ex_to_idx(self->op(2));
429 const idx &other_i1 = ex_to_idx(other->op(1));
430 const idx &other_i2 = ex_to_idx(other->op(2));
432 if (is_dummy_pair(self_i1, other_i1)) {
433 if (is_dummy_pair(self_i2, other_i2))
436 *self = delta_tensor(self_i2, other_i2);
439 } else if (is_dummy_pair(self_i1, other_i2)) {
440 if (is_dummy_pair(self_i2, other_i1))
443 *self = -delta_tensor(self_i2, other_i1);
446 } else if (is_dummy_pair(self_i2, other_i1)) {
447 *self = -delta_tensor(self_i1, other_i2);
450 } else if (is_dummy_pair(self_i2, other_i2)) {
451 *self = delta_tensor(self_i1, other_i1);
457 // If contracting with the delta tensor, let the delta do it
458 // (don't raise/lower delta indices)
459 if (is_ex_of_type(other->op(0), tensdelta))
462 // Try to contract first index
463 const idx *self_idx = &ex_to_idx(self->op(1));
464 const idx *free_idx = &ex_to_idx(self->op(2));
465 bool first_index_tried = false;
469 if (self_idx->is_symbolic()) {
470 for (int i=1; i<other->nops(); i++) {
471 const idx &other_idx = ex_to_idx(other->op(i));
472 if (is_dummy_pair(*self_idx, other_idx)) {
474 // Contraction found, remove metric tensor and substitute
475 // index in second object
476 *self = (static_cast<const spinidx *>(self_idx)->is_covariant() ? sign : -sign);
477 *other = other->subs(other_idx == *free_idx);
483 if (!first_index_tried) {
485 // No contraction with first index found, try second index
486 self_idx = &ex_to_idx(self->op(2));
487 free_idx = &ex_to_idx(self->op(1));
488 first_index_tried = true;
500 ex delta_tensor(const ex & i1, const ex & i2)
502 if (!is_ex_of_type(i1, idx) || !is_ex_of_type(i2, idx))
503 throw(std::invalid_argument("indices of delta tensor must be of type idx"));
505 return indexed(tensdelta(), sy_symm(), i1, i2);
508 ex metric_tensor(const ex & i1, const ex & i2)
510 if (!is_ex_of_type(i1, varidx) || !is_ex_of_type(i2, varidx))
511 throw(std::invalid_argument("indices of metric tensor must be of type varidx"));
513 return indexed(tensmetric(), sy_symm(), i1, i2);
516 ex lorentz_g(const ex & i1, const ex & i2, bool pos_sig)
518 if (!is_ex_of_type(i1, varidx) || !is_ex_of_type(i2, varidx))
519 throw(std::invalid_argument("indices of metric tensor must be of type varidx"));
521 return indexed(minkmetric(pos_sig), sy_symm(), i1, i2);
524 ex spinor_metric(const ex & i1, const ex & i2)
526 if (!is_ex_of_type(i1, spinidx) || !is_ex_of_type(i2, spinidx))
527 throw(std::invalid_argument("indices of spinor metric must be of type spinidx"));
528 if (!ex_to_idx(i1).get_dim().is_equal(2) || !ex_to_idx(i2).get_dim().is_equal(2))
529 throw(std::runtime_error("index dimension for spinor metric must be 2"));
531 return indexed(spinmetric(), sy_anti(), i1, i2);
534 ex epsilon_tensor(const ex & i1, const ex & i2)
536 if (!is_ex_of_type(i1, idx) || !is_ex_of_type(i2, idx))
537 throw(std::invalid_argument("indices of epsilon tensor must be of type idx"));
539 ex dim = ex_to_idx(i1).get_dim();
540 if (!dim.is_equal(ex_to_idx(i2).get_dim()))
541 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
542 if (!ex_to_idx(i1).get_dim().is_equal(_ex2()))
543 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
545 return indexed(tensepsilon(), sy_anti(), i1, i2);
548 ex epsilon_tensor(const ex & i1, const ex & i2, const ex & i3)
550 if (!is_ex_of_type(i1, idx) || !is_ex_of_type(i2, idx) || !is_ex_of_type(i3, idx))
551 throw(std::invalid_argument("indices of epsilon tensor must be of type idx"));
553 ex dim = ex_to_idx(i1).get_dim();
554 if (!dim.is_equal(ex_to_idx(i2).get_dim()) || !dim.is_equal(ex_to_idx(i3).get_dim()))
555 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
556 if (!ex_to_idx(i1).get_dim().is_equal(_ex3()))
557 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
559 return indexed(tensepsilon(), sy_anti(), i1, i2, i3);
562 ex lorentz_eps(const ex & i1, const ex & i2, const ex & i3, const ex & i4, bool pos_sig)
564 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))
565 throw(std::invalid_argument("indices of Lorentz epsilon tensor must be of type varidx"));
567 ex dim = ex_to_idx(i1).get_dim();
568 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()))
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(_ex4()))
571 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
573 return indexed(tensepsilon(true, pos_sig), sy_anti(), i1, i2, i3, i4);
576 ex eps0123(const ex & i1, const ex & i2, const ex & i3, const ex & i4, bool pos_sig)
578 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))
579 throw(std::invalid_argument("indices of epsilon tensor must be of type varidx"));
581 ex dim = ex_to_idx(i1).get_dim();
583 return lorentz_eps(i1, i2, i3, i4, pos_sig);
585 return indexed(tensepsilon(true, pos_sig), sy_anti(), i1, i2, i3, i4);