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