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"
37 GINAC_IMPLEMENT_REGISTERED_CLASS(tensor, basic)
38 GINAC_IMPLEMENT_REGISTERED_CLASS(tensdelta, tensor)
39 GINAC_IMPLEMENT_REGISTERED_CLASS(tensmetric, tensor)
40 GINAC_IMPLEMENT_REGISTERED_CLASS(minkmetric, tensmetric)
41 GINAC_IMPLEMENT_REGISTERED_CLASS(tensepsilon, tensor)
44 // default constructor, destructor, copy constructor assignment operator and helpers
47 #define DEFAULT_DESTROY(classname) \
48 void classname::destroy(bool call_parent) \
51 inherited::destroy(call_parent); \
54 #define DEFAULT_CTORS(classname) \
55 classname::classname() : inherited(TINFO_##classname) \
57 debugmsg(#classname " default constructor", LOGLEVEL_CONSTRUCT); \
59 void classname::copy(const classname & other) \
61 inherited::copy(other); \
63 DEFAULT_DESTROY(classname)
65 tensor::tensor(unsigned ti) : inherited(ti)
67 debugmsg("tensor constructor from unsigned", LOGLEVEL_CONSTRUCT); \
71 DEFAULT_CTORS(tensdelta)
72 DEFAULT_CTORS(tensmetric)
73 DEFAULT_DESTROY(minkmetric)
74 DEFAULT_DESTROY(tensepsilon)
76 minkmetric::minkmetric() : pos_sig(false)
78 debugmsg("minkmetric default constructor", LOGLEVEL_CONSTRUCT);
79 tinfo_key = TINFO_minkmetric;
82 minkmetric::minkmetric(bool ps) : pos_sig(ps)
84 debugmsg("minkmetric constructor from bool", LOGLEVEL_CONSTRUCT);
85 tinfo_key = TINFO_minkmetric;
88 void minkmetric::copy(const minkmetric & other)
90 inherited::copy(other);
91 pos_sig = other.pos_sig;
94 tensepsilon::tensepsilon() : minkowski(false), pos_sig(false)
96 debugmsg("tensepsilon default constructor", LOGLEVEL_CONSTRUCT);
97 tinfo_key = TINFO_tensepsilon;
100 tensepsilon::tensepsilon(bool mink, bool ps) : minkowski(mink), pos_sig(ps)
102 debugmsg("tensepsilon constructor from bool,bool", LOGLEVEL_CONSTRUCT);
103 tinfo_key = TINFO_tensepsilon;
106 void tensepsilon::copy(const tensepsilon & other)
108 inherited::copy(other);
109 minkowski = other.minkowski;
110 pos_sig = other.pos_sig;
117 #define DEFAULT_UNARCHIVE(classname) \
118 ex classname::unarchive(const archive_node &n, const lst &sym_lst) \
120 return (new classname(n, sym_lst))->setflag(status_flags::dynallocated); \
123 #define DEFAULT_ARCHIVING(classname) \
124 classname::classname(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst) \
126 debugmsg(#classname " constructor from archive_node", LOGLEVEL_CONSTRUCT); \
128 DEFAULT_UNARCHIVE(classname) \
129 void classname::archive(archive_node &n) const \
131 inherited::archive(n); \
134 DEFAULT_ARCHIVING(tensor)
135 DEFAULT_ARCHIVING(tensdelta)
136 DEFAULT_ARCHIVING(tensmetric)
137 DEFAULT_UNARCHIVE(minkmetric)
138 DEFAULT_UNARCHIVE(tensepsilon)
140 minkmetric::minkmetric(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
142 debugmsg("minkmetric constructor from archive_node", LOGLEVEL_CONSTRUCT);
143 n.find_bool("pos_sig", pos_sig);
146 void minkmetric::archive(archive_node &n) const
148 inherited::archive(n);
149 n.add_bool("pos_sig", pos_sig);
152 tensepsilon::tensepsilon(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
154 debugmsg("tensepsilon constructor from archive_node", LOGLEVEL_CONSTRUCT);
155 n.find_bool("minkowski", minkowski);
156 n.find_bool("pos_sig", pos_sig);
159 void tensepsilon::archive(archive_node &n) const
161 inherited::archive(n);
162 n.add_bool("minkowski", minkowski);
163 n.add_bool("pos_sig", pos_sig);
167 // functions overriding virtual functions from bases classes
170 #define DEFAULT_COMPARE(classname) \
171 int classname::compare_same_type(const basic & other) const \
173 /* by default, two tensors of the same class are always identical */ \
177 DEFAULT_COMPARE(tensor)
178 DEFAULT_COMPARE(tensdelta)
179 DEFAULT_COMPARE(tensmetric)
181 int minkmetric::compare_same_type(const basic & other) const
183 GINAC_ASSERT(is_of_type(other, minkmetric));
184 const minkmetric &o = static_cast<const minkmetric &>(other);
186 if (pos_sig != o.pos_sig)
187 return pos_sig ? -1 : 1;
189 return inherited::compare_same_type(other);
192 int tensepsilon::compare_same_type(const basic & other) const
194 GINAC_ASSERT(is_of_type(other, tensepsilon));
195 const tensepsilon &o = static_cast<const tensepsilon &>(other);
197 if (minkowski != o.minkowski)
198 return minkowski ? -1 : 1;
199 else if (pos_sig != o.pos_sig)
200 return pos_sig ? -1 : 1;
202 return inherited::compare_same_type(other);
205 void tensdelta::print(std::ostream & os, unsigned upper_precedence) const
207 debugmsg("tensdelta print",LOGLEVEL_PRINT);
211 void tensmetric::print(std::ostream & os, unsigned upper_precedence) const
213 debugmsg("tensmetric print",LOGLEVEL_PRINT);
217 void minkmetric::print(std::ostream & os, unsigned upper_precedence) const
219 debugmsg("minkmetric print",LOGLEVEL_PRINT);
223 void tensepsilon::print(std::ostream & os, unsigned upper_precedence) const
225 debugmsg("tensepsilon print",LOGLEVEL_PRINT);
229 /** Automatic symbolic evaluation of an indexed delta tensor. */
230 ex tensdelta::eval_indexed(const basic & i) const
232 GINAC_ASSERT(is_of_type(i, indexed));
233 GINAC_ASSERT(i.nops() == 3);
234 GINAC_ASSERT(is_ex_of_type(i.op(0), tensdelta));
236 const idx & i1 = ex_to_idx(i.op(1));
237 const idx & i2 = ex_to_idx(i.op(2));
239 // Trace of delta tensor is the dimension of the space
240 if (is_dummy_pair(i1, i2))
243 // No further simplifications
247 /** Automatic symbolic evaluation of an indexed metric tensor. */
248 ex tensmetric::eval_indexed(const basic & i) const
250 GINAC_ASSERT(is_of_type(i, indexed));
251 GINAC_ASSERT(i.nops() == 3);
252 GINAC_ASSERT(is_ex_of_type(i.op(0), tensmetric));
253 GINAC_ASSERT(is_ex_of_type(i.op(1), varidx));
254 GINAC_ASSERT(is_ex_of_type(i.op(2), varidx));
256 const varidx & i1 = ex_to_varidx(i.op(1));
257 const varidx & i2 = ex_to_varidx(i.op(2));
259 // A metric tensor with one covariant and one contravariant index gets
260 // replaced by a delta tensor
261 if (i1.is_covariant() != i2.is_covariant())
262 return delta_tensor(i1, i2);
264 // No further simplifications
268 /** Automatic symbolic evaluation of an indexed Lorentz metric tensor. */
269 ex minkmetric::eval_indexed(const basic & i) const
271 GINAC_ASSERT(is_of_type(i, indexed));
272 GINAC_ASSERT(i.nops() == 3);
273 GINAC_ASSERT(is_ex_of_type(i.op(0), minkmetric));
274 GINAC_ASSERT(is_ex_of_type(i.op(1), varidx));
275 GINAC_ASSERT(is_ex_of_type(i.op(2), varidx));
277 const varidx & i1 = ex_to_varidx(i.op(1));
278 const varidx & i2 = ex_to_varidx(i.op(2));
280 // Numeric evaluation
281 if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
282 int n1 = ex_to_numeric(i1.get_value()).to_int(), n2 = ex_to_numeric(i2.get_value()).to_int();
286 return pos_sig ? _ex_1() : _ex1();
288 return pos_sig ? _ex1() : _ex_1();
291 // Perform the usual evaluations of a metric tensor
292 return inherited::eval_indexed(i);
295 /** Automatic symbolic evaluation of an indexed epsilon tensor. */
296 ex tensepsilon::eval_indexed(const basic & i) const
298 GINAC_ASSERT(is_of_type(i, indexed));
299 GINAC_ASSERT(i.nops() > 1);
300 GINAC_ASSERT(is_ex_of_type(i.op(0), tensepsilon));
302 // Convolutions are zero
303 if (static_cast<const indexed &>(i).get_dummy_indices().size() != 0)
306 // Numeric evaluation
307 if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
309 // Get sign of index permutation (the indices should already be in
310 // a canonic order but we can't assume what exactly that order is)
312 v.reserve(i.nops() - 1);
313 for (unsigned j=1; j<i.nops(); j++)
314 v.push_back(ex_to_numeric(ex_to_idx(i.op(j)).get_value()).to_int());
315 int sign = permutation_sign(v);
317 // In a Minkowski space, check for covariant indices
319 for (unsigned j=1; j<i.nops(); j++) {
320 const ex & x = i.op(j);
321 if (!is_ex_of_type(x, varidx))
322 throw(std::runtime_error("indices of epsilon tensor in Minkowski space must be of type varidx"));
323 if (ex_to_varidx(x).is_covariant())
324 if (ex_to_idx(x).get_value().is_zero())
325 sign = (pos_sig ? -sign : sign);
327 sign = (pos_sig ? sign : -sign);
334 // No further simplifications
338 /** Contraction of an indexed delta tensor with something else. */
339 bool tensdelta::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
341 GINAC_ASSERT(is_ex_of_type(*self, indexed));
342 GINAC_ASSERT(is_ex_of_type(*other, indexed));
343 GINAC_ASSERT(self->nops() == 3);
344 GINAC_ASSERT(is_ex_of_type(self->op(0), tensdelta));
346 // Try to contract first index
347 const idx *self_idx = &ex_to_idx(self->op(1));
348 const idx *free_idx = &ex_to_idx(self->op(2));
349 bool first_index_tried = false;
352 if (self_idx->is_symbolic()) {
353 for (int i=1; i<other->nops(); i++) {
354 const idx &other_idx = ex_to_idx(other->op(i));
355 if (is_dummy_pair(*self_idx, other_idx)) {
357 // Contraction found, remove delta tensor and substitute
358 // index in second object
360 *other = other->subs(other_idx == *free_idx);
366 if (!first_index_tried) {
368 // No contraction with first index found, try second index
369 self_idx = &ex_to_idx(self->op(2));
370 free_idx = &ex_to_idx(self->op(1));
371 first_index_tried = true;
378 /** Contraction of an indexed metric tensor with something else. */
379 bool tensmetric::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
381 GINAC_ASSERT(is_ex_of_type(*self, indexed));
382 GINAC_ASSERT(is_ex_of_type(*other, indexed));
383 GINAC_ASSERT(self->nops() == 3);
384 GINAC_ASSERT(is_ex_of_type(self->op(0), tensmetric));
386 // If contracting with the delta tensor, let the delta do it
387 // (don't raise/lower delta indices)
388 if (is_ex_exactly_of_type(other->op(0), tensdelta))
391 // Try to contract first index
392 const idx *self_idx = &ex_to_idx(self->op(1));
393 const idx *free_idx = &ex_to_idx(self->op(2));
394 bool first_index_tried = false;
397 if (self_idx->is_symbolic()) {
398 for (int i=1; i<other->nops(); i++) {
399 const idx &other_idx = ex_to_idx(other->op(i));
400 if (is_dummy_pair(*self_idx, other_idx)) {
402 // Contraction found, remove metric tensor and substitute
403 // index in second object
405 *other = other->subs(other_idx == *free_idx);
411 if (!first_index_tried) {
413 // No contraction with first index found, try second index
414 self_idx = &ex_to_idx(self->op(2));
415 free_idx = &ex_to_idx(self->op(1));
416 first_index_tried = true;
427 ex delta_tensor(const ex & i1, const ex & i2)
429 if (!is_ex_of_type(i1, idx) || !is_ex_of_type(i2, idx))
430 throw(std::invalid_argument("indices of delta tensor must be of type idx"));
432 return indexed(tensdelta(), indexed::symmetric, i1, i2);
435 ex metric_tensor(const ex & i1, const ex & i2)
437 if (!is_ex_of_type(i1, varidx) || !is_ex_of_type(i2, varidx))
438 throw(std::invalid_argument("indices of metric tensor must be of type varidx"));
440 return indexed(tensmetric(), i1, i2);
443 ex lorentz_g(const ex & i1, const ex & i2, bool pos_sig)
445 if (!is_ex_of_type(i1, varidx) || !is_ex_of_type(i2, varidx))
446 throw(std::invalid_argument("indices of metric tensor must be of type varidx"));
448 return indexed(minkmetric(pos_sig), indexed::symmetric, i1, i2);
451 ex epsilon_tensor(const ex & i1, const ex & i2)
453 if (!is_ex_of_type(i1, idx) || !is_ex_of_type(i2, idx))
454 throw(std::invalid_argument("indices of epsilon tensor must be of type idx"));
456 ex dim = ex_to_idx(i1).get_dim();
457 if (!dim.is_equal(ex_to_idx(i2).get_dim()))
458 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
459 if (!ex_to_idx(i1).get_dim().is_equal(_ex2()))
460 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
462 return indexed(tensepsilon(), indexed::antisymmetric, i1, i2);
465 ex epsilon_tensor(const ex & i1, const ex & i2, const ex & i3)
467 if (!is_ex_of_type(i1, idx) || !is_ex_of_type(i2, idx) || !is_ex_of_type(i3, idx))
468 throw(std::invalid_argument("indices of epsilon tensor must be of type idx"));
470 ex dim = ex_to_idx(i1).get_dim();
471 if (!dim.is_equal(ex_to_idx(i2).get_dim()) || !dim.is_equal(ex_to_idx(i3).get_dim()))
472 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
473 if (!ex_to_idx(i1).get_dim().is_equal(_ex3()))
474 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
476 return indexed(tensepsilon(), indexed::antisymmetric, i1, i2, i3);
479 ex lorentz_eps(const ex & i1, const ex & i2, const ex & i3, const ex & i4, bool pos_sig)
481 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))
482 throw(std::invalid_argument("indices of Lorentz epsilon tensor must be of type varidx"));
484 ex dim = ex_to_idx(i1).get_dim();
485 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()))
486 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
487 if (!ex_to_idx(i1).get_dim().is_equal(_ex4()))
488 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
490 return indexed(tensepsilon(true, pos_sig), indexed::antisymmetric, i1, i2, i3, i4);