]> www.ginac.de Git - ginac.git/blob - ginac/tensor.cpp
added checks for bugs in GiNaC 0.8.0
[ginac.git] / ginac / tensor.cpp
1 /** @file tensor.cpp
2  *
3  *  Implementation of GiNaC's special tensors. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include <stdexcept>
24 #include <vector>
25
26 #include "tensor.h"
27 #include "idx.h"
28 #include "indexed.h"
29 #include "relational.h"
30 #include "numeric.h"
31 #include "archive.h"
32 #include "utils.h"
33 #include "debugmsg.h"
34
35 namespace GiNaC {
36
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)
42
43 //////////
44 // default constructor, destructor, copy constructor assignment operator and helpers
45 //////////
46
47 tensor::tensor(unsigned ti) : inherited(ti)
48 {
49         debugmsg("tensor constructor from unsigned", LOGLEVEL_CONSTRUCT); \
50 }
51
52 DEFAULT_CTORS(tensor)
53 DEFAULT_CTORS(tensdelta)
54 DEFAULT_CTORS(tensmetric)
55 DEFAULT_DESTROY(minkmetric)
56 DEFAULT_DESTROY(tensepsilon)
57
58 minkmetric::minkmetric() : pos_sig(false)
59 {
60         debugmsg("minkmetric default constructor", LOGLEVEL_CONSTRUCT);
61         tinfo_key = TINFO_minkmetric;
62 }
63
64 minkmetric::minkmetric(bool ps) : pos_sig(ps)
65 {
66         debugmsg("minkmetric constructor from bool", LOGLEVEL_CONSTRUCT);
67         tinfo_key = TINFO_minkmetric;
68 }
69
70 void minkmetric::copy(const minkmetric & other)
71 {
72         inherited::copy(other);
73         pos_sig = other.pos_sig;
74 }
75
76 tensepsilon::tensepsilon() : minkowski(false), pos_sig(false)
77 {
78         debugmsg("tensepsilon default constructor", LOGLEVEL_CONSTRUCT);
79         tinfo_key = TINFO_tensepsilon;
80 }
81
82 tensepsilon::tensepsilon(bool mink, bool ps) : minkowski(mink), pos_sig(ps)
83 {
84         debugmsg("tensepsilon constructor from bool,bool", LOGLEVEL_CONSTRUCT);
85         tinfo_key = TINFO_tensepsilon;
86 }
87
88 void tensepsilon::copy(const tensepsilon & other)
89 {
90         inherited::copy(other);
91         minkowski = other.minkowski;
92         pos_sig = other.pos_sig;
93 }
94
95 //////////
96 // archiving
97 //////////
98
99 DEFAULT_ARCHIVING(tensor)
100 DEFAULT_ARCHIVING(tensdelta)
101 DEFAULT_ARCHIVING(tensmetric)
102 DEFAULT_UNARCHIVE(minkmetric)
103 DEFAULT_UNARCHIVE(tensepsilon)
104
105 minkmetric::minkmetric(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
106 {
107         debugmsg("minkmetric constructor from archive_node", LOGLEVEL_CONSTRUCT);
108         n.find_bool("pos_sig", pos_sig);
109 }
110
111 void minkmetric::archive(archive_node &n) const
112 {
113         inherited::archive(n);
114         n.add_bool("pos_sig", pos_sig);
115 }
116
117 tensepsilon::tensepsilon(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
118 {
119         debugmsg("tensepsilon constructor from archive_node", LOGLEVEL_CONSTRUCT);
120         n.find_bool("minkowski", minkowski);
121         n.find_bool("pos_sig", pos_sig);
122 }
123
124 void tensepsilon::archive(archive_node &n) const
125 {
126         inherited::archive(n);
127         n.add_bool("minkowski", minkowski);
128         n.add_bool("pos_sig", pos_sig);
129 }
130
131 //////////
132 // functions overriding virtual functions from bases classes
133 //////////
134
135 DEFAULT_COMPARE(tensor)
136 DEFAULT_COMPARE(tensdelta)
137 DEFAULT_COMPARE(tensmetric)
138
139 int minkmetric::compare_same_type(const basic & other) const
140 {
141         GINAC_ASSERT(is_of_type(other, minkmetric));
142         const minkmetric &o = static_cast<const minkmetric &>(other);
143
144         if (pos_sig != o.pos_sig)
145                 return pos_sig ? -1 : 1;
146         else
147                 return inherited::compare_same_type(other);
148 }
149
150 int tensepsilon::compare_same_type(const basic & other) const
151 {
152         GINAC_ASSERT(is_of_type(other, tensepsilon));
153         const tensepsilon &o = static_cast<const tensepsilon &>(other);
154
155         if (minkowski != o.minkowski)
156                 return minkowski ? -1 : 1;
157         else if (pos_sig != o.pos_sig)
158                 return pos_sig ? -1 : 1;
159         else
160                 return inherited::compare_same_type(other);
161 }
162
163 DEFAULT_PRINT(tensdelta, "delta")
164 DEFAULT_PRINT(tensmetric, "g")
165 DEFAULT_PRINT(minkmetric, "eta")
166 DEFAULT_PRINT(tensepsilon, "eps")
167
168 /** Automatic symbolic evaluation of an indexed delta tensor. */
169 ex tensdelta::eval_indexed(const basic & i) const
170 {
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));
174
175         const idx & i1 = ex_to_idx(i.op(1));
176         const idx & i2 = ex_to_idx(i.op(2));
177
178         // Trace of delta tensor is the dimension of the space
179         if (is_dummy_pair(i1, i2))
180                 return i1.get_dim();
181
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();
185                 if (n1 == n2)
186                         return _ex1();
187                 else
188                         return _ex0();
189         }
190
191         // No further simplifications
192         return i.hold();
193 }
194
195 /** Automatic symbolic evaluation of an indexed metric tensor. */
196 ex tensmetric::eval_indexed(const basic & i) const
197 {
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));
203
204         const varidx & i1 = ex_to_varidx(i.op(1));
205         const varidx & i2 = ex_to_varidx(i.op(2));
206
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);
211
212         // No further simplifications
213         return i.hold();
214 }
215
216 /** Automatic symbolic evaluation of an indexed Lorentz metric tensor. */
217 ex minkmetric::eval_indexed(const basic & i) const
218 {
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));
224
225         const varidx & i1 = ex_to_varidx(i.op(1));
226         const varidx & i2 = ex_to_varidx(i.op(2));
227
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();
231                 if (n1 != n2)
232                         return _ex0();
233                 else if (n1 == 0)
234                         return pos_sig ? _ex_1() : _ex1();
235                 else
236                         return pos_sig ? _ex1() : _ex_1();
237         }
238
239         // Perform the usual evaluations of a metric tensor
240         return inherited::eval_indexed(i);
241 }
242
243 /** Automatic symbolic evaluation of an indexed epsilon tensor. */
244 ex tensepsilon::eval_indexed(const basic & i) const
245 {
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));
249
250         // Convolutions are zero
251         if (static_cast<const indexed &>(i).get_dummy_indices().size() != 0)
252                 return _ex0();
253
254         // Numeric evaluation
255         if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
256
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)
259                 std::vector<int> v;
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);
264
265                 // In a Minkowski space, check for covariant indices
266                 if (minkowski) {
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);
274                                         else
275                                                 sign = (pos_sig ? sign : -sign);
276                         }
277                 }
278
279                 return sign;
280         }
281
282         // No further simplifications
283         return i.hold();
284 }
285
286 /** Contraction of an indexed delta tensor with something else. */
287 bool tensdelta::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
288 {
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));
293
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;
298
299 again:
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)) {
304
305                                 // Contraction found, remove delta tensor and substitute
306                                 // index in second object
307                                 *self = _ex1();
308                                 *other = other->subs(other_idx == *free_idx);
309                                 return true;
310                         }
311                 }
312         }
313
314         if (!first_index_tried) {
315
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;
320                 goto again;
321         }
322
323         return false;
324 }
325
326 /** Contraction of an indexed metric tensor with something else. */
327 bool tensmetric::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
328 {
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));
333
334         // If contracting with the delta tensor, let the delta do it
335         // (don't raise/lower delta indices)
336         if (is_ex_exactly_of_type(other->op(0), tensdelta))
337                 return false;
338
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;
343
344 again:
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)) {
349
350                                 // Contraction found, remove metric tensor and substitute
351                                 // index in second object
352                                 *self = _ex1();
353                                 *other = other->subs(other_idx == *free_idx);
354                                 return true;
355                         }
356                 }
357         }
358
359         if (!first_index_tried) {
360
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;
365                 goto again;
366         }
367
368         return false;
369 }
370
371 //////////
372 // global functions
373 //////////
374
375 ex delta_tensor(const ex & i1, const ex & i2)
376 {
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"));
379
380         return indexed(tensdelta(), indexed::symmetric, i1, i2);
381 }
382
383 ex metric_tensor(const ex & i1, const ex & i2)
384 {
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"));
387
388         return indexed(tensmetric(), indexed::symmetric, i1, i2);
389 }
390
391 ex lorentz_g(const ex & i1, const ex & i2, bool pos_sig)
392 {
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"));
395
396         return indexed(minkmetric(pos_sig), indexed::symmetric, i1, i2);
397 }
398
399 ex epsilon_tensor(const ex & i1, const ex & i2)
400 {
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"));
403
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"));
409
410         return indexed(tensepsilon(), indexed::antisymmetric, i1, i2);
411 }
412
413 ex epsilon_tensor(const ex & i1, const ex & i2, const ex & i3)
414 {
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"));
417
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"));
423
424         return indexed(tensepsilon(), indexed::antisymmetric, i1, i2, i3);
425 }
426
427 ex lorentz_eps(const ex & i1, const ex & i2, const ex & i3, const ex & i4, bool pos_sig)
428 {
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"));
431
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"));
437
438         return indexed(tensepsilon(true, pos_sig), indexed::antisymmetric, i1, i2, i3, i4);
439 }
440
441 } // namespace GiNaC