From 4e5e7ba629c63455c001257bbea2fcc2d2314389 Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Fri, 31 May 2013 17:06:26 -0400 Subject: [PATCH] PEP 8 test_pybmolic, plus add one identity. --- test/test_pymbolic.py | 119 ++++++++++++++---------------------------- 1 file changed, 38 insertions(+), 81 deletions(-) diff --git a/test/test_pymbolic.py b/test/test_pymbolic.py index 71e3848..33173ba 100644 --- a/test/test_pymbolic.py +++ b/test/test_pymbolic.py @@ -25,7 +25,7 @@ THE SOFTWARE. import pymbolic.primitives as prim import pytest - +from pymbolic.mapper import IdentityMapper def test_expand(): @@ -36,15 +36,11 @@ def test_expand(): expand(u) - - def test_substitute(): from pymbolic import parse, substitute, evaluate u = parse("5+x.min**2") xmin = parse("x.min") - assert evaluate(substitute(u, {xmin:25})) == 630 - - + assert evaluate(substitute(u, {xmin: 25})) == 630 def test_fft_with_floats(): @@ -63,9 +59,6 @@ def test_fft_with_floats(): assert la.norm(f_a-f_a_numpy) < 1e-10 - - -from pymbolic.mapper import IdentityMapper class NearZeroKiller(IdentityMapper): def map_constant(self, expr): if isinstance(expr, complex): @@ -80,9 +73,6 @@ class NearZeroKiller(IdentityMapper): return expr - - - def test_fft(): numpy = pytest.importorskip("numpy") @@ -108,8 +98,6 @@ def test_fft(): print("result[%d] = %s" % (i, line)) - - def test_sparse_multiply(): numpy = pytest.importorskip("numpy") pytest.importorskip("scipy") @@ -129,11 +117,10 @@ def test_sparse_multiply(): assert la.norm(mat_vec-mat_vec_2) < 1e-14 - def test_no_comparison(): from pymbolic import parse - x = parse("17+3*x") + x = parse("17+3*x") y = parse("12-5*y") def expect_typeerror(f): @@ -150,8 +137,6 @@ def test_no_comparison(): expect_typeerror(lambda: x >= y) - - def test_parser(): from pymbolic import parse parse("(2*a[1]*b[1]+2*a[0]*b[0])*(hankel_1(-1,sqrt(a[1]**2+a[0]**2)*k) " @@ -183,11 +168,9 @@ def test_parser(): from pymbolic import variables f, x, y, z = variables("f x y z") - assert parse("f((x,y),z)") == f((x,y),z) - assert parse("f((x,),z)") == f((x,),z) - assert parse("f(x,(y,z),z)") == f(x,(y,z),z) - - + assert parse("f((x,y),z)") == f((x, y), z) + assert parse("f((x,),z)") == f((x,), z) + assert parse("f(x,(y,z),z)") == f(x, (y, z), z) def test_structure_preservation(): @@ -197,10 +180,7 @@ def test_structure_preservation(): assert x == x2 - - - -@pytest.mark.parametrize("dims", [2,3,4,5]) +@pytest.mark.parametrize("dims", [2, 3, 4, 5]) # START_GA_TEST def test_geometric_algebra(dims): pytest.importorskip("numpy") @@ -215,10 +195,10 @@ def test_geometric_algebra(dims): vec5 = MV(np.random.randn(dims)) # Fundamental identity - assert ((vec1 ^ vec2) + (vec1|vec2)).close_to(vec1*vec2) + assert ((vec1 ^ vec2) + (vec1 | vec2)).close_to(vec1*vec2) # Antisymmetry - assert (vec1^vec2^vec3).close_to(-vec2^vec1^vec3) + assert (vec1 ^ vec2 ^ vec3).close_to(- vec2 ^ vec1 ^ vec3) vecs = [vec1, vec2, vec3, vec4, vec5] @@ -226,10 +206,10 @@ def test_geometric_algebra(dims): from operator import xor as outer assert reduce(outer, vecs).close_to(0) - assert ( vec1.inv()*vec1 ).close_to( 1 ) - assert ( vec1*vec1.inv() ).close_to( 1 ) - assert ( (1/vec1)*vec1 ).close_to( 1 ) - assert ( vec1/vec1 ).close_to( 1 ) + assert (vec1.inv()*vec1).close_to(1) + assert (vec1*vec1.inv()).close_to(1) + assert ((1/vec1)*vec1).close_to(1) + assert (vec1/vec1).close_to(1) for a, b, c in [ (vec1, vec2, vec3), @@ -241,16 +221,14 @@ def test_geometric_algebra(dims): ]: # Associativity - assert ((a*b)*c).close_to( - a*(b*c)) - assert ((a^b)^c).close_to( - a^(b^c)) + assert ((a*b)*c).close_to(a*(b*c)) + assert ((a ^ b) ^ c).close_to(a ^ (b ^ c)) # The inner product is not associative. # scalar product - assert ( (c*b).project(0) ) .close_to( b.scalar_product(c) ) - assert ( (c.rev()*b).project(0) ) .close_to( b.rev().scalar_product(c) ) - assert ( (b.rev()*b).project(0) ) .close_to( b.norm_squared() ) + assert ((c*b).project(0)) .close_to(b.scalar_product(c)) + assert ((c.rev()*b).project(0)) .close_to(b.rev().scalar_product(c)) + assert ((b.rev()*b).project(0)) .close_to(b.norm_squared()) assert b.norm_squared() >= 0 assert c.norm_squared() >= 0 @@ -262,67 +240,46 @@ def test_geometric_algebra(dims): # (3.18) in [DFM] assert abs(b.scalar_product(a ^ c) - (b >> a).scalar_product(c)) < 1e-13 + # duality, (3.20) in [DFM] - assert ((a ^ b) << c) .close_to( a << (b << c) ) + assert ((a ^ b) << c) .close_to(a << (b << c)) + + # two definitions of the dual agree: (1.2.26) in [HS] + # and (sec 3.5.3) in [DFW] + assert (c << c.I.rev()).close_to(c | c.I.rev()) # inverse for div in list(b.gen_blades()) + [vec1, vec1.I]: - assert ( div.inv()*div ).close_to( 1 ) - assert ( div*div.inv() ).close_to( 1 ) - assert ( (1/div)*div ).close_to( 1 ) - assert ( div/div ).close_to( 1 ) - assert ((c/div)*div ).close_to( c ) - assert ((c*div)/div).close_to( c ) + assert (div.inv()*div).close_to(1) + assert (div*div.inv()).close_to(1) + assert ((1/div)*div).close_to(1) + assert (div/div).close_to(1) + assert ((c/div)*div).close_to(c) + assert ((c*div)/div).close_to(c) # reverse properties (Sec 2.9.5 [DFM]) assert c.rev().rev() == c - assert (b^c).rev() .close_to( (c.rev() ^ b.rev()) ) + assert (b ^ c).rev() .close_to((c.rev() ^ b.rev())) # dual properties # (1.2.26) in [HS] - assert c.dual() .close_to( c|c.I.rev() ) - assert c.dual() .close_to( c*c.I.rev() ) + assert c.dual() .close_to(c | c.I.rev()) + assert c.dual() .close_to(c*c.I.rev()) # involution properties (Sec 2.9.5 DFW) assert c.invol().invol() == c - assert (b^c).invol() .close_to( (b.invol() ^ c.invol()) ) + assert (b ^ c).invol() .close_to((b.invol() ^ c.invol())) # commutator properties - # Jacobi identity (1.1.56c) in [HS] - assert ( a.x(b.x(c)) + b.x(c.x(a)) + c.x(a.x(b)) ).close_to(0) + # Jacobi identity (1.1.56c) in [HS] or (8.2) in [DFW] + assert (a.x(b.x(c)) + b.x(c.x(a)) + c.x(a.x(b))).close_to(0) + # (1.57) in [HS] - assert a.x(b*c) .close_to( a.x(b)*c + b*a.x(c)) + assert a.x(b*c) .close_to(a.x(b)*c + b*a.x(c)) # END_GA_TEST - - - - -def playground(): - import numpy as np - from pymbolic.geometric_algebra import MultiVector as MV - - dims = 2 - vec1 = MV(np.random.randn(dims)) - vec2 = MV(np.random.randn(dims)) - vec3 = MV(np.random.randn(dims)) - vec4 = MV(np.random.randn(dims)) - vec5 = MV(np.random.randn(dims)) - - a = vec3^vec4 - print((a.rev()*a).project(0)) - print(a.scalar_product(a.rev())) - - #print(a.norm_squared()) - #print(((a.rev()*a).project(0) ).close_to( a.norm_squared() )) - - - - - - if __name__ == "__main__": import sys if len(sys.argv) > 1: -- GitLab