def run_tests(self): from sage.all import CIF, RIF pts = [ FinitePoint(CIF(1.2, 5.2), RIF(2.4)), FinitePoint(CIF(3.2, 4.2), RIF(1.4)), FinitePoint(CIF(2.2, -3.2), RIF(0.4)) ] t = FiniteTriangle(pts) for i in range(3): m = t.reflections[1][i] if not abs(m[0,0] + m[1,1]) < 1e-6: raise Exception('%r' % m) for i, pt in enumerate(pts): if not pt.translate_PGL(t.reflections[0][i]).dist(pt) < 1e-6: raise Exception("Vertex reflection not fixing") for j in range(3): if i != j: pt_t = pt.translate_PGL(t.reflections[1][j]) if not pt_t.dist(pt) < 1e-6: raise Exception( "Side reflection not fixing %r %r %r" % ( pt, pt_t, pt_t.dist(pt))) if not pt.translate_PGL(t.reflections[2][0]).dist(pt) < 1e-6: raise Exception("Plane reflection not fixing %r %r %r" % ( pt, pt.translate_PGL(t.reflections[2][0]), pt.translate_PGL(t.reflections[2][0]).dist(pt)))
def test_plane_reflection(self): from sage.all import CIF zs = [CIF(1.2, 3.2), CIF(4.2, 2.2), CIF(-1.1, 3.5)] pts = [ProjectivePoint(z) for z in zs] m = PlaneReflection.from_three_projective_points(*pts) if not m.isOrientationReversing: raise Exception("Orientation preserving not expected") for pt in pts: # They should be fixed if not pt.translate(m).is_close_to(pt): raise Exception("Point not fixed by plane reflection")
def _get_projective_endpoints_of_line(vertices, vertex_reflections): e = PrimitivesDistanceEngine([0,0], vertex_reflections) endpoints, h = e.fixed_projective_points_or_bounding_halfsphere() if not endpoints: raise Exception("Blah") CIF = vertices[0].z.parent() for i in range(1, 4): m = _adjoint2( ProjectivePoint.matrix_taking_0_1_inf_to_given_points( endpoints[0], ProjectivePoint(CIF(i)), endpoints[1])) if m.det().abs() > 0: try: vs = [ v.translate_PGL(m) for v in vertices ] except: continue if not all([not v.z != 0 for v in vs]): raise Exception("Images of vertices supposed to be " "on vertical line.") if vs[0].t > vs[1].t: return endpoints[::-1] if vs[0].t < vs[1].t: return endpoints raise Exception("Could not find endpoints")
def test_point_reflection(self): from sage.all import RIF, CIF pt = FinitePoint(CIF(1.3, 4.2), RIF(2.6)) pt2 = PointReflection.to_finite_point( PointReflection.from_finite_point(pt)) if not pt.dist(pt2) < 1e-6: raise Exception("%r %r %r" % (pt, pt2, pt.dist(pt2)))
def test_plane_line(self): from sage.all import CIF pts_plane = [ ProjectivePoint(CIF(-1)), ProjectivePoint(CIF(1)), ProjectivePoint(CIF(0, 1)) ] pts_line = [ ProjectivePoint(CIF(sage.all.e)), ProjectivePoint(CIF(-sage.all.e)) ] plane = PlaneReflection.from_three_projective_points(*pts_plane) line = LineReflection.from_two_projective_points(*pts_line) pp = PlaneLineDistanceEngine(plane, line) if not abs(1 - pp.dist()) < 1e-10: raise Exception("Wrong distance: %r" % pp.dist())
def matrix_multiplication_works(self, matrices): from sage.all import RIF, CIF, prod a = FinitePoint(CIF(RIF(3.5), RIF(-3.0)), RIF(8.5)) a0 = a.translate_PGL(prod(matrices)) for m in matrices[::-1]: a = a.translate_PGL(m) if not a.dist(a0) < 1e-6: raise Exception("Distance %r" % a.dist(a0))
def _intersection_of_two_lines_from_endpoints(pts_line1, pts_line2): m = matrix_taking_0_1_inf_to_given_points(pts_line1[0], pts_line1[1], pts_line2[0]) z = pts_line2[1].translate(_adjoint2(m)).as_ideal_point() if z == Infinity: raise Exception("Unexpected Infinity") CIF = z.parent() zReal = z.real() t = (zReal * 2 - 1).sqrt() / 2 return FinitePoint(CIF(zReal), t).translate_PGL(m)
def images_have_same_distance(self, m): from sage.rings.real_mpfi import RealIntervalFieldElement from sage.all import RIF, CIF a = FinitePoint(CIF(RIF(3.5), RIF(-3.0)), RIF(8.5)) b = FinitePoint(CIF(RIF(4.5), RIF(-4.5)), RIF(9.6)) d_before = a.dist(b) a = a.translate_PGL(m) b = b.translate_PGL(m) d_after = a.dist(b) if not isinstance(d_before, RealIntervalFieldElement): raise Exception("Expected distance to be RIF") if not isinstance(d_after, RealIntervalFieldElement): raise Exception("Expected distance to be RIF") if not abs(d_before - d_after) < 1e-12: raise Exception("Distance changed %r %r" % (d_before, d_after))
def compute_inradius_and_incenter(projectivePoints): """ Radius and center of an inscribed sphere of an ideal tetrahedron. The function expects four :class:`ProjectivePoint` and returns a pair of a ``RealIntervalField`` element and a :class:`FinitePoint`:: sage: from sage.all import * sage: z0 = ProjectivePoint(CIF(0), inverted = True) sage: z1 = ProjectivePoint(CIF(0)) sage: z2 = ProjectivePoint(CIF(0, RIF(3.4142, 3.4143))) sage: z3 = ProjectivePoint(CIF(RIF(3.4142, 3.4143), 0)) sage: ProjectivePoint.compute_inradius_and_incenter([z0, z1, z2, z3]) # doctest: +NUMERIC12 (0.317?, FinitePoint(1.0000? + 1.0000?*I, 3.108?)) """ # We assume that one of the points is at infinity. if not len(projectivePoints) == 4: raise Exception("Expecting 4 ProjectivePoint's") pointClosestToInf = -1 distToInf = sage.all.Infinity for i, projectivePoint in enumerate(projectivePoints): if projectivePoint.inverted: d = abs(projectivePoint.z).upper() if d < distToInf: pointClosestToInf = i distToInf = d if pointClosestToInf == -1: return compute_inradius_and_incenter( [ projectivePoint.as_ideal_point() for projectivePoint in projectivePoints ]) zInv = projectivePoints[pointClosestToInf].z CIF = zInv.parent() gl_matrix = matrix(CIF, [[ 1, 0], [-zInv, 1]]) sl_matrix = CIF(sage.all.I) * gl_matrix inv_sl_matrix = _adjoint2(sl_matrix) transformedIdealPoints = [ p.translate(gl_matrix).as_ideal_point() for i, p in enumerate(projectivePoints) if i != pointClosestToInf ] inradius, transformedInCenter = ( _compute_inradius_and_incenter_with_one_point_at_infinity( transformedIdealPoints)) return inradius, transformedInCenter.translate_PSL(inv_sl_matrix)
def test_plane_point(self): from sage.all import RealIntervalField, ComplexIntervalField CIF = ComplexIntervalField(120) RIF = RealIntervalField(120) pts = [ ProjectivePoint(CIF(1.01)), ProjectivePoint(CIF(3)), ProjectivePoint(CIF(2, 1)) ] pt = FinitePoint(CIF(2, 5), RIF(3)) plane = PlaneReflection.from_three_projective_points(*pts) p = PlanePointDistanceEngine(plane, PointReflection.from_finite_point(pt)) e = p.endpoint() if not abs(p.dist() - pt.dist(e)) < 1e-20: raise Exception("Distance incorrect %r %r" % (p.dist(), pt.dist(e)))
def compute_incenter_of_triangle(projectivePoints): """ Computes incenter of an ideal triangle. The function expects three :class:`ProjectivePoint` and returns a :class:`FinitePoint`:: sage: from sage.all import * sage: z0 = ProjectivePoint(CIF(0), inverted = True) sage: z1 = ProjectivePoint(CIF(0)) sage: z2 = ProjectivePoint(CIF(1)) sage: ProjectivePoint.compute_incenter_of_triangle([z0, z1, z2]) FinitePoint(0.50000000000000000?, 0.866025403784439?) """ if not len(projectivePoints) == 3: raise ValueError("Expecting 3 ProjectivePoint's") pointClosestToInf = -1 distToInf = sage.all.Infinity for i, projectivePoint in enumerate(projectivePoints): if projectivePoint.inverted: d = abs(projectivePoint.z).upper() if d < distToInf: pointClosestToInf = i distToInf = d if pointClosestToInf == -1: return compute_incenter_of_triangle( [ projectivePoint.as_ideal_point() for projectivePoint in projectivePoints ]) zInv = projectivePoints[pointClosestToInf].z CIF = zInv.parent() gl_matrix = matrix(CIF, [[ 1, 0], [-zInv, 1]]) sl_matrix = CIF(sage.all.I) * gl_matrix inv_sl_matrix = _adjoint2(sl_matrix) transformedIdealPoints = [ p.translate(gl_matrix).as_ideal_point() for i, p in enumerate(projectivePoints) if i != pointClosestToInf ] transformedInCenter = ( _compute_incenter_of_triangle_with_one_point_at_infinity( transformedIdealPoints)) return transformedInCenter.translate_PSL(inv_sl_matrix)
def fromComplexIntervalFieldAndIdealPoint(CIF, z): """ Constructs a :class:`ProjectivePoint` from a complex interval or ``idealPoint.Infinity``:: sage: from sage.all import CIF sage: from snappy.verify.upper_halfspace import ideal_point sage: z = CIF(1, 3) sage: ProjectivePoint.fromComplexIntervalFieldAndIdealPoint(CIF, z) ProjectivePoint(1 + 3*I) sage: ProjectivePoint.fromComplexIntervalFieldAndIdealPoint(CIF, ideal_point.Infinity) ProjectivePoint(0, inverted = True) """ if z == Infinity: return ProjectivePoint(CIF(0), inverted = True) return ProjectivePoint(z)
def test_plane_plane(self): from sage.all import RealIntervalField, ComplexIntervalField CIF = ComplexIntervalField(120) RIF = RealIntervalField(120) pts = [[ ProjectivePoint(CIF(1)), ProjectivePoint(CIF(3)), ProjectivePoint(CIF(2, 1)) ], [ ProjectivePoint(CIF(-1)), ProjectivePoint(CIF(-3)), ProjectivePoint(CIF(-2, 1)) ]] planes = [ PlaneReflection.from_three_projective_points(*pt) for pt in pts ] p = PlanePlaneDistanceEngine(*planes) e = p.endpoints() if not abs(p.dist() - e[0].dist(e[1])) < 1e-20: raise Exception("Distance incorrect %r %r" % (p.dist(), e[0].dist(e[1]))) e_baseline = [ FinitePoint(CIF(1.5), RIF(0.75).sqrt()), FinitePoint(CIF(-1.5), RIF(0.75).sqrt()) ] for e0, e_baseline0 in zip(e, e_baseline): if not e0.dist(e_baseline0) < 1e-15: raise Exception("%s %s" % (e0, e_baseline0))
def my_example(self): from sage.all import CIF, RIF finiteTriangle = FiniteTriangle([ FinitePoint(CIF(0), RIF(2)), FinitePoint(CIF(0.5), RIF(3)), FinitePoint(CIF(-1), RIF(2.5)) ]) idealTriangle = IdealTriangle([ ProjectivePoint(CIF(0, 1)), ProjectivePoint(CIF(1, 0)), ProjectivePoint(CIF(-1, 0)) ]) #print finiteTriangle.reflections[2][0] #print idealTriangle.reflections[1][0] e = FiniteIdealTriangleDistanceEngine(finiteTriangle, idealTriangle) d = e.compute_lowerbound() if d < 0.0001: raise Exception()
def test_matrix_taking_pts(self): from sage.all import CIF tri = [ ProjectivePoint(CIF(0)), ProjectivePoint(CIF(1)), ProjectivePoint(CIF(0), True) ] zs = [ CIF(1.2, 3.2), CIF(4.2, 2.2), CIF(-1.1, 3.5) ] import itertools for invs in itertools.product(*3*[[False, True]]): pts = [ ProjectivePoint(z, inverted = inv) for inv, z in zip(invs, zs) ] m = ProjectivePoint.matrix_taking_0_1_inf_to_given_points(*pts) for t, pt in zip(tri, pts): if not t.translate(m).is_close_to(pt): raise Exception("Points not matching")
def matrix2(self): from sage.all import RIF, CIF, matrix return matrix([[CIF(RIF(0.3), RIF(-1.4)), CIF(RIF(3.6), RIF(6.3))], [CIF(RIF(-0.3), RIF(1.1)), CIF(1)]])
def matrix1(self): from sage.all import RIF, CIF, matrix return matrix([[CIF(RIF(1.3), RIF(-0.4)), CIF(RIF(5.6), RIF(2.3))], [CIF(RIF(-0.3), RIF(0.1)), CIF(1)]])
def test_incenter(self): from snappy.snap.t3mlite import Perm4 from sage.all import RIF, CIF, matrix epsilon = 1e-12 bigEpsilon = 1e-6 z0 = ProjectivePoint(CIF(0), inverted = True) z1 = ProjectivePoint(CIF(0)) z2 = ProjectivePoint(CIF(1)) for z in [CIF(0,1), CIF(0.5, 0.5), CIF(1.2, 1.3)]: z3_1 = ProjectivePoint(z) z3_2 = ProjectivePoint(z.conjugate()) r1, p1 = ProjectivePoint.compute_inradius_and_incenter([z0, z1, z2, z3_1]) r2, p2 = ProjectivePoint.compute_inradius_and_incenter([z0, z1, z2, z3_2]) if not abs(r1 - r2) < epsilon: raise Exception("Different radii %r %r" % (r1, r2)) if not abs(p1.t - p2.t) < epsilon: raise Exception("Different heights") if not abs(p1.z - p2.z.conjugate()) < epsilon: raise Exception("Not conjugate") if not abs(p1.dist(p2) - 2 * r1) < epsilon: raise Exception("Wrong radius") z3 = ProjectivePoint(CIF(1.2, 1.3)) r1, p1 = ProjectivePoint.compute_inradius_and_incenter([z0, z1, z2, z3]) for p in Perm4.S4(): r2, p2 = ProjectivePoint.compute_inradius_and_incenter( self.perm4_act(p,[z0, z1, z2, z3])) if not abs(r1 - r2) < epsilon: raise Exception("Different radii %r %r" % (r1, r2)) if not p1.dist(p2) < bigEpsilon: raise Exception("Different incenter %r %r %r" % (p1.dist(p2), p1, p2)) m1 = self.matrix1() m2 = self.matrix2() for m in [m1, m2, m1 * m2, m1 * m1 * m2]: r2, p2 = ProjectivePoint.compute_inradius_and_incenter( [ z.translate(m) for z in [z0, z1, z2, z3]]) if not abs(r1 - r2) < epsilon: raise Exception("Different radii %r %r" % (r1, r2)) if not p1.translate_PGL(m).dist(p2) < bigEpsilon: raise Exception("Different incenter %r %r %r" % (p1.translate_PGL(m).dist(p2), p1.translate_PGL(m), p2)) z_values = [ CIF(1.1, 1), CIF(0.2, 1.3), CIF(0.4, -0.3), CIF(-0.1, 0.8) ] r1, p1 = ProjectivePoint.compute_inradius_and_incenter( [ ProjectivePoint(z_value) for z_value in z_values ]) import itertools for invs in itertools.product(*4*[[False, True]]): zs = [ ProjectivePoint(1 / z_value if inv else z_value, inverted = inv) for inv, z_value in zip(invs, z_values) ] r2, p2 = ProjectivePoint.compute_inradius_and_incenter(zs) if not abs(r1 - r2) < epsilon: raise Exception("Different radii %r %r" % (r1, r2)) if not p1.dist(p2) < bigEpsilon: raise Exception("Different incenter %r %r %r" % (p1.dist(p2), p2))