lingologos-geometry / tests /test_kernels.py
tailot's picture
geometry corpus with quantified certificates and honest attestations
a3bc68c verified
Raw History Blame Contribute Delete
10.9 kB
"""Exact checks of the six deterministic kernels."""
import math
import random
from fractions import Fraction as F
import pytest
from geometry.kernels import (
Affine,
Circle,
DegenerateConfigurationError,
Line,
NoSolutionError,
Point,
Surd,
circle_through_three_points,
intersect_lines,
intersection_point,
orthocenter,
position,
power_of_point,
rational_rotation,
secant_products_equal,
secant_through,
tangent_secant_relation,
tangents_from_point,
to_fraction,
triangle_centers,
vandermonde_matrix,
)
from geometry.kernels.common import determinant, solve_linear
RNG = random.Random(7)
def rnd_point(lo=-9, hi=9):
return Point(F(RNG.randint(lo, hi), RNG.choice([1, 1, 2, 3])), F(RNG.randint(lo, hi), RNG.choice([1, 1, 2, 3])))
def rnd_triangle():
while True:
a, b, c = rnd_point(), rnd_point(), rnd_point()
if (b - a).cross(c - a) != 0:
return a, b, c
# -- exact arithmetic ------------------------------------------------------------
def test_floats_are_rejected():
with pytest.raises(TypeError):
to_fraction(0.5)
with pytest.raises(TypeError):
Point(1.0, 2)
assert to_fraction("3/4") == F(3, 4)
def test_surd_field_laws_and_sign():
for _ in range(200):
m = RNG.choice([2, 3, 5, 7, 11, 13])
x = Surd.make(F(RNG.randint(-9, 9), 3), F(RNG.randint(-9, 9), 5), m)
y = Surd.make(F(RNG.randint(-9, 9), 2), F(RNG.randint(-9, 9), 7), m)
assert x + y == y + x
assert x * y == y * x
assert (x + y) - y == x
if y.sign() != 0:
assert (x / y) * y == x
assert (x * x.conjugate()) == x.norm()
diff = float(x) - float(y)
if abs(diff) > 1e-9:
assert (x - y).sign() == (1 if diff > 0 else -1)
def test_surd_collapses_perfect_squares():
assert Surd.make(1, 2, 9) == 7
assert Surd.make(0, 1, F(1, 4)) == F(1, 2)
assert Surd.make(3, 0, 5) == 3
with pytest.raises(NoSolutionError):
Surd.make(0, 1, -1)
def test_line_canonical_form():
assert Line(2, 4, 6) == Line(1, 2, 3) == Line(-1, -2, -3) == Line(F(1, 2), 1, F(3, 2))
with pytest.raises(DegenerateConfigurationError):
Line(0, 0, 1)
with pytest.raises(DegenerateConfigurationError):
Line.through(Point(1, 1), Point(1, 1))
def test_linear_algebra_helpers():
assert determinant([[1, 2], [3, 4]]) == -2
assert solve_linear([[2, 1], [1, 3]], [5, 10]) == [1, 3]
with pytest.raises(DegenerateConfigurationError):
solve_linear([[1, 2], [2, 4]], [1, 2])
# -- kernel 1: line-line intersection -----------------------------------------------
def test_line_intersection_known_cases():
l1 = Line.through(Point(0, 0), Point(2, 2))
l2 = Line.through(Point(0, 2), Point(2, 0))
res = intersect_lines(l1, l2)
assert res.kind == "point" and res.point == Point(1, 1)
par = intersect_lines(Line(1, 1, 0), Line(1, 1, 5))
assert par.kind == "parallel" and par.point is None
assert intersect_lines(Line(1, 1, 0), Line(2, 2, 0)).kind == "coincident"
with pytest.raises(NoSolutionError):
intersection_point(Line(1, 1, 0), Line(2, 2, 7))
def test_line_intersection_random_residuals_and_symmetry():
for _ in range(300):
a, b, c, d = rnd_point(), rnd_point(), rnd_point(), rnd_point()
if a == b or c == d:
continue
l1, l2 = Line.through(a, b), Line.through(c, d)
res = intersect_lines(l1, l2)
assert intersect_lines(l2, l1).kind == res.kind
if res.kind == "point":
assert l1.contains(res.point) and l2.contains(res.point)
assert intersect_lines(l2, l1).point == res.point
else:
assert l1.a * l2.b - l2.a * l1.b == 0
# -- kernel 2: circle through three points -------------------------------------------
def test_circumcircle_known_and_vandermonde_determinant():
res = circle_through_three_points(Point(0, 0), Point(4, 0), Point(0, 3))
assert res.center == Point(2, F(3, 2)) and res.circle.r2 == F(25, 4)
assert res.radius == F(5, 2)
assert res.coefficients == (-4, -3, 0)
pts = [Point(0, 0), Point(4, 0), Point(0, 3)]
assert determinant(vandermonde_matrix(pts)) == 12 # twice the signed area
def test_circumcircle_random_equidistance_and_area():
for _ in range(200):
a, b, c = rnd_triangle()
res = circle_through_three_points(a, b, c)
assert a.dist2(res.center) == b.dist2(res.center) == c.dist2(res.center) == res.circle.r2
assert res.determinant == (b - a).cross(c - a)
def test_circumcircle_collinear_raises():
with pytest.raises(DegenerateConfigurationError):
circle_through_three_points(Point(0, 0), Point(1, 1), Point(3, 3))
with pytest.raises(DegenerateConfigurationError):
circle_through_three_points(Point(0, 0), Point(0, 0), Point(3, 3))
# -- kernel 3: tangents -----------------------------------------------------------------
def test_tangents_exact_rational_contacts():
res = tangents_from_point(Point(5, 0), Circle(Point(0, 0), F(9)))
assert res.kind == "two"
xs = sorted((c.x, c.y) for c in res.contact_points)
assert xs == [(F(9, 5), F(-12, 5)), (F(9, 5), F(12, 5))]
assert res.tangent_length2 == 16 and res.half_aperture_sin2 == F(9, 25)
def test_tangents_irrational_contacts_have_exact_residuals():
for _ in range(150):
o = rnd_point()
r2 = F(RNG.randint(1, 30))
p = rnd_point(-12, 12)
res = tangents_from_point(p, Circle(o, r2))
pw = p.dist2(o) - r2
assert res.kind == ("none" if pw < 0 else "single" if pw == 0 else "two")
for c, line in zip(res.contact_points, res.lines):
dx, dy = c.x - o.x, c.y - o.y
assert dx * dx + dy * dy == r2
assert (c.x - p.x) * dx + (c.y - p.y) * dy == 0
a, b, cc = line
assert a * c.x + b * c.y + cc == 0 and a * p.x + b * p.y + cc == 0
if res.kind == "two":
assert len(res.contact_points) == 2 and res.contact_points[0] != res.contact_points[1]
s = res.half_aperture_sin2
assert s == r2 / p.dist2(o)
a1, a2 = res.angles
half = abs(a1 - a2) / 2
half = min(half, math.pi - half)
assert math.isclose(math.sin(half) ** 2, float(s), rel_tol=1e-9)
def test_tangent_from_point_on_circle_and_inside():
on = tangents_from_point(Point(3, 4), Circle(Point(0, 0), F(25)))
assert on.kind == "single" and on.contact_points[0].x == 3 and on.lines[0][0] == 3
inside = tangents_from_point(Point(1, 1), Circle(Point(0, 0), F(25)))
assert inside.kind == "none" and inside.contact_points == ()
# -- kernel 4: orthocenter and centroid ---------------------------------------------------
def test_triangle_centers_known_and_euler_relation():
tc = triangle_centers(Point(0, 0), Point(4, 0), Point(1, 3))
assert tc.centroid == Point(F(5, 3), 1) and tc.orthocenter == Point(1, 1) and tc.circumcenter == Point(2, 1)
assert tc.euler_relation_holds
for _ in range(150):
a, b, c = rnd_triangle()
t = triangle_centers(a, b, c)
assert t.centroid.scale(3) == t.orthocenter + t.circumcenter.scale(2)
assert all(l.contains(t.orthocenter) for l in t.altitudes)
assert all(l.contains(t.centroid) for l in t.medians)
def test_right_triangle_orthocenter_is_the_right_vertex():
assert orthocenter(Point(0, 0), Point(4, 0), Point(0, 3)) == Point(0, 0)
def test_degenerate_triangle_has_no_centers():
with pytest.raises(DegenerateConfigurationError):
triangle_centers(Point(0, 0), Point(1, 1), Point(2, 2))
# -- kernel 5: power of a point ------------------------------------------------------------------
def test_power_and_positions():
c = Circle(Point(0, 0), F(9))
assert power_of_point(Point(5, 0), c) == 16 and position(Point(5, 0), c) == "outside"
assert power_of_point(Point(3, 0), c) == 0 and position(Point(3, 0), c) == "on"
assert power_of_point(Point(1, 0), c) == -8 and position(Point(1, 0), c) == "inside"
def test_secant_secant_and_tangent_secant_relations_hold_exactly():
for _ in range(150):
o = rnd_point()
c = Circle(o, F(RNG.randint(2, 30)))
p = rnd_point(-12, 12)
dirs = []
while len(dirs) < 2:
d = Point(RNG.randint(-4, 4), RNG.randint(-4, 4))
if d == Point(0, 0):
continue
try:
secant_through(p, d, c)
dirs.append(d)
except NoSolutionError:
continue
equal, p1, p2 = secant_products_equal(p, c, dirs[0], dirs[1])
assert equal and p1 == p2 == power_of_point(p, c)
if power_of_point(p, c) >= 0:
ok, pw, prod = tangent_secant_relation(p, c, dirs[0])
assert ok and pw == prod
def test_secant_missing_circle_and_interior_tangent():
c = Circle(Point(0, 0), F(1))
with pytest.raises(NoSolutionError):
secant_through(Point(5, 5), Point(1, 0), c)
with pytest.raises(NoSolutionError):
tangent_secant_relation(Point(0, 0), c, Point(1, 0))
with pytest.raises(DegenerateConfigurationError):
secant_through(Point(5, 5), Point(0, 0), c)
# -- kernel 6: affine maps ---------------------------------------------------------------------------
def test_affine_basic_maps():
t = Affine.translation(2, -1)
assert t.apply(Point(1, 1)) == Point(3, 0)
r = Affine.rotation(0, 1)
assert r.apply(Point(1, 0)) == Point(0, 1)
rc = Affine.rotation_about(Point(1, 1), 0, 1)
assert rc.apply(Point(2, 1)) == Point(1, 2) and rc.apply(Point(1, 1)) == Point(1, 1)
with pytest.raises(ValueError):
Affine.rotation(F(1, 2), F(1, 2))
with pytest.raises(DegenerateConfigurationError):
Affine.scaling(0, 1)
def test_affine_inverse_composition_and_rigidity():
for _ in range(150):
c, s = rational_rotation(F(RNG.randint(-5, 5), RNG.randint(1, 4)))
assert c * c + s * s == 1
m = Affine.translation(RNG.randint(-5, 5), RNG.randint(-5, 5)).compose(Affine.rotation_about(rnd_point(), c, s))
assert m.is_rigid() and m.determinant() == 1
p, q = rnd_point(), rnd_point()
assert m.inverse().apply(m.apply(p)) == p
assert m.apply(p).dist2(m.apply(q)) == p.dist2(q)
n = Affine.scaling(2, 3)
assert not n.is_isometry()
assert (m.compose(n)).apply(p) == m.apply(n.apply(p))
def test_reflection_is_an_involution_with_fixed_line():
line = Line.through(Point(0, 1), Point(2, 2))
f = Affine.reflection(line)
assert f.determinant() == -1 and f.is_isometry() and not f.is_rigid()
for _ in range(50):
p = rnd_point()
assert f.apply(f.apply(p)) == p
assert f.apply(Point(2, 2)) == Point(2, 2)