# HG changeset patch
# User JeanPierre Flori <jeanpierre.flor@ssi.gouv.fr>
# Date 1337847098 7200
# Node ID f32794ad03dd6ed0746db26b586f83bb9cc5933a
# Parent 319b0d196dded363524beddbccbda00c933c0146
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


828  828  """ 
829  829  cdef GEx res 
830  830  try: 
831   res = self._gobj.evalf(0, R) 
 831  res = self._gobj.evalf(0, {'parent':R}) 
832  832  except TypeError as err: 
833  833  # try the evaluation again with the complex field 
834  834  # corresponding to the parent R 
… 
… 

839  839  R_complex = R.complex_field() 
840  840  except (TypeError, AttributeError): 
841  841  raise err 
842   res = self._gobj.evalf(0, R_complex) 
 842  res = self._gobj.evalf(0, {'parent':R}) 
843  843  if is_a_numeric(res): 
844  844  return R(py_object_from_numeric(res)) 
845  845  else: 
… 
… 

888  888  sage: f._convert(int) 
889  889  0.989992496600445*sqrt(2) 
890  890  """ 
891   cdef GEx res = self._gobj.evalf(0, R) 
 891  cdef GEx res = self._gobj.evalf(0, {'parent':R}) 
892  892  return new_Expression_from_GEx(self._parent, res) 
893  893  
894  894  def _mpfr_(self, R): 
diff git a/sage/symbolic/pynac.pyx b/sage/symbolic/pynac.pyx
a

b


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

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