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 delta_check(void)
49 // checks identities of the delta tensor
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);
58 result += check_equal(delta_tensor(i, j), delta_tensor(j, i));
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);
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));
73 static unsigned metric_check(void)
75 // checks identities of the metric tensor
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);
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()));
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);
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));
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));
106 static unsigned epsilon_check(void)
108 // checks identities of the epsilon tensor
112 symbol s_mu("mu"), s_nu("nu"), s_rho("rho"), s_sigma("sigma");
113 varidx mu(s_mu, 4), nu(s_nu, 4), rho(s_rho, 4), sigma(s_sigma, 4);
116 result += check_equal(lorentz_eps(mu, nu, rho, sigma) + lorentz_eps(sigma, rho, mu, nu), 0);
118 // convolution is zero
119 result += check_equal(lorentz_eps(mu, nu, rho, nu.toggle_variance()), 0);
120 result += check_equal(lorentz_eps(mu, nu, mu.toggle_variance(), nu.toggle_variance()), 0);
121 result += check_equal_simplify(lorentz_g(mu.toggle_variance(), nu.toggle_variance()) * lorentz_eps(mu, nu, rho, sigma), 0);
126 static unsigned symmetry_check(void)
128 // check symmetric/antisymmetric objects
132 symbol s_i("i"), s_j("j"), s_k("k");
133 idx i(s_i, 3), j(s_j, 3), k(s_k, 3);
137 result += check_equal(indexed(A, indexed::symmetric, i, j), indexed(A, indexed::symmetric, j, i));
138 result += check_equal(indexed(A, indexed::antisymmetric, i, j) + indexed(A, indexed::antisymmetric, j, i), 0);
139 result += check_equal(indexed(A, indexed::antisymmetric, i, j, k) - indexed(A, indexed::antisymmetric, j, k, i), 0);
144 static unsigned edyn_check(void)
146 // relativistic electrodynamics: check transformation laws of electric
147 // and magnetic fields by applying a Lorentz boost to the field tensor
152 ex gamma = 1 / sqrt(1 - pow(beta, 2));
153 symbol Ex("Ex"), Ey("Ey"), Ez("Ez");
154 symbol Bx("Bx"), By("By"), Bz("Bz");
156 // Lorentz transformation matrix (boost along x axis)
159 L.set(0, 1, -beta*gamma);
160 L.set(1, 0, -beta*gamma);
165 // Electromagnetic field tensor
181 symbol s_mu("mu"), s_nu("nu"), s_rho("rho"), s_sigma("sigma");
182 varidx mu(s_mu, 4), nu(s_nu, 4), rho(s_rho, 4), sigma(s_sigma, 4);
184 // Apply transformation law of second rank tensor
185 ex e = (indexed(L, mu, rho.toggle_variance())
186 * indexed(L, nu, sigma.toggle_variance())
187 * indexed(F, rho, sigma)).simplify_indexed();
189 // Extract transformed electric and magnetic fields
190 ex Ex_p = e.subs(lst(mu == 1, nu == 0)).normal();
191 ex Ey_p = e.subs(lst(mu == 2, nu == 0)).normal();
192 ex Ez_p = e.subs(lst(mu == 3, nu == 0)).normal();
193 ex Bx_p = e.subs(lst(mu == 3, nu == 2)).normal();
194 ex By_p = e.subs(lst(mu == 1, nu == 3)).normal();
195 ex Bz_p = e.subs(lst(mu == 2, nu == 1)).normal();
198 result += check_equal(Ex_p, Ex);
199 result += check_equal(Ey_p, gamma * (Ey - beta * Bz));
200 result += check_equal(Ez_p, gamma * (Ez + beta * By));
201 result += check_equal(Bx_p, Bx);
202 result += check_equal(By_p, gamma * (By + beta * Ez));
203 result += check_equal(Bz_p, gamma * (Bz - beta * Ey));
208 unsigned exam_indexed(void)
212 cout << "examining indexed objects" << flush;
213 clog << "----------indexed objects:" << endl;
215 result += delta_check(); cout << '.' << flush;
216 result += metric_check(); cout << '.' << flush;
217 result += epsilon_check(); cout << '.' << flush;
218 result += symmetry_check(); cout << '.' << flush;
219 result += edyn_check(); cout << '.' << flush;
222 cout << " passed " << endl;
223 clog << "(no output)" << endl;
225 cout << " failed " << endl;