Download tests/test_hyperbolic_kernels.py from tailot/lingologos-geometry: direct link, hf CLI and curl.
- Browser
- Download file 30 kB
-
https://huggingface.co/datasets/tailot/lingologos-geometry/resolve/main/tests/test_hyperbolic_kernels.py
- Command line
-
hf download hf://datasets/tailot/lingologos-geometry/tests/test_hyperbolic_kernels.py
-
curl -L -o test_hyperbolic_kernels.py https://huggingface.co/datasets/tailot/lingologos-geometry/resolve/main/tests/test_hyperbolic_kernels.py
30 kB
| """Exact certified tests for the hyperbolic kernels of the conformal Poincare disk. | |
| The model under test: points are the rational points of the open unit disk, | |
| x^2 + y^2 < 1 strictly, and the rational points of the boundary circle are a | |
| separate ideal type. Lines are Euclidean diameters with rational direction or | |
| Euclidean circles whose rational center (h, k) and squared radius r2 obey | |
| h^2 + k^2 - r2 = 1 exactly, which is orthogonality to the boundary. | |
| The disk is conformal, so a hyperbolic angle is the Euclidean angle between the | |
| two tangent directions at the common point, and angles travel as (cos^2, sign | |
| of the cosine) pairs: the square keeps every trigonometric expression inside | |
| one quadratic extension and the pair decides every comparison exactly, so a | |
| right angle is cos2 = 0 and an obtuse angle carries sign = -1. | |
| The distance is never represented, because it is a logarithm; only its cosh | |
| enters, and here cosh d = 1 + 2|p - q|^2 / ((1 - |p|^2)(1 - |q|^2)) is rational | |
| for rational points, so cosh^2 and sinh^2 = cosh^2 - 1 are exact rationals. | |
| A pair of lines is classified by the sign of one exact discriminant of the | |
| radical axis substitution; the oracle here recomputes that sign by a route | |
| sharing no algebra with the kernel, from distance and squared-radius incidence | |
| for two circles and from the cross product for a diameter and a circle. | |
| Every oracle is exact: hand-derived rational values, deterministic seeded | |
| families, or independent chord arithmetic over Fractions. No floating point | |
| number appears anywhere in this file. | |
| """ | |
| import json | |
| import random | |
| from fractions import Fraction | |
| import pytest | |
| from geometry.hyperbolic import ( | |
| DegenerateHyperbolicError, | |
| HLine, | |
| HPoint, | |
| HyperbolicError, | |
| HyperbolicTriangle, | |
| IdealPoint, | |
| SignedCos, | |
| angle_between, | |
| angle_sum_relation, | |
| angle_sum_relation_from_angles, | |
| circle_line, | |
| cosh2_distance, | |
| cosh2_values_are_consistent, | |
| diameter, | |
| hline_from_json, | |
| hline_through, | |
| hpoint_from_json, | |
| hyperbolic_point_to_json, | |
| ideal_point_from_json, | |
| intersection, | |
| intersection_kind, | |
| is_disjoint_in_disk, | |
| law_of_cosines_angles_residual, | |
| law_of_cosines_angles_sign_agreement, | |
| law_of_cosines_sides_residual, | |
| law_of_cosines_sides_sign_agreement, | |
| law_of_sines_equal, | |
| parallels_through, | |
| similar_but_not_congruent, | |
| signed_cos_from_json, | |
| sinh2_distance, | |
| ) | |
| from geometry.kernels.common import SurdPoint | |
| def sgn(value): | |
| return (value > 0) - (value < 0) | |
| def rnd_point(rng): | |
| while True: | |
| x = Fraction(rng.randrange(-5, 6), rng.choice([2, 3, 4, 7])) | |
| y = Fraction(rng.randrange(-5, 6), rng.choice([2, 3, 4, 7])) | |
| if x == 0 and y == 0: | |
| continue | |
| if x * x + y * y < 1: | |
| return HPoint(x, y) | |
| def rnd_line(rng): | |
| while True: | |
| p, q = rnd_point(rng), rnd_point(rng) | |
| try: | |
| return hline_through(p, q) | |
| except DegenerateHyperbolicError: | |
| continue | |
| def rnd_pair(rng): | |
| while True: | |
| l1, l2 = rnd_line(rng), rnd_line(rng) | |
| if l1 == l2: | |
| continue | |
| if l1.kind == "diameter" and l2.kind == "diameter": | |
| continue | |
| return l1, l2 | |
| def parallel_pair(rng): | |
| """The diameter through a rational ideal point and a random circle through it.""" | |
| m = rng.randrange(2, 6) | |
| n = rng.randrange(1, m) | |
| u, v = m * m - n * n, 2 * m * n | |
| norm = m * m + n * n | |
| e = IdealPoint(Fraction(u, norm), Fraction(v, norm)) | |
| while True: | |
| h = Fraction(rng.randrange(-4, 5), rng.choice([2, 3])) | |
| k = (1 - h * e.x) / e.y | |
| r2 = h * h + k * k - 1 | |
| if r2 > 0: | |
| return diameter(u, v), circle_line(h, k, r2), e | |
| def antipodal(u, v): | |
| return u.x == -v.x and u.y == -v.y | |
| def on_circle_line(pt, line): | |
| """Membership of an exact point in a circle line, written to avoid powers of algebraic values.""" | |
| acc = line.h * line.h + line.k * line.k - line.r2 | |
| acc = acc - 2 * line.h * pt.x | |
| acc = acc - 2 * line.k * pt.y | |
| acc = acc + pt.x * pt.x | |
| return acc + pt.y * pt.y == 0 | |
| def rnd_triangle(rng): | |
| while True: | |
| a, b, c = rnd_point(rng), rnd_point(rng), rnd_point(rng) | |
| if antipodal(a, b) or antipodal(a, c) or antipodal(b, c): | |
| continue | |
| try: | |
| t = HyperbolicTriangle(a, b, c) | |
| except DegenerateHyperbolicError: | |
| continue | |
| if not t.is_degenerate(): | |
| return t | |
| def oracle_kind(l1, l2): | |
| """Independent classification: Euclidean distance test on the two supporting curves. | |
| A hyperbolic line is the part inside the closed disk of a Euclidean circle | |
| orthogonal to the boundary, or of a line through the origin. Inversion in | |
| the boundary exchanges the two supporting curves, so their two Euclidean | |
| intersection points are exchanged too: either they are real and distinct, | |
| one strictly inside and one strictly outside, or they collapse into one | |
| point fixed by the inversion, which lies on the boundary. Counting | |
| Euclidean meetings therefore decides the classification, and it is done | |
| here with the schoolbook distance test only: for two circles the sign of | |
| (d^2 - (r1 + r2)^2) * (d^2 - (r1 - r2)^2) expanded as | |
| (d^2 - r1^2 - r2^2)^2 - 4 r1^2 r2^2, which stays rational because only | |
| squared radii appear, and for a diameter the squared distance of the | |
| circle center to the line through the origin compared with r^2. The | |
| kernel substitutes a rational parametrisation of the line and reads a | |
| discriminant, so the two routes share no algebra. | |
| """ | |
| if l1.kind == "diameter" and l2.kind == "diameter": | |
| return "intersecting" | |
| if l1.kind == "diameter" or l2.kind == "diameter": | |
| line = l1 if l1.kind == "diameter" else l2 | |
| circ = l2 if l1.kind == "diameter" else l1 | |
| gap = (line.u * circ.k - line.v * circ.h) ** 2 - line.norm2 * circ.r2 | |
| else: | |
| dh, dk = l1.h - l2.h, l1.k - l2.k | |
| d2 = dh * dh + dk * dk | |
| gap = (d2 - l1.r2 - l2.r2) ** 2 - 4 * l1.r2 * l2.r2 | |
| if gap < 0: | |
| return "intersecting" | |
| if gap == 0: | |
| return "parallel" | |
| return "ultraparallel" | |
| def test_point_validation_is_exact_and_strict(): | |
| """HPoint enforces x^2+y^2<1 strictly, IdealPoint exactly x^2+y^2=1, floats and bools are refused.""" | |
| HPoint(Fraction(9, 10), 0) | |
| with pytest.raises(HyperbolicError): | |
| HPoint(Fraction(3, 5), Fraction(4, 5)) | |
| with pytest.raises(HyperbolicError): | |
| HPoint(Fraction(6, 5), 0) | |
| IdealPoint(Fraction(3, 5), Fraction(4, 5)) | |
| with pytest.raises(HyperbolicError): | |
| IdealPoint(Fraction(1, 2), Fraction(1, 2)) | |
| with pytest.raises(TypeError): | |
| HPoint(0.5, 0) | |
| with pytest.raises(TypeError): | |
| HPoint(True, 0) | |
| with pytest.raises(TypeError): | |
| IdealPoint(1.0, 0) | |
| assert HPoint("1/2", 0).x == Fraction(1, 2) | |
| def test_hpoint_value_semantics(): | |
| """HPoint coordinates are Fractions with exact norm2 and dot, and no is_ideal_point shortcut.""" | |
| p = HPoint(Fraction(1, 2), Fraction(1, 3)) | |
| q = HPoint(Fraction(1, 3), Fraction(1, 2)) | |
| assert p.norm2() == Fraction(13, 36) | |
| assert p.dot(q) == Fraction(1, 3) | |
| assert p == HPoint(Fraction(1, 2), Fraction(1, 3)) | |
| assert p != q | |
| assert not hasattr(p, "is_ideal_point") | |
| assert isinstance(p.x + q.x, Fraction) and isinstance(p.x * q.y, Fraction) | |
| assert p.dot(q) == q.dot(p) and p.norm2() == p.dot(p) | |
| def test_line_through_two_points_uniqueness(): | |
| """hline_through gives the unique line through two points; coincident and antipodal pairs are refused.""" | |
| p, q = HPoint(Fraction(1, 2), Fraction(1, 6)), HPoint(Fraction(1, 3), Fraction(1, 3)) | |
| l = hline_through(p, q) | |
| assert l.contains(p) and l.contains(q) | |
| d = hline_through(HPoint(Fraction(1, 2), Fraction(1, 2)), HPoint(Fraction(1, 3), Fraction(1, 3))) | |
| assert d.kind == "diameter" | |
| r = HPoint(Fraction(1, 4), Fraction(1, 4)) | |
| assert d.contains(r) | |
| assert hline_through(HPoint(Fraction(1, 2), Fraction(1, 2)), r) == hline_through(r, HPoint(Fraction(1, 3), Fraction(1, 3))) == d | |
| with pytest.raises(DegenerateHyperbolicError): | |
| hline_through(p, p) | |
| with pytest.raises(DegenerateHyperbolicError): | |
| hline_through(p, HPoint(-p.x, -p.y)) | |
| with pytest.raises(HyperbolicError): | |
| hline_through((0, 0), p) | |
| def test_diameter_canonicalisation_and_collinear_collapse(): | |
| """Diameter directions collapse to one canonical coprime integer pair with fixed sign.""" | |
| a = diameter(Fraction(4, 6), Fraction(2, 6)) | |
| b = diameter(-2, -1) | |
| assert a == b | |
| assert a.u == 2 and a.v == 1 and a.norm2 == 5 | |
| assert diameter(0, -3) == diameter(0, 5) | |
| with pytest.raises(DegenerateHyperbolicError): | |
| diameter(0, 0) | |
| coll = hline_through(HPoint(Fraction(1, 3), 0), HPoint(Fraction(-1, 7), 0)) | |
| assert coll.kind == "diameter" and coll.u == 1 and coll.v == 0 | |
| origin_line = hline_through(HPoint(0, 0), HPoint(Fraction(1, 2), 0)) | |
| assert origin_line == diameter(1, 0) | |
| def test_circle_line_orthogonality_validation(): | |
| """A circle line must satisfy h^2+k^2-r2 = 1 exactly with r2 > 0.""" | |
| for t in (2, 3, 5): | |
| circle_line(t, 0, t * t - 1) | |
| circle_line(1, 1, 1) | |
| with pytest.raises(HyperbolicError): | |
| circle_line(1, 1, 2) | |
| with pytest.raises(HyperbolicError): | |
| circle_line(1, 0, 0) | |
| with pytest.raises(HyperbolicError): | |
| circle_line(0, 0, -1) | |
| with pytest.raises(TypeError): | |
| circle_line(1.5, 1, 1) | |
| with pytest.raises(HyperbolicError): | |
| HLine("hyperbola") | |
| def test_hand_intersection_classification(): | |
| """Hand-derived pairs: one intersecting with exact point, one parallel with exact ideal point, one ultraparallel.""" | |
| d1 = diameter(1, 0) | |
| # Center (5/4, 5/4) is orthogonal (25/16 + 25/16 - 17/8 = 1); on the | |
| # x-axis the circle reads t^2 - (5/2) t + 1 = 0, so t = 1/2 and t = 2. | |
| cc = circle_line(Fraction(5, 4), Fraction(5, 4), Fraction(17, 8)) | |
| assert intersection_kind(d1, cc) == "intersecting" | |
| assert intersection(d1, cc) == [HPoint(Fraction(1, 2), 0)] | |
| assert not is_disjoint_in_disk(d1, cc) | |
| # On diameter (3, 4) the circle (0, 5/4; 9/16) reads (5 t - 1)^2 = 0, so | |
| # the single crossing is t = 1/5, the ideal point (3/5, 4/5). | |
| d2 = diameter(3, 4) | |
| par = circle_line(0, Fraction(5, 4), Fraction(9, 16)) | |
| assert intersection_kind(d2, par) == "parallel" | |
| assert intersection(d2, par) == [IdealPoint(Fraction(3, 5), Fraction(4, 5))] | |
| assert is_disjoint_in_disk(d2, par) | |
| up = circle_line(0, 2, 3) | |
| assert intersection_kind(d1, up) == "ultraparallel" | |
| assert intersection(d1, up) == [] | |
| assert is_disjoint_in_disk(d1, up) | |
| two_d = intersection(diameter(1, 0), diameter(0, 1)) | |
| assert two_d == [HPoint(0, 0)] and intersection_kind(diameter(1, 0), diameter(0, 1)) == "intersecting" | |
| with pytest.raises(HyperbolicError): | |
| intersection(cc, cc) | |
| with pytest.raises(HyperbolicError): | |
| intersection_kind(d1, (0, 0)) | |
| def test_seeded_intersection_against_chord_oracle(): | |
| """For 30 seeded pairs, six sharing an ideal point, the returned points satisfy both line equations and the classification matches chord incidence.""" | |
| rng = random.Random(20241118) | |
| kinds = set() | |
| for index in range(30): | |
| shared = None | |
| if index % 5 == 0: | |
| # Random rational lines are never exactly tangent, so six pairs are | |
| # built sharing one rational ideal point and must classify parallel. | |
| l1, l2, shared = parallel_pair(rng) | |
| else: | |
| l1, l2 = rnd_pair(rng) | |
| kind = intersection_kind(l1, l2) | |
| assert kind == oracle_kind(l1, l2) | |
| kinds.add(kind) | |
| pts = intersection(l1, l2) | |
| if kind == "intersecting": | |
| assert len(pts) == 1 and not is_disjoint_in_disk(l1, l2) | |
| elif kind == "parallel": | |
| assert len(pts) == 1 and pts[0].x * pts[0].x + pts[0].y * pts[0].y == 1 | |
| assert is_disjoint_in_disk(l1, l2) | |
| else: | |
| assert pts == [] and is_disjoint_in_disk(l1, l2) | |
| if shared is not None: | |
| assert pts == [shared] | |
| for pt in pts: | |
| if isinstance(pt, SurdPoint): | |
| for ln in (l1, l2): | |
| if ln.kind == "diameter": | |
| assert (ln.u * pt.y - ln.v * pt.x).sign() == 0 | |
| else: | |
| assert on_circle_line(pt, ln) | |
| assert (pt.x * pt.x + pt.y * pt.y - 1).sign() < 0 | |
| elif isinstance(pt, HPoint): | |
| assert l1.contains(pt) and l2.contains(pt) and pt.norm2() < 1 | |
| else: | |
| assert l1.contains(pt) and l2.contains(pt) | |
| if kind != "parallel": | |
| assert not (isinstance(pt, IdealPoint)) or pt.norm2() == 1 | |
| assert kinds == {"intersecting", "parallel", "ultraparallel"} | |
| def test_intersection_operations_are_symmetric(): | |
| """intersection, intersection_kind and is_disjoint_in_disk are invariant under swapping the two lines.""" | |
| rng = random.Random(777) | |
| for _ in range(20): | |
| l1, l2 = rnd_pair(rng) | |
| assert intersection_kind(l1, l2) == intersection_kind(l2, l1) | |
| assert intersection(l1, l2) == intersection(l2, l1) | |
| assert is_disjoint_in_disk(l1, l2) == is_disjoint_in_disk(l2, l1) | |
| assert is_disjoint_in_disk(l1, l2) == (intersection_kind(l1, l2) != "intersecting") | |
| def test_right_angles_are_exact(): | |
| """Conformality gives cos^2 = 0 exactly at a diameter crossing its own perpendicular chord.""" | |
| right = angle_between(diameter(1, 0), diameter(0, 1), HPoint(0, 0)) | |
| assert right.is_right() and right.cos2 == 0 and right.sign == 0 and right.sin2 == 1 | |
| # The circle (5/4, 0; 9/16) is orthogonal (25/16 - 9/16 = 1) and cuts the | |
| # x-axis at t = 5/4 - 3/4 = 1/2, where its radius (-3/4, 0) is horizontal, | |
| # so its tangent is vertical and the angle with the x-axis is right. | |
| d = diameter(1, 0) | |
| cc = circle_line(Fraction(5, 4), 0, Fraction(9, 16)) | |
| p = HPoint(Fraction(1, 2), 0) | |
| assert cc.contains(p) and intersection(d, cc) == [p] | |
| assert angle_between(d, cc, p) == SignedCos(0, 0) | |
| # Two circles (-6, -1; 36) and (-1, 5; 25) are orthogonal (36 + 1 - 36 = 1, | |
| # 1 + 25 - 25 = 1) and cross at (-6/61, 5/61); the two radii there are | |
| # (360/61, 66/61) and (55/61, -300/61), whose scalar product is | |
| # (19800 - 19800) / 61^2 = 0, so the two tangents are perpendicular too. | |
| o1 = circle_line(-6, -1, 36) | |
| o2 = circle_line(-1, 5, 25) | |
| q = HPoint(Fraction(-6, 61), Fraction(5, 61)) | |
| assert intersection(o1, o2) == [q] | |
| assert angle_between(o1, o2, q) == SignedCos(0, 0) | |
| # The same rule holds at an ideal point: the circle (-1/5, 7/5; 1) is | |
| # orthogonal (1/25 + 49/25 - 1 = 1), contains (3/5, 4/5), and its radius | |
| # (4/5, -3/5) there gives the tangent (-3/5, -4/5), the reversed direction | |
| # of diameter (3, 4), so the two lines touch with cos^2 = 1 and sign -1. | |
| at = IdealPoint(Fraction(3, 5), Fraction(4, 5)) | |
| tangent_circle = circle_line(Fraction(-1, 5), Fraction(7, 5), 1) | |
| assert tangent_circle.contains(at) and diameter(3, 4).contains(at) | |
| assert intersection_kind(diameter(3, 4), tangent_circle) == "parallel" | |
| assert angle_between(diameter(3, 4), tangent_circle, at) == SignedCos(1, -1) | |
| def test_angle_symmetry_and_obtuse_hand_value(): | |
| """Swapping the two lines leaves the SignedCos unchanged; the hand pair gives the exact obtuse value 9/13.""" | |
| # (-3, -1; 9) and (1/2, 2; 13/4) are orthogonal (9 + 1 - 9 = 1 and | |
| # 1/4 + 4 - 13/4 = 1); their radical axis is 7 x + 6 y = 0 and | |
| # (-6/17, 7/17) lies on it with (-6/17 + 3)^2 + (7/17 + 1)^2 = 2601/289 = 9. | |
| c1 = circle_line(-3, -1, 9) | |
| c2 = circle_line(Fraction(1, 2), 2, Fraction(13, 4)) | |
| g = HPoint(Fraction(-6, 17), Fraction(7, 17)) | |
| assert c1.contains(g) and c2.contains(g) | |
| assert intersection_kind(c1, c2) == "intersecting" | |
| assert intersection(c1, c2) == [g] | |
| ang = angle_between(c1, c2, g) | |
| assert ang == SignedCos(Fraction(9, 13), -1) | |
| assert ang.sin2 == Fraction(4, 13) and not ang.is_right() | |
| assert angle_between(c2, c1, g) == ang | |
| with pytest.raises(HyperbolicError): | |
| angle_between(c1, c2, HPoint(0, 0)) | |
| rng = random.Random(31337) | |
| for _ in range(12): | |
| l1, l2 = rnd_pair(rng) | |
| pts = [x for x in intersection(l1, l2) if isinstance(x, HPoint)] | |
| if not pts: | |
| continue | |
| a1 = angle_between(l1, l2, pts[0]) | |
| assert angle_between(l2, l1, pts[0]) == a1 | |
| assert 0 <= a1.cos2 <= 1 and a1.sign in (-1, 0, 1) | |
| def test_signed_cos_canonical_rules(): | |
| """SignedCos forces the canonical (cos2, sign) pairs and from_cos recovers the sign exactly.""" | |
| assert SignedCos(0, 1) == SignedCos(0, 0) | |
| assert SignedCos(0, -1).sign == 0 | |
| with pytest.raises(HyperbolicError): | |
| SignedCos(Fraction(1, 4), 0) | |
| with pytest.raises(HyperbolicError): | |
| SignedCos(Fraction(5, 4), 1) | |
| with pytest.raises(HyperbolicError): | |
| SignedCos(Fraction(-1, 4), 1) | |
| with pytest.raises(HyperbolicError): | |
| SignedCos(Fraction(1, 4), True) | |
| assert SignedCos.from_cos(Fraction(-2, 3)) == SignedCos(Fraction(4, 9), -1) | |
| assert SignedCos.from_cos(0) == SignedCos(0, 0) | |
| assert SignedCos.from_cos(1) == SignedCos(1, 1) | |
| assert SignedCos(Fraction(1, 2), 1).sin2 == Fraction(1, 2) | |
| with pytest.raises(HyperbolicError): | |
| SignedCos.from_cos(Fraction(3, 2)) | |
| def test_cosh2_distance_hand_values_and_identities(): | |
| """cosh^2 of (0,0),(1/2,0) is 25/9; cosh^2 - sinh^2 = 1, the axis half-angle form and a norm bound hold.""" | |
| assert cosh2_distance(HPoint(0, 0), HPoint(Fraction(1, 2), 0)) == Fraction(25, 9) | |
| assert cosh2_distance(HPoint(0, 0), HPoint(0, 0)) == 1 | |
| assert sinh2_distance(HPoint(0, 0), HPoint(Fraction(1, 2), 0)) == Fraction(16, 9) | |
| rng = random.Random(4242) | |
| for _ in range(15): | |
| p, q = rnd_point(rng), rnd_point(rng) | |
| c2 = cosh2_distance(p, q) | |
| assert c2 >= 1 | |
| assert c2 == cosh2_distance(q, p) | |
| assert sinh2_distance(p, q) == c2 - 1 | |
| np, nq = p.norm2(), q.norm2() | |
| den = (1 - np) * (1 - nq) | |
| # |p - q|^2 <= 2 (np + nq) because 2 |p| |q| <= np + nq, and the | |
| # kernel value is ((den + 2 |p-q|^2) / den)^2, so this is a bound. | |
| assert c2 <= ((den + 4 * (np + nq)) / den) ** 2 | |
| rng = random.Random(99001) | |
| for _ in range(10): | |
| # On the x-axis the distance obeys tanh(d/2) = (x - y) / (1 - x y), | |
| # hence cosh d = ((1-xy)^2 + (x-y)^2) / ((1-xy)^2 - (x-y)^2), a closed | |
| # half-angle form that the kernel formula never states. | |
| x = Fraction(rng.randint(-9, 9), 10) | |
| y = Fraction(rng.randint(-9, 9), 10) | |
| top = (1 - x * y) ** 2 + (x - y) ** 2 | |
| bot = (1 - x * y) ** 2 - (x - y) ** 2 | |
| p, q = HPoint(x, 0), HPoint(y, 0) | |
| assert cosh2_distance(p, q) == (top * top) / (bot * bot) | |
| with pytest.raises(TypeError): | |
| cosh2_distance(HPoint(0, 0), (0, 0)) | |
| def test_hand_right_triangle_invariants(): | |
| """The hand right triangle (0,0),(1/2,0),(0,1/2) has the derived angle and side values and both law residuals vanish.""" | |
| t = HyperbolicTriangle(HPoint(0, 0), HPoint(Fraction(1, 2), 0), HPoint(0, Fraction(1, 2))) | |
| assert not t.is_degenerate() | |
| assert t.cosh2_sides() == {"ab": Fraction(25, 9), "bc": Fraction(625, 81), "ca": Fraction(25, 9)} | |
| ang = t.angles() | |
| assert ang["a"] == SignedCos(0, 0) | |
| assert ang["b"] == SignedCos(Fraction(25, 34), 1) | |
| assert ang["c"] == SignedCos(Fraction(25, 34), 1) | |
| assert law_of_cosines_sides_residual(t) == 0 | |
| assert law_of_cosines_angles_residual(t) == 0 | |
| assert law_of_cosines_sides_sign_agreement(t) | |
| assert law_of_cosines_angles_sign_agreement(t) | |
| assert law_of_sines_equal(t) | |
| assert cosh2_values_are_consistent(t) | |
| assert angle_sum_relation(t) == "less_than_pi" | |
| lab, lbc, lca = t.lines() | |
| assert lab.contains(t.a) and lbc.contains(t.b) and lca.contains(t.c) | |
| def test_law_functions_hold_for_seeded_triangles(): | |
| """Both rationalised law residuals vanish with sign agreement for 12 seeded proper triangles.""" | |
| rng = random.Random(5150) | |
| for _ in range(12): | |
| t = rnd_triangle(rng) | |
| assert law_of_cosines_sides_residual(t) == 0 | |
| assert law_of_cosines_angles_residual(t) == 0 | |
| assert law_of_cosines_sides_sign_agreement(t) | |
| assert law_of_cosines_angles_sign_agreement(t) | |
| assert law_of_sines_equal(t) | |
| assert cosh2_values_are_consistent(t) | |
| def test_laws_are_invariant_under_vertex_rotation(): | |
| """Rotating the vertex names permutes the side keys cyclically and keeps the law residuals zero.""" | |
| t = HyperbolicTriangle(HPoint(Fraction(1, 4), 0), HPoint(0, Fraction(1, 3)), HPoint(Fraction(1, 5), Fraction(1, 6))) | |
| rot = HyperbolicTriangle(t.b, t.c, t.a) | |
| s, r = t.cosh2_sides(), rot.cosh2_sides() | |
| assert r["ab"] == s["bc"] and r["bc"] == s["ca"] and r["ca"] == s["ab"] | |
| ang, rang = t.angles(), rot.angles() | |
| assert rang["a"] == ang["b"] and rang["b"] == ang["c"] and rang["c"] == ang["a"] | |
| assert law_of_cosines_sides_residual(t) == law_of_cosines_sides_residual(rot) == 0 | |
| assert law_of_cosines_angles_residual(t) == law_of_cosines_angles_residual(rot) == 0 | |
| def test_degenerate_triangles_are_detected_with_exact_angle_data(): | |
| """Three points on one hyperbolic line are degenerate with angles (0, pi, 0) and vanishing law residuals.""" | |
| t = HyperbolicTriangle(HPoint(Fraction(1, 4), 0), HPoint(Fraction(1, 2), 0), HPoint(Fraction(3, 4), 0)) | |
| assert t.is_degenerate() | |
| ang = t.angles() | |
| assert ang["a"] == SignedCos(1, 1) | |
| assert ang["b"] == SignedCos(1, -1) | |
| assert ang["c"] == SignedCos(1, 1) | |
| assert law_of_cosines_sides_residual(t) == 0 | |
| assert law_of_cosines_angles_residual(t) == 0 | |
| assert angle_sum_relation(t) == "equal_to_pi" | |
| coincident = HyperbolicTriangle(HPoint(0, 0), HPoint(0, 0), HPoint(Fraction(1, 2), 0)) | |
| assert coincident.is_degenerate() | |
| # The circle (0, 5/4; 9/16) is orthogonal (25/16 - 9/16 = 1) and holds the | |
| # two ideal points (+/- 3/5, 4/5); three of its rational disk points obey | |
| # x^2 + (y - 5/4)^2 = 9/16 exactly, so they are collinear in the | |
| # hyperbolic sense too, on a line that is not a diameter. | |
| c = circle_line(0, Fraction(5, 4), Fraction(9, 16)) | |
| for e in (IdealPoint(Fraction(3, 5), Fraction(4, 5)), IdealPoint(Fraction(-3, 5), Fraction(4, 5))): | |
| assert c.contains(e) | |
| b1 = HPoint(Fraction(9, 20), Fraction(13, 20)) | |
| b2 = HPoint(0, Fraction(1, 2)) | |
| b3 = HPoint(Fraction(-9, 20), Fraction(13, 20)) | |
| for x in (b1, b2, b3): | |
| assert c.contains(x) and x.norm2() < 1 | |
| assert hline_through(b1, b2) == hline_through(b2, b3) == c | |
| u = HyperbolicTriangle(b1, b2, b3) | |
| assert u.is_degenerate() | |
| uang = u.angles() | |
| assert uang["a"] == SignedCos(1, 1) and uang["b"] == SignedCos(1, -1) | |
| assert uang["c"] == SignedCos(1, 1) | |
| assert law_of_cosines_sides_residual(u) == law_of_cosines_angles_residual(u) == 0 | |
| assert angle_sum_relation(u) == "equal_to_pi" | |
| def test_parallels_through_hand_point(): | |
| """The two hand witnesses through (1/3,1/4) miss the line through (0,1/3),(1/3,1/3) and at least one is ultraparallel.""" | |
| p = HPoint(Fraction(1, 3), Fraction(1, 4)) | |
| l = hline_through(HPoint(0, Fraction(1, 3)), HPoint(Fraction(1, 3), Fraction(1, 3))) | |
| m1, m2 = parallels_through(p, l) | |
| assert m1 != m2 | |
| kinds = set() | |
| for m in (m1, m2): | |
| assert m.contains(p) | |
| assert is_disjoint_in_disk(m, l) | |
| kinds.add(intersection_kind(m, l)) | |
| assert "ultraparallel" in kinds | |
| with pytest.raises(DegenerateHyperbolicError): | |
| parallels_through(HPoint(0, Fraction(1, 3)), l) | |
| def test_parallels_through_seeded_cases(): | |
| """Every seeded parallels pair passes through the point, misses the line in the disk, and is never a single class pair of intersecting.""" | |
| rng = random.Random(8080) | |
| for _ in range(10): | |
| l = rnd_line(rng) | |
| while True: | |
| p = rnd_point(rng) | |
| if not l.contains(p): | |
| break | |
| m1, m2 = parallels_through(p, l, limit=60) | |
| assert m1 != m2 | |
| for m in (m1, m2): | |
| assert m.contains(p) | |
| assert is_disjoint_in_disk(m, l) | |
| assert intersection_kind(m, l) in ("parallel", "ultraparallel") | |
| def test_similar_but_not_congruent_route_is_degenerate_family(): | |
| """The seeded witnesses have equal angles, different sides, and are both reported degenerate (AAA is congruence for proper triangles).""" | |
| for seed in (3, 7, 11): | |
| t1, t2 = similar_but_not_congruent(seed) | |
| assert t1.angles() == t2.angles() | |
| assert t1.cosh2_sides() != t2.cosh2_sides() | |
| assert t1.is_degenerate() and t2.is_degenerate() | |
| assert angle_sum_relation(t1) == "equal_to_pi" | |
| again = similar_but_not_congruent(7) | |
| x1, x2 = similar_but_not_congruent(7) | |
| y1, y2 = similar_but_not_congruent(7) | |
| assert (x1.a.x, x1.b.x, x1.c.x) == (y1.a.x, y1.b.x, y1.c.x) | |
| assert (x2.a.x, x2.b.x, x2.c.x) == (y2.a.x, y2.b.x, y2.c.x) | |
| def test_angle_sum_relation_is_never_greater_for_proper_triangles(): | |
| """Every proper seeded triangle answers less_than_pi and every degenerate one equal_to_pi.""" | |
| rng = random.Random(6161) | |
| for _ in range(12): | |
| t = rnd_triangle(rng) | |
| assert angle_sum_relation(t) == "less_than_pi" | |
| deg = HyperbolicTriangle(HPoint(Fraction(1, 4), 0), HPoint(Fraction(1, 2), 0), HPoint(Fraction(3, 4), 0)) | |
| assert angle_sum_relation(deg) == "equal_to_pi" | |
| on_circle = HyperbolicTriangle( | |
| HPoint(Fraction(9, 20), Fraction(13, 20)), | |
| HPoint(0, Fraction(1, 2)), | |
| HPoint(Fraction(-9, 20), Fraction(13, 20)), | |
| ) | |
| assert on_circle.is_degenerate() and angle_sum_relation(on_circle) == "equal_to_pi" | |
| # The greater_than_pi answer belongs to the angle predicate, not to a | |
| # triangle: the predicate takes any triple of angles of the interval | |
| # (0, pi), and here cos a = -1/2 and cos b = -2/3 make both angles obtuse, | |
| # so a + b already exceeds pi and the predicate reads cos a + cos b < 0. | |
| synthetic_greater = {"a": SignedCos(Fraction(1, 4), -1), "b": SignedCos(Fraction(4, 9), -1), "c": SignedCos(0, 0)} | |
| assert angle_sum_relation_from_angles(synthetic_greater) == "greater_than_pi" | |
| # No triangle of this dataset reaches undetermined_in_rational_tower: the | |
| # pair route only hesitates when the two square roots of the cosine sum | |
| # cannot be merged inside the rationals, and a survey of 400 seeded proper | |
| # triangles always decided the sign, so the test states no witness for it. | |
| def test_gram_consistency_rejects_impossible_side_data(): | |
| """A fake triangle with cosh^2 sides (100, 2, 2) fails the Gram condition and below-1 values are rejected.""" | |
| class Fake: | |
| def __init__(self, sides): | |
| self._s = sides | |
| def cosh2_sides(self): | |
| return self._s | |
| assert not cosh2_values_are_consistent(Fake({"ab": 100, "bc": 2, "ca": 2})) | |
| assert not cosh2_values_are_consistent(Fake({"ab": Fraction(3, 4), "bc": 2, "ca": 2})) | |
| assert cosh2_values_are_consistent(Fake({"ab": 1, "bc": 1, "ca": 1})) | |
| t = HyperbolicTriangle(HPoint(Fraction(1, 4), 0), HPoint(Fraction(1, 2), 0), HPoint(Fraction(3, 4), 0)) | |
| sides = t.cosh2_sides() | |
| broken = {"ab": sides["ab"] + Fraction(1, 7), "bc": sides["bc"], "ca": sides["ca"]} | |
| fake = Fake(broken) | |
| fake.angles = t.angles | |
| assert law_of_cosines_sides_residual(fake) != 0 | |
| def test_json_round_trips_are_exact(): | |
| """to_json and from_json restore every value type exactly and malformed payloads are rejected.""" | |
| p = HPoint(Fraction(1, 3), Fraction(-2, 7)) | |
| assert hpoint_from_json(p.to_json()) == p | |
| g = IdealPoint(Fraction(-3, 5), Fraction(4, 5)) | |
| assert ideal_point_from_json(g.to_json()) == g | |
| for line in (diameter(2, -3), circle_line(0, Fraction(5, 4), Fraction(9, 16))): | |
| assert hline_from_json(line.to_json()) == line | |
| payload = p.to_json() + g.to_json() + list(diameter(1, 0).to_json().values()) | |
| assert all(isinstance(v, str) for v in payload) | |
| sc = SignedCos(Fraction(16, 25), -1) | |
| assert signed_cos_from_json(sc.to_json()) == sc | |
| with pytest.raises(HyperbolicError): | |
| hpoint_from_json(["3/5", "4/5"]) | |
| with pytest.raises(HyperbolicError): | |
| ideal_point_from_json(["1/2", "1/2"]) | |
| with pytest.raises(HyperbolicError): | |
| hline_from_json({"kind": "circle", "h": "1", "k": "1", "r2": "2"}) | |
| with pytest.raises(HyperbolicError): | |
| signed_cos_from_json({"cos2": "1/4", "sign": "0"}) | |
| def test_hyperbolic_point_to_json_tags_every_point_kind(): | |
| """The tagged serializer names the kind of every returned point and keeps each coordinate exact.""" | |
| p = HPoint(Fraction(1, 3), Fraction(-2, 7)) | |
| assert hyperbolic_point_to_json(p) == {"kind": "point", "x": "1/3", "y": "-2/7"} | |
| assert hpoint_from_json(hyperbolic_point_to_json(p)) == p | |
| g = IdealPoint(Fraction(-3, 5), Fraction(4, 5)) | |
| assert hyperbolic_point_to_json(g) == {"kind": "ideal", "x": "-3/5", "y": "4/5"} | |
| assert ideal_point_from_json(hyperbolic_point_to_json(g)) == g | |
| # The x-axis against the circle (3, 0; 8), orthogonal since 9 - 8 = 1, | |
| # reads x^2 - 6 x + 1 = 0, so the crossing of the disk is 3 - sqrt(8). | |
| surd = intersection(diameter(1, 0), circle_line(3, 0, 8))[0] | |
| assert isinstance(surd, SurdPoint) | |
| assert hyperbolic_point_to_json(surd) == { | |
| "kind": "surd", | |
| "x": {"a": "3", "b": "-1", "m": "8"}, | |
| "y": {"a": "0", "b": "0", "m": "0"}, | |
| } | |
| assert json.dumps(hyperbolic_point_to_json(surd), sort_keys=True) | |
| with pytest.raises(TypeError): | |
| hyperbolic_point_to_json((1, 2)) | |