]> www.ginac.de Git - ginac.git/blob - ginac/tensor.cpp
final (?) update for 1.1
[ginac.git] / ginac / tensor.cpp
1 /** @file tensor.cpp
2  *
3  *  Implementation of GiNaC's special tensors. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2003 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 <iostream>
24 #include <stdexcept>
25 #include <vector>
26
27 #include "tensor.h"
28 #include "idx.h"
29 #include "indexed.h"
30 #include "symmetry.h"
31 #include "relational.h"
32 #include "operators.h"
33 #include "lst.h"
34 #include "numeric.h"
35 #include "matrix.h"
36 #include "print.h"
37 #include "archive.h"
38 #include "utils.h"
39
40 namespace GiNaC {
41
42 GINAC_IMPLEMENT_REGISTERED_CLASS(tensor, basic)
43 GINAC_IMPLEMENT_REGISTERED_CLASS(tensdelta, tensor)
44 GINAC_IMPLEMENT_REGISTERED_CLASS(tensmetric, tensor)
45 GINAC_IMPLEMENT_REGISTERED_CLASS(minkmetric, tensmetric)
46 GINAC_IMPLEMENT_REGISTERED_CLASS(spinmetric, tensmetric)
47 GINAC_IMPLEMENT_REGISTERED_CLASS(tensepsilon, tensor)
48
49 //////////
50 // default ctor, dtor, copy ctor, assignment operator and helpers
51 //////////
52
53 DEFAULT_CTORS(tensor)
54 DEFAULT_CTORS(tensdelta)
55 DEFAULT_CTORS(tensmetric)
56 DEFAULT_COPY(spinmetric)
57 DEFAULT_DESTROY(spinmetric)
58 DEFAULT_DESTROY(minkmetric)
59 DEFAULT_DESTROY(tensepsilon)
60
61 minkmetric::minkmetric() : pos_sig(false)
62 {
63         tinfo_key = TINFO_minkmetric;
64 }
65
66 spinmetric::spinmetric()
67 {
68         tinfo_key = TINFO_spinmetric;
69 }
70
71 minkmetric::minkmetric(bool ps) : pos_sig(ps)
72 {
73         tinfo_key = TINFO_minkmetric;
74 }
75
76 void minkmetric::copy(const minkmetric & other)
77 {
78         inherited::copy(other);
79         pos_sig = other.pos_sig;
80 }
81
82 tensepsilon::tensepsilon() : minkowski(false), pos_sig(false)
83 {
84         tinfo_key = TINFO_tensepsilon;
85 }
86
87 tensepsilon::tensepsilon(bool mink, bool ps) : minkowski(mink), pos_sig(ps)
88 {
89         tinfo_key = TINFO_tensepsilon;
90 }
91
92 void tensepsilon::copy(const tensepsilon & other)
93 {
94         inherited::copy(other);
95         minkowski = other.minkowski;
96         pos_sig = other.pos_sig;
97 }
98
99 //////////
100 // archiving
101 //////////
102
103 DEFAULT_ARCHIVING(tensor)
104 DEFAULT_ARCHIVING(tensdelta)
105 DEFAULT_ARCHIVING(tensmetric)
106 DEFAULT_ARCHIVING(spinmetric)
107 DEFAULT_UNARCHIVE(minkmetric)
108 DEFAULT_UNARCHIVE(tensepsilon)
109
110 minkmetric::minkmetric(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
111 {
112         n.find_bool("pos_sig", pos_sig);
113 }
114
115 void minkmetric::archive(archive_node &n) const
116 {
117         inherited::archive(n);
118         n.add_bool("pos_sig", pos_sig);
119 }
120
121 tensepsilon::tensepsilon(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
122 {
123         n.find_bool("minkowski", minkowski);
124         n.find_bool("pos_sig", pos_sig);
125 }
126
127 void tensepsilon::archive(archive_node &n) const
128 {
129         inherited::archive(n);
130         n.add_bool("minkowski", minkowski);
131         n.add_bool("pos_sig", pos_sig);
132 }
133
134 //////////
135 // functions overriding virtual functions from base classes
136 //////////
137
138 DEFAULT_COMPARE(tensor)
139 DEFAULT_COMPARE(tensdelta)
140 DEFAULT_COMPARE(tensmetric)
141 DEFAULT_COMPARE(spinmetric)
142
143 int minkmetric::compare_same_type(const basic & other) const
144 {
145         GINAC_ASSERT(is_a<minkmetric>(other));
146         const minkmetric &o = static_cast<const minkmetric &>(other);
147
148         if (pos_sig != o.pos_sig)
149                 return pos_sig ? -1 : 1;
150         else
151                 return inherited::compare_same_type(other);
152 }
153
154 int tensepsilon::compare_same_type(const basic & other) const
155 {
156         GINAC_ASSERT(is_a<tensepsilon>(other));
157         const tensepsilon &o = static_cast<const tensepsilon &>(other);
158
159         if (minkowski != o.minkowski)
160                 return minkowski ? -1 : 1;
161         else if (pos_sig != o.pos_sig)
162                 return pos_sig ? -1 : 1;
163         else
164                 return inherited::compare_same_type(other);
165 }
166
167 DEFAULT_PRINT_LATEX(tensdelta, "delta", "\\delta")
168 DEFAULT_PRINT(tensmetric, "g")
169 DEFAULT_PRINT_LATEX(minkmetric, "eta", "\\eta")
170 DEFAULT_PRINT_LATEX(spinmetric, "eps", "\\varepsilon")
171 DEFAULT_PRINT_LATEX(tensepsilon, "eps", "\\varepsilon")
172
173 /** Automatic symbolic evaluation of an indexed delta tensor. */
174 ex tensdelta::eval_indexed(const basic & i) const
175 {
176         GINAC_ASSERT(is_a<indexed>(i));
177         GINAC_ASSERT(i.nops() == 3);
178         GINAC_ASSERT(is_a<tensdelta>(i.op(0)));
179
180         const idx & i1 = ex_to<idx>(i.op(1));
181         const idx & i2 = ex_to<idx>(i.op(2));
182
183         // The dimension of the indices must be equal, otherwise we use the minimal
184         // dimension
185         if (!i1.get_dim().is_equal(i2.get_dim())) {
186                 ex min_dim = i1.minimal_dim(i2);
187                 return i.subs(lst(i1 == i1.replace_dim(min_dim), i2 == i2.replace_dim(min_dim)));
188         }
189
190         // Trace of delta tensor is the (effective) dimension of the space
191         if (is_dummy_pair(i1, i2)) {
192                 try {
193                         return i1.minimal_dim(i2);
194                 } catch (std::exception &e) {
195                         return i.hold();
196                 }
197         }
198
199         // Numeric evaluation
200         if (static_cast<const indexed &>(i).all_index_values_are(info_flags::integer)) {
201                 int n1 = ex_to<numeric>(i1.get_value()).to_int(), n2 = ex_to<numeric>(i2.get_value()).to_int();
202                 if (n1 == n2)
203                         return _ex1;
204                 else
205                         return _ex0;
206         }
207
208         // No further simplifications
209         return i.hold();
210 }
211
212 /** Automatic symbolic evaluation of an indexed metric tensor. */
213 ex tensmetric::eval_indexed(const basic & i) const
214 {
215         GINAC_ASSERT(is_a<indexed>(i));
216         GINAC_ASSERT(i.nops() == 3);
217         GINAC_ASSERT(is_a<tensmetric>(i.op(0)));
218         GINAC_ASSERT(is_a<varidx>(i.op(1)));
219         GINAC_ASSERT(is_a<varidx>(i.op(2)));
220
221         const varidx & i1 = ex_to<varidx>(i.op(1));
222         const varidx & i2 = ex_to<varidx>(i.op(2));
223
224         // The dimension of the indices must be equal, otherwise we use the minimal
225         // dimension
226         if (!i1.get_dim().is_equal(i2.get_dim())) {
227                 ex min_dim = i1.minimal_dim(i2);
228                 return i.subs(lst(i1 == i1.replace_dim(min_dim), i2 == i2.replace_dim(min_dim)));
229         }
230
231         // A metric tensor with one covariant and one contravariant index gets
232         // replaced by a delta tensor
233         if (i1.is_covariant() != i2.is_covariant())
234                 return delta_tensor(i1, i2);
235
236         // No further simplifications
237         return i.hold();
238 }
239
240 /** Automatic symbolic evaluation of an indexed Lorentz metric tensor. */
241 ex minkmetric::eval_indexed(const basic & i) const
242 {
243         GINAC_ASSERT(is_a<indexed>(i));
244         GINAC_ASSERT(i.nops() == 3);
245         GINAC_ASSERT(is_a<minkmetric>(i.op(0)));
246         GINAC_ASSERT(is_a<varidx>(i.op(1)));
247         GINAC_ASSERT(is_a<varidx>(i.op(2)));
248
249         const varidx & i1 = ex_to<varidx>(i.op(1));
250         const varidx & i2 = ex_to<varidx>(i.op(2));
251
252         // Numeric evaluation
253         if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
254                 int n1 = ex_to<numeric>(i1.get_value()).to_int(), n2 = ex_to<numeric>(i2.get_value()).to_int();
255                 if (n1 != n2)
256                         return _ex0;
257                 else if (n1 == 0)
258                         return pos_sig ? _ex_1 : _ex1;
259                 else
260                         return pos_sig ? _ex1 : _ex_1;
261         }
262
263         // Perform the usual evaluations of a metric tensor
264         return inherited::eval_indexed(i);
265 }
266
267 /** Automatic symbolic evaluation of an indexed metric tensor. */
268 ex spinmetric::eval_indexed(const basic & i) const
269 {
270         GINAC_ASSERT(is_a<indexed>(i));
271         GINAC_ASSERT(i.nops() == 3);
272         GINAC_ASSERT(is_a<spinmetric>(i.op(0)));
273         GINAC_ASSERT(is_a<spinidx>(i.op(1)));
274         GINAC_ASSERT(is_a<spinidx>(i.op(2)));
275
276         const spinidx & i1 = ex_to<spinidx>(i.op(1));
277         const spinidx & i2 = ex_to<spinidx>(i.op(2));
278
279         // Convolutions are zero
280         if (!(static_cast<const indexed &>(i).get_dummy_indices().empty()))
281                 return _ex0;
282
283         // Numeric evaluation
284         if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
285                 int n1 = ex_to<numeric>(i1.get_value()).to_int(), n2 = ex_to<numeric>(i2.get_value()).to_int();
286                 if (n1 == n2)
287                         return _ex0;
288                 else if (n1 < n2)
289                         return _ex1;
290                 else
291                         return _ex_1;
292         }
293
294         // No further simplifications
295         return i.hold();
296 }
297
298 /** Automatic symbolic evaluation of an indexed epsilon tensor. */
299 ex tensepsilon::eval_indexed(const basic & i) const
300 {
301         GINAC_ASSERT(is_a<indexed>(i));
302         GINAC_ASSERT(i.nops() > 1);
303         GINAC_ASSERT(is_a<tensepsilon>(i.op(0)));
304
305         // Convolutions are zero
306         if (!(static_cast<const indexed &>(i).get_dummy_indices().empty()))
307                 return _ex0;
308
309         // Numeric evaluation
310         if (static_cast<const indexed &>(i).all_index_values_are(info_flags::nonnegint)) {
311
312                 // Get sign of index permutation (the indices should already be in
313                 // a canonic order but we can't assume what exactly that order is)
314                 std::vector<int> v;
315                 v.reserve(i.nops() - 1);
316                 for (size_t j=1; j<i.nops(); j++)
317                         v.push_back(ex_to<numeric>(ex_to<idx>(i.op(j)).get_value()).to_int());
318                 int sign = permutation_sign(v.begin(), v.end());
319
320                 // In a Minkowski space, check for covariant indices
321                 if (minkowski) {
322                         for (size_t j=1; j<i.nops(); j++) {
323                                 const ex & x = i.op(j);
324                                 if (!is_a<varidx>(x))
325                                         throw(std::runtime_error("indices of epsilon tensor in Minkowski space must be of type varidx"));
326                                 if (ex_to<varidx>(x).is_covariant())
327                                         if (ex_to<idx>(x).get_value().is_zero())
328                                                 sign = (pos_sig ? -sign : sign);
329                                         else
330                                                 sign = (pos_sig ? sign : -sign);
331                         }
332                 }
333
334                 return sign;
335         }
336
337         // No further simplifications
338         return i.hold();
339 }
340
341 bool tensor::replace_contr_index(exvector::iterator self, exvector::iterator other) const
342 {
343         // Try to contract the first index
344         const idx *self_idx = &ex_to<idx>(self->op(1));
345         const idx *free_idx = &ex_to<idx>(self->op(2));
346         bool first_index_tried = false;
347
348 again:
349         if (self_idx->is_symbolic()) {
350                 for (size_t i=1; i<other->nops(); i++) {
351                         const idx &other_idx = ex_to<idx>(other->op(i));
352                         if (is_dummy_pair(*self_idx, other_idx)) {
353
354                                 // Contraction found, remove this tensor and substitute the
355                                 // index in the second object
356                                 try {
357                                         // minimal_dim() throws an exception when index dimensions are not comparable
358                                         ex min_dim = self_idx->minimal_dim(other_idx);
359                                         *other = other->subs(other_idx == free_idx->replace_dim(min_dim));
360                                         *self = _ex1; // *other is assigned first because assigning *self invalidates free_idx
361                                         return true;
362                                 } catch (std::exception &e) {
363                                         return false;
364                                 }
365                         }
366                 }
367         }
368
369         if (!first_index_tried) {
370
371                 // No contraction with the first index found, try the second index
372                 self_idx = &ex_to<idx>(self->op(2));
373                 free_idx = &ex_to<idx>(self->op(1));
374                 first_index_tried = true;
375                 goto again;
376         }
377
378         return false;
379 }
380
381 /** Contraction of an indexed delta tensor with something else. */
382 bool tensdelta::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
383 {
384         GINAC_ASSERT(is_a<indexed>(*self));
385         GINAC_ASSERT(is_a<indexed>(*other));
386         GINAC_ASSERT(self->nops() == 3);
387         GINAC_ASSERT(is_a<tensdelta>(self->op(0)));
388
389         // Replace the dummy index with this tensor's other index and remove
390         // the tensor (this is valid for contractions with all other tensors)
391         return replace_contr_index(self, other);
392 }
393
394 /** Contraction of an indexed metric tensor with something else. */
395 bool tensmetric::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
396 {
397         GINAC_ASSERT(is_a<indexed>(*self));
398         GINAC_ASSERT(is_a<indexed>(*other));
399         GINAC_ASSERT(self->nops() == 3);
400         GINAC_ASSERT(is_a<tensmetric>(self->op(0)));
401
402         // If contracting with the delta tensor, let the delta do it
403         // (don't raise/lower delta indices)
404         if (is_a<tensdelta>(other->op(0)))
405                 return false;
406
407         // Replace the dummy index with this tensor's other index and remove
408         // the tensor
409         return replace_contr_index(self, other);
410 }
411
412 /** Contraction of an indexed spinor metric with something else. */
413 bool spinmetric::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
414 {
415         GINAC_ASSERT(is_a<indexed>(*self));
416         GINAC_ASSERT(is_a<indexed>(*other));
417         GINAC_ASSERT(self->nops() == 3);
418         GINAC_ASSERT(is_a<spinmetric>(self->op(0)));
419
420         // Contractions between spinor metrics
421         if (is_a<spinmetric>(other->op(0))) {
422                 const idx &self_i1 = ex_to<idx>(self->op(1));
423                 const idx &self_i2 = ex_to<idx>(self->op(2));
424                 const idx &other_i1 = ex_to<idx>(other->op(1));
425                 const idx &other_i2 = ex_to<idx>(other->op(2));
426
427                 if (is_dummy_pair(self_i1, other_i1)) {
428                         if (is_dummy_pair(self_i2, other_i2))
429                                 *self = _ex2;
430                         else
431                                 *self = delta_tensor(self_i2, other_i2);
432                         *other = _ex1;
433                         return true;
434                 } else if (is_dummy_pair(self_i1, other_i2)) {
435                         if (is_dummy_pair(self_i2, other_i1))
436                                 *self = _ex_2;
437                         else
438                                 *self = -delta_tensor(self_i2, other_i1);
439                         *other = _ex1;
440                         return true;
441                 } else if (is_dummy_pair(self_i2, other_i1)) {
442                         *self = -delta_tensor(self_i1, other_i2);
443                         *other = _ex1;
444                         return true;
445                 } else if (is_dummy_pair(self_i2, other_i2)) {
446                         *self = delta_tensor(self_i1, other_i1);
447                         *other = _ex1;
448                         return true;
449                 }
450         }
451
452         // If contracting with the delta tensor, let the delta do it
453         // (don't raise/lower delta indices)
454         if (is_a<tensdelta>(other->op(0)))
455                 return false;
456
457         // Try to contract first index
458         const idx *self_idx = &ex_to<idx>(self->op(1));
459         const idx *free_idx = &ex_to<idx>(self->op(2));
460         bool first_index_tried = false;
461         int sign = 1;
462
463 again:
464         if (self_idx->is_symbolic()) {
465                 for (size_t i=1; i<other->nops(); i++) {
466                         const idx &other_idx = ex_to<idx>(other->op(i));
467                         if (is_dummy_pair(*self_idx, other_idx)) {
468
469                                 // Contraction found, remove metric tensor and substitute
470                                 // index in second object (assign *self last because this
471                                 // invalidates free_idx)
472                                 *other = other->subs(other_idx == *free_idx);
473                                 *self = (static_cast<const spinidx *>(self_idx)->is_covariant() ? sign : -sign);
474                                 return true;
475                         }
476                 }
477         }
478
479         if (!first_index_tried) {
480
481                 // No contraction with first index found, try second index
482                 self_idx = &ex_to<idx>(self->op(2));
483                 free_idx = &ex_to<idx>(self->op(1));
484                 first_index_tried = true;
485                 sign = -sign;
486                 goto again;
487         }
488
489         return false;
490 }
491
492 /** Contraction of epsilon tensor with something else. */
493 bool tensepsilon::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const
494 {
495         GINAC_ASSERT(is_a<indexed>(*self));
496         GINAC_ASSERT(is_a<indexed>(*other));
497         GINAC_ASSERT(is_a<tensepsilon>(self->op(0)));
498         size_t num = self->nops() - 1;
499
500         if (is_exactly_a<tensepsilon>(other->op(0)) && num+1 == other->nops()) {
501
502                 // Contraction of two epsilon tensors is a determinant
503                 bool variance = is_a<varidx>(self->op(1));
504                 matrix M(num, num);
505                 for (size_t i=0; i<num; i++) {
506                         for (size_t j=0; j<num; j++) {
507                                 if (minkowski)
508                                         M(i, j) = lorentz_g(self->op(i+1), other->op(j+1), pos_sig);
509                                 else if (variance)
510                                         M(i, j) = metric_tensor(self->op(i+1), other->op(j+1));
511                                 else
512                                         M(i, j) = delta_tensor(self->op(i+1), other->op(j+1));
513                         }
514                 }
515                 int sign = minkowski ? -1 : 1;
516                 *self = sign * M.determinant().simplify_indexed();
517                 *other = _ex1;
518                 return true;
519         }
520
521         return false;
522 }
523
524 //////////
525 // global functions
526 //////////
527
528 ex delta_tensor(const ex & i1, const ex & i2)
529 {
530         if (!is_a<idx>(i1) || !is_a<idx>(i2))
531                 throw(std::invalid_argument("indices of delta tensor must be of type idx"));
532
533         return indexed(tensdelta(), sy_symm(), i1, i2);
534 }
535
536 ex metric_tensor(const ex & i1, const ex & i2)
537 {
538         if (!is_a<varidx>(i1) || !is_a<varidx>(i2))
539                 throw(std::invalid_argument("indices of metric tensor must be of type varidx"));
540
541         return indexed(tensmetric(), sy_symm(), i1, i2);
542 }
543
544 ex lorentz_g(const ex & i1, const ex & i2, bool pos_sig)
545 {
546         if (!is_a<varidx>(i1) || !is_a<varidx>(i2))
547                 throw(std::invalid_argument("indices of metric tensor must be of type varidx"));
548
549         return indexed(minkmetric(pos_sig), sy_symm(), i1, i2);
550 }
551
552 ex spinor_metric(const ex & i1, const ex & i2)
553 {
554         if (!is_a<spinidx>(i1) || !is_a<spinidx>(i2))
555                 throw(std::invalid_argument("indices of spinor metric must be of type spinidx"));
556         if (!ex_to<idx>(i1).get_dim().is_equal(2) || !ex_to<idx>(i2).get_dim().is_equal(2))
557                 throw(std::runtime_error("index dimension for spinor metric must be 2"));
558
559         return indexed(spinmetric(), sy_anti(), i1, i2);
560 }
561
562 ex epsilon_tensor(const ex & i1, const ex & i2)
563 {
564         if (!is_a<idx>(i1) || !is_a<idx>(i2))
565                 throw(std::invalid_argument("indices of epsilon tensor must be of type idx"));
566
567         ex dim = ex_to<idx>(i1).get_dim();
568         if (!dim.is_equal(ex_to<idx>(i2).get_dim()))
569                 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
570         if (!ex_to<idx>(i1).get_dim().is_equal(_ex2))
571                 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
572
573         return indexed(tensepsilon(), sy_anti(), i1, i2);
574 }
575
576 ex epsilon_tensor(const ex & i1, const ex & i2, const ex & i3)
577 {
578         if (!is_a<idx>(i1) || !is_a<idx>(i2) || !is_a<idx>(i3))
579                 throw(std::invalid_argument("indices of epsilon tensor must be of type idx"));
580
581         ex dim = ex_to<idx>(i1).get_dim();
582         if (!dim.is_equal(ex_to<idx>(i2).get_dim()) || !dim.is_equal(ex_to<idx>(i3).get_dim()))
583                 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
584         if (!ex_to<idx>(i1).get_dim().is_equal(_ex3))
585                 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
586
587         return indexed(tensepsilon(), sy_anti(), i1, i2, i3);
588 }
589
590 ex lorentz_eps(const ex & i1, const ex & i2, const ex & i3, const ex & i4, bool pos_sig)
591 {
592         if (!is_a<varidx>(i1) || !is_a<varidx>(i2) || !is_a<varidx>(i3) || !is_a<varidx>(i4))
593                 throw(std::invalid_argument("indices of Lorentz epsilon tensor must be of type varidx"));
594
595         ex dim = ex_to<idx>(i1).get_dim();
596         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()))
597                 throw(std::invalid_argument("all indices of epsilon tensor must have the same dimension"));
598         if (!ex_to<idx>(i1).get_dim().is_equal(_ex4))
599                 throw(std::runtime_error("index dimension of epsilon tensor must match number of indices"));
600
601         return indexed(tensepsilon(true, pos_sig), sy_anti(), i1, i2, i3, i4);
602 }
603
604 } // namespace GiNaC