3 * Implementation of GiNaC's special tensors. */
6 * GiNaC Copyright (C) 1999-2002 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"
41 GINAC_IMPLEMENT_REGISTERED_CLASS(tensor, basic)
42 GINAC_IMPLEMENT_REGISTERED_CLASS(tensdelta, tensor)
43 GINAC_IMPLEMENT_REGISTERED_CLASS(tensmetric, tensor)
44 GINAC_IMPLEMENT_REGISTERED_CLASS(minkmetric, tensmetric)
45 GINAC_IMPLEMENT_REGISTERED_CLASS(spinmetric, tensmetric)
46 GINAC_IMPLEMENT_REGISTERED_CLASS(tensepsilon, tensor)
49 // default ctor, dtor, copy ctor, assignment operator and helpers
53 DEFAULT_CTORS(tensdelta)
54 DEFAULT_CTORS(tensmetric)
55 DEFAULT_COPY(spinmetric)
56 DEFAULT_DESTROY(spinmetric)
57 DEFAULT_DESTROY(minkmetric)
58 DEFAULT_DESTROY(tensepsilon)
60 minkmetric::minkmetric() : pos_sig(false)
62 tinfo_key = TINFO_minkmetric;
65 spinmetric::spinmetric()
67 tinfo_key = TINFO_spinmetric;
70 minkmetric::minkmetric(bool ps) : pos_sig(ps)
72 tinfo_key = TINFO_minkmetric;
75 void minkmetric::copy(const minkmetric & other)
77 inherited::copy(other);
78 pos_sig = other.pos_sig;
81 tensepsilon::tensepsilon() : minkowski(false), pos_sig(false)
83 tinfo_key = TINFO_tensepsilon;
86 tensepsilon::tensepsilon(bool mink, bool ps) : minkowski(mink), pos_sig(ps)
88 tinfo_key = TINFO_tensepsilon;
91 void tensepsilon::copy(const tensepsilon & other)
93 inherited::copy(other);
94 minkowski = other.minkowski;
95 pos_sig = other.pos_sig;
102 DEFAULT_ARCHIVING(tensor)
103 DEFAULT_ARCHIVING(tensdelta)
104 DEFAULT_ARCHIVING(tensmetric)
105 DEFAULT_ARCHIVING(spinmetric)
106 DEFAULT_UNARCHIVE(minkmetric)
107 DEFAULT_UNARCHIVE(tensepsilon)
109 minkmetric::minkmetric(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
111 n.find_bool("pos_sig", pos_sig);
114 void minkmetric::archive(archive_node &n) const
116 inherited::archive(n);
117 n.add_bool("pos_sig", pos_sig);
120 tensepsilon::tensepsilon(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
122 n.find_bool("minkowski", minkowski);
123 n.find_bool("pos_sig", pos_sig);
126 void tensepsilon::archive(archive_node &n) const
128 inherited::archive(n);
129 n.add_bool("minkowski", minkowski);
130 n.add_bool("pos_sig", pos_sig);
134 // functions overriding virtual functions from base classes
137 DEFAULT_COMPARE(tensor)
138 DEFAULT_COMPARE(tensdelta)
139 DEFAULT_COMPARE(tensmetric)
140 DEFAULT_COMPARE(spinmetric)
142 int minkmetric::compare_same_type(const basic & other) const
144 GINAC_ASSERT(is_a<minkmetric>(other));
145 const minkmetric &o = static_cast<const minkmetric &>(other);
147 if (pos_sig != o.pos_sig)
148 return pos_sig ? -1 : 1;
150 return inherited::compare_same_type(other);
153 int tensepsilon::compare_same_type(const basic & other) const
155 GINAC_ASSERT(is_a<tensepsilon>(other));
156 const tensepsilon &o = static_cast<const tensepsilon &>(other);
158 if (minkowski != o.minkowski)
159 return minkowski ? -1 : 1;
160 else if (pos_sig != o.pos_sig)
161 return pos_sig ? -1 : 1;
163 return inherited::compare_same_type(other);
166 DEFAULT_PRINT_LATEX(tensdelta, "delta", "\\delta")
167 DEFAULT_PRINT(tensmetric, "g")
168 DEFAULT_PRINT_LATEX(minkmetric, "eta", "\\eta")
169 DEFAULT_PRINT_LATEX(spinmetric, "eps", "\\varepsilon")
170 DEFAULT_PRINT_LATEX(tensepsilon, "eps", "\\varepsilon")
172 /** Automatic symbolic evaluation of an indexed delta tensor. */
173 ex tensdelta::eval_indexed(const basic & i) const
175 GINAC_ASSERT(is_a<indexed>(i));
176 GINAC_ASSERT(i.nops() == 3);
177 GINAC_ASSERT(is_a<tensdelta>(i.op(0)));
179 const idx & i1 = ex_to<idx>(i.op(1));
180 const idx & i2 = ex_to<idx>(i.op(2));
182 // Trace of delta tensor is the (effective) dimension of the space
183 if (is_dummy_pair(i1, i2)) {
185 return i1.minimal_dim(i2);
186 } catch (std::exception &e) {
191 // Numeric evaluation
192 if (static_cast<const indexed &>(i).all_index_values_are(info_flags::integer)) {
193 int n1 = ex_to<numeric>(i1.get_value()).to_int(), n2 = ex_to<numeric>(i2.get_value()).to_int();
200 // No further simplifications
204 /** Automatic symbolic evaluation of an indexed metric tensor. */
205 ex tensmetric::eval_indexed(const basic & i) const
207 GINAC_ASSERT(is_a<indexed>(i));
208 GINAC_ASSERT(i.nops() == 3);
209 GINAC_ASSERT(is_a<tensmetric>(i.op(0)));
210 GINAC_ASSERT(is_a<varidx>(i.op(1)));
211 GINAC_ASSERT(is_a<varidx>(i.op(2)));
213 const varidx & i1 = ex_to<varidx>(i.op(1));
214 const varidx & i2 = ex_to<varidx>(i.op(2));
216 // The dimension of the indices must be equal, otherwise we use the minimal
218 if (!i1.get_dim().is_equal(i2.get_dim())) {
219 ex min_dim = i1.minimal_dim(i2);
220 return i.subs(lst(i1 == i1.replace_dim(min_dim), i2 == i2.replace_dim(min_dim)));
223 // A metric tensor with one covariant and one contravariant index gets
224 // replaced by a delta tensor
225 if (i1.is_covariant() != i2.is_covariant())
226 return delta_tensor(i1, i2);
228 // No further simplifications
232 /** Automatic symbolic evaluation of an indexed Lorentz metric tensor. */
233 ex minkmetric::eval_indexed(const basic & i) const
235 GINAC_ASSERT(is_a<indexed>(i));
236 GINAC_ASSERT(i.nops() == 3);
237 GINAC_ASSERT(is_a<minkmetric>(i.op(0)));
238 GINAC_ASSERT(is_a<varidx>(i.op(1)));
239 GINAC_ASSERT(is_a<varidx>(i.op(2)));
241 const varidx & i1 = ex_to<varidx>(i.op(1));
242 const varidx & i2 = ex_to<varidx>(i.op(2));
244 // Numeric evaluation
245 if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
246 int n1 = ex_to<numeric>(i1.get_value()).to_int(), n2 = ex_to<numeric>(i2.get_value()).to_int();
250 return pos_sig ? _ex_1 : _ex1;
252 return pos_sig ? _ex1 : _ex_1;
255 // Perform the usual evaluations of a metric tensor
256 return inherited::eval_indexed(i);
259 /** Automatic symbolic evaluation of an indexed metric tensor. */
260 ex spinmetric::eval_indexed(const basic & i) const
262 GINAC_ASSERT(is_a<indexed>(i));
263 GINAC_ASSERT(i.nops() == 3);
264 GINAC_ASSERT(is_a<spinmetric>(i.op(0)));
265 GINAC_ASSERT(is_a<spinidx>(i.op(1)));
266 GINAC_ASSERT(is_a<spinidx>(i.op(2)));
268 const spinidx & i1 = ex_to<spinidx>(i.op(1));
269 const spinidx & i2 = ex_to<spinidx>(i.op(2));
271 // Convolutions are zero
272 if (!(static_cast<const indexed &>(i).get_dummy_indices().empty()))
275 // Numeric evaluation
276 if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
277 int n1 = ex_to<numeric>(i1.get_value()).to_int(), n2 = ex_to<numeric>(i2.get_value()).to_int();
286 // No further simplifications
290 /** Automatic symbolic evaluation of an indexed epsilon tensor. */
291 ex tensepsilon::eval_indexed(const basic & i) const
293 GINAC_ASSERT(is_a<indexed>(i));
294 GINAC_ASSERT(i.nops() > 1);
295 GINAC_ASSERT(is_a<tensepsilon>(i.op(0)));
297 // Convolutions are zero
298 if (!(static_cast<const indexed &>(i).get_dummy_indices().empty()))
301 // Numeric evaluation
302 if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
304 // Get sign of index permutation (the indices should already be in
305 // a canonic order but we can't assume what exactly that order is)
307 v.reserve(i.nops() - 1);
308 for (unsigned j=1; j<i.nops(); j++)
309 v.push_back(ex_to<numeric>(ex_to<idx>(i.op(j)).get_value()).to_int());
310 int sign = permutation_sign(v.begin(), v.end());
312 // In a Minkowski space, check for covariant indices
314 for (unsigned j=1; j<i.nops(); j++) {
315 const ex & x = i.op(j);
316 if (!is_ex_of_type(x, varidx))
317 throw(std::runtime_error("indices of epsilon tensor in Minkowski space must be of type varidx"));
318 if (ex_to<varidx>(x).is_covariant())
319 if (ex_to<idx>(x).get_value().is_zero())
320 sign = (pos_sig ? -sign : sign);
322 sign = (pos_sig ? sign : -sign);
329 // No further simplifications
333 bool tensor::replace_contr_index(exvector::iterator self, exvector::iterator other) const
335 // Try to contract the first index
336 const idx *self_idx = &ex_to<idx>(self->op(1));
337 const idx *free_idx = &ex_to<idx>(self->op(2));
338 bool first_index_tried = false;
341 if (self_idx->is_symbolic()) {
342 for (unsigned i=1; i<other->nops(); i++) {
343 const idx &other_idx = ex_to<idx>(other->op(i));
344 if (is_dummy_pair(*self_idx, other_idx)) {
346 // Contraction found, remove this tensor and substitute the
347 // index in the second object
349 // minimal_dim() throws an exception when index dimensions are not comparable
350 ex min_dim = self_idx->minimal_dim(other_idx);
351 *other = other->subs(other_idx == free_idx->replace_dim(min_dim));
352 *self = _ex1; // *other is assigned first because assigning *self invalidates free_idx
354 } catch (std::exception &e) {
361 if (!first_index_tried) {
363 // No contraction with the first index found, try the second index
364 self_idx = &ex_to<idx>(self->op(2));
365 free_idx = &ex_to<idx>(self->op(1));
366 first_index_tried = true;
373 /** Contraction of an indexed delta tensor with something else. */
374 bool tensdelta::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
376 GINAC_ASSERT(is_a<indexed>(*self));
377 GINAC_ASSERT(is_a<indexed>(*other));
378 GINAC_ASSERT(self->nops() == 3);
379 GINAC_ASSERT(is_a<tensdelta>(self->op(0)));
381 // Replace the dummy index with this tensor's other index and remove
382 // the tensor (this is valid for contractions with all other tensors)
383 return replace_contr_index(self, other);
386 /** Contraction of an indexed metric tensor with something else. */
387 bool tensmetric::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
389 GINAC_ASSERT(is_a<indexed>(*self));
390 GINAC_ASSERT(is_a<indexed>(*other));
391 GINAC_ASSERT(self->nops() == 3);
392 GINAC_ASSERT(is_a<tensmetric>(self->op(0)));
394 // If contracting with the delta tensor, let the delta do it
395 // (don't raise/lower delta indices)
396 if (is_ex_of_type(other->op(0), tensdelta))
399 // Replace the dummy index with this tensor's other index and remove
400 // the tensor (this is valid for contractions with all other tensors)
401 return replace_contr_index(self, other);
404 /** Contraction of an indexed spinor metric with something else. */
405 bool spinmetric::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
407 GINAC_ASSERT(is_a<indexed>(*self));
408 GINAC_ASSERT(is_a<indexed>(*other));
409 GINAC_ASSERT(self->nops() == 3);
410 GINAC_ASSERT(is_a<spinmetric>(self->op(0)));
412 // Contractions between spinor metrics
413 if (is_ex_of_type(other->op(0), spinmetric)) {
414 const idx &self_i1 = ex_to<idx>(self->op(1));
415 const idx &self_i2 = ex_to<idx>(self->op(2));
416 const idx &other_i1 = ex_to<idx>(other->op(1));
417 const idx &other_i2 = ex_to<idx>(other->op(2));
419 if (is_dummy_pair(self_i1, other_i1)) {
420 if (is_dummy_pair(self_i2, other_i2))
423 *self = delta_tensor(self_i2, other_i2);
426 } else if (is_dummy_pair(self_i1, other_i2)) {
427 if (is_dummy_pair(self_i2, other_i1))
430 *self = -delta_tensor(self_i2, other_i1);
433 } else if (is_dummy_pair(self_i2, other_i1)) {
434 *self = -delta_tensor(self_i1, other_i2);
437 } else if (is_dummy_pair(self_i2, other_i2)) {
438 *self = delta_tensor(self_i1, other_i1);
444 // If contracting with the delta tensor, let the delta do it
445 // (don't raise/lower delta indices)
446 if (is_ex_of_type(other->op(0), tensdelta))
449 // Try to contract first index
450 const idx *self_idx = &ex_to<idx>(self->op(1));
451 const idx *free_idx = &ex_to<idx>(self->op(2));
452 bool first_index_tried = false;
456 if (self_idx->is_symbolic()) {
457 for (unsigned i=1; i<other->nops(); i++) {
458 const idx &other_idx = ex_to<idx>(other->op(i));
459 if (is_dummy_pair(*self_idx, other_idx)) {
461 // Contraction found, remove metric tensor and substitute
462 // index in second object (assign *self last because this
463 // invalidates free_idx)
464 *other = other->subs(other_idx == *free_idx);
465 *self = (static_cast<const spinidx *>(self_idx)->is_covariant() ? sign : -sign);
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();
518 ex delta_tensor(const ex & i1, const ex & i2)
520 if (!is_ex_of_type(i1, idx) || !is_ex_of_type(i2, idx))
521 throw(std::invalid_argument("indices of delta tensor must be of type idx"));
523 return indexed(tensdelta(), sy_symm(), i1, i2);
526 ex metric_tensor(const ex & i1, const ex & i2)
528 if (!is_ex_of_type(i1, varidx) || !is_ex_of_type(i2, varidx))
529 throw(std::invalid_argument("indices of metric tensor must be of type varidx"));
531 return indexed(tensmetric(), sy_symm(), i1, i2);
534 ex lorentz_g(const ex & i1, const ex & i2, bool pos_sig)
536 if (!is_ex_of_type(i1, varidx) || !is_ex_of_type(i2, varidx))
537 throw(std::invalid_argument("indices of metric tensor must be of type varidx"));
539 return indexed(minkmetric(pos_sig), sy_symm(), i1, i2);
542 ex spinor_metric(const ex & i1, const ex & i2)
544 if (!is_ex_of_type(i1, spinidx) || !is_ex_of_type(i2, spinidx))
545 throw(std::invalid_argument("indices of spinor metric must be of type spinidx"));
546 if (!ex_to<idx>(i1).get_dim().is_equal(2) || !ex_to<idx>(i2).get_dim().is_equal(2))
547 throw(std::runtime_error("index dimension for spinor metric must be 2"));
549 return indexed(spinmetric(), sy_anti(), i1, i2);
552 ex epsilon_tensor(const ex & i1, const ex & i2)
554 if (!is_ex_of_type(i1, idx) || !is_ex_of_type(i2, idx))
555 throw(std::invalid_argument("indices of epsilon tensor must be of type idx"));
557 ex dim = ex_to<idx>(i1).get_dim();
558 if (!dim.is_equal(ex_to<idx>(i2).get_dim()))
559 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
560 if (!ex_to<idx>(i1).get_dim().is_equal(_ex2))
561 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
563 return indexed(tensepsilon(), sy_anti(), i1, i2);
566 ex epsilon_tensor(const ex & i1, const ex & i2, const ex & i3)
568 if (!is_ex_of_type(i1, idx) || !is_ex_of_type(i2, idx) || !is_ex_of_type(i3, idx))
569 throw(std::invalid_argument("indices of epsilon tensor must be of type idx"));
571 ex dim = ex_to<idx>(i1).get_dim();
572 if (!dim.is_equal(ex_to<idx>(i2).get_dim()) || !dim.is_equal(ex_to<idx>(i3).get_dim()))
573 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
574 if (!ex_to<idx>(i1).get_dim().is_equal(_ex3))
575 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
577 return indexed(tensepsilon(), sy_anti(), i1, i2, i3);
580 ex lorentz_eps(const ex & i1, const ex & i2, const ex & i3, const ex & i4, bool pos_sig)
582 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))
583 throw(std::invalid_argument("indices of Lorentz epsilon tensor must be of type varidx"));
585 ex dim = ex_to<idx>(i1).get_dim();
586 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()))
587 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
588 if (!ex_to<idx>(i1).get_dim().is_equal(_ex4))
589 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
591 return indexed(tensepsilon(true, pos_sig), sy_anti(), i1, i2, i3, i4);