def _evalf_(self, f, x, a, b, parent=None, algorithm=None):
        """
        Returns numerical approximation of the integral

        EXAMPLES::

            sage: from sage.symbolic.integration.integral import definite_integral
            sage: h = definite_integral(sin(x)*log(x)/x^2, x, 1, 2); h
            integrate(log(x)*sin(x)/x^2, x, 1, 2)
            sage: h.n() # indirect doctest
            0.14839875208053...

        TESTS:

        Check if #3863 is fixed::

            sage: integrate(x^2.7 * e^(-2.4*x), x, 0, 3).n()
            0.154572952320790
        """
        from sage.gsl.integration import numerical_integral
        # The gsl routine returns a tuple, which also contains the error.
        # We only return the result.
        return numerical_integral(f, a, b)[0]
Beispiel #2
0
    def _evalf_(self, f, x, a, b, parent=None):
        """
        Returns numerical approximation of the integral
        
        EXAMPLES::

            sage: from sage.symbolic.integration.integral import definite_integral
            sage: h = definite_integral(sin(x)*log(x)/x^2, x, 1, 2); h
            integrate(log(x)*sin(x)/x^2, x, 1, 2)
            sage: h.n() # indirect doctest
            0.14839875208053...

        TESTS:

        Check if #3863 is fixed::

            sage: integrate(x^2.7 * e^(-2.4*x), x, 0, 3).n()
            0.154572952320790
        """
        from sage.gsl.integration import numerical_integral
        # The gsl routine returns a tuple, which also contains the error.
        # We only return the result.
        return numerical_integral(f, a, b)[0]
Beispiel #3
0
def Li(x, eps_rel=None, err_bound=False):
    r"""
    Return value of the function Li(x) as a real double field element.
    
    This is the function
    
    .. math::
    
                \int_2^{x} dt / \log(t).     
    
    
    
    The function Li(x) is an approximation for the number of primes up
    to `x`. In fact, the famous Riemann Hypothesis is
    equivalent to the statement that for `x \geq 2.01` we have
    
    .. math::
    
                 |\pi(x) - Li(x)| \leq \sqrt{x} \log(x).     
    
    
    For "small" `x`, `Li(x)` is always slightly bigger
    than `\pi(x)`. However it is a theorem that there are (very
    large, e.g., around `10^{316}`) values of `x` so
    that `\pi(x) > Li(x)`. See
    "A new bound for the smallest x with `\pi(x) > li(x)`",
    Bays and Hudson, Mathematics of Computation, 69 (2000) 1285-1296.
    
    ALGORITHM: Computed numerically using GSL.
    
    INPUT:
    
    
    -  ``x`` - a real number = 2.
    
    
    OUTPUT:
    
    
    -  ``x`` - a real double
    
    
    EXAMPLES::
    
        sage: Li(2)
        0.0
        sage: Li(5)
        2.58942452992
        sage: Li(1000)
        176.56449421
        sage: Li(10^5)
        9628.76383727
        sage: prime_pi(10^5)
        9592
        sage: Li(1)
        Traceback (most recent call last):
        ...
        ValueError: Li only defined for x at least 2.    
    
    ::
    
        sage: for n in range(1,7):
        ...    print '%-10s%-10s%-20s'%(10^n, prime_pi(10^n), Li(10^n))
        10        4         5.12043572467       
        100       25        29.080977804        
        1000      168       176.56449421        
        10000     1229      1245.09205212       
        100000    9592      9628.76383727       
        1000000   78498     78626.5039957
    """
    x = float(x)
    if x < 2:
        raise ValueError, "Li only defined for x at least 2."
    if eps_rel:
        ans = numerical_integral(_one_over_log, 2, float(x), eps_rel=eps_rel)
    else:
        ans = numerical_integral(_one_over_log, 2, float(x))
    if err_bound:
        return real_double.RDF(ans[0]), ans[1]
    else:
        return real_double.RDF(ans[0])
Beispiel #4
0
def Li(x, eps_rel=None, err_bound=False):
    r"""
    Return value of the function Li(x) as a real double field element.
    
    This is the function
    
    .. math::
    
                \int_2^{x} dt / \log(t).     
    
    
    
    The function Li(x) is an approximation for the number of primes up
    to `x`. In fact, the famous Riemann Hypothesis is
    equivalent to the statement that for `x \geq 2.01` we have
    
    .. math::
    
                 |\pi(x) - Li(x)| \leq \sqrt{x} \log(x).     
    
    
    For "small" `x`, `Li(x)` is always slightly bigger
    than `\pi(x)`. However it is a theorem that there are (very
    large, e.g., around `10^{316}`) values of `x` so
    that `\pi(x) > Li(x)`. See
    "A new bound for the smallest x with `\pi(x) > li(x)`",
    Bays and Hudson, Mathematics of Computation, 69 (2000) 1285-1296.
    
    ALGORITHM: Computed numerically using GSL.
    
    INPUT:
    
    
    -  ``x`` - a real number = 2.
    
    
    OUTPUT:
    
    
    -  ``x`` - a real double
    
    
    EXAMPLES::
    
        sage: Li(2)
        0.0
        sage: Li(5)
        2.58942452992
        sage: Li(1000)
        176.56449421
        sage: Li(10^5)
        9628.76383727
        sage: prime_pi(10^5)
        9592
        sage: Li(1)
        Traceback (most recent call last):
        ...
        ValueError: Li only defined for x at least 2.    
    
    ::
    
        sage: for n in range(1,7):
        ...    print '%-10s%-10s%-20s'%(10^n, prime_pi(10^n), Li(10^n))
        10        4         5.12043572467       
        100       25        29.080977804        
        1000      168       176.56449421        
        10000     1229      1245.09205212       
        100000    9592      9628.76383727       
        1000000   78498     78626.5039957
    """
    x = float(x)
    if x < 2:
        raise ValueError, "Li only defined for x at least 2."
    if eps_rel:
        ans = numerical_integral(_one_over_log, 2, float(x),
                             eps_rel=eps_rel)
    else:
        ans = numerical_integral(_one_over_log, 2, float(x))
    if err_bound:
        return real_double.RDF(ans[0]), ans[1]
    else:
        return real_double.RDF(ans[0])