Ticket #9556: trac_9556-dynamic_class_everywhere.part2.patch

File trac_9556-dynamic_class_everywhere.part2.patch, 4.3 KB (added by burcin, 6 years ago)
  • sage/symbolic/expression.pyx

    # HG changeset patch
    # User Burcin Erocal <burcin@erocal.org>
    # Date 1371771131 -7200
    #      Fri Jun 21 01:32:11 2013 +0200
    # Node ID 1597f479446f5e6061e64ef2fe2dedf6683762db
    # Parent  e699be228a8195f17dab172bb9aae795055271c2
    trac 9556: additional features for dynamic methods of symbolic expressions
    
    diff --git a/sage/symbolic/expression.pyx b/sage/symbolic/expression.pyx
    a b  
    43314331        """
    43324332        return self.number_of_operands()
    43334333
     4334    def _unpack_operands(self):
     4335        """
     4336        Unpack the operands of this expression converting each to a Python
     4337        object if possible.
     4338
     4339        This corresponds to the conversion performed when arguments of a
     4340        function are unpacked as they are being passed to custom methods of
     4341        a symbolic function.
     4342
     4343        EXAMPLES::
     4344
     4345            sage: t = SR._force_pyobject((1, 2, x, x+1, x+2))
     4346            sage: t._unpack_operands()
     4347            (1, 2, x, x + 1, x + 2)
     4348            sage: type(t._unpack_operands())
     4349            <type 'tuple'>
     4350            sage: map(type, t._unpack_operands())
     4351            [<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'>]
     4352            sage: u = SR._force_pyobject((t, x^2))
     4353            sage: u._unpack_operands()
     4354            ((1, 2, x, x + 1, x + 2), x^2)
     4355            sage: type(u._unpack_operands()[0])
     4356            <type 'tuple'>
     4357        """
     4358        from sage.symbolic.pynac import unpack_operands
     4359        return unpack_operands(self)
     4360
    43344361    def operands(self):
    43354362        """
    43364363        Returns a list containing the operands of this expression.
     
    98689895            # callable methods need to be wrapped to extract the operands
    98699896            # and pass them as arguments
    98709897            from sage.symbolic.function_factory import eval_on_operands
    9871             from sage.structure.parent import getattr_from_other_class
     9898            from sage.structure.misc import getattr_from_other_class
    98729899            for name in dir(eval_methods):
    9873                 m = getattr_from_other_class(func_class, eval_methods, name)
     9900                m = getattr(eval_methods, name)
    98749901                if callable(m):
    9875                     setattr(eval_methods, name, eval_on_operands(m))
     9902                    setattr(eval_methods, name,
     9903                            eval_on_operands(getattr_from_other_class(
     9904                                Expression, eval_methods, name)))
    98769905            cls = dynamic_class('Expression_with_dynamic_methods',
    98779906                    (eval_methods, Expression))
    98789907        else:
  • sage/symbolic/function_factory.py

    diff --git a/sage/symbolic/function_factory.py b/sage/symbolic/function_factory.py
    a b  
    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(*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.