Example #1
0
        def f(v):
            n = len(v)
            if n == self.complex_gram_matrix().nrows():
                i = (w - a) / b

                def z(x):
                    re, im = x.parts()
                    return frac(re) + i * frac(im)

                return vector(z(i * x) for x in v)
            L = [0] * n
            for i in range(n // 2):
                u = (v[i + i] + v[i + i + 1] * a) / b
                L[i + i] = frac(-a * u - b * v[i + i + 1])
                L[i + i + 1] = frac(u)
            return vector(L)
Example #2
0
        def f(v):
            n = len(v)
            if n == self.complex_gram_matrix().nrows():
                zeta = (w + b - a) / (b + b)
                isqrt3 = 2 * zeta - 1

                def z(x):
                    re, im = x.parts()
                    return frac(re) + isqrt3 * frac(im)

                return vector(z(zeta * x) for x in v)
            L = [0] * n
            for i in range(n // 2):
                L[i + i] = frac((c * v[i + i] - d * v[i + i + 1]) / (b + b))
                L[i + i + 1] = frac((v[i + i] + e * v[i + i + 1]) / (b + b))
            return vector(L)
def diophantine_approx(alpha, Q):
    r"""
    Return a diophantine approximation p/q to real number alpha following
    Dirichlet Theorem (1842).

    .. NOTES::

        This function is very slow compared to using continued fractions.
        It tests all of the possible values of q.

    INPUT:

    - `alpha` -- real number
    - `Q` -- real number > 1

    OUTPUT:

    - a tuple of integers (p,q) such that `|\alpha q-p|<1/Q`.

    EXAMPLES::

        sage: from sage_sample import diophantine_approx
        sage: diophantine_approx(pi, 10)
        (22, 7)
        sage: diophantine_approx(pi, 100)
        (22, 7)
        sage: diophantine_approx(pi, 200)
        (355, 113)
        sage: diophantine_approx(pi, 1000)
        (355, 113)
        sage: diophantine_approx(pi, 35000) # not tested, code is very slow
    """
    for q in range(1, Q):
        if frac(alpha * q) <= 1. / Q or 1 - 1. / Q <= frac(alpha * q):
            p = round(alpha * q)
            return (p, q)
Example #4
0
 def a(x):
     c, d = x.parts()
     return frac(c) + g * frac(d)
Example #5
0
 def z(x):
     re, im = x.parts()
     return frac(re) + isqrt3 * frac(im)