]> www.ginac.de Git - ginac.git/blob - ginac/tensor.cpp
- subs() can be used to substitute functions, tensors and indexed objects
[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 "lst.h"
31 #include "numeric.h"
32 #include "archive.h"
33 #include "utils.h"
34 #include "debugmsg.h"
35
36 namespace GiNaC {
37
38 GINAC_IMPLEMENT_REGISTERED_CLASS(tensor, basic)
39 GINAC_IMPLEMENT_REGISTERED_CLASS(tensdelta, tensor)
40 GINAC_IMPLEMENT_REGISTERED_CLASS(tensmetric, tensor)
41 GINAC_IMPLEMENT_REGISTERED_CLASS(minkmetric, tensmetric)
42 GINAC_IMPLEMENT_REGISTERED_CLASS(tensepsilon, tensor)
43
44 //////////
45 // default constructor, destructor, copy constructor assignment operator and helpers
46 //////////
47
48 tensor::tensor(unsigned ti) : inherited(ti)
49 {
50         debugmsg("tensor constructor from unsigned", LOGLEVEL_CONSTRUCT); \
51 }
52
53 DEFAULT_CTORS(tensor)
54 DEFAULT_CTORS(tensdelta)
55 DEFAULT_CTORS(tensmetric)
56 DEFAULT_DESTROY(minkmetric)
57 DEFAULT_DESTROY(tensepsilon)
58
59 minkmetric::minkmetric() : pos_sig(false)
60 {
61         debugmsg("minkmetric default constructor", LOGLEVEL_CONSTRUCT);
62         tinfo_key = TINFO_minkmetric;
63 }
64
65 minkmetric::minkmetric(bool ps) : pos_sig(ps)
66 {
67         debugmsg("minkmetric constructor from bool", LOGLEVEL_CONSTRUCT);
68         tinfo_key = TINFO_minkmetric;
69 }
70
71 void minkmetric::copy(const minkmetric & other)
72 {
73         inherited::copy(other);
74         pos_sig = other.pos_sig;
75 }
76
77 tensepsilon::tensepsilon() : minkowski(false), pos_sig(false)
78 {
79         debugmsg("tensepsilon default constructor", LOGLEVEL_CONSTRUCT);
80         tinfo_key = TINFO_tensepsilon;
81 }
82
83 tensepsilon::tensepsilon(bool mink, bool ps) : minkowski(mink), pos_sig(ps)
84 {
85         debugmsg("tensepsilon constructor from bool,bool", LOGLEVEL_CONSTRUCT);
86         tinfo_key = TINFO_tensepsilon;
87 }
88
89 void tensepsilon::copy(const tensepsilon & other)
90 {
91         inherited::copy(other);
92         minkowski = other.minkowski;
93         pos_sig = other.pos_sig;
94 }
95
96 //////////
97 // archiving
98 //////////
99
100 DEFAULT_ARCHIVING(tensor)
101 DEFAULT_ARCHIVING(tensdelta)
102 DEFAULT_ARCHIVING(tensmetric)
103 DEFAULT_UNARCHIVE(minkmetric)
104 DEFAULT_UNARCHIVE(tensepsilon)
105
106 minkmetric::minkmetric(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
107 {
108         debugmsg("minkmetric constructor from archive_node", LOGLEVEL_CONSTRUCT);
109         n.find_bool("pos_sig", pos_sig);
110 }
111
112 void minkmetric::archive(archive_node &n) const
113 {
114         inherited::archive(n);
115         n.add_bool("pos_sig", pos_sig);
116 }
117
118 tensepsilon::tensepsilon(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
119 {
120         debugmsg("tensepsilon constructor from archive_node", LOGLEVEL_CONSTRUCT);
121         n.find_bool("minkowski", minkowski);
122         n.find_bool("pos_sig", pos_sig);
123 }
124
125 void tensepsilon::archive(archive_node &n) const
126 {
127         inherited::archive(n);
128         n.add_bool("minkowski", minkowski);
129         n.add_bool("pos_sig", pos_sig);
130 }
131
132 //////////
133 // functions overriding virtual functions from bases classes
134 //////////
135
136 DEFAULT_COMPARE(tensor)
137 DEFAULT_COMPARE(tensdelta)
138 DEFAULT_COMPARE(tensmetric)
139
140 int minkmetric::compare_same_type(const basic & other) const
141 {
142         GINAC_ASSERT(is_of_type(other, minkmetric));
143         const minkmetric &o = static_cast<const minkmetric &>(other);
144
145         if (pos_sig != o.pos_sig)
146                 return pos_sig ? -1 : 1;
147         else
148                 return inherited::compare_same_type(other);
149 }
150
151 int tensepsilon::compare_same_type(const basic & other) const
152 {
153         GINAC_ASSERT(is_of_type(other, tensepsilon));
154         const tensepsilon &o = static_cast<const tensepsilon &>(other);
155
156         if (minkowski != o.minkowski)
157                 return minkowski ? -1 : 1;
158         else if (pos_sig != o.pos_sig)
159                 return pos_sig ? -1 : 1;
160         else
161                 return inherited::compare_same_type(other);
162 }
163
164 DEFAULT_PRINT(tensdelta, "delta")
165 DEFAULT_PRINT(tensmetric, "g")
166 DEFAULT_PRINT(minkmetric, "eta")
167 DEFAULT_PRINT(tensepsilon, "eps")
168
169 ex tensor::subs(const lst & ls, const lst & lr) const
170 {
171         GINAC_ASSERT(ls.nops() == lr.nops());
172
173         for (unsigned i=0; i<ls.nops(); i++) {
174                 if (is_ex_of_type(ls.op(i), tensor) &&
175                     compare_same_type(ex_to_tensor(ls.op(i)))==0)
176                         return lr.op(i);
177         }
178         return *this;
179 }
180
181 /** Automatic symbolic evaluation of an indexed delta tensor. */
182 ex tensdelta::eval_indexed(const basic & i) const
183 {
184         GINAC_ASSERT(is_of_type(i, indexed));
185         GINAC_ASSERT(i.nops() == 3);
186         GINAC_ASSERT(is_ex_of_type(i.op(0), tensdelta));
187
188         const idx & i1 = ex_to_idx(i.op(1));
189         const idx & i2 = ex_to_idx(i.op(2));
190
191         // Trace of delta tensor is the dimension of the space
192         if (is_dummy_pair(i1, i2))
193                 return i1.get_dim();
194
195         // Numeric evaluation
196         if (static_cast<const indexed &>(i).all_index_values_are(info_flags::integer)) {
197                 int n1 = ex_to_numeric(i1.get_value()).to_int(), n2 = ex_to_numeric(i2.get_value()).to_int();
198                 if (n1 == n2)
199                         return _ex1();
200                 else
201                         return _ex0();
202         }
203
204         // No further simplifications
205         return i.hold();
206 }
207
208 /** Automatic symbolic evaluation of an indexed metric tensor. */
209 ex tensmetric::eval_indexed(const basic & i) const
210 {
211         GINAC_ASSERT(is_of_type(i, indexed));
212         GINAC_ASSERT(i.nops() == 3);
213         GINAC_ASSERT(is_ex_of_type(i.op(0), tensmetric));
214         GINAC_ASSERT(is_ex_of_type(i.op(1), varidx));
215         GINAC_ASSERT(is_ex_of_type(i.op(2), varidx));
216
217         const varidx & i1 = ex_to_varidx(i.op(1));
218         const varidx & i2 = ex_to_varidx(i.op(2));
219
220         // A metric tensor with one covariant and one contravariant index gets
221         // replaced by a delta tensor
222         if (i1.is_covariant() != i2.is_covariant())
223                 return delta_tensor(i1, i2);
224
225         // No further simplifications
226         return i.hold();
227 }
228
229 /** Automatic symbolic evaluation of an indexed Lorentz metric tensor. */
230 ex minkmetric::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), minkmetric));
235         GINAC_ASSERT(is_ex_of_type(i.op(1), varidx));
236         GINAC_ASSERT(is_ex_of_type(i.op(2), varidx));
237
238         const varidx & i1 = ex_to_varidx(i.op(1));
239         const varidx & i2 = ex_to_varidx(i.op(2));
240
241         // Numeric evaluation
242         if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
243                 int n1 = ex_to_numeric(i1.get_value()).to_int(), n2 = ex_to_numeric(i2.get_value()).to_int();
244                 if (n1 != n2)
245                         return _ex0();
246                 else if (n1 == 0)
247                         return pos_sig ? _ex_1() : _ex1();
248                 else
249                         return pos_sig ? _ex1() : _ex_1();
250         }
251
252         // Perform the usual evaluations of a metric tensor
253         return inherited::eval_indexed(i);
254 }
255
256 /** Automatic symbolic evaluation of an indexed epsilon tensor. */
257 ex tensepsilon::eval_indexed(const basic & i) const
258 {
259         GINAC_ASSERT(is_of_type(i, indexed));
260         GINAC_ASSERT(i.nops() > 1);
261         GINAC_ASSERT(is_ex_of_type(i.op(0), tensepsilon));
262
263         // Convolutions are zero
264         if (static_cast<const indexed &>(i).get_dummy_indices().size() != 0)
265                 return _ex0();
266
267         // Numeric evaluation
268         if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
269
270                 // Get sign of index permutation (the indices should already be in
271                 // a canonic order but we can't assume what exactly that order is)
272                 std::vector<int> v;
273                 v.reserve(i.nops() - 1);
274                 for (unsigned j=1; j<i.nops(); j++)
275                         v.push_back(ex_to_numeric(ex_to_idx(i.op(j)).get_value()).to_int());
276                 int sign = permutation_sign(v);
277
278                 // In a Minkowski space, check for covariant indices
279                 if (minkowski) {
280                         for (unsigned j=1; j<i.nops(); j++) {
281                                 const ex & x = i.op(j);
282                                 if (!is_ex_of_type(x, varidx))
283                                         throw(std::runtime_error("indices of epsilon tensor in Minkowski space must be of type varidx"));
284                                 if (ex_to_varidx(x).is_covariant())
285                                         if (ex_to_idx(x).get_value().is_zero())
286                                                 sign = (pos_sig ? -sign : sign);
287                                         else
288                                                 sign = (pos_sig ? sign : -sign);
289                         }
290                 }
291
292                 return sign;
293         }
294
295         // No further simplifications
296         return i.hold();
297 }
298
299 /** Contraction of an indexed delta tensor with something else. */
300 bool tensdelta::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
301 {
302         GINAC_ASSERT(is_ex_of_type(*self, indexed));
303         GINAC_ASSERT(is_ex_of_type(*other, indexed));
304         GINAC_ASSERT(self->nops() == 3);
305         GINAC_ASSERT(is_ex_of_type(self->op(0), tensdelta));
306
307         // Try to contract first index
308         const idx *self_idx = &ex_to_idx(self->op(1));
309         const idx *free_idx = &ex_to_idx(self->op(2));
310         bool first_index_tried = false;
311
312 again:
313         if (self_idx->is_symbolic()) {
314                 for (int i=1; i<other->nops(); i++) {
315                         const idx &other_idx = ex_to_idx(other->op(i));
316                         if (is_dummy_pair(*self_idx, other_idx)) {
317
318                                 // Contraction found, remove delta tensor and substitute
319                                 // index in second object
320                                 *self = _ex1();
321                                 *other = other->subs(other_idx == *free_idx);
322                                 return true;
323                         }
324                 }
325         }
326
327         if (!first_index_tried) {
328
329                 // No contraction with first index found, try second index
330                 self_idx = &ex_to_idx(self->op(2));
331                 free_idx = &ex_to_idx(self->op(1));
332                 first_index_tried = true;
333                 goto again;
334         }
335
336         return false;
337 }
338
339 /** Contraction of an indexed metric tensor with something else. */
340 bool tensmetric::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
341 {
342         GINAC_ASSERT(is_ex_of_type(*self, indexed));
343         GINAC_ASSERT(is_ex_of_type(*other, indexed));
344         GINAC_ASSERT(self->nops() == 3);
345         GINAC_ASSERT(is_ex_of_type(self->op(0), tensmetric));
346
347         // If contracting with the delta tensor, let the delta do it
348         // (don't raise/lower delta indices)
349         if (is_ex_of_type(other->op(0), tensdelta))
350                 return false;
351
352         // Try to contract first index
353         const idx *self_idx = &ex_to_idx(self->op(1));
354         const idx *free_idx = &ex_to_idx(self->op(2));
355         bool first_index_tried = false;
356
357 again:
358         if (self_idx->is_symbolic()) {
359                 for (int i=1; i<other->nops(); i++) {
360                         const idx &other_idx = ex_to_idx(other->op(i));
361                         if (is_dummy_pair(*self_idx, other_idx)) {
362
363                                 // Contraction found, remove metric tensor and substitute
364                                 // index in second object
365                                 *self = _ex1();
366                                 *other = other->subs(other_idx == *free_idx);
367                                 return true;
368                         }
369                 }
370         }
371
372         if (!first_index_tried) {
373
374                 // No contraction with first index found, try second index
375                 self_idx = &ex_to_idx(self->op(2));
376                 free_idx = &ex_to_idx(self->op(1));
377                 first_index_tried = true;
378                 goto again;
379         }
380
381         return false;
382 }
383
384 //////////
385 // global functions
386 //////////
387
388 ex delta_tensor(const ex & i1, const ex & i2)
389 {
390         if (!is_ex_of_type(i1, idx) || !is_ex_of_type(i2, idx))
391                 throw(std::invalid_argument("indices of delta tensor must be of type idx"));
392
393         return indexed(tensdelta(), indexed::symmetric, i1, i2);
394 }
395
396 ex metric_tensor(const ex & i1, const ex & i2)
397 {
398         if (!is_ex_of_type(i1, varidx) || !is_ex_of_type(i2, varidx))
399                 throw(std::invalid_argument("indices of metric tensor must be of type varidx"));
400
401         return indexed(tensmetric(), indexed::symmetric, i1, i2);
402 }
403
404 ex lorentz_g(const ex & i1, const ex & i2, bool pos_sig)
405 {
406         if (!is_ex_of_type(i1, varidx) || !is_ex_of_type(i2, varidx))
407                 throw(std::invalid_argument("indices of metric tensor must be of type varidx"));
408
409         return indexed(minkmetric(pos_sig), indexed::symmetric, i1, i2);
410 }
411
412 ex epsilon_tensor(const ex & i1, const ex & i2)
413 {
414         if (!is_ex_of_type(i1, idx) || !is_ex_of_type(i2, idx))
415                 throw(std::invalid_argument("indices of epsilon tensor must be of type idx"));
416
417         ex dim = ex_to_idx(i1).get_dim();
418         if (!dim.is_equal(ex_to_idx(i2).get_dim()))
419                 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
420         if (!ex_to_idx(i1).get_dim().is_equal(_ex2()))
421                 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
422
423         return indexed(tensepsilon(), indexed::antisymmetric, i1, i2);
424 }
425
426 ex epsilon_tensor(const ex & i1, const ex & i2, const ex & i3)
427 {
428         if (!is_ex_of_type(i1, idx) || !is_ex_of_type(i2, idx) || !is_ex_of_type(i3, idx))
429                 throw(std::invalid_argument("indices of epsilon tensor must be of type idx"));
430
431         ex dim = ex_to_idx(i1).get_dim();
432         if (!dim.is_equal(ex_to_idx(i2).get_dim()) || !dim.is_equal(ex_to_idx(i3).get_dim()))
433                 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
434         if (!ex_to_idx(i1).get_dim().is_equal(_ex3()))
435                 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
436
437         return indexed(tensepsilon(), indexed::antisymmetric, i1, i2, i3);
438 }
439
440 ex lorentz_eps(const ex & i1, const ex & i2, const ex & i3, const ex & i4, bool pos_sig)
441 {
442         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))
443                 throw(std::invalid_argument("indices of Lorentz epsilon tensor must be of type varidx"));
444
445         ex dim = ex_to_idx(i1).get_dim();
446         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()))
447                 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
448         if (!ex_to_idx(i1).get_dim().is_equal(_ex4()))
449                 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
450
451         return indexed(tensepsilon(true, pos_sig), indexed::antisymmetric, i1, i2, i3, i4);
452 }
453
454 } // namespace GiNaC