# 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


4331  4331  """ 
4332  4332  return self.number_of_operands() 
4333  4333  
 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  
4334  4361  def operands(self): 
4335  4362  """ 
4336  4363  Returns a list containing the operands of this expression. 
… 
… 

9868  9895  # callable methods need to be wrapped to extract the operands 
9869  9896  # and pass them as arguments 
9870  9897  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 
9872  9899  for name in dir(eval_methods): 
9873   m = getattr_from_other_class(func_class, eval_methods, name) 
 9900  m = getattr(eval_methods, name) 
9874  9901  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))) 
9876  9905  cls = dynamic_class('Expression_with_dynamic_methods', 
9877  9906  (eval_methods, Expression)) 
9878  9907  else: 
diff git a/sage/symbolic/function_factory.py b/sage/symbolic/function_factory.py
a

b


381  381  'Some documentation.' 
382  382  """ 
383  383  @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) 
386  388  return new_f 
diff git a/sage/symbolic/pynac.pyx b/sage/symbolic/pynac.pyx
a

b


100  100  res.append(new_Expression_from_GEx(SR, seq.op(i))) 
101  101  return tuple(res) 
102  102  
 103  def 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  
103  123  cdef public object exvector_to_PyTuple(GExVector seq): 
104  124  """ 
105  125  Converts arguments list given to a function to a PyTuple. 