]> www.ginac.de Git - ginac.git/blob - check/exam_indexed.cpp
Fix most remaining GCC compiler warnings.
[ginac.git] / check / exam_indexed.cpp
1 /** @file exam_indexed.cpp
2  *
3  *  Here we test manipulations on GiNaC's indexed objects. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2015 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  */
22
23 #include "ginac.h"
24 using namespace GiNaC;
25
26 #include <iostream>
27 using namespace std;
28
29 static unsigned check_equal(const ex &e1, const ex &e2)
30 {
31         ex e = e1 - e2;
32         if (!e.is_zero()) {
33                 clog << e1 << "-" << e2 << " erroneously returned "
34                      << e << " instead of 0" << endl;
35                 return 1;
36         }
37         return 0;
38 }
39
40 static unsigned check_equal_simplify(const ex &e1, const ex &e2)
41 {
42         ex e = simplify_indexed(e1) - e2;
43         if (!e.is_zero()) {
44                 clog << "simplify_indexed(" << e1 << ")-" << e2 << " erroneously returned "
45                      << e << " instead of 0" << endl;
46                 return 1;
47         }
48         return 0;
49 }
50
51 static unsigned check_equal_simplify(const ex &e1, const ex &e2, const scalar_products &sp)
52 {
53         ex e = simplify_indexed(e1, sp) - e2;
54         if (!e.is_zero()) {
55                 clog << "simplify_indexed(" << e1 << ")-" << e2 << " erroneously returned "
56                      << e << " instead of 0" << endl;
57                 return 1;
58         }
59         return 0;
60 }
61
62 static unsigned delta_check()
63 {
64         // checks identities of the delta tensor
65
66         unsigned result = 0;
67
68         symbol s_i("i"), s_j("j"), s_k("k");
69         idx i(s_i, 3), j(s_j, 3), k(s_k, 3);
70         symbol A("A");
71
72         // symmetry
73         result += check_equal(delta_tensor(i, j), delta_tensor(j, i));
74
75         // trace = dimension of index space
76         result += check_equal(delta_tensor(i, i), 3);
77         result += check_equal_simplify(delta_tensor(i, j) * delta_tensor(i, j), 3);
78
79         // contraction with delta tensor
80         result += check_equal_simplify(delta_tensor(i, j) * indexed(A, k), delta_tensor(i, j) * indexed(A, k));
81         result += check_equal_simplify(delta_tensor(i, j) * indexed(A, j), indexed(A, i));
82         result += check_equal_simplify(delta_tensor(i, j) * indexed(A, i), indexed(A, j));
83         result += check_equal_simplify(delta_tensor(i, j) * delta_tensor(j, k) * indexed(A, i), indexed(A, k));
84
85         return result;
86 }
87
88 static unsigned metric_check()
89 {
90         // checks identities of the metric tensor
91
92         unsigned result = 0;
93
94         symbol s_mu("mu"), s_nu("nu"), s_rho("rho"), s_sigma("sigma");
95         varidx mu(s_mu, 4), nu(s_nu, 4), rho(s_rho, 4), sigma(s_sigma, 4);
96         symbol A("A");
97
98         // becomes delta tensor if indices have opposite variance
99         result += check_equal(metric_tensor(mu, nu.toggle_variance()), delta_tensor(mu, nu.toggle_variance()));
100
101         // scalar contraction = dimension of index space
102         result += check_equal(metric_tensor(mu, mu.toggle_variance()), 4);
103         result += check_equal_simplify(metric_tensor(mu, nu) * metric_tensor(mu.toggle_variance(), nu.toggle_variance()), 4);
104
105         // contraction with metric tensor
106         result += check_equal_simplify(metric_tensor(mu, nu) * indexed(A, nu), metric_tensor(mu, nu) * indexed(A, nu));
107         result += check_equal_simplify(metric_tensor(mu, nu) * indexed(A, nu.toggle_variance()), indexed(A, mu));
108         result += check_equal_simplify(metric_tensor(mu, nu) * indexed(A, mu.toggle_variance()), indexed(A, nu));
109         result += check_equal_simplify(metric_tensor(mu, nu) * metric_tensor(mu.toggle_variance(), rho.toggle_variance()) * indexed(A, nu.toggle_variance()), indexed(A, rho.toggle_variance()));
110         result += check_equal_simplify(metric_tensor(mu, rho) * metric_tensor(nu, sigma) * indexed(A, rho.toggle_variance(), sigma.toggle_variance()), indexed(A, mu, nu));
111         result += check_equal_simplify(indexed(A, mu.toggle_variance()) * metric_tensor(mu, nu) - indexed(A, mu.toggle_variance()) * metric_tensor(nu, mu), 0);
112         result += check_equal_simplify(indexed(A, mu.toggle_variance(), nu.toggle_variance()) * metric_tensor(nu, rho), indexed(A, mu.toggle_variance(), rho));
113
114         // contraction with delta tensor yields a metric tensor
115         result += check_equal_simplify(delta_tensor(mu, nu.toggle_variance()) * metric_tensor(nu, rho), metric_tensor(mu, rho));
116         result += check_equal_simplify(metric_tensor(mu, nu) * indexed(A, nu.toggle_variance()) * delta_tensor(mu.toggle_variance(), rho), indexed(A, rho));
117
118         return result;
119 }
120
121 static unsigned epsilon_check()
122 {
123         // checks identities of the epsilon tensor
124
125         unsigned result = 0;
126
127         symbol s_mu("mu"), s_nu("nu"), s_rho("rho"), s_sigma("sigma"), s_tau("tau");
128         symbol d("d");
129         varidx mu(s_mu, 4), nu(s_nu, 4), rho(s_rho, 4), sigma(s_sigma, 4), tau(s_tau, 4);
130         varidx mu_co(s_mu, 4, true), nu_co(s_nu, 4, true), rho_co(s_rho, 4, true), sigma_co(s_sigma, 4, true), tau_co(s_tau, 4, true);
131
132         // antisymmetry
133         result += check_equal(lorentz_eps(mu, nu, rho, sigma) + lorentz_eps(sigma, rho, mu, nu), 0);
134
135         // convolution is zero
136         result += check_equal(lorentz_eps(mu, nu, rho, nu_co), 0);
137         result += check_equal(lorentz_eps(mu, nu, mu_co, nu_co), 0);
138         result += check_equal_simplify(lorentz_g(mu_co, nu_co) * lorentz_eps(mu, nu, rho, sigma), 0);
139
140         // contraction with symmetric tensor is zero
141         result += check_equal_simplify(lorentz_eps(mu, nu, rho, sigma) * indexed(d, sy_symm(), mu_co, nu_co), 0);
142         result += check_equal_simplify(lorentz_eps(mu, nu, rho, sigma) * indexed(d, sy_symm(), nu_co, sigma_co, rho_co), 0);
143         result += check_equal_simplify(lorentz_eps(mu, nu, rho, sigma) * indexed(d, mu_co) * indexed(d, nu_co), 0);
144         result += check_equal_simplify(lorentz_eps(mu_co, nu, rho, sigma) * indexed(d, mu) * indexed(d, nu_co), 0);
145         ex e = lorentz_eps(mu, nu, rho, sigma) * indexed(d, mu_co) - lorentz_eps(mu_co, nu, rho, sigma) * indexed(d, mu);
146         result += check_equal_simplify(e, 0);
147
148         // contractions of epsilon tensors
149         result += check_equal_simplify(lorentz_eps(mu, nu, rho, sigma) * lorentz_eps(mu_co, nu_co, rho_co, sigma_co), -24);
150         result += check_equal_simplify(lorentz_eps(tau, nu, rho, sigma) * lorentz_eps(mu_co, nu_co, rho_co, sigma_co), -6 * delta_tensor(tau, mu_co));
151
152         return result;
153 }
154
155 DECLARE_FUNCTION_2P(symm_fcn)
156 REGISTER_FUNCTION(symm_fcn, set_symmetry(sy_symm(0, 1)));
157 DECLARE_FUNCTION_2P(anti_fcn)
158 REGISTER_FUNCTION(anti_fcn, set_symmetry(sy_anti(0, 1)));
159
160 static unsigned symmetry_check()
161 {
162         // check symmetric/antisymmetric objects
163
164         unsigned result = 0;
165
166         idx i(symbol("i"), 3), j(symbol("j"), 3), k(symbol("k"), 3), l(symbol("l"), 3);
167         symbol A("A"), B("B"), C("C");
168         ex e;
169
170         result += check_equal(indexed(A, sy_symm(), i, j), indexed(A, sy_symm(), j, i));
171         result += check_equal(indexed(A, sy_anti(), i, j) + indexed(A, sy_anti(), j, i), 0);
172         result += check_equal(indexed(A, sy_anti(), i, j, k) - indexed(A, sy_anti(), j, k, i), 0);
173         e = indexed(A, sy_symm(), i, j, k) *
174             indexed(B, sy_anti(), l, k, i);
175         result += check_equal_simplify(e, 0);
176         e = indexed(A, sy_symm(), i, i, j, j) *
177             indexed(B, sy_anti(), k, l); // GiNaC 0.8.0 had a bug here
178         result += check_equal_simplify(e, e);
179
180         symmetry R = sy_symm(sy_anti(0, 1), sy_anti(2, 3));
181         e = indexed(A, R, i, j, k, l) + indexed(A, R, j, i, k, l);
182         result += check_equal(e, 0);
183         e = indexed(A, R, i, j, k, l) + indexed(A, R, i, j, l, k);
184         result += check_equal(e, 0);
185         e = indexed(A, R, i, j, k, l) - indexed(A, R, j, i, l, k);
186         result += check_equal(e, 0);
187         e = indexed(A, R, i, j, k, l) + indexed(A, R, k, l, j, i);
188         result += check_equal(e, 0);
189
190         e = indexed(A, i, j);
191         result += check_equal(symmetrize(e) + antisymmetrize(e), e);
192         e = indexed(A, sy_symm(), i, j, k, l);
193         result += check_equal(symmetrize(e), e);
194         result += check_equal(antisymmetrize(e), 0);
195         e = indexed(A, sy_anti(), i, j, k, l);
196         result += check_equal(symmetrize(e), 0);
197         result += check_equal(antisymmetrize(e), e);
198
199         e = (indexed(A, sy_anti(), i, j, k, l) * (indexed(B, j) * indexed(C, k) + indexed(B, k) * indexed(C, j)) + indexed(B, i, l)).expand();
200         result += check_equal_simplify(e, indexed(B, i, l));
201
202         result += check_equal(symm_fcn(0, 1) + symm_fcn(1, 0), 2*symm_fcn(0, 1));
203         result += check_equal(anti_fcn(0, 1) + anti_fcn(1, 0), 0);
204         result += check_equal(anti_fcn(0, 0), 0);
205
206         return result;
207 }
208
209 static unsigned scalar_product_check()
210 {
211         // check scalar product replacement
212
213         unsigned result = 0;
214
215     idx i(symbol("i"), 3), j(symbol("j"), 3);
216     symbol A("A"), B("B"), C("C");
217         ex e;
218
219     scalar_products sp;
220     sp.add(A, B, 0); // A and B are orthogonal
221     sp.add(A, C, 0); // A and C are orthogonal
222     sp.add(A, A, 4); // A^2 = 4 (A has length 2)
223
224     e = (indexed(A + B, i) * indexed(A + C, i)).expand(expand_options::expand_indexed);
225         result += check_equal_simplify(e, indexed(B, i) * indexed(C, i) + 4, sp);
226         e = indexed(A, i, i) * indexed(B, j, j); // GiNaC 0.8.0 had a bug here
227         result += check_equal_simplify(e, e, sp);
228
229         return result;
230 }
231
232 static unsigned edyn_check()
233 {
234         // Relativistic electrodynamics
235
236         // Test 1: check transformation laws of electric and magnetic fields by
237         // applying a Lorentz boost to the field tensor
238
239         unsigned result = 0;
240
241         symbol beta("beta");
242         ex gamma = 1 / sqrt(1 - pow(beta, 2));
243         symbol Ex("Ex"), Ey("Ey"), Ez("Ez");
244         symbol Bx("Bx"), By("By"), Bz("Bz");
245
246         // Lorentz transformation matrix (boost along x axis)
247         matrix L(4, 4);
248         L =       gamma, -beta*gamma, 0, 0,
249             -beta*gamma,       gamma, 0, 0,
250                       0,           0, 1, 0,
251                       0,           0, 0, 1;
252
253         // Electromagnetic field tensor
254         matrix F(4, 4);
255         F =  0, -Ex, -Ey, -Ez,
256                 Ex,   0, -Bz,  By,
257                 Ey,  Bz,   0, -Bx,
258                 Ez, -By,  Bx,   0;
259
260         // Indices
261         symbol s_mu("mu"), s_nu("nu"), s_rho("rho"), s_sigma("sigma");
262         varidx mu(s_mu, 4), nu(s_nu, 4), rho(s_rho, 4), sigma(s_sigma, 4);
263
264         // Apply transformation law of second rank tensor
265         ex e = (indexed(L, mu, rho.toggle_variance())
266               * indexed(L, nu, sigma.toggle_variance())
267               * indexed(F, rho, sigma)).simplify_indexed();
268
269         // Extract transformed electric and magnetic fields
270         ex Ex_p = e.subs(lst(mu == 1, nu == 0)).normal();
271         ex Ey_p = e.subs(lst(mu == 2, nu == 0)).normal();
272         ex Ez_p = e.subs(lst(mu == 3, nu == 0)).normal();
273         ex Bx_p = e.subs(lst(mu == 3, nu == 2)).normal();
274         ex By_p = e.subs(lst(mu == 1, nu == 3)).normal();
275         ex Bz_p = e.subs(lst(mu == 2, nu == 1)).normal();
276
277         // Check results
278         result += check_equal(Ex_p, Ex);
279         result += check_equal(Ey_p, gamma * (Ey - beta * Bz));
280         result += check_equal(Ez_p, gamma * (Ez + beta * By));
281         result += check_equal(Bx_p, Bx);
282         result += check_equal(By_p, gamma * (By + beta * Ez));
283         result += check_equal(Bz_p, gamma * (Bz - beta * Ey));
284
285         // Test 2: check energy density and Poynting vector of electromagnetic field
286
287         // Minkowski metric
288         ex eta = diag_matrix(lst(1, -1, -1, -1));
289
290         // Covariant field tensor
291         ex F_mu_nu = (indexed(eta, mu.toggle_variance(), rho.toggle_variance())
292                     * indexed(eta, nu.toggle_variance(), sigma.toggle_variance())
293                     * indexed(F, rho, sigma)).simplify_indexed();
294
295         // Energy-momentum tensor
296         ex T = (-indexed(eta, rho, sigma) * F_mu_nu.subs(s_nu == s_rho) 
297                 * F_mu_nu.subs(lst(s_mu == s_nu, s_nu == s_sigma))
298               + indexed(eta, mu.toggle_variance(), nu.toggle_variance())
299                 * F_mu_nu.subs(lst(s_mu == s_rho, s_nu == s_sigma))
300                 * indexed(F, rho, sigma) / 4).simplify_indexed() / (4 * Pi);
301
302         // Extract energy density and Poynting vector
303         ex E = T.subs(lst(s_mu == 0, s_nu == 0)).normal();
304         ex Px = T.subs(lst(s_mu == 0, s_nu == 1));
305         ex Py = T.subs(lst(s_mu == 0, s_nu == 2)); 
306         ex Pz = T.subs(lst(s_mu == 0, s_nu == 3));
307
308         // Check results
309         result += check_equal(E, (Ex*Ex+Ey*Ey+Ez*Ez+Bx*Bx+By*By+Bz*Bz) / (8 * Pi));
310         result += check_equal(Px, (Ez*By-Ey*Bz) / (4 * Pi));
311         result += check_equal(Py, (Ex*Bz-Ez*Bx) / (4 * Pi));
312         result += check_equal(Pz, (Ey*Bx-Ex*By) / (4 * Pi));
313
314         return result;
315 }
316
317 static unsigned spinor_check()
318 {
319         // check identities of the spinor metric
320
321         unsigned result = 0;
322
323         symbol psi("psi");
324         spinidx A(symbol("A")), B(symbol("B")), C(symbol("C")), D(symbol("D"));
325         ex A_co = A.toggle_variance(), B_co = B.toggle_variance();
326         ex e;
327
328         e = spinor_metric(A_co, B_co) * spinor_metric(A, B);
329         result += check_equal_simplify(e, 2);
330         e = spinor_metric(A_co, B_co) * spinor_metric(B, A);
331         result += check_equal_simplify(e, -2);
332         e = spinor_metric(A_co, B_co) * spinor_metric(A, C);
333         result += check_equal_simplify(e, delta_tensor(B_co, C));
334         e = spinor_metric(A_co, B_co) * spinor_metric(B, C);
335         result += check_equal_simplify(e, -delta_tensor(A_co, C));
336         e = spinor_metric(A_co, B_co) * spinor_metric(C, A);
337         result += check_equal_simplify(e, -delta_tensor(B_co, C));
338         e = spinor_metric(A, B) * indexed(psi, B_co);
339         result += check_equal_simplify(e, indexed(psi, A));
340         e = spinor_metric(A, B) * indexed(psi, A_co);
341         result += check_equal_simplify(e, -indexed(psi, B));
342         e = spinor_metric(A_co, B_co) * indexed(psi, B);
343         result += check_equal_simplify(e, -indexed(psi, A_co));
344         e = spinor_metric(A_co, B_co) * indexed(psi, A);
345         result += check_equal_simplify(e, indexed(psi, B_co));
346         e = spinor_metric(D, A) * spinor_metric(A_co, B_co) * spinor_metric(B, C) - spinor_metric(D, A_co) * spinor_metric(A, B_co) * spinor_metric(B, C);
347         result += check_equal_simplify(e, 0);
348
349         return result;
350 }
351
352 static unsigned dummy_check()
353 {
354         // check dummy index renaming/repositioning
355
356         unsigned result = 0;
357
358         symbol p("p"), q("q");
359         idx i(symbol("i"), 3), j(symbol("j"), 3), n(symbol("n"), 3);
360         varidx mu(symbol("mu"), 4), nu(symbol("nu"), 4);
361         ex e;
362
363         e = indexed(p, i) * indexed(q, i) - indexed(p, j) * indexed(q, j);
364         result += check_equal_simplify(e, 0);
365
366         e = indexed(p, i) * indexed(p, i) * indexed(q, j) * indexed(q, j)
367           - indexed(p, n) * indexed(p, n) * indexed(q, j) * indexed(q, j);
368         result += check_equal_simplify(e, 0);
369
370         e = indexed(p, mu, mu.toggle_variance()) - indexed(p, nu, nu.toggle_variance());
371         result += check_equal_simplify(e, 0);
372
373         e = indexed(p, mu.toggle_variance(), nu, mu) * indexed(q, i)
374           - indexed(p, mu, nu, mu.toggle_variance()) * indexed(q, i);
375         result += check_equal_simplify(e, 0);
376
377         e = indexed(p, mu, mu.toggle_variance()) - indexed(p, nu.toggle_variance(), nu);
378         result += check_equal_simplify(e, 0);
379         e = indexed(p, mu.toggle_variance(), mu) - indexed(p, nu, nu.toggle_variance());
380         result += check_equal_simplify(e, 0);
381
382         // GiNaC 1.2.1 had a bug here because p.i*p.i -> (p.i)^2
383         e = indexed(p, i) * indexed(p, i) * indexed(p, j) + indexed(p, j);
384         ex fi = exprseq(e.get_free_indices());
385         if (!fi.is_equal(exprseq(j))) {
386                 clog << "get_free_indices(" << e << ") erroneously returned "
387                      << fi << " instead of (.j)" << endl;
388                 ++result;
389         }
390
391         return result;
392 }
393
394 unsigned exam_indexed()
395 {
396         unsigned result = 0;
397         
398         cout << "examining indexed objects" << flush;
399
400         result += delta_check();  cout << '.' << flush;
401         result += metric_check();  cout << '.' << flush;
402         result += epsilon_check();  cout << '.' << flush;
403         result += symmetry_check();  cout << '.' << flush;
404         result += scalar_product_check();  cout << '.' << flush;
405         result += edyn_check();  cout << '.' << flush;
406         result += spinor_check(); cout << '.' << flush;
407         result += dummy_check(); cout << '.' << flush;
408         
409         return result;
410 }
411
412 int main(int argc, char** argv)
413 {
414         return exam_indexed();
415 }