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 tensor::tensor(unsigned ti) : inherited(ti)
49 debugmsg("tensor constructor from unsigned", LOGLEVEL_CONSTRUCT); \
53 DEFAULT_CTORS(tensdelta)
54 DEFAULT_CTORS(tensmetric)
55 DEFAULT_DESTROY(minkmetric)
56 DEFAULT_DESTROY(tensepsilon)
58 minkmetric::minkmetric() : pos_sig(false)
60 debugmsg("minkmetric default constructor", LOGLEVEL_CONSTRUCT);
61 tinfo_key = TINFO_minkmetric;
64 minkmetric::minkmetric(bool ps) : pos_sig(ps)
66 debugmsg("minkmetric constructor from bool", LOGLEVEL_CONSTRUCT);
67 tinfo_key = TINFO_minkmetric;
70 void minkmetric::copy(const minkmetric & other)
72 inherited::copy(other);
73 pos_sig = other.pos_sig;
76 tensepsilon::tensepsilon() : minkowski(false), pos_sig(false)
78 debugmsg("tensepsilon default constructor", LOGLEVEL_CONSTRUCT);
79 tinfo_key = TINFO_tensepsilon;
82 tensepsilon::tensepsilon(bool mink, bool ps) : minkowski(mink), pos_sig(ps)
84 debugmsg("tensepsilon constructor from bool,bool", LOGLEVEL_CONSTRUCT);
85 tinfo_key = TINFO_tensepsilon;
88 void tensepsilon::copy(const tensepsilon & other)
90 inherited::copy(other);
91 minkowski = other.minkowski;
92 pos_sig = other.pos_sig;
99 DEFAULT_ARCHIVING(tensor)
100 DEFAULT_ARCHIVING(tensdelta)
101 DEFAULT_ARCHIVING(tensmetric)
102 DEFAULT_UNARCHIVE(minkmetric)
103 DEFAULT_UNARCHIVE(tensepsilon)
105 minkmetric::minkmetric(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
107 debugmsg("minkmetric constructor from archive_node", LOGLEVEL_CONSTRUCT);
108 n.find_bool("pos_sig", pos_sig);
111 void minkmetric::archive(archive_node &n) const
113 inherited::archive(n);
114 n.add_bool("pos_sig", pos_sig);
117 tensepsilon::tensepsilon(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
119 debugmsg("tensepsilon constructor from archive_node", LOGLEVEL_CONSTRUCT);
120 n.find_bool("minkowski", minkowski);
121 n.find_bool("pos_sig", pos_sig);
124 void tensepsilon::archive(archive_node &n) const
126 inherited::archive(n);
127 n.add_bool("minkowski", minkowski);
128 n.add_bool("pos_sig", pos_sig);
132 // functions overriding virtual functions from bases classes
135 DEFAULT_COMPARE(tensor)
136 DEFAULT_COMPARE(tensdelta)
137 DEFAULT_COMPARE(tensmetric)
139 int minkmetric::compare_same_type(const basic & other) const
141 GINAC_ASSERT(is_of_type(other, minkmetric));
142 const minkmetric &o = static_cast<const minkmetric &>(other);
144 if (pos_sig != o.pos_sig)
145 return pos_sig ? -1 : 1;
147 return inherited::compare_same_type(other);
150 int tensepsilon::compare_same_type(const basic & other) const
152 GINAC_ASSERT(is_of_type(other, tensepsilon));
153 const tensepsilon &o = static_cast<const tensepsilon &>(other);
155 if (minkowski != o.minkowski)
156 return minkowski ? -1 : 1;
157 else if (pos_sig != o.pos_sig)
158 return pos_sig ? -1 : 1;
160 return inherited::compare_same_type(other);
163 DEFAULT_PRINT(tensdelta, "delta")
164 DEFAULT_PRINT(tensmetric, "g")
165 DEFAULT_PRINT(minkmetric, "eta")
166 DEFAULT_PRINT(tensepsilon, "eps")
168 /** Automatic symbolic evaluation of an indexed delta tensor. */
169 ex tensdelta::eval_indexed(const basic & i) const
171 GINAC_ASSERT(is_of_type(i, indexed));
172 GINAC_ASSERT(i.nops() == 3);
173 GINAC_ASSERT(is_ex_of_type(i.op(0), tensdelta));
175 const idx & i1 = ex_to_idx(i.op(1));
176 const idx & i2 = ex_to_idx(i.op(2));
178 // Trace of delta tensor is the dimension of the space
179 if (is_dummy_pair(i1, i2))
182 // Numeric evaluation
183 if (static_cast<const indexed &>(i).all_index_values_are(info_flags::integer)) {
184 int n1 = ex_to_numeric(i1.get_value()).to_int(), n2 = ex_to_numeric(i2.get_value()).to_int();
191 // No further simplifications
195 /** Automatic symbolic evaluation of an indexed metric tensor. */
196 ex tensmetric::eval_indexed(const basic & i) const
198 GINAC_ASSERT(is_of_type(i, indexed));
199 GINAC_ASSERT(i.nops() == 3);
200 GINAC_ASSERT(is_ex_of_type(i.op(0), tensmetric));
201 GINAC_ASSERT(is_ex_of_type(i.op(1), varidx));
202 GINAC_ASSERT(is_ex_of_type(i.op(2), varidx));
204 const varidx & i1 = ex_to_varidx(i.op(1));
205 const varidx & i2 = ex_to_varidx(i.op(2));
207 // A metric tensor with one covariant and one contravariant index gets
208 // replaced by a delta tensor
209 if (i1.is_covariant() != i2.is_covariant())
210 return delta_tensor(i1, i2);
212 // No further simplifications
216 /** Automatic symbolic evaluation of an indexed Lorentz metric tensor. */
217 ex minkmetric::eval_indexed(const basic & i) const
219 GINAC_ASSERT(is_of_type(i, indexed));
220 GINAC_ASSERT(i.nops() == 3);
221 GINAC_ASSERT(is_ex_of_type(i.op(0), minkmetric));
222 GINAC_ASSERT(is_ex_of_type(i.op(1), varidx));
223 GINAC_ASSERT(is_ex_of_type(i.op(2), varidx));
225 const varidx & i1 = ex_to_varidx(i.op(1));
226 const varidx & i2 = ex_to_varidx(i.op(2));
228 // Numeric evaluation
229 if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
230 int n1 = ex_to_numeric(i1.get_value()).to_int(), n2 = ex_to_numeric(i2.get_value()).to_int();
234 return pos_sig ? _ex_1() : _ex1();
236 return pos_sig ? _ex1() : _ex_1();
239 // Perform the usual evaluations of a metric tensor
240 return inherited::eval_indexed(i);
243 /** Automatic symbolic evaluation of an indexed epsilon tensor. */
244 ex tensepsilon::eval_indexed(const basic & i) const
246 GINAC_ASSERT(is_of_type(i, indexed));
247 GINAC_ASSERT(i.nops() > 1);
248 GINAC_ASSERT(is_ex_of_type(i.op(0), tensepsilon));
250 // Convolutions are zero
251 if (static_cast<const indexed &>(i).get_dummy_indices().size() != 0)
254 // Numeric evaluation
255 if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
257 // Get sign of index permutation (the indices should already be in
258 // a canonic order but we can't assume what exactly that order is)
260 v.reserve(i.nops() - 1);
261 for (unsigned j=1; j<i.nops(); j++)
262 v.push_back(ex_to_numeric(ex_to_idx(i.op(j)).get_value()).to_int());
263 int sign = permutation_sign(v);
265 // In a Minkowski space, check for covariant indices
267 for (unsigned j=1; j<i.nops(); j++) {
268 const ex & x = i.op(j);
269 if (!is_ex_of_type(x, varidx))
270 throw(std::runtime_error("indices of epsilon tensor in Minkowski space must be of type varidx"));
271 if (ex_to_varidx(x).is_covariant())
272 if (ex_to_idx(x).get_value().is_zero())
273 sign = (pos_sig ? -sign : sign);
275 sign = (pos_sig ? sign : -sign);
282 // No further simplifications
286 /** Contraction of an indexed delta tensor with something else. */
287 bool tensdelta::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
289 GINAC_ASSERT(is_ex_of_type(*self, indexed));
290 GINAC_ASSERT(is_ex_of_type(*other, indexed));
291 GINAC_ASSERT(self->nops() == 3);
292 GINAC_ASSERT(is_ex_of_type(self->op(0), tensdelta));
294 // Try to contract first index
295 const idx *self_idx = &ex_to_idx(self->op(1));
296 const idx *free_idx = &ex_to_idx(self->op(2));
297 bool first_index_tried = false;
300 if (self_idx->is_symbolic()) {
301 for (int i=1; i<other->nops(); i++) {
302 const idx &other_idx = ex_to_idx(other->op(i));
303 if (is_dummy_pair(*self_idx, other_idx)) {
305 // Contraction found, remove delta tensor and substitute
306 // index in second object
308 *other = other->subs(other_idx == *free_idx);
314 if (!first_index_tried) {
316 // No contraction with first index found, try second index
317 self_idx = &ex_to_idx(self->op(2));
318 free_idx = &ex_to_idx(self->op(1));
319 first_index_tried = true;
326 /** Contraction of an indexed metric tensor with something else. */
327 bool tensmetric::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
329 GINAC_ASSERT(is_ex_of_type(*self, indexed));
330 GINAC_ASSERT(is_ex_of_type(*other, indexed));
331 GINAC_ASSERT(self->nops() == 3);
332 GINAC_ASSERT(is_ex_of_type(self->op(0), tensmetric));
334 // If contracting with the delta tensor, let the delta do it
335 // (don't raise/lower delta indices)
336 if (is_ex_of_type(other->op(0), tensdelta))
339 // Try to contract first index
340 const idx *self_idx = &ex_to_idx(self->op(1));
341 const idx *free_idx = &ex_to_idx(self->op(2));
342 bool first_index_tried = false;
345 if (self_idx->is_symbolic()) {
346 for (int i=1; i<other->nops(); i++) {
347 const idx &other_idx = ex_to_idx(other->op(i));
348 if (is_dummy_pair(*self_idx, other_idx)) {
350 // Contraction found, remove metric tensor and substitute
351 // index in second object
353 *other = other->subs(other_idx == *free_idx);
359 if (!first_index_tried) {
361 // No contraction with first index found, try second index
362 self_idx = &ex_to_idx(self->op(2));
363 free_idx = &ex_to_idx(self->op(1));
364 first_index_tried = true;
375 ex delta_tensor(const ex & i1, const ex & i2)
377 if (!is_ex_of_type(i1, idx) || !is_ex_of_type(i2, idx))
378 throw(std::invalid_argument("indices of delta tensor must be of type idx"));
380 return indexed(tensdelta(), indexed::symmetric, i1, i2);
383 ex metric_tensor(const ex & i1, const ex & i2)
385 if (!is_ex_of_type(i1, varidx) || !is_ex_of_type(i2, varidx))
386 throw(std::invalid_argument("indices of metric tensor must be of type varidx"));
388 return indexed(tensmetric(), indexed::symmetric, i1, i2);
391 ex lorentz_g(const ex & i1, const ex & i2, bool pos_sig)
393 if (!is_ex_of_type(i1, varidx) || !is_ex_of_type(i2, varidx))
394 throw(std::invalid_argument("indices of metric tensor must be of type varidx"));
396 return indexed(minkmetric(pos_sig), indexed::symmetric, i1, i2);
399 ex epsilon_tensor(const ex & i1, const ex & i2)
401 if (!is_ex_of_type(i1, idx) || !is_ex_of_type(i2, idx))
402 throw(std::invalid_argument("indices of epsilon tensor must be of type idx"));
404 ex dim = ex_to_idx(i1).get_dim();
405 if (!dim.is_equal(ex_to_idx(i2).get_dim()))
406 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
407 if (!ex_to_idx(i1).get_dim().is_equal(_ex2()))
408 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
410 return indexed(tensepsilon(), indexed::antisymmetric, i1, i2);
413 ex epsilon_tensor(const ex & i1, const ex & i2, const ex & i3)
415 if (!is_ex_of_type(i1, idx) || !is_ex_of_type(i2, idx) || !is_ex_of_type(i3, idx))
416 throw(std::invalid_argument("indices of epsilon tensor must be of type idx"));
418 ex dim = ex_to_idx(i1).get_dim();
419 if (!dim.is_equal(ex_to_idx(i2).get_dim()) || !dim.is_equal(ex_to_idx(i3).get_dim()))
420 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
421 if (!ex_to_idx(i1).get_dim().is_equal(_ex3()))
422 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
424 return indexed(tensepsilon(), indexed::antisymmetric, i1, i2, i3);
427 ex lorentz_eps(const ex & i1, const ex & i2, const ex & i3, const ex & i4, bool pos_sig)
429 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))
430 throw(std::invalid_argument("indices of Lorentz epsilon tensor must be of type varidx"));
432 ex dim = ex_to_idx(i1).get_dim();
433 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()))
434 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
435 if (!ex_to_idx(i1).get_dim().is_equal(_ex4()))
436 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
438 return indexed(tensepsilon(true, pos_sig), indexed::antisymmetric, i1, i2, i3, i4);