]> www.ginac.de Git - ginac.git/blob - ginac/tensor.cpp
cf0204b56319522260d5143dc3b1aea28778c270
[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 #define DEFAULT_DESTROY(classname) \
48 void classname::destroy(bool call_parent) \
49 { \
50         if (call_parent) \
51                 inherited::destroy(call_parent); \
52 }
53
54 #define DEFAULT_CTORS(classname) \
55 classname::classname() : inherited(TINFO_##classname) \
56 { \
57         debugmsg(#classname " default constructor", LOGLEVEL_CONSTRUCT); \
58 } \
59 void classname::copy(const classname & other) \
60 { \
61         inherited::copy(other); \
62 } \
63 DEFAULT_DESTROY(classname)
64
65 tensor::tensor(unsigned ti) : inherited(ti)
66 {
67         debugmsg("tensor constructor from unsigned", LOGLEVEL_CONSTRUCT); \
68 }
69
70 DEFAULT_CTORS(tensor)
71 DEFAULT_CTORS(tensdelta)
72 DEFAULT_CTORS(tensmetric)
73 DEFAULT_DESTROY(minkmetric)
74 DEFAULT_DESTROY(tensepsilon)
75
76 minkmetric::minkmetric() : pos_sig(false)
77 {
78         debugmsg("minkmetric default constructor", LOGLEVEL_CONSTRUCT);
79         tinfo_key = TINFO_minkmetric;
80 }
81
82 minkmetric::minkmetric(bool ps) : pos_sig(ps)
83 {
84         debugmsg("minkmetric constructor from bool", LOGLEVEL_CONSTRUCT);
85         tinfo_key = TINFO_minkmetric;
86 }
87
88 void minkmetric::copy(const minkmetric & other)
89 {
90         inherited::copy(other);
91         pos_sig = other.pos_sig;
92 }
93
94 tensepsilon::tensepsilon() : minkowski(false), pos_sig(false)
95 {
96         debugmsg("tensepsilon default constructor", LOGLEVEL_CONSTRUCT);
97         tinfo_key = TINFO_tensepsilon;
98 }
99
100 tensepsilon::tensepsilon(bool mink, bool ps) : minkowski(mink), pos_sig(ps)
101 {
102         debugmsg("tensepsilon constructor from bool,bool", LOGLEVEL_CONSTRUCT);
103         tinfo_key = TINFO_tensepsilon;
104 }
105
106 void tensepsilon::copy(const tensepsilon & other)
107 {
108         inherited::copy(other);
109         minkowski = other.minkowski;
110         pos_sig = other.pos_sig;
111 }
112
113 //////////
114 // archiving
115 //////////
116
117 #define DEFAULT_UNARCHIVE(classname) \
118 ex classname::unarchive(const archive_node &n, const lst &sym_lst) \
119 { \
120         return (new classname(n, sym_lst))->setflag(status_flags::dynallocated); \
121 }
122
123 #define DEFAULT_ARCHIVING(classname) \
124 classname::classname(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst) \
125 { \
126         debugmsg(#classname " constructor from archive_node", LOGLEVEL_CONSTRUCT); \
127 } \
128 DEFAULT_UNARCHIVE(classname) \
129 void classname::archive(archive_node &n) const \
130 { \
131         inherited::archive(n); \
132 }
133
134 DEFAULT_ARCHIVING(tensor)
135 DEFAULT_ARCHIVING(tensdelta)
136 DEFAULT_ARCHIVING(tensmetric)
137 DEFAULT_UNARCHIVE(minkmetric)
138 DEFAULT_UNARCHIVE(tensepsilon)
139
140 minkmetric::minkmetric(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
141 {
142         debugmsg("minkmetric constructor from archive_node", LOGLEVEL_CONSTRUCT);
143         n.find_bool("pos_sig", pos_sig);
144 }
145
146 void minkmetric::archive(archive_node &n) const
147 {
148         inherited::archive(n);
149         n.add_bool("pos_sig", pos_sig);
150 }
151
152 tensepsilon::tensepsilon(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
153 {
154         debugmsg("tensepsilon constructor from archive_node", LOGLEVEL_CONSTRUCT);
155         n.find_bool("minkowski", minkowski);
156         n.find_bool("pos_sig", pos_sig);
157 }
158
159 void tensepsilon::archive(archive_node &n) const
160 {
161         inherited::archive(n);
162         n.add_bool("minkowski", minkowski);
163         n.add_bool("pos_sig", pos_sig);
164 }
165
166 //////////
167 // functions overriding virtual functions from bases classes
168 //////////
169
170 #define DEFAULT_COMPARE(classname) \
171 int classname::compare_same_type(const basic & other) const \
172 { \
173         /* by default, two tensors of the same class are always identical */ \
174         return 0; \
175 }
176
177 DEFAULT_COMPARE(tensor)
178 DEFAULT_COMPARE(tensdelta)
179 DEFAULT_COMPARE(tensmetric)
180
181 int minkmetric::compare_same_type(const basic & other) const
182 {
183         GINAC_ASSERT(is_of_type(other, minkmetric));
184         const minkmetric &o = static_cast<const minkmetric &>(other);
185
186         if (pos_sig != o.pos_sig)
187                 return pos_sig ? -1 : 1;
188         else
189                 return inherited::compare_same_type(other);
190 }
191
192 int tensepsilon::compare_same_type(const basic & other) const
193 {
194         GINAC_ASSERT(is_of_type(other, tensepsilon));
195         const tensepsilon &o = static_cast<const tensepsilon &>(other);
196
197         if (minkowski != o.minkowski)
198                 return minkowski ? -1 : 1;
199         else if (pos_sig != o.pos_sig)
200                 return pos_sig ? -1 : 1;
201         else
202                 return inherited::compare_same_type(other);
203 }
204
205 void tensdelta::print(std::ostream & os, unsigned upper_precedence) const
206 {
207         debugmsg("tensdelta print",LOGLEVEL_PRINT);
208         os << "delta";
209 }
210
211 void tensmetric::print(std::ostream & os, unsigned upper_precedence) const
212 {
213         debugmsg("tensmetric print",LOGLEVEL_PRINT);
214         os << "g";
215 }
216
217 void minkmetric::print(std::ostream & os, unsigned upper_precedence) const
218 {
219         debugmsg("minkmetric print",LOGLEVEL_PRINT);
220         os << "eta";
221 }
222
223 void tensepsilon::print(std::ostream & os, unsigned upper_precedence) const
224 {
225         debugmsg("tensepsilon print",LOGLEVEL_PRINT);
226         os << "eps";
227 }
228
229 /** Automatic symbolic evaluation of an indexed delta tensor. */
230 ex tensdelta::eval_indexed(const basic & i) const
231 {
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));
235
236         const idx & i1 = ex_to_idx(i.op(1));
237         const idx & i2 = ex_to_idx(i.op(2));
238
239         // Trace of delta tensor is the dimension of the space
240         if (is_dummy_pair(i1, i2))
241                 return i1.get_dim();
242
243         // No further simplifications
244         return i.hold();
245 }
246
247 /** Automatic symbolic evaluation of an indexed metric tensor. */
248 ex tensmetric::eval_indexed(const basic & i) const
249 {
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));
255
256         const varidx & i1 = ex_to_varidx(i.op(1));
257         const varidx & i2 = ex_to_varidx(i.op(2));
258
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);
263
264         // No further simplifications
265         return i.hold();
266 }
267
268 /** Automatic symbolic evaluation of an indexed Lorentz metric tensor. */
269 ex minkmetric::eval_indexed(const basic & i) const
270 {
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));
276
277         const varidx & i1 = ex_to_varidx(i.op(1));
278         const varidx & i2 = ex_to_varidx(i.op(2));
279
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();
283                 if (n1 != n2)
284                         return _ex0();
285                 else if (n1 == 0)
286                         return pos_sig ? _ex_1() : _ex1();
287                 else
288                         return pos_sig ? _ex1() : _ex_1();
289         }
290
291         // Perform the usual evaluations of a metric tensor
292         return inherited::eval_indexed(i);
293 }
294
295 /** Automatic symbolic evaluation of an indexed epsilon tensor. */
296 ex tensepsilon::eval_indexed(const basic & i) const
297 {
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));
301
302         // Convolutions are zero
303         if (static_cast<const indexed &>(i).get_dummy_indices().size() != 0)
304                 return _ex0();
305
306         // Numeric evaluation
307         if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
308
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)
311                 vector<int> v;
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);
316
317                 // In a Minkowski space, check for covariant indices
318                 if (minkowski) {
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);
326                                         else
327                                                 sign = (pos_sig ? sign : -sign);
328                         }
329                 }
330
331                 return sign;
332         }
333
334         // No further simplifications
335         return i.hold();
336 }
337
338 /** Contraction of an indexed delta tensor with something else. */
339 bool tensdelta::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
340 {
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));
345
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;
350
351 again:
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)) {
356
357                                 // Contraction found, remove delta tensor and substitute
358                                 // index in second object
359                                 *self = _ex1();
360                                 *other = other->subs(other_idx == *free_idx);
361                                 return true;
362                         }
363                 }
364         }
365
366         if (!first_index_tried) {
367
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;
372                 goto again;
373         }
374
375         return false;
376 }
377
378 /** Contraction of an indexed metric tensor with something else. */
379 bool tensmetric::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
380 {
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));
385
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))
389                 return false;
390
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;
395
396 again:
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)) {
401
402                                 // Contraction found, remove metric tensor and substitute
403                                 // index in second object
404                                 *self = _ex1();
405                                 *other = other->subs(other_idx == *free_idx);
406                                 return true;
407                         }
408                 }
409         }
410
411         if (!first_index_tried) {
412
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;
417                 goto again;
418         }
419
420         return false;
421 }
422
423 //////////
424 // global functions
425 //////////
426
427 ex delta_tensor(const ex & i1, const ex & i2)
428 {
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"));
431
432         return indexed(tensdelta(), indexed::symmetric, i1, i2);
433 }
434
435 ex metric_tensor(const ex & i1, const ex & i2)
436 {
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"));
439
440         return indexed(tensmetric(), i1, i2);
441 }
442
443 ex lorentz_g(const ex & i1, const ex & i2, bool pos_sig)
444 {
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"));
447
448         return indexed(minkmetric(pos_sig), indexed::symmetric, i1, i2);
449 }
450
451 ex epsilon_tensor(const ex & i1, const ex & i2)
452 {
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"));
455
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"));
461
462         return indexed(tensepsilon(), indexed::antisymmetric, i1, i2);
463 }
464
465 ex epsilon_tensor(const ex & i1, const ex & i2, const ex & i3)
466 {
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"));
469
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"));
475
476         return indexed(tensepsilon(), indexed::antisymmetric, i1, i2, i3);
477 }
478
479 ex lorentz_eps(const ex & i1, const ex & i2, const ex & i3, const ex & i4, bool pos_sig)
480 {
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"));
483
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"));
489
490         return indexed(tensepsilon(true, pos_sig), indexed::antisymmetric, i1, i2, i3, i4);
491 }
492
493 } // namespace GiNaC