Ticket #7748: trac_7748-gamma_wrapper.patch

File trac_7748-gamma_wrapper.patch, 8.6 KB (added by burcin, 12 years ago)

wrapper for gamma and incomplete gamma

  • sage/functions/other.py

    # HG changeset patch
    # User Burcin Erocal <burcin@erocal.org>
    # Date 1266687929 -3600
    # Node ID 77f33abe74684be4e3e3ab0ee1abe13aa5706072
    # Parent  a0ce93292e40d05415d90614464b257b5cce98ec
    trac 7748: add wrapper for gamma and incomplete gamma functions to call the
    appropriate one based on number of arguments.
    
    diff --git a/sage/functions/other.py b/sage/functions/other.py
    a b  
    1818from sage.symbolic.function import is_inexact
    1919from sage.functions.log import exp
    2020from sage.functions.transcendental import Ei
     21from sage.libs.mpmath import utils as mpmath_utils
    2122
    2223one_half = ~SR(2)
    2324
     
    394395
    395396        EXAMPLES::
    396397
    397             sage: gamma(CDF(0.5,14))
     398            sage: from sage.functions.other import gamma1
     399            sage: gamma1(CDF(0.5,14))
    398400            -4.05370307804e-10 - 5.77329983455e-10*I
    399             sage: gamma(CDF(I))
     401            sage: gamma1(CDF(I))
    400402            -0.154949828302 - 0.498015668118*I
    401403
    402404        Recall that `\Gamma(n)` is `n-1` factorial::
    403405
    404             sage: gamma(11) == factorial(10)
     406            sage: gamma1(11) == factorial(10)
    405407            True
    406             sage: gamma(6)
     408            sage: gamma1(6)
    407409            120
    408             sage: gamma(1/2)
     410            sage: gamma1(1/2)
    409411            sqrt(pi)
    410             sage: gamma(-1)
     412            sage: gamma1(-1)
    411413            Infinity
    412             sage: gamma(I)
     414            sage: gamma1(I)
    413415            gamma(I)
    414             sage: gamma(x/2)(x=5)
     416            sage: gamma1(x/2)(x=5)
    415417            3/4*sqrt(pi)
    416418
    417             sage: gamma(float(6))
     419            sage: gamma1(float(6))
    418420            120.0
    419             sage: gamma(x)
     421            sage: gamma1(x)
    420422            gamma(x)
    421423       
    422424        ::
    423425       
    424             sage: gamma(pi)
     426            sage: gamma1(pi)
    425427            gamma(pi)
    426             sage: gamma(i)
     428            sage: gamma1(i)
    427429            gamma(I)
    428             sage: gamma(i).n()
     430            sage: gamma1(i).n()
    429431            -0.154949828301811 - 0.498015668118356*I
    430             sage: gamma(int(5))
     432            sage: gamma1(int(5))
    431433            24
    432434
    433435
    434             sage: plot(gamma(x),(x,1,5))
    435 
    436         The gamma function only works with input that can be coerced to the
    437         Symbolic Ring::
    438        
    439             sage: Q.<i> = NumberField(x^2+1)
    440             sage: gamma(i)
    441             doctest:...: DeprecationWarning: Calling symbolic functions with arguments that cannot be coerced into symbolic expressions is deprecated.
    442             -0.154949828301811 - 0.498015668118356*I
    443 
    444         We make an exception for elements of AA or QQbar, which cannot be
    445         coerced into symbolic expressions to allow this usage::
    446 
    447             sage: t = QQbar(sqrt(2)) + sqrt(3); t
    448             3.146264369941973?
    449             sage: t.parent()
    450             Algebraic Field
    451 
    452         Symbolic functions convert the arguments to symbolic expressions if they
    453         are in QQbar or AA::
    454 
    455             sage: gamma(QQbar(I))
    456             -0.154949828301811 - 0.498015668118356*I
     436            sage: plot(gamma1(x),(x,1,5))
    457437
    458438        TESTS:
    459439
     
    461441        convert back to Sage::
    462442
    463443            sage: z = var('z')
    464             sage: maxima(gamma(z)).sage()
     444            sage: maxima(gamma1(z)).sage()
    465445            gamma(z)
    466             sage: latex(gamma(z))
     446            sage: latex(gamma1(z))
    467447            \Gamma\left(z\right)
    468448
    469449        Test that Trac ticket 5556 is fixed::
    470450
    471             sage: gamma(3/4)
     451            sage: gamma1(3/4)
    472452            gamma(3/4)
    473453
    474             sage: gamma(3/4).n(100)
     454            sage: gamma1(3/4).n(100)
    475455            1.2254167024651776451290983034
    476456
    477457        Check that negative integer input works::
     
    488468            Infinity
    489469        """
    490470        GinacFunction.__init__(self, "gamma", latex_name=r'\Gamma',
    491                 ginac_name='tgamma')
     471                ginac_name='tgamma',
     472                conversions={'mathematica':'Gamma','maple':'GAMMA'})
    492473
    493     def __call__(self, x, coerce=True, hold=False, prec=None):
     474    def __call__(self, x, prec=None, coerce=True, hold=False):
    494475        """
    495476        Note that the ``prec`` argument is deprecated. The precision for
    496477        the result is deduced from the precision of the input. Convert
     
    502483            sage: t.prec()
    503484            100
    504485
    505 
    506486            sage: gamma(6, prec=53)
    507487            doctest:...: DeprecationWarning: The prec keyword argument is deprecated. Explicitly set the precision of the input, for example gamma(RealField(300)(1)), or use the prec argument to .n() for exact inputs, e.g., gamma(1).n(300), instead.
    508488            120.000000000000
     
    518498        if prec is not None:
    519499            from sage.misc.misc import deprecation
    520500            deprecation("The prec keyword argument is deprecated. Explicitly set the precision of the input, for example gamma(RealField(300)(1)), or use the prec argument to .n() for exact inputs, e.g., gamma(1).n(300), instead.")
     501            import mpmath
     502            return mpmath_utils.call(mpmath.gamma, x, prec=prec)
    521503
    522504        # this is a kludge to keep
    523505        #     sage: Q.<i> = NumberField(x^2+1)
     
    542524                x = parent.complex_field()(x)
    543525            res = GinacFunction.__call__(self, x, coerce=coerce, hold=hold)
    544526
    545         if prec is not None:
    546             return res.n(prec)
    547 
    548527        return res
    549528
    550 gamma = Function_gamma()
     529gamma1 = Function_gamma()
    551530
    552531class Function_gamma_inc(BuiltinFunction):
    553532    def __init__(self):
     
    573552            sage: gamma_inc(2., 5)
    574553            0.0404276819945128
    575554        """
    576         BuiltinFunction.__init__(self, "gamma", nargs=2, latex_name=r"\Gamma")
     555        BuiltinFunction.__init__(self, "gamma", nargs=2, latex_name=r"\Gamma",
     556                conversions={'maxima':'gamma_incomplete', 'mathematica':'Gamma',
     557                    'maple':'GAMMA'})
    577558
    578559    def _eval_(self, x, y):
    579560        """
     
    636617# synonym.
    637618incomplete_gamma = gamma_inc=Function_gamma_inc()
    638619
     620def gamma(a, *args, **kwds):
     621    r"""
     622    Gamma and incomplete gamma functions.
     623    This is defined by the integral
     624    `\Gamma(a, z) = \int_z^\infty t^{a-1}e^{-t} dt`.
     625
     626    EXAMPLES::
     627
     628            sage: gamma(CDF(0.5,14))
     629            -4.05370307804e-10 - 5.77329983455e-10*I
     630            sage: gamma(CDF(I))
     631            -0.154949828302 - 0.498015668118*I
     632
     633        Recall that `\Gamma(n)` is `n-1` factorial::
     634
     635            sage: gamma(11) == factorial(10)
     636            True
     637            sage: gamma(6)
     638            120
     639            sage: gamma(1/2)
     640            sqrt(pi)
     641            sage: gamma(-1)
     642            Infinity
     643
     644        The gamma function only works with input that can be coerced to the
     645        Symbolic Ring::
     646       
     647            sage: Q.<i> = NumberField(x^2+1)
     648            sage: gamma(i)
     649            doctest:...: DeprecationWarning: Calling symbolic functions with arguments that cannot be coerced into symbolic expressions is deprecated.
     650            -0.154949828301811 - 0.498015668118356*I
     651
     652        We make an exception for elements of AA or QQbar, which cannot be
     653        coerced into symbolic expressions to allow this usage::
     654
     655            sage: t = QQbar(sqrt(2)) + sqrt(3); t
     656            3.146264369941973?
     657            sage: t.parent()
     658            Algebraic Field
     659
     660        Symbolic functions convert the arguments to symbolic expressions if they
     661        are in QQbar or AA::
     662
     663            sage: gamma(QQbar(I))
     664            -0.154949828301811 - 0.498015668118356*I
     665    """
     666    if not args:
     667        return gamma1(a, **kwds)
     668    if len(args) > 1:
     669        raise TypeError, "Symbolic function gamma takes at most 2 arguments (%s given)"%(len(args)+1)
     670    return incomplete_gamma(a,args[0],**kwds)
    639671
    640672class Function_psi1(GinacFunction):
    641673    def __init__(self):
     
    775807        sage: psi(2, x, 3)
    776808        Traceback (most recent call last):
    777809        ...
    778         TypeError: Symbolic function psi takes exactly 2 arguments (3 given)
     810        TypeError: Symbolic function psi takes at most 2 arguments (3 given)
    779811    """
    780812    if not args:
    781813        return psi1(x, **kwds)
    782814    if len(args) > 1:
    783         raise TypeError, "Symbolic function psi takes exactly 2 arguments (%s given)"%(len(args)+1)
     815        raise TypeError, "Symbolic function psi takes at most 2 arguments (%s given)"%(len(args)+1)
    784816    return psi2(x,args[0],**kwds)
    785817
    786818
  • sage/misc/functional.py

    diff --git a/sage/misc/functional.py b/sage/misc/functional.py
    a b  
    696696        sage: sin(x).integral(x, 0,pi/2)
    697697        1
    698698        sage: integral(exp(-x), (x, 1, oo))
    699         gamma_incomplete(1, 1)
     699        e^(-1)
    700700
    701701    Numerical approximation::
    702702