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