Ticket #13933: 13933-slow-call.patch

File 13933-slow-call.patch, 5.3 KB (added by Robert Bradshaw, 10 years ago)

Any reason why this isn't a good idea?

  • sage/functions/generalized.py

    # HG changeset patch
    # User Robert Bradshaw <robertwb@math.washington.edu>
    # Date 1357727595 28800
    # Node ID 8f72d533b968eb0a877796c52c3c55c2510318b6
    # Parent  61e2b9fe95f251269a0a672144b61ec491676f35
    imported patch slow-basic-call.patch
    
    diff --git a/sage/functions/generalized.py b/sage/functions/generalized.py
    a b  
    441441            sgn(x)
    442442        """
    443443        BuiltinFunction.__init__(self, "sgn", latex_name=r"\mathrm{sgn}",
    444                 conversions=dict(maxima='signum',mathematica='Sign',sympy='sign'))
     444                conversions=dict(maxima='signum',mathematica='Sign',sympy='sign'),
     445                alt_name="sign")
    445446
    446447    def _eval_(self, x):
    447448        """
  • sage/symbolic/function.pxd

    diff --git a/sage/symbolic/function.pxd b/sage/symbolic/function.pxd
    a b  
    2020    cdef unsigned int _serial
    2121    cdef int _nargs
    2222    cdef object _name
     23    cdef object _alt_name
    2324    cdef object _latex_name
    2425    cdef object _conversions
    2526    cdef object _evalf_params_first
  • sage/symbolic/function.pyx

    diff --git a/sage/symbolic/function.pyx b/sage/symbolic/function.pyx
    a b  
    4747    subclasses :class:`BuiltinFunction` or :class:`SymbolicFunction`.
    4848    """
    4949    def __init__(self, name, nargs, latex_name=None, conversions=None,
    50             evalf_params_first=True):
     50            evalf_params_first=True, alt_name=None):
    5151        """
    5252        This is an abstract base class. It's not possible to test it directly.
    5353
     
    8484            ValueError: eval_func parameter must be callable
    8585        """
    8686        self._name = name
     87        self._alt_name = alt_name
    8788        self._nargs = nargs
    8889        self._latex_name = latex_name
    8990        self._evalf_params_first = evalf_params_first
     
    272273            return cmp(self._serial, (<Function>other)._serial)
    273274        return False
    274275
    275     def __call__(self, *args, coerce=True, hold=False):
     276    def __call__(self, *args, bint coerce=True, bint hold=False):
    276277        """
    277278        Evaluates this function at the given arguments.
    278279       
     
    353354            [e^x   0]
    354355            [  0  -1]
    355356        """
     357        if len(args) == 1 and not hold:
     358            arg = args[0]
     359            method = getattr(arg, self._name, None)
     360            if method is not None:
     361                return method()
     362            elif self._alt_name is not None:
     363                method = getattr(arg, self._alt_name, None)
     364                if method is not None:
     365                    return method()
     366
    356367        if self._nargs > 0 and len(args) != self._nargs:
    357368            raise TypeError, "Symbolic function %s takes exactly %s arguments (%s given)"%(self._name, self._nargs, len(args))
    358369
     
    721732
    722733        g_foptions_assign(g_registered_functions().index(self._serial), opt)
    723734
    724     def __call__(self, *args, coerce=True, hold=False,
    725             dont_call_method_on_arg=False):
     735    def __call__(self, *args, bint coerce=True, bint hold=False,
     736            bint dont_call_method_on_arg=False):
    726737        """
    727738        Evaluate this function on the given arguments and return the result.
    728739
     
    733744            sage: gamma(15)
    734745            87178291200
    735746        """
    736         # we want to convert the result to the original parent if the input
    737         # is not exact, so we store the parent here
    738         org_parent = parent_c(args[0])
    739        
    740747        # if there is only one argument, and the argument has an attribute
    741748        # with the same name as this function, try to call it to get the result
    742749        # The argument dont_call_method_on_arg is used to prevent infinite loops
    743750        # when .exp(), .log(), etc. methods call this symbolic function on
    744751        # themselves
    745         if len(args) == 1 and not hold and not dont_call_method_on_arg and \
    746                 hasattr(args[0], self._name):
    747             return getattr(args[0], self._name)()
     752        if len(args) == 1 and not hold and not dont_call_method_on_arg:
     753            arg = args[0]
     754            method = getattr(arg, self._name, None)
     755            if method is not None:
     756                return method()
     757            elif self._alt_name is not None:
     758                method = getattr(arg, self._alt_name, None)
     759                if method is not None:
     760                    return method()
    748761
    749762        res = super(GinacFunction, self).__call__(*args, coerce=coerce,
    750763                hold=hold)
    751764
     765        # we want to convert the result to the original parent if the input
     766        # is not exact, so we store the parent here
     767        org_parent = parent_c(args[0])
     768       
    752769        # convert the result back to the original parent previously stored
    753770        # otherwise we end up with
    754771        #     sage: arctan(RR(1))
     
    792809    of this class.
    793810    """
    794811    def __init__(self, name, nargs=1, latex_name=None, conversions=None,
    795             evalf_params_first=True):
     812            evalf_params_first=True, alt_name=None):
    796813        """
    797814        TESTS::
    798815
     
    802819            0
    803820        """
    804821        Function.__init__(self, name, nargs, latex_name, conversions,
    805                 evalf_params_first)
     822                evalf_params_first, alt_name = alt_name)
    806823
    807824    cdef _is_registered(self):
    808825        """