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