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 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 cosh_dist(self, other): """ Returns cosh of the distance of this finite point to another finite point:: sage: from sage.all import * sage: a = FinitePoint(CIF(1,2),RIF(3)) sage: b = FinitePoint(CIF(4,5),RIF(6)) sage: a.cosh_dist(b) # doctest: +NUMERIC12 1.7500000000000000? """ # The distance between two points in the upper half plane model is # given by # 2 2 # (x2-x1) + (y2-y1) # dist( (x1,y1), (x2, y2) = arcosh( 1 + --------------------- ) # 2 * y1 * y2 # # according to # http://en.wikipedia.org/wiki/Poincar%C3%A9_half-plane_model . # # For the upper half space model, y1 and y2 are the two heights # t and (x2-x1)^2 is the square of the absolute value of the difference # of the two z. r = 1 + (((self.t - other.t)**2 + _abs_sqr(self.z - other.z)) / (2 * self.t * other.t)) RIF = r.parent() return r.intersection(RIF(1, sage.all.Infinity))
def round_RBF_to_half_int(x): x = RIF(x) try: k = x.unique_round() if (x - k).contains_zero(): return int(k) except ValueError: pass try: k = (2*x).unique_round() if (2*x - k).contains_zero(): return float(k)/2 except ValueError: pass try: return float(x) except TypeError: # old version of Sage return float(x.n(x.prec()))
def round_RBF_to_half_int(x): x = RIF(x) try: k = x.unique_round() if (x - k).contains_zero(): return int(k) except ValueError: pass try: k = (2*x).unique_round() if (2*x - k).contains_zero(): return float(k)/2 except ValueError: pass try: return float(x) except TypeError: # old version of Sage return float(x.n(x.prec()))
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 run_test(): from sage.all import RIF, sin, Infinity intervals = [ RIF(sin(1.2 * i), sin(1.2 * i) + sin(1.43 * i)**2) for i in range(200) ] t = _IntervalTreeTester() for i, interval in enumerate(intervals): t.insert(interval, i) if i % 50 == 0: for j in intervals: t.check_find_result(j) t.check_consistency() num_true = len(intervals) num_have = len(t.find(RIF(-Infinity, Infinity))) if num_true != num_have: raise Exception("Inconsistent number of intervals: %d %d" % (num_true, num_have))
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 to_finite_point(m): if not (isinstance(m, ExtendedMatrix) and m.isOrientationReversing): raise Exception("Expected orientation-reversing extended matrix.") a = m.matrix[0, 0] c = m.matrix[1, 0] RIF = a.real().parent() # Image of infinity p = a / c pt = FinitePoint(p, RIF(1)) imgPt = pt.translate_PGL(m) imgHeight = imgPt.t return FinitePoint(p, imgHeight.sqrt())
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 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 plot_bounds(dop, ini=None, pt=None, eps=None, **kwds): r""" EXAMPLES:: sage: from ore_algebra import * sage: from ore_algebra.analytic.naive_sum import * sage: Dops, x, Dx = DifferentialOperators() sage: plot_bounds(Dx - 1, [CBF(1)], CBF(i)/2, RBF(1e-20)) Graphics object consisting of 5 graphics primitives sage: plot_bounds(x*Dx^3 + 2*Dx^2 + x*Dx, eps=1e-8) Graphics object consisting of 5 graphics primitives sage: dop = x*Dx^2 + Dx + x sage: plot_bounds(dop, eps=1e-8, ....: ini=LogSeriesInitialValues(0, {0: (1, 0)}, dop)) Graphics object consisting of 5 graphics primitives sage: dop = ((x^2 + 10*x + 50)*Dx^10 + (5/9*x^2 + 50/9*x + 155/9)*Dx^9 ....: + (-10/3*x^2 - 100/3*x - 190/3)*Dx^8 + (30*x^2 + 300*x + 815)*Dx^7 ....: + (145*x^2 + 1445*x + 3605)*Dx^6 + (5/2*x^2 + 25*x + 115/2)*Dx^5 ....: + (20*x^2 + 395/2*x + 1975/4)*Dx^4 + (-5*x^2 - 50*x - 130)*Dx^3 ....: + (5/4*x^2 + 25/2*x + 105/4)*Dx^2 + (-20*x^2 - 195*x - 480)*Dx ....: + 5*x - 10) sage: plot_bounds(dop, pol_part_len=4, bound_inverse="solve", eps=1e-10) # long time Graphics object consisting of 5 graphics primitives """ import sage.plot.all as plot from sage.all import VectorSpace, QQ, RIF from ore_algebra.analytic.bounds import abs_min_nonzero_root if ini is None: ini = _random_ini(dop) if pt is None: rad = abs_min_nonzero_root(dop.leading_coefficient()) pt = QQ(2) if rad == infinity else RIF(rad/2).simplest_rational() if eps is None: eps = RBF(1e-50) logger.info("point: %s", pt) logger.info("initial values: %s", ini) recd = [] maj = bounds.DiffOpBound(dop, max_effort=0, **kwds) series_sum(dop, ini, pt, eps, stride=1, record_bounds_in=recd, maj=maj) ref_sum = recd[-1][1][0].add_error(recd[-1][2]) recd[-1:] = [] # Note: this won't work well when the errors get close to the double # precision underflow threshold. err = [(psum[0]-ref_sum).abs() for n, psum, _ in recd] large = float(1e200) # plot() is not robust to large values error_plot_upper = plot.line( [(n, v.upper()) for (n, v) in enumerate(err) if abs(float(v.upper())) < large], color="lightgray", scale="semilogy") error_plot = plot.line( [(n, v.lower()) for (n, v) in enumerate(err) if abs(float(v.lower())) < large], color="black", scale="semilogy") bound_plot_lower = plot.line( [(n, bound.lower()) for n, _, bound in recd if abs(float(bound.lower())) < large], color="lightblue", scale="semilogy") bound_plot = plot.line( [(n, bound.upper()) for n, _, bound in recd if abs(float(bound.upper())) < large], color="blue", scale="semilogy") title = repr(dop) + " @ x=" + repr(pt) title = title if len(title) < 80 else title[:77]+"..." myplot = error_plot_upper + error_plot + bound_plot_lower + bound_plot ymax = myplot.ymax() if ymax < float('inf'): txt = plot.text(title, (myplot.xmax(), ymax), horizontal_alignment='right', vertical_alignment='top') myplot += txt return myplot