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)
53 tensor::tensor() : inherited(TINFO_tensor)
55 setflag(status_flags::evaluated | status_flags::expanded);
58 DEFAULT_CTOR(tensdelta)
59 DEFAULT_CTOR(tensmetric)
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 tensepsilon::tensepsilon() : minkowski(false), pos_sig(false)
78 tinfo_key = TINFO_tensepsilon;
81 tensepsilon::tensepsilon(bool mink, bool ps) : minkowski(mink), pos_sig(ps)
83 tinfo_key = TINFO_tensepsilon;
90 DEFAULT_ARCHIVING(tensor)
91 DEFAULT_ARCHIVING(tensdelta)
92 DEFAULT_ARCHIVING(tensmetric)
93 DEFAULT_ARCHIVING(spinmetric)
94 DEFAULT_UNARCHIVE(minkmetric)
95 DEFAULT_UNARCHIVE(tensepsilon)
97 minkmetric::minkmetric(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
99 n.find_bool("pos_sig", pos_sig);
102 void minkmetric::archive(archive_node &n) const
104 inherited::archive(n);
105 n.add_bool("pos_sig", pos_sig);
108 tensepsilon::tensepsilon(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
110 n.find_bool("minkowski", minkowski);
111 n.find_bool("pos_sig", pos_sig);
114 void tensepsilon::archive(archive_node &n) const
116 inherited::archive(n);
117 n.add_bool("minkowski", minkowski);
118 n.add_bool("pos_sig", pos_sig);
122 // functions overriding virtual functions from base classes
125 DEFAULT_COMPARE(tensor)
126 DEFAULT_COMPARE(tensdelta)
127 DEFAULT_COMPARE(tensmetric)
128 DEFAULT_COMPARE(spinmetric)
130 int minkmetric::compare_same_type(const basic & other) const
132 GINAC_ASSERT(is_a<minkmetric>(other));
133 const minkmetric &o = static_cast<const minkmetric &>(other);
135 if (pos_sig != o.pos_sig)
136 return pos_sig ? -1 : 1;
138 return inherited::compare_same_type(other);
141 int tensepsilon::compare_same_type(const basic & other) const
143 GINAC_ASSERT(is_a<tensepsilon>(other));
144 const tensepsilon &o = static_cast<const tensepsilon &>(other);
146 if (minkowski != o.minkowski)
147 return minkowski ? -1 : 1;
148 else if (pos_sig != o.pos_sig)
149 return pos_sig ? -1 : 1;
151 return inherited::compare_same_type(other);
154 DEFAULT_PRINT_LATEX(tensdelta, "delta", "\\delta")
155 DEFAULT_PRINT(tensmetric, "g")
156 DEFAULT_PRINT_LATEX(minkmetric, "eta", "\\eta")
157 DEFAULT_PRINT_LATEX(spinmetric, "eps", "\\varepsilon")
158 DEFAULT_PRINT_LATEX(tensepsilon, "eps", "\\varepsilon")
160 /** Automatic symbolic evaluation of an indexed delta tensor. */
161 ex tensdelta::eval_indexed(const basic & i) const
163 GINAC_ASSERT(is_a<indexed>(i));
164 GINAC_ASSERT(i.nops() == 3);
165 GINAC_ASSERT(is_a<tensdelta>(i.op(0)));
167 const idx & i1 = ex_to<idx>(i.op(1));
168 const idx & i2 = ex_to<idx>(i.op(2));
170 // The dimension of the indices must be equal, otherwise we use the minimal
172 if (!i1.get_dim().is_equal(i2.get_dim())) {
173 ex min_dim = i1.minimal_dim(i2);
174 return i.subs(lst(i1 == i1.replace_dim(min_dim), i2 == i2.replace_dim(min_dim)));
177 // Trace of delta tensor is the (effective) dimension of the space
178 if (is_dummy_pair(i1, i2)) {
180 return i1.minimal_dim(i2);
181 } catch (std::exception &e) {
186 // Numeric evaluation
187 if (static_cast<const indexed &>(i).all_index_values_are(info_flags::integer)) {
188 int n1 = ex_to<numeric>(i1.get_value()).to_int(), n2 = ex_to<numeric>(i2.get_value()).to_int();
195 // No further simplifications
199 /** Automatic symbolic evaluation of an indexed metric tensor. */
200 ex tensmetric::eval_indexed(const basic & i) const
202 GINAC_ASSERT(is_a<indexed>(i));
203 GINAC_ASSERT(i.nops() == 3);
204 GINAC_ASSERT(is_a<tensmetric>(i.op(0)));
205 GINAC_ASSERT(is_a<varidx>(i.op(1)));
206 GINAC_ASSERT(is_a<varidx>(i.op(2)));
208 const varidx & i1 = ex_to<varidx>(i.op(1));
209 const varidx & i2 = ex_to<varidx>(i.op(2));
211 // The dimension of the indices must be equal, otherwise we use the minimal
213 if (!i1.get_dim().is_equal(i2.get_dim())) {
214 ex min_dim = i1.minimal_dim(i2);
215 return i.subs(lst(i1 == i1.replace_dim(min_dim), i2 == i2.replace_dim(min_dim)));
218 // A metric tensor with one covariant and one contravariant index gets
219 // replaced by a delta tensor
220 if (i1.is_covariant() != i2.is_covariant())
221 return delta_tensor(i1, i2);
223 // No further simplifications
227 /** Automatic symbolic evaluation of an indexed Lorentz metric tensor. */
228 ex minkmetric::eval_indexed(const basic & i) const
230 GINAC_ASSERT(is_a<indexed>(i));
231 GINAC_ASSERT(i.nops() == 3);
232 GINAC_ASSERT(is_a<minkmetric>(i.op(0)));
233 GINAC_ASSERT(is_a<varidx>(i.op(1)));
234 GINAC_ASSERT(is_a<varidx>(i.op(2)));
236 const varidx & i1 = ex_to<varidx>(i.op(1));
237 const varidx & i2 = ex_to<varidx>(i.op(2));
239 // Numeric evaluation
240 if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
241 int n1 = ex_to<numeric>(i1.get_value()).to_int(), n2 = ex_to<numeric>(i2.get_value()).to_int();
245 return pos_sig ? _ex_1 : _ex1;
247 return pos_sig ? _ex1 : _ex_1;
250 // Perform the usual evaluations of a metric tensor
251 return inherited::eval_indexed(i);
254 /** Automatic symbolic evaluation of an indexed metric tensor. */
255 ex spinmetric::eval_indexed(const basic & i) const
257 GINAC_ASSERT(is_a<indexed>(i));
258 GINAC_ASSERT(i.nops() == 3);
259 GINAC_ASSERT(is_a<spinmetric>(i.op(0)));
260 GINAC_ASSERT(is_a<spinidx>(i.op(1)));
261 GINAC_ASSERT(is_a<spinidx>(i.op(2)));
263 const spinidx & i1 = ex_to<spinidx>(i.op(1));
264 const spinidx & i2 = ex_to<spinidx>(i.op(2));
266 // Convolutions are zero
267 if (!(static_cast<const indexed &>(i).get_dummy_indices().empty()))
270 // Numeric evaluation
271 if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
272 int n1 = ex_to<numeric>(i1.get_value()).to_int(), n2 = ex_to<numeric>(i2.get_value()).to_int();
281 // No further simplifications
285 /** Automatic symbolic evaluation of an indexed epsilon tensor. */
286 ex tensepsilon::eval_indexed(const basic & i) const
288 GINAC_ASSERT(is_a<indexed>(i));
289 GINAC_ASSERT(i.nops() > 1);
290 GINAC_ASSERT(is_a<tensepsilon>(i.op(0)));
292 // Convolutions are zero
293 if (!(static_cast<const indexed &>(i).get_dummy_indices().empty()))
296 // Numeric evaluation
297 if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
299 // Get sign of index permutation (the indices should already be in
300 // a canonic order but we can't assume what exactly that order is)
302 v.reserve(i.nops() - 1);
303 for (size_t j=1; j<i.nops(); j++)
304 v.push_back(ex_to<numeric>(ex_to<idx>(i.op(j)).get_value()).to_int());
305 int sign = permutation_sign(v.begin(), v.end());
307 // In a Minkowski space, check for covariant indices
309 for (size_t j=1; j<i.nops(); j++) {
310 const ex & x = i.op(j);
311 if (!is_a<varidx>(x))
312 throw(std::runtime_error("indices of epsilon tensor in Minkowski space must be of type varidx"));
313 if (ex_to<varidx>(x).is_covariant())
314 if (ex_to<idx>(x).get_value().is_zero())
315 sign = (pos_sig ? -sign : sign);
317 sign = (pos_sig ? sign : -sign);
324 // No further simplifications
328 bool tensor::replace_contr_index(exvector::iterator self, exvector::iterator other) const
330 // Try to contract the first index
331 const idx *self_idx = &ex_to<idx>(self->op(1));
332 const idx *free_idx = &ex_to<idx>(self->op(2));
333 bool first_index_tried = false;
336 if (self_idx->is_symbolic()) {
337 for (size_t i=1; i<other->nops(); i++) {
338 const idx &other_idx = ex_to<idx>(other->op(i));
339 if (is_dummy_pair(*self_idx, other_idx)) {
341 // Contraction found, remove this tensor and substitute the
342 // index in the second object
344 // minimal_dim() throws an exception when index dimensions are not comparable
345 ex min_dim = self_idx->minimal_dim(other_idx);
346 *other = other->subs(other_idx == free_idx->replace_dim(min_dim));
347 *self = _ex1; // *other is assigned first because assigning *self invalidates free_idx
349 } catch (std::exception &e) {
356 if (!first_index_tried) {
358 // No contraction with the first index found, try the second index
359 self_idx = &ex_to<idx>(self->op(2));
360 free_idx = &ex_to<idx>(self->op(1));
361 first_index_tried = true;
368 /** Contraction of an indexed delta tensor with something else. */
369 bool tensdelta::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
371 GINAC_ASSERT(is_a<indexed>(*self));
372 GINAC_ASSERT(is_a<indexed>(*other));
373 GINAC_ASSERT(self->nops() == 3);
374 GINAC_ASSERT(is_a<tensdelta>(self->op(0)));
376 // Replace the dummy index with this tensor's other index and remove
377 // the tensor (this is valid for contractions with all other tensors)
378 return replace_contr_index(self, other);
381 /** Contraction of an indexed metric tensor with something else. */
382 bool tensmetric::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<tensmetric>(self->op(0)));
389 // If contracting with the delta tensor, let the delta do it
390 // (don't raise/lower delta indices)
391 if (is_a<tensdelta>(other->op(0)))
394 // Replace the dummy index with this tensor's other index and remove
396 return replace_contr_index(self, other);
399 /** Contraction of an indexed spinor metric with something else. */
400 bool spinmetric::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
402 GINAC_ASSERT(is_a<indexed>(*self));
403 GINAC_ASSERT(is_a<indexed>(*other));
404 GINAC_ASSERT(self->nops() == 3);
405 GINAC_ASSERT(is_a<spinmetric>(self->op(0)));
407 // Contractions between spinor metrics
408 if (is_a<spinmetric>(other->op(0))) {
409 const idx &self_i1 = ex_to<idx>(self->op(1));
410 const idx &self_i2 = ex_to<idx>(self->op(2));
411 const idx &other_i1 = ex_to<idx>(other->op(1));
412 const idx &other_i2 = ex_to<idx>(other->op(2));
414 if (is_dummy_pair(self_i1, other_i1)) {
415 if (is_dummy_pair(self_i2, other_i2))
418 *self = delta_tensor(self_i2, other_i2);
421 } else if (is_dummy_pair(self_i1, other_i2)) {
422 if (is_dummy_pair(self_i2, other_i1))
425 *self = -delta_tensor(self_i2, other_i1);
428 } else if (is_dummy_pair(self_i2, other_i1)) {
429 *self = -delta_tensor(self_i1, other_i2);
432 } else if (is_dummy_pair(self_i2, other_i2)) {
433 *self = delta_tensor(self_i1, other_i1);
439 // If contracting with the delta tensor, let the delta do it
440 // (don't raise/lower delta indices)
441 if (is_a<tensdelta>(other->op(0)))
444 // Try to contract first index
445 const idx *self_idx = &ex_to<idx>(self->op(1));
446 const idx *free_idx = &ex_to<idx>(self->op(2));
447 bool first_index_tried = false;
451 if (self_idx->is_symbolic()) {
452 for (size_t i=1; i<other->nops(); i++) {
453 const idx &other_idx = ex_to<idx>(other->op(i));
454 if (is_dummy_pair(*self_idx, other_idx)) {
456 // Contraction found, remove metric tensor and substitute
457 // index in second object (assign *self last because this
458 // invalidates free_idx)
459 *other = other->subs(other_idx == *free_idx);
460 *self = (static_cast<const spinidx *>(self_idx)->is_covariant() ? sign : -sign);
466 if (!first_index_tried) {
468 // No contraction with first index found, try second index
469 self_idx = &ex_to<idx>(self->op(2));
470 free_idx = &ex_to<idx>(self->op(1));
471 first_index_tried = true;
479 /** Contraction of epsilon tensor with something else. */
480 bool tensepsilon::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
482 GINAC_ASSERT(is_a<indexed>(*self));
483 GINAC_ASSERT(is_a<indexed>(*other));
484 GINAC_ASSERT(is_a<tensepsilon>(self->op(0)));
485 size_t num = self->nops() - 1;
487 if (is_exactly_a<tensepsilon>(other->op(0)) && num+1 == other->nops()) {
489 // Contraction of two epsilon tensors is a determinant
490 bool variance = is_a<varidx>(self->op(1));
492 for (size_t i=0; i<num; i++) {
493 for (size_t j=0; j<num; j++) {
495 M(i, j) = lorentz_g(self->op(i+1), other->op(j+1), pos_sig);
497 M(i, j) = metric_tensor(self->op(i+1), other->op(j+1));
499 M(i, j) = delta_tensor(self->op(i+1), other->op(j+1));
502 int sign = minkowski ? -1 : 1;
503 *self = sign * M.determinant().simplify_indexed();
515 ex delta_tensor(const ex & i1, const ex & i2)
517 if (!is_a<idx>(i1) || !is_a<idx>(i2))
518 throw(std::invalid_argument("indices of delta tensor must be of type idx"));
520 return indexed(tensdelta(), sy_symm(), i1, i2);
523 ex metric_tensor(const ex & i1, const ex & i2)
525 if (!is_a<varidx>(i1) || !is_a<varidx>(i2))
526 throw(std::invalid_argument("indices of metric tensor must be of type varidx"));
528 return indexed(tensmetric(), sy_symm(), i1, i2);
531 ex lorentz_g(const ex & i1, const ex & i2, bool pos_sig)
533 if (!is_a<varidx>(i1) || !is_a<varidx>(i2))
534 throw(std::invalid_argument("indices of metric tensor must be of type varidx"));
536 return indexed(minkmetric(pos_sig), sy_symm(), i1, i2);
539 ex spinor_metric(const ex & i1, const ex & i2)
541 if (!is_a<spinidx>(i1) || !is_a<spinidx>(i2))
542 throw(std::invalid_argument("indices of spinor metric must be of type spinidx"));
543 if (!ex_to<idx>(i1).get_dim().is_equal(2) || !ex_to<idx>(i2).get_dim().is_equal(2))
544 throw(std::runtime_error("index dimension for spinor metric must be 2"));
546 return indexed(spinmetric(), sy_anti(), i1, i2);
549 ex epsilon_tensor(const ex & i1, const ex & i2)
551 if (!is_a<idx>(i1) || !is_a<idx>(i2))
552 throw(std::invalid_argument("indices of epsilon tensor must be of type idx"));
554 ex dim = ex_to<idx>(i1).get_dim();
555 if (!dim.is_equal(ex_to<idx>(i2).get_dim()))
556 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
557 if (!ex_to<idx>(i1).get_dim().is_equal(_ex2))
558 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
560 return indexed(tensepsilon(), sy_anti(), i1, i2);
563 ex epsilon_tensor(const ex & i1, const ex & i2, const ex & i3)
565 if (!is_a<idx>(i1) || !is_a<idx>(i2) || !is_a<idx>(i3))
566 throw(std::invalid_argument("indices of epsilon tensor must be of type idx"));
568 ex dim = ex_to<idx>(i1).get_dim();
569 if (!dim.is_equal(ex_to<idx>(i2).get_dim()) || !dim.is_equal(ex_to<idx>(i3).get_dim()))
570 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
571 if (!ex_to<idx>(i1).get_dim().is_equal(_ex3))
572 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
574 return indexed(tensepsilon(), sy_anti(), i1, i2, i3);
577 ex lorentz_eps(const ex & i1, const ex & i2, const ex & i3, const ex & i4, bool pos_sig)
579 if (!is_a<varidx>(i1) || !is_a<varidx>(i2) || !is_a<varidx>(i3) || !is_a<varidx>(i4))
580 throw(std::invalid_argument("indices of Lorentz epsilon tensor must be of type varidx"));
582 ex dim = ex_to<idx>(i1).get_dim();
583 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()))
584 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
585 if (!ex_to<idx>(i1).get_dim().is_equal(_ex4))
586 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
588 return indexed(tensepsilon(true, pos_sig), sy_anti(), i1, i2, i3, i4);