]> www.ginac.de Git - ginac.git/blob - ginac/tensor.cpp
- added skeleton implementation of color and clifford classes (don't bother
[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         // No further simplifications
183         return i.hold();
184 }
185
186 /** Automatic symbolic evaluation of an indexed metric tensor. */
187 ex tensmetric::eval_indexed(const basic & i) const
188 {
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));
194
195         const varidx & i1 = ex_to_varidx(i.op(1));
196         const varidx & i2 = ex_to_varidx(i.op(2));
197
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);
202
203         // No further simplifications
204         return i.hold();
205 }
206
207 /** Automatic symbolic evaluation of an indexed Lorentz metric tensor. */
208 ex minkmetric::eval_indexed(const basic & i) const
209 {
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));
215
216         const varidx & i1 = ex_to_varidx(i.op(1));
217         const varidx & i2 = ex_to_varidx(i.op(2));
218
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();
222                 if (n1 != n2)
223                         return _ex0();
224                 else if (n1 == 0)
225                         return pos_sig ? _ex_1() : _ex1();
226                 else
227                         return pos_sig ? _ex1() : _ex_1();
228         }
229
230         // Perform the usual evaluations of a metric tensor
231         return inherited::eval_indexed(i);
232 }
233
234 /** Automatic symbolic evaluation of an indexed epsilon tensor. */
235 ex tensepsilon::eval_indexed(const basic & i) const
236 {
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));
240
241         // Convolutions are zero
242         if (static_cast<const indexed &>(i).get_dummy_indices().size() != 0)
243                 return _ex0();
244
245         // Numeric evaluation
246         if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
247
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)
250                 std::vector<int> v;
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);
255
256                 // In a Minkowski space, check for covariant indices
257                 if (minkowski) {
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);
265                                         else
266                                                 sign = (pos_sig ? sign : -sign);
267                         }
268                 }
269
270                 return sign;
271         }
272
273         // No further simplifications
274         return i.hold();
275 }
276
277 /** Contraction of an indexed delta tensor with something else. */
278 bool tensdelta::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
279 {
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));
284
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;
289
290 again:
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)) {
295
296                                 // Contraction found, remove delta tensor and substitute
297                                 // index in second object
298                                 *self = _ex1();
299                                 *other = other->subs(other_idx == *free_idx);
300                                 return true;
301                         }
302                 }
303         }
304
305         if (!first_index_tried) {
306
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;
311                 goto again;
312         }
313
314         return false;
315 }
316
317 /** Contraction of an indexed metric tensor with something else. */
318 bool tensmetric::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
319 {
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));
324
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))
328                 return false;
329
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;
334
335 again:
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)) {
340
341                                 // Contraction found, remove metric tensor and substitute
342                                 // index in second object
343                                 *self = _ex1();
344                                 *other = other->subs(other_idx == *free_idx);
345                                 return true;
346                         }
347                 }
348         }
349
350         if (!first_index_tried) {
351
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;
356                 goto again;
357         }
358
359         return false;
360 }
361
362 //////////
363 // global functions
364 //////////
365
366 ex delta_tensor(const ex & i1, const ex & i2)
367 {
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"));
370
371         return indexed(tensdelta(), indexed::symmetric, i1, i2);
372 }
373
374 ex metric_tensor(const ex & i1, const ex & i2)
375 {
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"));
378
379         return indexed(tensmetric(), indexed::symmetric, i1, i2);
380 }
381
382 ex lorentz_g(const ex & i1, const ex & i2, bool pos_sig)
383 {
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"));
386
387         return indexed(minkmetric(pos_sig), indexed::symmetric, i1, i2);
388 }
389
390 ex epsilon_tensor(const ex & i1, const ex & i2)
391 {
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"));
394
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"));
400
401         return indexed(tensepsilon(), indexed::antisymmetric, i1, i2);
402 }
403
404 ex epsilon_tensor(const ex & i1, const ex & i2, const ex & i3)
405 {
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"));
408
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"));
414
415         return indexed(tensepsilon(), indexed::antisymmetric, i1, i2, i3);
416 }
417
418 ex lorentz_eps(const ex & i1, const ex & i2, const ex & i3, const ex & i4, bool pos_sig)
419 {
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"));
422
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"));
428
429         return indexed(tensepsilon(true, pos_sig), indexed::antisymmetric, i1, i2, i3, i4);
430 }
431
432 } // namespace GiNaC