]> www.ginac.de Git - ginac.git/blob - check/exam_indexed.cpp
546eea78ae0b1b43856e76019400cc667efd7a54
[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-2001 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 "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(void)
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(void)
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(void)
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
127         // antisymmetry
128         result += check_equal(lorentz_eps(mu, nu, rho, sigma) + lorentz_eps(sigma, rho, mu, nu), 0);
129
130         // convolution is zero
131         result += check_equal(lorentz_eps(mu, nu, rho, nu.toggle_variance()), 0);
132         result += check_equal(lorentz_eps(mu, nu, mu.toggle_variance(), nu.toggle_variance()), 0);
133         result += check_equal_simplify(lorentz_g(mu.toggle_variance(), nu.toggle_variance()) * lorentz_eps(mu, nu, rho, sigma), 0);
134
135         // contraction with symmetric tensor is zero
136         result += check_equal_simplify(lorentz_eps(mu, nu, rho, sigma) * indexed(d, sy_symm(), mu.toggle_variance(), nu.toggle_variance()), 0);
137         result += check_equal_simplify(lorentz_eps(mu, nu, rho, sigma) * indexed(d, sy_symm(), nu.toggle_variance(), sigma.toggle_variance(), rho.toggle_variance()), 0);
138         ex e = lorentz_eps(mu, nu, rho, sigma) * indexed(d, sy_symm(), mu.toggle_variance(), tau);
139         result += check_equal_simplify(e, e);
140
141         return result;
142 }
143
144 static unsigned symmetry_check(void)
145 {
146         // check symmetric/antisymmetric objects
147
148         unsigned result = 0;
149
150         idx i(symbol("i"), 3), j(symbol("j"), 3), k(symbol("k"), 3), l(symbol("l"), 3);
151         symbol A("A"), B("B");
152         ex e;
153
154         result += check_equal(indexed(A, sy_symm(), i, j), indexed(A, sy_symm(), j, i));
155         result += check_equal(indexed(A, sy_anti(), i, j) + indexed(A, sy_anti(), j, i), 0);
156         result += check_equal(indexed(A, sy_anti(), i, j, k) - indexed(A, sy_anti(), j, k, i), 0);
157         e = indexed(A, sy_symm(), i, j, k) *
158             indexed(B, sy_anti(), l, k, i);
159         result += check_equal_simplify(e, 0);
160         e = indexed(A, sy_symm(), i, i, j, j) *
161             indexed(B, sy_anti(), k, l); // GiNaC 0.8.0 had a bug here
162         result += check_equal_simplify(e, e);
163
164         symmetry R = sy_symm(sy_anti(0, 1), sy_anti(2, 3));
165         e = indexed(A, R, i, j, k, l) + indexed(A, R, j, i, k, l);
166         result += check_equal(e, 0);
167         e = indexed(A, R, i, j, k, l) + indexed(A, R, i, j, l, k);
168         result += check_equal(e, 0);
169         e = indexed(A, R, i, j, k, l) - indexed(A, R, j, i, l, k);
170         result += check_equal(e, 0);
171         e = indexed(A, R, i, j, k, l) + indexed(A, R, k, l, j, i);
172         result += check_equal(e, 0);
173
174         e = indexed(A, i, j);
175         result += check_equal(symmetrize(e) + antisymmetrize(e), e);
176         e = indexed(A, sy_symm(), i, j, k, l);
177         result += check_equal(symmetrize(e), e);
178         result += check_equal(antisymmetrize(e), 0);
179         e = indexed(A, sy_anti(), i, j, k, l);
180         result += check_equal(symmetrize(e), 0);
181         result += check_equal(antisymmetrize(e), e);
182
183         return result;
184 }
185
186 static unsigned scalar_product_check(void)
187 {
188         // check scalar product replacement
189
190         unsigned result = 0;
191
192     idx i(symbol("i"), 3), j(symbol("j"), 3);
193     symbol A("A"), B("B"), C("C");
194         ex e;
195
196     scalar_products sp;
197     sp.add(A, B, 0); // A and B are orthogonal
198     sp.add(A, C, 0); // A and C are orthogonal
199     sp.add(A, A, 4); // A^2 = 4 (A has length 2)
200
201     e = (indexed(A + B, i) * indexed(A + C, i)).expand(expand_options::expand_indexed);
202         result += check_equal_simplify(e, indexed(B, i) * indexed(C, i) + 4, sp);
203         e = indexed(A, i, i) * indexed(B, j, j); // GiNaC 0.8.0 had a bug here
204         result += check_equal_simplify(e, e, sp);
205
206         return result;
207 }
208
209 static unsigned edyn_check(void)
210 {
211         // Relativistic electrodynamics
212
213         // Test 1: check transformation laws of electric and magnetic fields by
214         // applying a Lorentz boost to the field tensor
215
216         unsigned result = 0;
217
218         symbol beta("beta");
219         ex gamma = 1 / sqrt(1 - pow(beta, 2));
220         symbol Ex("Ex"), Ey("Ey"), Ez("Ez");
221         symbol Bx("Bx"), By("By"), Bz("Bz");
222
223         // Lorentz transformation matrix (boost along x axis)
224         matrix L(4, 4);
225         L(0, 0) = gamma;
226         L(0, 1) = -beta*gamma;
227         L(1, 0) = -beta*gamma;
228         L(1, 1) = gamma;
229         L(2, 2) = 1; L(3, 3) = 1;
230
231         // Electromagnetic field tensor
232         matrix F(4, 4, lst(
233                  0, -Ex, -Ey, -Ez,
234                 Ex,   0, -Bz,  By,
235                 Ey,  Bz,   0, -Bx,
236                 Ez, -By,  Bx,   0
237         ));
238
239         // Indices
240         symbol s_mu("mu"), s_nu("nu"), s_rho("rho"), s_sigma("sigma");
241         varidx mu(s_mu, 4), nu(s_nu, 4), rho(s_rho, 4), sigma(s_sigma, 4);
242
243         // Apply transformation law of second rank tensor
244         ex e = (indexed(L, mu, rho.toggle_variance())
245               * indexed(L, nu, sigma.toggle_variance())
246               * indexed(F, rho, sigma)).simplify_indexed();
247
248         // Extract transformed electric and magnetic fields
249         ex Ex_p = e.subs(lst(mu == 1, nu == 0)).normal();
250         ex Ey_p = e.subs(lst(mu == 2, nu == 0)).normal();
251         ex Ez_p = e.subs(lst(mu == 3, nu == 0)).normal();
252         ex Bx_p = e.subs(lst(mu == 3, nu == 2)).normal();
253         ex By_p = e.subs(lst(mu == 1, nu == 3)).normal();
254         ex Bz_p = e.subs(lst(mu == 2, nu == 1)).normal();
255
256         // Check results
257         result += check_equal(Ex_p, Ex);
258         result += check_equal(Ey_p, gamma * (Ey - beta * Bz));
259         result += check_equal(Ez_p, gamma * (Ez + beta * By));
260         result += check_equal(Bx_p, Bx);
261         result += check_equal(By_p, gamma * (By + beta * Ez));
262         result += check_equal(Bz_p, gamma * (Bz - beta * Ey));
263
264         // Test 2: check energy density and Poynting vector of electromagnetic field
265
266         // Minkowski metric
267         ex eta = diag_matrix(lst(1, -1, -1, -1));
268
269         // Covariant field tensor
270         ex F_mu_nu = (indexed(eta, mu.toggle_variance(), rho.toggle_variance())
271                     * indexed(eta, nu.toggle_variance(), sigma.toggle_variance())
272                     * indexed(F, rho, sigma)).simplify_indexed();
273
274         // Energy-momentum tensor
275         ex T = (-indexed(eta, rho, sigma) * F_mu_nu.subs(s_nu == s_rho) 
276                 * F_mu_nu.subs(lst(s_mu == s_nu, s_nu == s_sigma))
277               + indexed(eta, mu.toggle_variance(), nu.toggle_variance())
278                 * F_mu_nu.subs(lst(s_mu == s_rho, s_nu == s_sigma))
279                 * indexed(F, rho, sigma) / 4).simplify_indexed() / (4 * Pi);
280
281         // Extract energy density and Poynting vector
282         ex E = T.subs(lst(s_mu == 0, s_nu == 0)).normal();
283         ex Px = T.subs(lst(s_mu == 0, s_nu == 1));
284         ex Py = T.subs(lst(s_mu == 0, s_nu == 2)); 
285         ex Pz = T.subs(lst(s_mu == 0, s_nu == 3));
286
287         // Check results
288         result += check_equal(E, (Ex*Ex+Ey*Ey+Ez*Ez+Bx*Bx+By*By+Bz*Bz) / (8 * Pi));
289         result += check_equal(Px, (Ez*By-Ey*Bz) / (4 * Pi));
290         result += check_equal(Py, (Ex*Bz-Ez*Bx) / (4 * Pi));
291         result += check_equal(Pz, (Ey*Bx-Ex*By) / (4 * Pi));
292
293         return result;
294 }
295
296 static unsigned spinor_check(void)
297 {
298         // check identities of the spinor metric
299
300         unsigned result = 0;
301
302         symbol psi("psi");
303         spinidx A(symbol("A"), 2), B(symbol("B"), 2), C(symbol("C"), 2);
304         ex A_co = A.toggle_variance(), B_co = B.toggle_variance();
305         ex e;
306
307         e = spinor_metric(A_co, B_co) * spinor_metric(A, B);
308         result += check_equal_simplify(e, 2);
309         e = spinor_metric(A_co, B_co) * spinor_metric(B, A);
310         result += check_equal_simplify(e, -2);
311         e = spinor_metric(A_co, B_co) * spinor_metric(A, C);
312         result += check_equal_simplify(e, delta_tensor(B_co, C));
313         e = spinor_metric(A_co, B_co) * spinor_metric(B, C);
314         result += check_equal_simplify(e, -delta_tensor(A_co, C));
315         e = spinor_metric(A_co, B_co) * spinor_metric(C, A);
316         result += check_equal_simplify(e, -delta_tensor(B_co, C));
317         e = spinor_metric(A, B) * indexed(psi, B_co);
318         result += check_equal_simplify(e, indexed(psi, A));
319         e = spinor_metric(A, B) * indexed(psi, A_co);
320         result += check_equal_simplify(e, -indexed(psi, B));
321         e = spinor_metric(A_co, B_co) * indexed(psi, B);
322         result += check_equal_simplify(e, -indexed(psi, A_co));
323         e = spinor_metric(A_co, B_co) * indexed(psi, A);
324         result += check_equal_simplify(e, indexed(psi, B_co));
325
326         return result;
327 }
328
329 static unsigned dummy_check(void)
330 {
331         // check dummy index renaming
332
333         unsigned result = 0;
334
335         symbol p("p"), q("q");
336         idx i(symbol("i"), 3), j(symbol("j"), 3), n(symbol("n"), 3);
337         varidx mu(symbol("mu"), 4), nu(symbol("nu"), 4);
338         ex e;
339
340         e = indexed(p, i) * indexed(q, i) - indexed(p, j) * indexed(q, j);
341         result += check_equal_simplify(e, 0);
342
343         e = indexed(p, i) * indexed(p, i) * indexed(q, j) * indexed(q, j)
344           - indexed(p, n) * indexed(p, n) * indexed(q, j) * indexed(q, j);
345         result += check_equal_simplify(e, 0);
346
347         e = indexed(p, mu, mu.toggle_variance()) - indexed(p, nu, nu.toggle_variance());
348         result += check_equal_simplify(e, 0);
349
350         return result;
351 }
352
353 unsigned exam_indexed(void)
354 {
355         unsigned result = 0;
356         
357         cout << "examining indexed objects" << flush;
358         clog << "----------indexed objects:" << endl;
359
360         result += delta_check();  cout << '.' << flush;
361         result += metric_check();  cout << '.' << flush;
362         result += epsilon_check();  cout << '.' << flush;
363         result += symmetry_check();  cout << '.' << flush;
364         result += scalar_product_check();  cout << '.' << flush;
365         result += edyn_check();  cout << '.' << flush;
366         result += spinor_check(); cout << '.' << flush;
367         result += dummy_check(); cout << '.' << flush;
368         
369         if (!result) {
370                 cout << " passed " << endl;
371                 clog << "(no output)" << endl;
372         } else {
373                 cout << " failed " << endl;
374         }
375         
376         return result;
377 }