# HG changeset patch
# User JeanPierre Flori <jeanpierre.flor@ssi.gouv.fr>
# Date 1337847098 7200
# Thu May 24 10:11:38 2012 +0200
# Node ID b867edd171d975c718e1efc146d4f0de3a9013da
# Parent 159247f5892d8a1e321a786e0bee58389e240683
trac 12289: convert parent keyword argument of _evalf_() to an arbitrary dict
Custom evaluation methods defined in symbolic functions only accept parent as
a keyword argument. This framework does not allow the user to select different
systems for the numeric evaluation of a function.
Pynac passes around a Python object around during numeric evaluation. This
patch, along with the corresponding changes in Pynac, makes this object an
arbitrary dict. This dictionary can later be used to pass around different
options relevant for numeric evaluation.
diff git a/sage/symbolic/expression.pyx b/sage/symbolic/expression.pyx
a

b


943  943  """ 
944  944  cdef GEx res 
945  945  try: 
946   res = self._gobj.evalf(0, R) 
 946  res = self._gobj.evalf(0, {'parent':R}) 
947  947  except TypeError as err: 
948  948  # try the evaluation again with the complex field 
949  949  # corresponding to the parent R 
… 
… 

954  954  R_complex = R.complex_field() 
955  955  except (TypeError, AttributeError): 
956  956  raise err 
957   res = self._gobj.evalf(0, R_complex) 
 957  res = self._gobj.evalf(0, {'parent':R}) 
958  958  if is_a_numeric(res): 
959  959  return R(py_object_from_numeric(res)) 
960  960  else: 
… 
… 

1003  1003  sage: f._convert(int) 
1004  1004  0.989992496600445*sqrt(2) 
1005  1005  """ 
1006   cdef GEx res = self._gobj.evalf(0, R) 
 1006  cdef GEx res = self._gobj.evalf(0, {'parent':R}) 
1007  1007  return new_Expression_from_GEx(self._parent, res) 
1008  1008  
1009  1009  def _mpfr_(self, R): 
diff git a/sage/symbolic/pynac.pyx b/sage/symbolic/pynac.pyx
a

b


1081  1081  """ 
1082  1082  return py_is_cinteger(x) 
1083  1083  
1084   cdef public object py_float(object n, PyObject* parent) except +: 
 1084  cdef public object py_float(object n, PyObject* kwds) except +: 
1085  1085  """ 
1086  1086  Evaluate pynac numeric objects numerically. 
1087  1087  
1088  1088  TESTS:: 
1089  1089  
1090  1090  sage: from sage.symbolic.pynac import py_float_for_doctests as py_float 
1091   sage: py_float(I, ComplexField(10)) 
 1091  sage: py_float(I, {'parent':ComplexField(10)}) 
1092  1092  1.0*I 
1093   sage: py_float(pi, RealField(100)) 
 1093  sage: py_float(pi, {'parent':RealField(100)}) 
1094  1094  3.1415926535897932384626433833 
1095  1095  sage: py_float(10, CDF) 
1096  1096  10.0 
1097   sage: type(py_float(10, CDF)) 
 1097  sage: type(py_float(10, {'parent':CDF})) 
1098  1098  <type 'sage.rings.complex_double.ComplexDoubleElement'> 
1099   sage: py_float(1/2, CC) 
 1099  sage: py_float(1/2, {'parent':CC})) 
1100  1100  0.500000000000000 
1101   sage: type(py_float(1/2, CC)) 
 1101  sage: type(py_float(1/2, {'parent':CC})) 
1102  1102  <type 'sage.rings.complex_number.ComplexNumber'> 
1103  1103  """ 
1104   if parent is not NULL: 
1105   return (<object>parent)(n) 
 1104  if kwds is not NULL: 
 1105  return (<object>kwds)['parent'](n) 
1106  1106  else: 
1107  1107  try: 
1108  1108  return RR(n) 
1109  1109  except TypeError: 
1110  1110  return CC(n) 
1111  1111  
1112   def py_float_for_doctests(n, prec): 
 1112  def py_float_for_doctests(n, kwds): 
1113  1113  """ 
1114  1114  This function is for testing py_float. 
1115  1115  
1116  1116  EXAMPLES:: 
1117   
 1117  
1118  1118  sage: from sage.symbolic.pynac import py_float_for_doctests 
1119   sage: py_float_for_doctests(pi, RealField(80)) 
 1119  sage: py_float_for_doctests(pi, {'parent':RealField(80)}) 
1120  1120  3.1415926535897932384626 
1121  1121  """ 
1122   return py_float(n, <PyObject*>prec) 
 1122  return py_float(n, <PyObject*>kwds) 
1123  1123  
1124  1124  # TODO: Optimize this 
1125  1125  from sage.rings.real_double import RDF 
… 
… 

1894  1894  pc = c._pynac 
1895  1895  return pc.object 
1896  1896  
1897   cdef public object py_eval_constant(unsigned serial, object parent) except +: 
 1897  cdef public object py_eval_constant(unsigned serial, object kwds) except +: 
1898  1898  from sage.symbolic.constants import constants_table 
1899  1899  constant = constants_table[serial] 
1900   return parent(constant) 
 1900  return kwds['parent'](constant) 
1901  1901  
1902  1902  cdef public object py_eval_unsigned_infinity() except +: 
1903  1903  """ 