]> www.ginac.de Git - ginac.git/blob - check/exam_indexed.cpp
2426430c8e4450e27a9614983046827f87e8489c
[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 delta_check(void)
48 {
49         // checks identities of the delta tensor
50
51         unsigned result = 0;
52
53         symbol s_i("i"), s_j("j"), s_k("k");
54         idx i(s_i, 3), j(s_j, 3), k(s_k, 3);
55         symbol A("A");
56
57         // symmetry
58         result += check_equal(delta_tensor(i, j), delta_tensor(j, i));
59
60         // trace = dimension of index space
61         result += check_equal(delta_tensor(i, i), 3);
62         result += check_equal_simplify(delta_tensor(i, j) * delta_tensor(i, j), 3);
63
64         // contraction with delta tensor
65         result += check_equal_simplify(delta_tensor(i, j) * indexed(A, k), delta_tensor(i, j) * indexed(A, k));
66         result += check_equal_simplify(delta_tensor(i, j) * indexed(A, j), indexed(A, i));
67         result += check_equal_simplify(delta_tensor(i, j) * indexed(A, i), indexed(A, j));
68         result += check_equal_simplify(delta_tensor(i, j) * delta_tensor(j, k) * indexed(A, i), indexed(A, k));
69
70         return result;
71 }
72
73 static unsigned metric_check(void)
74 {
75         // checks identities of the metric tensor
76
77         unsigned result = 0;
78
79         symbol s_mu("mu"), s_nu("nu"), s_rho("rho"), s_sigma("sigma");
80         varidx mu(s_mu, 4), nu(s_nu, 4), rho(s_rho, 4), sigma(s_sigma, 4);
81         symbol A("A");
82
83         // becomes delta tensor if indices have opposite variance
84         result += check_equal(metric_tensor(mu, nu.toggle_variance()), delta_tensor(mu, nu.toggle_variance()));
85
86         // scalar contraction = dimension of index space
87         result += check_equal(metric_tensor(mu, mu.toggle_variance()), 4);
88         result += check_equal_simplify(metric_tensor(mu, nu) * metric_tensor(mu.toggle_variance(), nu.toggle_variance()), 4);
89
90         // contraction with metric tensor
91         result += check_equal_simplify(metric_tensor(mu, nu) * indexed(A, nu), metric_tensor(mu, nu) * indexed(A, nu));
92         result += check_equal_simplify(metric_tensor(mu, nu) * indexed(A, nu.toggle_variance()), indexed(A, mu));
93         result += check_equal_simplify(metric_tensor(mu, nu) * indexed(A, mu.toggle_variance()), indexed(A, nu));
94         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()));
95         result += check_equal_simplify(metric_tensor(mu, rho) * metric_tensor(nu, sigma) * indexed(A, rho.toggle_variance(), sigma.toggle_variance()), indexed(A, mu, nu));
96         result += check_equal_simplify(indexed(A, mu.toggle_variance()) * metric_tensor(mu, nu) - indexed(A, mu.toggle_variance()) * metric_tensor(nu, mu), 0);
97         result += check_equal_simplify(indexed(A, mu.toggle_variance(), nu.toggle_variance()) * metric_tensor(nu, rho), indexed(A, mu.toggle_variance(), rho));
98
99         // contraction with delta tensor yields a metric tensor
100         result += check_equal_simplify(delta_tensor(mu, nu.toggle_variance()) * metric_tensor(nu, rho), metric_tensor(mu, rho));
101         result += check_equal_simplify(metric_tensor(mu, nu) * indexed(A, nu.toggle_variance()) * delta_tensor(mu.toggle_variance(), rho), indexed(A, rho));
102
103         return result;
104 }
105
106 static unsigned epsilon_check(void)
107 {
108         // checks identities of the epsilon tensor
109
110         unsigned result = 0;
111
112         symbol s_mu("mu"), s_nu("nu"), s_rho("rho"), s_sigma("sigma"), s_tau("tau");
113         symbol d("d");
114         varidx mu(s_mu, 4), nu(s_nu, 4), rho(s_rho, 4), sigma(s_sigma, 4), tau(s_tau, 4);
115
116         // antisymmetry
117         result += check_equal(lorentz_eps(mu, nu, rho, sigma) + lorentz_eps(sigma, rho, mu, nu), 0);
118
119         // convolution is zero
120         result += check_equal(lorentz_eps(mu, nu, rho, nu.toggle_variance()), 0);
121         result += check_equal(lorentz_eps(mu, nu, mu.toggle_variance(), nu.toggle_variance()), 0);
122         result += check_equal_simplify(lorentz_g(mu.toggle_variance(), nu.toggle_variance()) * lorentz_eps(mu, nu, rho, sigma), 0);
123
124         // contraction with symmetric tensor is zero
125         result += check_equal_simplify(lorentz_eps(mu, nu, rho, sigma) * indexed(d, indexed::symmetric, mu.toggle_variance(), nu.toggle_variance()), 0);
126         result += check_equal_simplify(lorentz_eps(mu, nu, rho, sigma) * indexed(d, indexed::symmetric, nu.toggle_variance(), sigma.toggle_variance(), rho.toggle_variance()), 0);
127         ex e = lorentz_eps(mu, nu, rho, sigma) * indexed(d, indexed::symmetric, mu.toggle_variance(), tau);
128         result += check_equal_simplify(e, e);
129
130         return result;
131 }
132
133 static unsigned symmetry_check(void)
134 {
135         // check symmetric/antisymmetric objects
136
137         unsigned result = 0;
138
139         symbol s_i("i"), s_j("j"), s_k("k");
140         idx i(s_i, 3), j(s_j, 3), k(s_k, 3);
141         symbol A("A");
142         ex e, e1, e2;
143
144         result += check_equal(indexed(A, indexed::symmetric, i, j), indexed(A, indexed::symmetric, j, i));
145         result += check_equal(indexed(A, indexed::antisymmetric, i, j) + indexed(A, indexed::antisymmetric, j, i), 0);
146         result += check_equal(indexed(A, indexed::antisymmetric, i, j, k) - indexed(A, indexed::antisymmetric, j, k, i), 0);
147
148         return result;
149 }
150
151 static unsigned edyn_check(void)
152 {
153         // Relativistic electrodynamics
154
155         // Test 1: check transformation laws of electric and magnetic fields by
156         // applying a Lorentz boost to the field tensor
157
158         unsigned result = 0;
159
160         symbol beta("beta");
161         ex gamma = 1 / sqrt(1 - pow(beta, 2));
162         symbol Ex("Ex"), Ey("Ey"), Ez("Ez");
163         symbol Bx("Bx"), By("By"), Bz("Bz");
164
165         // Lorentz transformation matrix (boost along x axis)
166         matrix L(4, 4);
167         L.set(0, 0, gamma);
168         L.set(0, 1, -beta*gamma);
169         L.set(1, 0, -beta*gamma);
170         L.set(1, 1, gamma);
171         L.set(2, 2, 1); L.set(3, 3, 1);
172
173         // Electromagnetic field tensor
174         matrix F(4, 4, lst(
175                  0, -Ex, -Ey, -Ez,
176                 Ex,   0, -Bz,  By,
177                 Ey,  Bz,   0, -Bx,
178                 Ez, -By,  Bx // 0
179         ));
180
181         // Indices
182         symbol s_mu("mu"), s_nu("nu"), s_rho("rho"), s_sigma("sigma");
183         varidx mu(s_mu, 4), nu(s_nu, 4), rho(s_rho, 4), sigma(s_sigma, 4);
184
185         // Apply transformation law of second rank tensor
186         ex e = (indexed(L, mu, rho.toggle_variance())
187               * indexed(L, nu, sigma.toggle_variance())
188               * indexed(F, rho, sigma)).simplify_indexed();
189
190         // Extract transformed electric and magnetic fields
191         ex Ex_p = e.subs(lst(mu == 1, nu == 0)).normal();
192         ex Ey_p = e.subs(lst(mu == 2, nu == 0)).normal();
193         ex Ez_p = e.subs(lst(mu == 3, nu == 0)).normal();
194         ex Bx_p = e.subs(lst(mu == 3, nu == 2)).normal();
195         ex By_p = e.subs(lst(mu == 1, nu == 3)).normal();
196         ex Bz_p = e.subs(lst(mu == 2, nu == 1)).normal();
197
198         // Check results
199         result += check_equal(Ex_p, Ex);
200         result += check_equal(Ey_p, gamma * (Ey - beta * Bz));
201         result += check_equal(Ez_p, gamma * (Ez + beta * By));
202         result += check_equal(Bx_p, Bx);
203         result += check_equal(By_p, gamma * (By + beta * Ez));
204         result += check_equal(Bz_p, gamma * (Bz - beta * Ey));
205
206         // Test 2: check energy density and Poynting vector of electromagnetic field
207
208         // Minkowski metric
209         ex eta = diag_matrix(lst(1, -1, -1, -1));
210
211         // Covariant field tensor
212         ex F_mu_nu = (indexed(eta, mu.toggle_variance(), rho.toggle_variance())
213                     * indexed(eta, nu.toggle_variance(), sigma.toggle_variance())
214                     * indexed(F, rho, sigma)).simplify_indexed();
215
216         // Energy-momentum tensor
217         ex T = (-indexed(eta, rho, sigma) * F_mu_nu.subs(s_nu == s_rho) 
218                 * F_mu_nu.subs(lst(s_mu == s_nu, s_nu == s_sigma))
219               + indexed(eta, mu.toggle_variance(), nu.toggle_variance())
220                 * F_mu_nu.subs(lst(s_mu == s_rho, s_nu == s_sigma))
221                 * indexed(F, rho, sigma) / 4).simplify_indexed() / (4 * Pi);
222
223         // Extract energy density and Poynting vector
224         ex E = T.subs(lst(s_mu == 0, s_nu == 0)).normal();
225         ex Px = T.subs(lst(s_mu == 0, s_nu == 1));
226         ex Py = T.subs(lst(s_mu == 0, s_nu == 2)); 
227         ex Pz = T.subs(lst(s_mu == 0, s_nu == 3));
228
229         // Check results
230         result += check_equal(E, (Ex*Ex+Ey*Ey+Ez*Ez+Bx*Bx+By*By+Bz*Bz) / (8 * Pi));
231         result += check_equal(Px, (Ez*By-Ey*Bz) / (4 * Pi));
232         result += check_equal(Py, (Ex*Bz-Ez*Bx) / (4 * Pi));
233         result += check_equal(Pz, (Ey*Bx-Ex*By) / (4 * Pi));
234
235         return result;
236 }
237
238 unsigned exam_indexed(void)
239 {
240         unsigned result = 0;
241         
242         cout << "examining indexed objects" << flush;
243         clog << "----------indexed objects:" << endl;
244
245         result += delta_check();  cout << '.' << flush;
246         result += metric_check();  cout << '.' << flush;
247         result += epsilon_check();  cout << '.' << flush;
248         result += symmetry_check();  cout << '.' << flush;
249         result += edyn_check();  cout << '.' << flush;
250         
251         if (!result) {
252                 cout << " passed " << endl;
253                 clog << "(no output)" << endl;
254         } else {
255                 cout << " failed " << endl;
256         }
257         
258         return result;
259 }