Skip to content

Commit

Permalink
Fix assertEqual for difficult expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
meuns committed Dec 7, 2019
1 parent 3c4a5b8 commit 5fc94d2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 37 deletions.
57 changes: 26 additions & 31 deletions test/test_chapter13.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,46 @@
from sympy import Symbol, S, sqrt
from galgebra.ga import Ga

USE_DIAGONAL_G = True


class TestChapter13(TestCase):

def setUp(self):
"""
Initialize CGA.
"""
g = [
[ 1, 0, 0, 0, 0],
[ 0, 1, 0, 0, 0],
[ 0, 0, 1, 0, 0],
[ 0, 0, 0, 1, 0],
[ 0, 0, 0, 0, -1],
]

ga, e, e_1, e_2, e_3, eb = Ga.build('e e1 e2 e3 eb', g=g)
o = S.Half * (e + eb)
inf = eb - e

self.ga = ga
self.o = o
self.e_1 = e_1
self.e_2 = e_2
self.e_3 = e_3
self.inf = inf

"""
# test_13_2_2 will fails with this
g = [
[ 0, 0, 0, 0, -1],
[ 0, 1, 0, 0, 0],
[ 0, 0, 1, 0, 0],
[ 0, 0, 0, 1, 0],
[-1, 0, 0, 0, 0],
]
if USE_DIAGONAL_G:
# This is way faster with galgebra but o and inf can't be printed...
g = [
[ 1, 0, 0, 0, 0],
[ 0, 1, 0, 0, 0],
[ 0, 0, 1, 0, 0],
[ 0, 0, 0, 1, 0],
[ 0, 0, 0, 0, -1],
]

ga, e, e_1, e_2, e_3, eb = Ga.build('e e1 e2 e3 eb', g=g)
o = S.Half * (e + eb)
inf = eb - e

else:
g = [
[ 0, 0, 0, 0, -1],
[ 0, 1, 0, 0, 0],
[ 0, 0, 1, 0, 0],
[ 0, 0, 0, 1, 0],
[-1, 0, 0, 0, 0],
]

ga, o, e_1, e_2, e_3, inf = Ga.build('o e1 e2 e3 inf', g=g)

ga, o, e_1, e_2, e_3, inf = Ga.build('o e1 e2 e3 inf', g=g)
self.ga = ga
self.o = o
self.e_1 = e_1
self.e_2 = e_2
self.e_3 = e_3
self.inf = inf
"""

def vector(self, x, y, z):
"""
Expand Down
28 changes: 22 additions & 6 deletions test/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import unittest

from sympy import S, simplify
from sympy import expand, S, simplify
from galgebra.ga import Ga
from galgebra.mv import Mv
from galgebra.metric import Simp


def com(A, B):
Expand All @@ -24,19 +25,29 @@ def assertEqual(self, first, second):
if isinstance(second, Mv):
second = second.obj

# We need to help sympy a little...
first = Simp.apply(expand(first))
second = Simp.apply(expand(second))

# Check
diff = simplify(first - second)

self.assertTrue(diff == 0, "\n%s\n==\n%s\n%s" % (first, second, diff))

def assertProjEqual(self, X, Y):
def assertProjEqual(self, first, second):
"""
Compare two points, two planes or two lines up to a scalar.
"""
assert isinstance(X, Mv)
assert isinstance(Y, Mv)
assert isinstance(first, Mv)
assert isinstance(second, Mv)

# TODO: this should use Mv methods and not the derived test case methods...
first /= self.norm(first)
second /= self.norm(second)

X /= self.norm(X)
Y /= self.norm(Y)
# We need to help sympy a little...
X = Simp.apply(expand(first.obj))
Y = Simp.apply(expand(second.obj))

# We can't easily retrieve the sign, so we test both
diff = simplify(X.obj - Y.obj)
Expand All @@ -55,6 +66,11 @@ def assertNotEqual(self, first, second):
if isinstance(second, Mv):
second = second.obj

# We need to help sympy a little...
first = Simp.apply(expand(first))
second = Simp.apply(expand(second))

# Check
diff = simplify(first - second)

self.assertTrue(diff != 0, "\n%s\n!=\n%s\n%s" % (first, second, diff))

0 comments on commit 5fc94d2

Please sign in to comment.