Browse code

Add test for circumsphere.

Add unit test for test_circumsphere.

philippeitis authored on 16/12/2019 05:40:31 • Joseph Weston committed on 05/05/2020 19:10:25
Showing 1 changed files
... ...
@@ -60,3 +60,36 @@ def test_triangulation_find_opposing_vertices_raises_if_simplex_is_invalid():
60 60
 
61 61
     with pytest.raises(ValueError):
62 62
         tri.get_opposing_vertices((2, 3, 5))
63
+
64
+
65
+def test_circumsphere():
66
+    """ Test that circumsphere works correctly for a random center and random points on a sphere """
67
+    from adaptive.learner.triangulation import circumsphere, fast_norm
68
+    from numpy import allclose
69
+    from numpy.random import normal, uniform
70
+    center_diff_err = "Calculated center [%s] differs from true center [%s]\n"
71
+
72
+    def generate_random_sphere_points(dim, radius=0):
73
+        """ Refer to https://math.stackexchange.com/a/1585996 """
74
+
75
+        vec = [None] * (dim + 1)
76
+        center = uniform(-100, 100, dim)
77
+        radius = uniform(1.0, 100.0) if radius == 0 else radius
78
+        for i in range(dim + 1):
79
+            points = normal(0, size=dim)
80
+            x = fast_norm(points)
81
+            points = points / x * radius
82
+            vec[i] = tuple(points + center)
83
+
84
+        return radius, center, vec
85
+
86
+    for dim in range(2, 10):
87
+        radius, center, points = generate_random_sphere_points(dim)
88
+        circ_center, circ_radius = circumsphere(points)
89
+        err_msg = ""
90
+        if not allclose(circ_center, center):
91
+            err_msg += center_diff_err % (",".join([str(x) for x in circ_center]), ",".join([str(x) for x in center]))
92
+        if not allclose(radius, circ_radius):
93
+            err_msg += "Calculated radius %s differs from true radius %s" % (circ_radius, radius)
94
+        if err_msg:
95
+            raise AssertionError(err_msg)