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 // No further simplifications
186 /** Automatic symbolic evaluation of an indexed metric tensor. */
187 ex tensmetric::eval_indexed(const basic & i) const
189 GINAC_ASSERT(is_of_type(i, indexed));
190 GINAC_ASSERT(i.nops() == 3);
191 GINAC_ASSERT(is_ex_of_type(i.op(0), tensmetric));
192 GINAC_ASSERT(is_ex_of_type(i.op(1), varidx));
193 GINAC_ASSERT(is_ex_of_type(i.op(2), varidx));
195 const varidx & i1 = ex_to_varidx(i.op(1));
196 const varidx & i2 = ex_to_varidx(i.op(2));
198 // A metric tensor with one covariant and one contravariant index gets
199 // replaced by a delta tensor
200 if (i1.is_covariant() != i2.is_covariant())
201 return delta_tensor(i1, i2);
203 // No further simplifications
207 /** Automatic symbolic evaluation of an indexed Lorentz metric tensor. */
208 ex minkmetric::eval_indexed(const basic & i) const
210 GINAC_ASSERT(is_of_type(i, indexed));
211 GINAC_ASSERT(i.nops() == 3);
212 GINAC_ASSERT(is_ex_of_type(i.op(0), minkmetric));
213 GINAC_ASSERT(is_ex_of_type(i.op(1), varidx));
214 GINAC_ASSERT(is_ex_of_type(i.op(2), varidx));
216 const varidx & i1 = ex_to_varidx(i.op(1));
217 const varidx & i2 = ex_to_varidx(i.op(2));
219 // Numeric evaluation
220 if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
221 int n1 = ex_to_numeric(i1.get_value()).to_int(), n2 = ex_to_numeric(i2.get_value()).to_int();
225 return pos_sig ? _ex_1() : _ex1();
227 return pos_sig ? _ex1() : _ex_1();
230 // Perform the usual evaluations of a metric tensor
231 return inherited::eval_indexed(i);
234 /** Automatic symbolic evaluation of an indexed epsilon tensor. */
235 ex tensepsilon::eval_indexed(const basic & i) const
237 GINAC_ASSERT(is_of_type(i, indexed));
238 GINAC_ASSERT(i.nops() > 1);
239 GINAC_ASSERT(is_ex_of_type(i.op(0), tensepsilon));
241 // Convolutions are zero
242 if (static_cast<const indexed &>(i).get_dummy_indices().size() != 0)
245 // Numeric evaluation
246 if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
248 // Get sign of index permutation (the indices should already be in
249 // a canonic order but we can't assume what exactly that order is)
251 v.reserve(i.nops() - 1);
252 for (unsigned j=1; j<i.nops(); j++)
253 v.push_back(ex_to_numeric(ex_to_idx(i.op(j)).get_value()).to_int());
254 int sign = permutation_sign(v);
256 // In a Minkowski space, check for covariant indices
258 for (unsigned j=1; j<i.nops(); j++) {
259 const ex & x = i.op(j);
260 if (!is_ex_of_type(x, varidx))
261 throw(std::runtime_error("indices of epsilon tensor in Minkowski space must be of type varidx"));
262 if (ex_to_varidx(x).is_covariant())
263 if (ex_to_idx(x).get_value().is_zero())
264 sign = (pos_sig ? -sign : sign);
266 sign = (pos_sig ? sign : -sign);
273 // No further simplifications
277 /** Contraction of an indexed delta tensor with something else. */
278 bool tensdelta::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
280 GINAC_ASSERT(is_ex_of_type(*self, indexed));
281 GINAC_ASSERT(is_ex_of_type(*other, indexed));
282 GINAC_ASSERT(self->nops() == 3);
283 GINAC_ASSERT(is_ex_of_type(self->op(0), tensdelta));
285 // Try to contract first index
286 const idx *self_idx = &ex_to_idx(self->op(1));
287 const idx *free_idx = &ex_to_idx(self->op(2));
288 bool first_index_tried = false;
291 if (self_idx->is_symbolic()) {
292 for (int i=1; i<other->nops(); i++) {
293 const idx &other_idx = ex_to_idx(other->op(i));
294 if (is_dummy_pair(*self_idx, other_idx)) {
296 // Contraction found, remove delta tensor and substitute
297 // index in second object
299 *other = other->subs(other_idx == *free_idx);
305 if (!first_index_tried) {
307 // No contraction with first index found, try second index
308 self_idx = &ex_to_idx(self->op(2));
309 free_idx = &ex_to_idx(self->op(1));
310 first_index_tried = true;
317 /** Contraction of an indexed metric tensor with something else. */
318 bool tensmetric::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
320 GINAC_ASSERT(is_ex_of_type(*self, indexed));
321 GINAC_ASSERT(is_ex_of_type(*other, indexed));
322 GINAC_ASSERT(self->nops() == 3);
323 GINAC_ASSERT(is_ex_of_type(self->op(0), tensmetric));
325 // If contracting with the delta tensor, let the delta do it
326 // (don't raise/lower delta indices)
327 if (is_ex_exactly_of_type(other->op(0), tensdelta))
330 // Try to contract 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 (int 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 metric tensor and substitute
342 // index in second object
344 *other = other->subs(other_idx == *free_idx);
350 if (!first_index_tried) {
352 // No contraction with first index found, try second index
353 self_idx = &ex_to_idx(self->op(2));
354 free_idx = &ex_to_idx(self->op(1));
355 first_index_tried = true;
366 ex delta_tensor(const ex & i1, const ex & i2)
368 if (!is_ex_of_type(i1, idx) || !is_ex_of_type(i2, idx))
369 throw(std::invalid_argument("indices of delta tensor must be of type idx"));
371 return indexed(tensdelta(), indexed::symmetric, i1, i2);
374 ex metric_tensor(const ex & i1, const ex & i2)
376 if (!is_ex_of_type(i1, varidx) || !is_ex_of_type(i2, varidx))
377 throw(std::invalid_argument("indices of metric tensor must be of type varidx"));
379 return indexed(tensmetric(), indexed::symmetric, i1, i2);
382 ex lorentz_g(const ex & i1, const ex & i2, bool pos_sig)
384 if (!is_ex_of_type(i1, varidx) || !is_ex_of_type(i2, varidx))
385 throw(std::invalid_argument("indices of metric tensor must be of type varidx"));
387 return indexed(minkmetric(pos_sig), indexed::symmetric, i1, i2);
390 ex epsilon_tensor(const ex & i1, const ex & i2)
392 if (!is_ex_of_type(i1, idx) || !is_ex_of_type(i2, idx))
393 throw(std::invalid_argument("indices of epsilon tensor must be of type idx"));
395 ex dim = ex_to_idx(i1).get_dim();
396 if (!dim.is_equal(ex_to_idx(i2).get_dim()))
397 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
398 if (!ex_to_idx(i1).get_dim().is_equal(_ex2()))
399 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
401 return indexed(tensepsilon(), indexed::antisymmetric, i1, i2);
404 ex epsilon_tensor(const ex & i1, const ex & i2, const ex & i3)
406 if (!is_ex_of_type(i1, idx) || !is_ex_of_type(i2, idx) || !is_ex_of_type(i3, idx))
407 throw(std::invalid_argument("indices of epsilon tensor must be of type idx"));
409 ex dim = ex_to_idx(i1).get_dim();
410 if (!dim.is_equal(ex_to_idx(i2).get_dim()) || !dim.is_equal(ex_to_idx(i3).get_dim()))
411 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
412 if (!ex_to_idx(i1).get_dim().is_equal(_ex3()))
413 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
415 return indexed(tensepsilon(), indexed::antisymmetric, i1, i2, i3);
418 ex lorentz_eps(const ex & i1, const ex & i2, const ex & i3, const ex & i4, bool pos_sig)
420 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))
421 throw(std::invalid_argument("indices of Lorentz epsilon tensor must be of type varidx"));
423 ex dim = ex_to_idx(i1).get_dim();
424 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()))
425 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
426 if (!ex_to_idx(i1).get_dim().is_equal(_ex4()))
427 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
429 return indexed(tensepsilon(true, pos_sig), indexed::antisymmetric, i1, i2, i3, i4);