Ticket #14802: trac_14802-dynamic_attribute_args.patch

File trac_14802-dynamic_attribute_args.patch, 5.8 KB (added by Burcin Erocal, 9 years ago)
  • sage/symbolic/expression.pyx

    # HG changeset patch
    # User Burcin Erocal <burcin@erocal.org>
    # Date 1372351418 -7200
    #      Thu Jun 27 18:43:38 2013 +0200
    # Node ID 4bd3ec3f4936e189d395646c6d5ec3838a92687f
    # Parent  5779e4323648f28320d140c9ffc23837802a8a8a
    trac 9556: additional features for dynamic methods of symbolic expressions
    
    diff --git a/sage/symbolic/expression.pyx b/sage/symbolic/expression.pyx
    a b  
    43484348        """
    43494349        return self.number_of_operands()
    43504350
     4351    def _unpack_operands(self):
     4352        """
     4353        Unpack the operands of this expression converting each to a Python
     4354        object if possible.
     4355
     4356        This corresponds to the conversion performed when arguments of a
     4357        function are unpacked as they are being passed to custom methods of
     4358        a symbolic function.
     4359
     4360        EXAMPLES::
     4361
     4362            sage: t = SR._force_pyobject((1, 2, x, x+1, x+2))
     4363            sage: t._unpack_operands()
     4364            (1, 2, x, x + 1, x + 2)
     4365            sage: type(t._unpack_operands())
     4366            <type 'tuple'>
     4367            sage: map(type, t._unpack_operands())
     4368            [<type 'sage.rings.integer.Integer'>, <type 'sage.rings.integer.Integer'>, <type 'sage.symbolic.expression.Expression'>, <type 'sage.symbolic.expression.Expression'>, <type 'sage.symbolic.expression.Expression'>]
     4369            sage: u = SR._force_pyobject((t, x^2))
     4370            sage: u._unpack_operands()
     4371            ((1, 2, x, x + 1, x + 2), x^2)
     4372            sage: type(u._unpack_operands()[0])
     4373            <type 'tuple'>
     4374        """
     4375        from sage.symbolic.pynac import unpack_operands
     4376        return unpack_operands(self)
     4377
    43514378    def operands(self):
    43524379        """
    43534380        Returns a list containing the operands of this expression.
     
    997910006        ....:         BuiltinFunction.__init__(self, 'tfunc', nargs=1)
    998010007        ....:
    998110008        ....:     class EvaluationMethods:
    9982         ....:         def argp1(self, x):
     10009        ....:         def argp1(fn, self, x):
    998310010        ....:             '''
    998410011        ....:             Some documentation about a bogus function.
    998510012        ....:             '''
     
    1002410051        ....:         BuiltinFunction.__init__(self, 'tfunc', nargs=2)
    1002510052        ....:
    1002610053        ....:     class EvaluationMethods:
    10027         ....:         def argsum(self, x, y):
     10054        ....:         def argsum(fn, self, x, y):
    1002810055        ....:             return x + y
    1002910056        ....:
    1003010057        sage: tfunc2 = TFunc2()
     
    1004210069            # callable methods need to be wrapped to extract the operands
    1004310070            # and pass them as arguments
    1004410071            from sage.symbolic.function_factory import eval_on_operands
    10045             from sage.structure.parent import getattr_from_other_class
     10072            from sage.structure.misc import getattr_from_other_class
    1004610073            for name in dir(eval_methods):
    10047                 m = getattr_from_other_class(func_class, eval_methods, name)
     10074                m = getattr(eval_methods(), name)
    1004810075                if callable(m):
    10049                     setattr(eval_methods, name, eval_on_operands(m))
     10076                    new_m = eval_on_operands(getattr_from_other_class(
     10077                        func_class, eval_methods, name))
     10078                    setattr(eval_methods, name, new_m)
    1005010079            cls = dynamic_class('Expression_with_dynamic_methods',
    10051                     (eval_methods, Expression))
     10080                    (Expression,), eval_methods)
    1005210081        else:
    1005310082            cls = Expression
    1005410083
  • sage/symbolic/function_factory.py

    diff --git a/sage/symbolic/function_factory.py b/sage/symbolic/function_factory.py
    a b  
    360360def eval_on_operands(f):
    361361    """
    362362    Given a method ``f`` return a new method which takes a single symbolic
    363     expression argument and passes the operands of the given expression as
    364     arguments to ``f``.
     363    expression argument and appends operands of the given expression to
     364    the arguments of ``f``.
    365365
    366366    EXAMPLES::
    367367
    368         sage: def f(x, y):
     368        sage: def f(ex, x, y):
    369369        ....:     '''
    370370        ....:     Some documentation.
    371371        ....:     '''
    372372        ....:     return x + 2*y
    373373        ....:
    374         sage: f(x, 1)
     374        sage: f(None, x, 1)
    375375        x + 2
    376376        sage: from sage.symbolic.function_factory import eval_on_operands
    377377        sage: g = eval_on_operands(f)
     
    381381        'Some documentation.'
    382382    """
    383383    @sage_wraps(f)
    384     def new_f(ex):
    385         return f(*ex.operands())
     384    def new_f(ex, *args, **kwds):
     385        new_args = list(ex._unpack_operands())
     386        new_args.extend(args)
     387        return f(ex, *new_args, **kwds)
    386388    return new_f
  • sage/symbolic/pynac.pyx

    diff --git a/sage/symbolic/pynac.pyx b/sage/symbolic/pynac.pyx
    a b  
    100100            res.append(new_Expression_from_GEx(SR, seq.op(i)))
    101101    return tuple(res)
    102102
     103def unpack_operands(Expression ex):
     104    """
     105    EXAMPLES::
     106
     107        sage: from sage.symbolic.pynac import unpack_operands
     108        sage: t = SR._force_pyobject((1, 2, x, x+1, x+2))
     109        sage: unpack_operands(t)
     110        (1, 2, x, x + 1, x + 2)
     111        sage: type(unpack_operands(t))
     112        <type 'tuple'>
     113        sage: map(type, unpack_operands(t))
     114        [<type 'sage.rings.integer.Integer'>, <type 'sage.rings.integer.Integer'>, <type 'sage.symbolic.expression.Expression'>, <type 'sage.symbolic.expression.Expression'>, <type 'sage.symbolic.expression.Expression'>]
     115        sage: u = SR._force_pyobject((t, x^2))
     116        sage: unpack_operands(u)
     117        ((1, 2, x, x + 1, x + 2), x^2)
     118        sage: type(unpack_operands(u)[0])
     119        <type 'tuple'>
     120    """
     121    return exprseq_to_PyTuple(ex._gobj)
     122
    103123cdef public object exvector_to_PyTuple(GExVector seq):
    104124    """
    105125    Converts arguments list given to a function to a PyTuple.