]> www.ginac.de Git - ginac.git/blobdiff - check/exam_indexed.cpp
match() (find()): use exmap (exset) to store matched subexpressions.
[ginac.git] / check / exam_indexed.cpp
index bbbbf4278f388718b29f580549a81f41fea0a441..b7c12ba2ad2a52d11ac4d613caf3343f83ff5d91 100644 (file)
@@ -3,7 +3,7 @@
  *  Here we test manipulations on GiNaC's indexed objects. */
 
 /*
- *  GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
+ *  GiNaC Copyright (C) 1999-2008 Johannes Gutenberg University Mainz, Germany
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
  *
  *  You should have received a copy of the GNU General Public License
  *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
-#include "exams.h"
+#include <iostream>
+#include "ginac.h"
+using namespace std;
+using namespace GiNaC;
 
 static unsigned check_equal(const ex &e1, const ex &e2)
 {
@@ -55,7 +58,7 @@ static unsigned check_equal_simplify(const ex &e1, const ex &e2, const scalar_pr
        return 0;
 }
 
-static unsigned delta_check(void)
+static unsigned delta_check()
 {
        // checks identities of the delta tensor
 
@@ -81,7 +84,7 @@ static unsigned delta_check(void)
        return result;
 }
 
-static unsigned metric_check(void)
+static unsigned metric_check()
 {
        // checks identities of the metric tensor
 
@@ -114,7 +117,7 @@ static unsigned metric_check(void)
        return result;
 }
 
-static unsigned epsilon_check(void)
+static unsigned epsilon_check()
 {
        // checks identities of the epsilon tensor
 
@@ -123,48 +126,86 @@ static unsigned epsilon_check(void)
        symbol s_mu("mu"), s_nu("nu"), s_rho("rho"), s_sigma("sigma"), s_tau("tau");
        symbol d("d");
        varidx mu(s_mu, 4), nu(s_nu, 4), rho(s_rho, 4), sigma(s_sigma, 4), tau(s_tau, 4);
+       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);
 
        // antisymmetry
        result += check_equal(lorentz_eps(mu, nu, rho, sigma) + lorentz_eps(sigma, rho, mu, nu), 0);
 
        // convolution is zero
-       result += check_equal(lorentz_eps(mu, nu, rho, nu.toggle_variance()), 0);
-       result += check_equal(lorentz_eps(mu, nu, mu.toggle_variance(), nu.toggle_variance()), 0);
-       result += check_equal_simplify(lorentz_g(mu.toggle_variance(), nu.toggle_variance()) * lorentz_eps(mu, nu, rho, sigma), 0);
+       result += check_equal(lorentz_eps(mu, nu, rho, nu_co), 0);
+       result += check_equal(lorentz_eps(mu, nu, mu_co, nu_co), 0);
+       result += check_equal_simplify(lorentz_g(mu_co, nu_co) * lorentz_eps(mu, nu, rho, sigma), 0);
 
        // contraction with symmetric tensor is zero
-       result += check_equal_simplify(lorentz_eps(mu, nu, rho, sigma) * indexed(d, indexed::symmetric, mu.toggle_variance(), nu.toggle_variance()), 0);
-       result += check_equal_simplify(lorentz_eps(mu, nu, rho, sigma) * indexed(d, indexed::symmetric, nu.toggle_variance(), sigma.toggle_variance(), rho.toggle_variance()), 0);
-       ex e = lorentz_eps(mu, nu, rho, sigma) * indexed(d, indexed::symmetric, mu.toggle_variance(), tau);
-       result += check_equal_simplify(e, e);
+       result += check_equal_simplify(lorentz_eps(mu, nu, rho, sigma) * indexed(d, sy_symm(), mu_co, nu_co), 0);
+       result += check_equal_simplify(lorentz_eps(mu, nu, rho, sigma) * indexed(d, sy_symm(), nu_co, sigma_co, rho_co), 0);
+       result += check_equal_simplify(lorentz_eps(mu, nu, rho, sigma) * indexed(d, mu_co) * indexed(d, nu_co), 0);
+       result += check_equal_simplify(lorentz_eps(mu_co, nu, rho, sigma) * indexed(d, mu) * indexed(d, nu_co), 0);
+       ex e = lorentz_eps(mu, nu, rho, sigma) * indexed(d, mu_co) - lorentz_eps(mu_co, nu, rho, sigma) * indexed(d, mu);
+       result += check_equal_simplify(e, 0);
+
+       // contractions of epsilon tensors
+       result += check_equal_simplify(lorentz_eps(mu, nu, rho, sigma) * lorentz_eps(mu_co, nu_co, rho_co, sigma_co), -24);
+       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));
 
        return result;
 }
 
-static unsigned symmetry_check(void)
+DECLARE_FUNCTION_2P(symm_fcn)
+REGISTER_FUNCTION(symm_fcn, set_symmetry(sy_symm(0, 1)));
+DECLARE_FUNCTION_2P(anti_fcn)
+REGISTER_FUNCTION(anti_fcn, set_symmetry(sy_anti(0, 1)));
+
+static unsigned symmetry_check()
 {
        // check symmetric/antisymmetric objects
 
        unsigned result = 0;
 
        idx i(symbol("i"), 3), j(symbol("j"), 3), k(symbol("k"), 3), l(symbol("l"), 3);
-       symbol A("A"), B("B");
+       symbol A("A"), B("B"), C("C");
        ex e;
 
-       result += check_equal(indexed(A, indexed::symmetric, i, j), indexed(A, indexed::symmetric, j, i));
-       result += check_equal(indexed(A, indexed::antisymmetric, i, j) + indexed(A, indexed::antisymmetric, j, i), 0);
-       result += check_equal(indexed(A, indexed::antisymmetric, i, j, k) - indexed(A, indexed::antisymmetric, j, k, i), 0);
-       e = indexed(A, indexed::symmetric, i, j, k) *
-           indexed(B, indexed::antisymmetric, l, k, i);
+       result += check_equal(indexed(A, sy_symm(), i, j), indexed(A, sy_symm(), j, i));
+       result += check_equal(indexed(A, sy_anti(), i, j) + indexed(A, sy_anti(), j, i), 0);
+       result += check_equal(indexed(A, sy_anti(), i, j, k) - indexed(A, sy_anti(), j, k, i), 0);
+       e = indexed(A, sy_symm(), i, j, k) *
+           indexed(B, sy_anti(), l, k, i);
        result += check_equal_simplify(e, 0);
-       e = indexed(A, indexed::symmetric, i, i, j, j) *
-           indexed(B, indexed::antisymmetric, k, l); // GiNaC 0.8.0 had a bug here
+       e = indexed(A, sy_symm(), i, i, j, j) *
+           indexed(B, sy_anti(), k, l); // GiNaC 0.8.0 had a bug here
        result += check_equal_simplify(e, e);
 
+       symmetry R = sy_symm(sy_anti(0, 1), sy_anti(2, 3));
+       e = indexed(A, R, i, j, k, l) + indexed(A, R, j, i, k, l);
+       result += check_equal(e, 0);
+       e = indexed(A, R, i, j, k, l) + indexed(A, R, i, j, l, k);
+       result += check_equal(e, 0);
+       e = indexed(A, R, i, j, k, l) - indexed(A, R, j, i, l, k);
+       result += check_equal(e, 0);
+       e = indexed(A, R, i, j, k, l) + indexed(A, R, k, l, j, i);
+       result += check_equal(e, 0);
+
+       e = indexed(A, i, j);
+       result += check_equal(symmetrize(e) + antisymmetrize(e), e);
+       e = indexed(A, sy_symm(), i, j, k, l);
+       result += check_equal(symmetrize(e), e);
+       result += check_equal(antisymmetrize(e), 0);
+       e = indexed(A, sy_anti(), i, j, k, l);
+       result += check_equal(symmetrize(e), 0);
+       result += check_equal(antisymmetrize(e), e);
+
+       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();
+       result += check_equal_simplify(e, indexed(B, i, l));
+
+       result += check_equal(symm_fcn(0, 1) + symm_fcn(1, 0), 2*symm_fcn(0, 1));
+       result += check_equal(anti_fcn(0, 1) + anti_fcn(1, 0), 0);
+       result += check_equal(anti_fcn(0, 0), 0);
+
        return result;
 }
 
-static unsigned scalar_product_check(void)
+static unsigned scalar_product_check()
 {
        // check scalar product replacement
 
@@ -187,7 +228,7 @@ static unsigned scalar_product_check(void)
        return result;
 }
 
-static unsigned edyn_check(void)
+static unsigned edyn_check()
 {
        // Relativistic electrodynamics
 
@@ -203,19 +244,17 @@ static unsigned edyn_check(void)
 
        // Lorentz transformation matrix (boost along x axis)
        matrix L(4, 4);
-       L.set(0, 0, gamma);
-       L.set(0, 1, -beta*gamma);
-       L.set(1, 0, -beta*gamma);
-       L.set(1, 1, gamma);
-       L.set(2, 2, 1); L.set(3, 3, 1);
+       L =       gamma, -beta*gamma, 0, 0,
+           -beta*gamma,       gamma, 0, 0,
+                     0,           0, 1, 0,
+                     0,           0, 0, 1;
 
        // Electromagnetic field tensor
-       matrix F(4, 4, lst(
-                0, -Ex, -Ey, -Ez,
+       matrix F(4, 4);
+       F =  0, -Ex, -Ey, -Ez,
                Ex,   0, -Bz,  By,
                Ey,  Bz,   0, -Bx,
-               Ez, -By,  Bx // 0
-       ));
+               Ez, -By,  Bx,   0;
 
        // Indices
        symbol s_mu("mu"), s_nu("nu"), s_rho("rho"), s_sigma("sigma");
@@ -274,14 +313,14 @@ static unsigned edyn_check(void)
        return result;
 }
 
-static unsigned spinor_check(void)
+static unsigned spinor_check()
 {
        // check identities of the spinor metric
 
        unsigned result = 0;
 
        symbol psi("psi");
-       spinidx A(symbol("A"), 2), B(symbol("B"), 2), C(symbol("C"), 2);
+       spinidx A(symbol("A")), B(symbol("B")), C(symbol("C")), D(symbol("D"));
        ex A_co = A.toggle_variance(), B_co = B.toggle_variance();
        ex e;
 
@@ -303,16 +342,59 @@ static unsigned spinor_check(void)
        result += check_equal_simplify(e, -indexed(psi, A_co));
        e = spinor_metric(A_co, B_co) * indexed(psi, A);
        result += check_equal_simplify(e, indexed(psi, B_co));
+       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);
+       result += check_equal_simplify(e, 0);
 
        return result;
 }
 
-unsigned exam_indexed(void)
+static unsigned dummy_check()
+{
+       // check dummy index renaming/repositioning
+
+       unsigned result = 0;
+
+       symbol p("p"), q("q");
+       idx i(symbol("i"), 3), j(symbol("j"), 3), n(symbol("n"), 3);
+       varidx mu(symbol("mu"), 4), nu(symbol("nu"), 4);
+       ex e;
+
+       e = indexed(p, i) * indexed(q, i) - indexed(p, j) * indexed(q, j);
+       result += check_equal_simplify(e, 0);
+
+       e = indexed(p, i) * indexed(p, i) * indexed(q, j) * indexed(q, j)
+         - indexed(p, n) * indexed(p, n) * indexed(q, j) * indexed(q, j);
+       result += check_equal_simplify(e, 0);
+
+       e = indexed(p, mu, mu.toggle_variance()) - indexed(p, nu, nu.toggle_variance());
+       result += check_equal_simplify(e, 0);
+
+       e = indexed(p, mu.toggle_variance(), nu, mu) * indexed(q, i)
+         - indexed(p, mu, nu, mu.toggle_variance()) * indexed(q, i);
+       result += check_equal_simplify(e, 0);
+
+       e = indexed(p, mu, mu.toggle_variance()) - indexed(p, nu.toggle_variance(), nu);
+       result += check_equal_simplify(e, 0);
+       e = indexed(p, mu.toggle_variance(), mu) - indexed(p, nu, nu.toggle_variance());
+       result += check_equal_simplify(e, 0);
+
+       // GiNaC 1.2.1 had a bug here because p.i*p.i -> (p.i)^2
+       e = indexed(p, i) * indexed(p, i) * indexed(p, j) + indexed(p, j);
+       ex fi = exprseq(e.get_free_indices());
+       if (!fi.is_equal(exprseq(j))) {
+               clog << "get_free_indices(" << e << ") erroneously returned "
+                    << fi << " instead of (.j)" << endl;
+               ++result;
+       }
+
+       return result;
+}
+
+unsigned exam_indexed()
 {
        unsigned result = 0;
        
        cout << "examining indexed objects" << flush;
-       clog << "----------indexed objects:" << endl;
 
        result += delta_check();  cout << '.' << flush;
        result += metric_check();  cout << '.' << flush;
@@ -321,13 +403,12 @@ unsigned exam_indexed(void)
        result += scalar_product_check();  cout << '.' << flush;
        result += edyn_check();  cout << '.' << flush;
        result += spinor_check(); cout << '.' << flush;
-       
-       if (!result) {
-               cout << " passed " << endl;
-               clog << "(no output)" << endl;
-       } else {
-               cout << " failed " << endl;
-       }
+       result += dummy_check(); cout << '.' << flush;
        
        return result;
 }
+
+int main(int argc, char** argv)
+{
+       return exam_indexed();
+}