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"
38 GINAC_IMPLEMENT_REGISTERED_CLASS(tensor, basic)
39 GINAC_IMPLEMENT_REGISTERED_CLASS(tensdelta, tensor)
40 GINAC_IMPLEMENT_REGISTERED_CLASS(tensmetric, tensor)
41 GINAC_IMPLEMENT_REGISTERED_CLASS(minkmetric, tensmetric)
42 GINAC_IMPLEMENT_REGISTERED_CLASS(tensepsilon, tensor)
45 // default constructor, destructor, copy constructor assignment operator and helpers
48 tensor::tensor(unsigned ti) : inherited(ti)
50 debugmsg("tensor constructor from unsigned", LOGLEVEL_CONSTRUCT); \
54 DEFAULT_CTORS(tensdelta)
55 DEFAULT_CTORS(tensmetric)
56 DEFAULT_DESTROY(minkmetric)
57 DEFAULT_DESTROY(tensepsilon)
59 minkmetric::minkmetric() : pos_sig(false)
61 debugmsg("minkmetric default constructor", LOGLEVEL_CONSTRUCT);
62 tinfo_key = TINFO_minkmetric;
65 minkmetric::minkmetric(bool ps) : pos_sig(ps)
67 debugmsg("minkmetric constructor from bool", LOGLEVEL_CONSTRUCT);
68 tinfo_key = TINFO_minkmetric;
71 void minkmetric::copy(const minkmetric & other)
73 inherited::copy(other);
74 pos_sig = other.pos_sig;
77 tensepsilon::tensepsilon() : minkowski(false), pos_sig(false)
79 debugmsg("tensepsilon default constructor", LOGLEVEL_CONSTRUCT);
80 tinfo_key = TINFO_tensepsilon;
83 tensepsilon::tensepsilon(bool mink, bool ps) : minkowski(mink), pos_sig(ps)
85 debugmsg("tensepsilon constructor from bool,bool", LOGLEVEL_CONSTRUCT);
86 tinfo_key = TINFO_tensepsilon;
89 void tensepsilon::copy(const tensepsilon & other)
91 inherited::copy(other);
92 minkowski = other.minkowski;
93 pos_sig = other.pos_sig;
100 DEFAULT_ARCHIVING(tensor)
101 DEFAULT_ARCHIVING(tensdelta)
102 DEFAULT_ARCHIVING(tensmetric)
103 DEFAULT_UNARCHIVE(minkmetric)
104 DEFAULT_UNARCHIVE(tensepsilon)
106 minkmetric::minkmetric(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
108 debugmsg("minkmetric constructor from archive_node", LOGLEVEL_CONSTRUCT);
109 n.find_bool("pos_sig", pos_sig);
112 void minkmetric::archive(archive_node &n) const
114 inherited::archive(n);
115 n.add_bool("pos_sig", pos_sig);
118 tensepsilon::tensepsilon(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
120 debugmsg("tensepsilon constructor from archive_node", LOGLEVEL_CONSTRUCT);
121 n.find_bool("minkowski", minkowski);
122 n.find_bool("pos_sig", pos_sig);
125 void tensepsilon::archive(archive_node &n) const
127 inherited::archive(n);
128 n.add_bool("minkowski", minkowski);
129 n.add_bool("pos_sig", pos_sig);
133 // functions overriding virtual functions from bases classes
136 DEFAULT_COMPARE(tensor)
137 DEFAULT_COMPARE(tensdelta)
138 DEFAULT_COMPARE(tensmetric)
140 int minkmetric::compare_same_type(const basic & other) const
142 GINAC_ASSERT(is_of_type(other, minkmetric));
143 const minkmetric &o = static_cast<const minkmetric &>(other);
145 if (pos_sig != o.pos_sig)
146 return pos_sig ? -1 : 1;
148 return inherited::compare_same_type(other);
151 int tensepsilon::compare_same_type(const basic & other) const
153 GINAC_ASSERT(is_of_type(other, tensepsilon));
154 const tensepsilon &o = static_cast<const tensepsilon &>(other);
156 if (minkowski != o.minkowski)
157 return minkowski ? -1 : 1;
158 else if (pos_sig != o.pos_sig)
159 return pos_sig ? -1 : 1;
161 return inherited::compare_same_type(other);
164 DEFAULT_PRINT(tensdelta, "delta")
165 DEFAULT_PRINT(tensmetric, "g")
166 DEFAULT_PRINT(minkmetric, "eta")
167 DEFAULT_PRINT(tensepsilon, "eps")
169 /** Automatic symbolic evaluation of an indexed delta tensor. */
170 ex tensdelta::eval_indexed(const basic & i) const
172 GINAC_ASSERT(is_of_type(i, indexed));
173 GINAC_ASSERT(i.nops() == 3);
174 GINAC_ASSERT(is_ex_of_type(i.op(0), tensdelta));
176 const idx & i1 = ex_to_idx(i.op(1));
177 const idx & i2 = ex_to_idx(i.op(2));
179 // Trace of delta tensor is the dimension of the space
180 if (is_dummy_pair(i1, i2))
183 // Numeric evaluation
184 if (static_cast<const indexed &>(i).all_index_values_are(info_flags::integer)) {
185 int n1 = ex_to_numeric(i1.get_value()).to_int(), n2 = ex_to_numeric(i2.get_value()).to_int();
192 // No further simplifications
196 /** Automatic symbolic evaluation of an indexed metric tensor. */
197 ex tensmetric::eval_indexed(const basic & i) const
199 GINAC_ASSERT(is_of_type(i, indexed));
200 GINAC_ASSERT(i.nops() == 3);
201 GINAC_ASSERT(is_ex_of_type(i.op(0), tensmetric));
202 GINAC_ASSERT(is_ex_of_type(i.op(1), varidx));
203 GINAC_ASSERT(is_ex_of_type(i.op(2), varidx));
205 const varidx & i1 = ex_to_varidx(i.op(1));
206 const varidx & i2 = ex_to_varidx(i.op(2));
208 // A metric tensor with one covariant and one contravariant index gets
209 // replaced by a delta tensor
210 if (i1.is_covariant() != i2.is_covariant())
211 return delta_tensor(i1, i2);
213 // No further simplifications
217 /** Automatic symbolic evaluation of an indexed Lorentz metric tensor. */
218 ex minkmetric::eval_indexed(const basic & i) const
220 GINAC_ASSERT(is_of_type(i, indexed));
221 GINAC_ASSERT(i.nops() == 3);
222 GINAC_ASSERT(is_ex_of_type(i.op(0), minkmetric));
223 GINAC_ASSERT(is_ex_of_type(i.op(1), varidx));
224 GINAC_ASSERT(is_ex_of_type(i.op(2), varidx));
226 const varidx & i1 = ex_to_varidx(i.op(1));
227 const varidx & i2 = ex_to_varidx(i.op(2));
229 // Numeric evaluation
230 if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
231 int n1 = ex_to_numeric(i1.get_value()).to_int(), n2 = ex_to_numeric(i2.get_value()).to_int();
235 return pos_sig ? _ex_1() : _ex1();
237 return pos_sig ? _ex1() : _ex_1();
240 // Perform the usual evaluations of a metric tensor
241 return inherited::eval_indexed(i);
244 /** Automatic symbolic evaluation of an indexed epsilon tensor. */
245 ex tensepsilon::eval_indexed(const basic & i) const
247 GINAC_ASSERT(is_of_type(i, indexed));
248 GINAC_ASSERT(i.nops() > 1);
249 GINAC_ASSERT(is_ex_of_type(i.op(0), tensepsilon));
251 // Convolutions are zero
252 if (static_cast<const indexed &>(i).get_dummy_indices().size() != 0)
255 // Numeric evaluation
256 if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
258 // Get sign of index permutation (the indices should already be in
259 // a canonic order but we can't assume what exactly that order is)
261 v.reserve(i.nops() - 1);
262 for (unsigned j=1; j<i.nops(); j++)
263 v.push_back(ex_to_numeric(ex_to_idx(i.op(j)).get_value()).to_int());
264 int sign = permutation_sign(v);
266 // In a Minkowski space, check for covariant indices
268 for (unsigned j=1; j<i.nops(); j++) {
269 const ex & x = i.op(j);
270 if (!is_ex_of_type(x, varidx))
271 throw(std::runtime_error("indices of epsilon tensor in Minkowski space must be of type varidx"));
272 if (ex_to_varidx(x).is_covariant())
273 if (ex_to_idx(x).get_value().is_zero())
274 sign = (pos_sig ? -sign : sign);
276 sign = (pos_sig ? sign : -sign);
283 // No further simplifications
287 /** Contraction of an indexed delta tensor with something else. */
288 bool tensdelta::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
290 GINAC_ASSERT(is_ex_of_type(*self, indexed));
291 GINAC_ASSERT(is_ex_of_type(*other, indexed));
292 GINAC_ASSERT(self->nops() == 3);
293 GINAC_ASSERT(is_ex_of_type(self->op(0), tensdelta));
295 // Try to contract first index
296 const idx *self_idx = &ex_to_idx(self->op(1));
297 const idx *free_idx = &ex_to_idx(self->op(2));
298 bool first_index_tried = false;
301 if (self_idx->is_symbolic()) {
302 for (int i=1; i<other->nops(); i++) {
303 const idx &other_idx = ex_to_idx(other->op(i));
304 if (is_dummy_pair(*self_idx, other_idx)) {
306 // Contraction found, remove delta tensor and substitute
307 // index in second object
309 *other = other->subs(other_idx == *free_idx);
315 if (!first_index_tried) {
317 // No contraction with first index found, try second index
318 self_idx = &ex_to_idx(self->op(2));
319 free_idx = &ex_to_idx(self->op(1));
320 first_index_tried = true;
327 /** Contraction of an indexed metric tensor with something else. */
328 bool tensmetric::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
330 GINAC_ASSERT(is_ex_of_type(*self, indexed));
331 GINAC_ASSERT(is_ex_of_type(*other, indexed));
332 GINAC_ASSERT(self->nops() == 3);
333 GINAC_ASSERT(is_ex_of_type(self->op(0), tensmetric));
335 // If contracting with the delta tensor, let the delta do it
336 // (don't raise/lower delta indices)
337 if (is_ex_of_type(other->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 metric 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;
376 ex delta_tensor(const ex & i1, const ex & i2)
378 if (!is_ex_of_type(i1, idx) || !is_ex_of_type(i2, idx))
379 throw(std::invalid_argument("indices of delta tensor must be of type idx"));
381 return indexed(tensdelta(), indexed::symmetric, i1, i2);
384 ex metric_tensor(const ex & i1, const ex & i2)
386 if (!is_ex_of_type(i1, varidx) || !is_ex_of_type(i2, varidx))
387 throw(std::invalid_argument("indices of metric tensor must be of type varidx"));
389 return indexed(tensmetric(), indexed::symmetric, i1, i2);
392 ex lorentz_g(const ex & i1, const ex & i2, bool pos_sig)
394 if (!is_ex_of_type(i1, varidx) || !is_ex_of_type(i2, varidx))
395 throw(std::invalid_argument("indices of metric tensor must be of type varidx"));
397 return indexed(minkmetric(pos_sig), indexed::symmetric, i1, i2);
400 ex epsilon_tensor(const ex & i1, const ex & i2)
402 if (!is_ex_of_type(i1, idx) || !is_ex_of_type(i2, idx))
403 throw(std::invalid_argument("indices of epsilon tensor must be of type idx"));
405 ex dim = ex_to_idx(i1).get_dim();
406 if (!dim.is_equal(ex_to_idx(i2).get_dim()))
407 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
408 if (!ex_to_idx(i1).get_dim().is_equal(_ex2()))
409 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
411 return indexed(tensepsilon(), indexed::antisymmetric, i1, i2);
414 ex epsilon_tensor(const ex & i1, const ex & i2, const ex & i3)
416 if (!is_ex_of_type(i1, idx) || !is_ex_of_type(i2, idx) || !is_ex_of_type(i3, idx))
417 throw(std::invalid_argument("indices of epsilon tensor must be of type idx"));
419 ex dim = ex_to_idx(i1).get_dim();
420 if (!dim.is_equal(ex_to_idx(i2).get_dim()) || !dim.is_equal(ex_to_idx(i3).get_dim()))
421 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
422 if (!ex_to_idx(i1).get_dim().is_equal(_ex3()))
423 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
425 return indexed(tensepsilon(), indexed::antisymmetric, i1, i2, i3);
428 ex lorentz_eps(const ex & i1, const ex & i2, const ex & i3, const ex & i4, bool pos_sig)
430 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))
431 throw(std::invalid_argument("indices of Lorentz epsilon tensor must be of type varidx"));
433 ex dim = ex_to_idx(i1).get_dim();
434 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()))
435 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
436 if (!ex_to_idx(i1).get_dim().is_equal(_ex4()))
437 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
439 return indexed(tensepsilon(true, pos_sig), indexed::antisymmetric, i1, i2, i3, i4);