1 /** @file exam_indexed.cpp
3 * Here we test manipulations on GiNaC's indexed objects. */
6 * GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
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.
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.
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
25 static unsigned check_equal(const ex &e1, const ex &e2)
29 clog << e1 << "-" << e2 << " erroneously returned "
30 << e << " instead of 0" << endl;
36 static unsigned check_equal_simplify(const ex &e1, const ex &e2)
38 ex e = simplify_indexed(e1) - e2;
40 clog << "simplify_indexed(" << e1 << ")-" << e2 << " erroneously returned "
41 << e << " instead of 0" << endl;
47 static unsigned check_equal_simplify(const ex &e1, const ex &e2, const scalar_products &sp)
49 ex e = simplify_indexed(e1, sp) - e2;
51 clog << "simplify_indexed(" << e1 << ")-" << e2 << " erroneously returned "
52 << e << " instead of 0" << endl;
58 static unsigned delta_check(void)
60 // checks identities of the delta tensor
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);
69 result += check_equal(delta_tensor(i, j), delta_tensor(j, i));
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);
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));
84 static unsigned metric_check(void)
86 // checks identities of the metric tensor
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);
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()));
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);
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));
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));
117 static unsigned epsilon_check(void)
119 // checks identities of the epsilon tensor
123 symbol s_mu("mu"), s_nu("nu"), s_rho("rho"), s_sigma("sigma"), s_tau("tau");
125 varidx mu(s_mu, 4), nu(s_nu, 4), rho(s_rho, 4), sigma(s_sigma, 4), tau(s_tau, 4);
128 result += check_equal(lorentz_eps(mu, nu, rho, sigma) + lorentz_eps(sigma, rho, mu, nu), 0);
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);
135 // contraction with symmetric tensor is zero
136 result += check_equal_simplify(lorentz_eps(mu, nu, rho, sigma) * indexed(d, indexed::symmetric, mu.toggle_variance(), nu.toggle_variance()), 0);
137 result += check_equal_simplify(lorentz_eps(mu, nu, rho, sigma) * indexed(d, indexed::symmetric, nu.toggle_variance(), sigma.toggle_variance(), rho.toggle_variance()), 0);
138 ex e = lorentz_eps(mu, nu, rho, sigma) * indexed(d, indexed::symmetric, mu.toggle_variance(), tau);
139 result += check_equal_simplify(e, e);
144 static unsigned symmetry_check(void)
146 // check symmetric/antisymmetric objects
150 idx i(symbol("i"), 3), j(symbol("j"), 3), k(symbol("k"), 3), l(symbol("l"), 3);
151 symbol A("A"), B("B");
154 result += check_equal(indexed(A, indexed::symmetric, i, j), indexed(A, indexed::symmetric, j, i));
155 result += check_equal(indexed(A, indexed::antisymmetric, i, j) + indexed(A, indexed::antisymmetric, j, i), 0);
156 result += check_equal(indexed(A, indexed::antisymmetric, i, j, k) - indexed(A, indexed::antisymmetric, j, k, i), 0);
157 e = indexed(A, indexed::symmetric, i, j, k) *
158 indexed(B, indexed::antisymmetric, l, k, i);
159 result += check_equal_simplify(e, 0);
160 e = indexed(A, indexed::symmetric, i, i, j, j) *
161 indexed(B, indexed::antisymmetric, k, l); // GiNaC 0.8.0 had a bug here
162 result += check_equal_simplify(e, e);
167 static unsigned scalar_product_check(void)
169 // check scalar product replacement
173 idx i(symbol("i"), 3), j(symbol("j"), 3);
174 symbol A("A"), B("B"), C("C");
178 sp.add(A, B, 0); // A and B are orthogonal
179 sp.add(A, C, 0); // A and C are orthogonal
180 sp.add(A, A, 4); // A^2 = 4 (A has length 2)
182 e = (indexed(A + B, i) * indexed(A + C, i)).expand(expand_options::expand_indexed);
183 result += check_equal_simplify(e, indexed(B, i) * indexed(C, i) + 4, sp);
184 e = indexed(A, i, i) * indexed(B, j, j); // GiNaC 0.8.0 had a bug here
185 result += check_equal_simplify(e, e, sp);
190 static unsigned edyn_check(void)
192 // Relativistic electrodynamics
194 // Test 1: check transformation laws of electric and magnetic fields by
195 // applying a Lorentz boost to the field tensor
200 ex gamma = 1 / sqrt(1 - pow(beta, 2));
201 symbol Ex("Ex"), Ey("Ey"), Ez("Ez");
202 symbol Bx("Bx"), By("By"), Bz("Bz");
204 // Lorentz transformation matrix (boost along x axis)
207 L.set(0, 1, -beta*gamma);
208 L.set(1, 0, -beta*gamma);
210 L.set(2, 2, 1); L.set(3, 3, 1);
212 // Electromagnetic field tensor
221 symbol s_mu("mu"), s_nu("nu"), s_rho("rho"), s_sigma("sigma");
222 varidx mu(s_mu, 4), nu(s_nu, 4), rho(s_rho, 4), sigma(s_sigma, 4);
224 // Apply transformation law of second rank tensor
225 ex e = (indexed(L, mu, rho.toggle_variance())
226 * indexed(L, nu, sigma.toggle_variance())
227 * indexed(F, rho, sigma)).simplify_indexed();
229 // Extract transformed electric and magnetic fields
230 ex Ex_p = e.subs(lst(mu == 1, nu == 0)).normal();
231 ex Ey_p = e.subs(lst(mu == 2, nu == 0)).normal();
232 ex Ez_p = e.subs(lst(mu == 3, nu == 0)).normal();
233 ex Bx_p = e.subs(lst(mu == 3, nu == 2)).normal();
234 ex By_p = e.subs(lst(mu == 1, nu == 3)).normal();
235 ex Bz_p = e.subs(lst(mu == 2, nu == 1)).normal();
238 result += check_equal(Ex_p, Ex);
239 result += check_equal(Ey_p, gamma * (Ey - beta * Bz));
240 result += check_equal(Ez_p, gamma * (Ez + beta * By));
241 result += check_equal(Bx_p, Bx);
242 result += check_equal(By_p, gamma * (By + beta * Ez));
243 result += check_equal(Bz_p, gamma * (Bz - beta * Ey));
245 // Test 2: check energy density and Poynting vector of electromagnetic field
248 ex eta = diag_matrix(lst(1, -1, -1, -1));
250 // Covariant field tensor
251 ex F_mu_nu = (indexed(eta, mu.toggle_variance(), rho.toggle_variance())
252 * indexed(eta, nu.toggle_variance(), sigma.toggle_variance())
253 * indexed(F, rho, sigma)).simplify_indexed();
255 // Energy-momentum tensor
256 ex T = (-indexed(eta, rho, sigma) * F_mu_nu.subs(s_nu == s_rho)
257 * F_mu_nu.subs(lst(s_mu == s_nu, s_nu == s_sigma))
258 + indexed(eta, mu.toggle_variance(), nu.toggle_variance())
259 * F_mu_nu.subs(lst(s_mu == s_rho, s_nu == s_sigma))
260 * indexed(F, rho, sigma) / 4).simplify_indexed() / (4 * Pi);
262 // Extract energy density and Poynting vector
263 ex E = T.subs(lst(s_mu == 0, s_nu == 0)).normal();
264 ex Px = T.subs(lst(s_mu == 0, s_nu == 1));
265 ex Py = T.subs(lst(s_mu == 0, s_nu == 2));
266 ex Pz = T.subs(lst(s_mu == 0, s_nu == 3));
269 result += check_equal(E, (Ex*Ex+Ey*Ey+Ez*Ez+Bx*Bx+By*By+Bz*Bz) / (8 * Pi));
270 result += check_equal(Px, (Ez*By-Ey*Bz) / (4 * Pi));
271 result += check_equal(Py, (Ex*Bz-Ez*Bx) / (4 * Pi));
272 result += check_equal(Pz, (Ey*Bx-Ex*By) / (4 * Pi));
277 unsigned exam_indexed(void)
281 cout << "examining indexed objects" << flush;
282 clog << "----------indexed objects:" << endl;
284 result += delta_check(); cout << '.' << flush;
285 result += metric_check(); cout << '.' << flush;
286 result += epsilon_check(); cout << '.' << flush;
287 result += symmetry_check(); cout << '.' << flush;
288 result += scalar_product_check(); cout << '.' << flush;
289 result += edyn_check(); cout << '.' << flush;
292 cout << " passed " << endl;
293 clog << "(no output)" << endl;
295 cout << " failed " << endl;