Skip to content

Commit c4693c8

Browse files
committed
Refactor chapter 13
1 parent 4689050 commit c4693c8

File tree

1 file changed

+47
-45
lines changed

1 file changed

+47
-45
lines changed

test/test_chapter13.py

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
class TestChapter13(TestCase):
88

9-
def test_13_1_2(self):
9+
def setUp(self):
1010
"""
11-
Points as null vectors.
11+
Initialize CGA.
1212
"""
1313
g = [
1414
[+0, +0, +0, +0, -1],
@@ -18,16 +18,44 @@ def test_13_1_2(self):
1818
[-1, +0, +0, +1, +0],
1919
]
2020

21-
GA, o, e_1, e_2, e_3, inf = Ga.build('o e1 e2 e3 inf', g=g)
21+
self.ga, self.o, self.e_1, self.e_2, self.e_3, self.inf = Ga.build('o e1 e2 e3 inf', g=g)
2222

23-
def vector(x, y, z):
24-
return x * e_1 + y * e_2 + z * e_3
23+
def vector(self, x, y, z):
24+
"""
25+
Make a vector.
26+
"""
27+
return x * self.e_1 + y * self.e_2 + z * self.e_3
2528

26-
def point(v):
27-
return o + v + S.Half * v * v * inf
29+
def point(self, alpha, v):
30+
"""
31+
Make a point.
32+
"""
33+
return alpha * (self.o + v + S.Half * v * v * self.inf)
2834

29-
p = point(vector(Symbol('px', real=True), Symbol('py', real=True), Symbol('pz', real=True)))
30-
q = point(vector(Symbol('qx', real=True), Symbol('qy', real=True), Symbol('qz', real=True)))
35+
def dual_plane(self, n, delta):
36+
"""
37+
Make a dual plane.
38+
"""
39+
return n + delta * self.inf
40+
41+
def dual_sphere(self, alpha, c, r):
42+
"""
43+
Make a dual sphere.
44+
"""
45+
return alpha * (c - S.Half * r * r * self.inf)
46+
47+
def dual_im_sphere(self, alpha, c, r):
48+
"""
49+
Make a dual imaginary sphere.
50+
"""
51+
return alpha * (c + S.Half * r * r * self.inf)
52+
53+
def test_13_1_2(self):
54+
"""
55+
Points as null vectors.
56+
"""
57+
p = self.point(1, self.vector(Symbol('px', real=True), Symbol('py', real=True), Symbol('pz', real=True)))
58+
q = self.point(1, self.vector(Symbol('qx', real=True), Symbol('qy', real=True), Symbol('qz', real=True)))
3159

3260
self.assertEqual(p | q, -S.Half * q * q + p | q - S.Half * p * p)
3361
self.assertEqual(p | q, -S.Half * (q - p) * (q - p))
@@ -36,59 +64,33 @@ def test_13_1_3(self):
3664
"""
3765
General vectors represent dual planes and spheres.
3866
"""
39-
g = [
40-
[+0, +0, +0, +0, -1],
41-
[+0, +1, +0, +0, +0],
42-
[+0, +0, +1, +0, +0],
43-
[+0, +0, +0, +1, +0],
44-
[-1, +0, +0, +1, +0],
45-
]
46-
47-
GA, o, e_1, e_2, e_3, inf = Ga.build('o e1 e2 e3 inf', g=g)
48-
49-
def vector(x, y, z):
50-
return x * e_1 + y * e_2 + z * e_3
51-
52-
# Point
53-
def point(alpha, v):
54-
return alpha * (o + v + S.Half * v * v * inf)
55-
5667
alpha = Symbol('alpha', real=True)
57-
p = point(alpha, vector(Symbol('px', real=True), Symbol('py', real=True), Symbol('pz', real=True)))
68+
p = self.point(alpha, self.vector(Symbol('px', real=True), Symbol('py', real=True), Symbol('pz', real=True)))
5869
self.assertEqual(p | p, S.Zero)
59-
self.assertEqual(inf | p, -alpha)
70+
self.assertEqual(self.inf | p, -alpha)
6071

6172
# Dual plane
62-
def dual_plane(n, delta):
63-
return n + delta * inf
64-
6573
nx = Symbol('nx', real=True)
6674
ny = Symbol('ny', real=True)
6775
nz = Symbol('nz', real=True)
68-
p = dual_plane(vector(nx, ny, nz), Symbol('delta', real=True))
76+
p = self.dual_plane(self.vector(nx, ny, nz), Symbol('delta', real=True))
6977
self.assertEqual(p | p, nx * nx + ny * ny + nz * nz)
70-
self.assertEqual(inf | p, S.Zero)
78+
self.assertEqual(self.inf | p, S.Zero)
7179

7280
# Dual sphere
73-
def dual_sphere(alpha, c, r):
74-
return alpha * (c - S.Half * r * r * inf)
75-
76-
def dual_im_sphere(alpha, c, r):
77-
return alpha * (c + S.Half * r * r * inf)
78-
7981
cx = Symbol('cx', real=True)
8082
cy = Symbol('cy', real=True)
8183
cz = Symbol('cz', real=True)
8284
r = Symbol('r', real=True)
8385

84-
c = point(1, vector(cx, cy, cz))
86+
c = self.point(1, self.vector(cx, cy, cz))
8587
self.assertEqual(c * c, S.Zero)
86-
self.assertEqual(-inf | c, S.One)
88+
self.assertEqual(-self.inf | c, S.One)
8789

88-
s = dual_sphere(alpha, c, r)
90+
s = self.dual_sphere(alpha, c, r)
8991
self.assertEqual(s | s, alpha * alpha * r * r)
90-
self.assertEqual(-inf | s, alpha)
92+
self.assertEqual(-self.inf | s, alpha)
9193

92-
im_s = dual_im_sphere(alpha, c, r)
94+
im_s = self.dual_im_sphere(alpha, c, r)
9395
self.assertEqual(im_s | im_s, -alpha * alpha * r * r)
94-
self.assertEqual(-inf | im_s, alpha)
96+
self.assertEqual(-self.inf | im_s, alpha)

0 commit comments

Comments
 (0)