• ## sage/functions/hyperbolic.py

# HG changeset patch
# User Burcin Erocal <burcin@erocal.org>
# Date 1326180292 -3600
trac 12289: add algorithm argument to numerical_approx() and  _evalf_ methods

Add an algorithm keyword argument to the numerical_approx() method of symbolic
expressions. This argument is passed on to the _evalf_() method of all
symbolic functions, so the signatures of these functions need to be modified
as well.

diff --git a/sage/functions/hyperbolic.py b/sage/functions/hyperbolic.py
 a BuiltinFunction.__init__(self, name, latex_name=latex_name, conversions=conversions) def _evalf_(self, x, parent): def _evalf_(self, x, parent=None, algorithm=None): """ EXAMPLES::
• ## sage/functions/other.py

diff --git a/sage/functions/other.py b/sage/functions/other.py
 a """ BuiltinFunction.__init__(self, "erf", latex_name=r"\text{erf}") def _evalf_(self, x, parent): def _evalf_(self, x, parent=None, algorithm=None): """ EXAMPLES:: return sqrt(pi)*(1-erf(sqrt(y))) return None def _evalf_(self, x, y, parent=None): def _evalf_(self, x, y, parent=None, algorithm=None): """ EXAMPLES::
• ## sage/functions/transcendental.py

diff --git a/sage/functions/transcendental.py b/sage/functions/transcendental.py
 a return self._evalf_(x, parent(x)) return None def _evalf_(self, x, parent=None): def _evalf_(self, x, parent=None, algorithm=None): """ EXAMPLES::
• ## sage/functions/trig.py

diff --git a/sage/functions/trig.py b/sage/functions/trig.py
 a """ BuiltinFunction.__init__(self, "sec", latex_name=r"\sec") def _evalf_(self, x, parent=None): def _evalf_(self, x, parent=None, algorithm=None): """ EXAMPLES:: """ BuiltinFunction.__init__(self, "csc", latex_name=r"\csc") def _evalf_(self, x, parent=None): def _evalf_(self, x, parent=None, algorithm=None): """ EXAMPLES:: """ return 1 / tan(x) def _evalf_(self, x, parent=None): def _evalf_(self, x, parent=None, algorithm=None): """ EXAMPLES:: BuiltinFunction.__init__(self, "arccot", latex_name=r'{\rm arccot}', conversions=dict(maxima='acot')) def _evalf_(self, x, parent=None): def _evalf_(self, x, parent=None, algorithm=None): """ EXAMPLES:: BuiltinFunction.__init__(self, "arccsc", latex_name=r'{\rm arccsc}', conversions=dict(maxima='acsc')) def _evalf_(self, x, parent=None): def _evalf_(self, x, parent=None, algorithm=None): """ EXAMPLES:: BuiltinFunction.__init__(self, "arcsec", latex_name=r'{\rm arcsec}', conversions=dict(maxima='asec')) def _evalf_(self, x, parent=None): def _evalf_(self, x, parent=None, algorithm=None): """ EXAMPLES::
• ## sage/symbolic/expression.pyx

diff --git a/sage/symbolic/expression.pyx b/sage/symbolic/expression.pyx
 a else: raise TypeError, "Cannot evaluate symbolic expression to a numeric value." cpdef _convert(self, R): cpdef _convert(self, kwds): """ Convert all the numeric coefficients and constants in this expression to the given ring R. This results in an expression which contains sage: f = sqrt(2) * cos(3); f sqrt(2)*cos(3) sage: f._convert(RDF) sage: f._convert({'parent':RDF}) -1.40006081534 sage: f._convert(float) sage: f._convert({'parent':float}) -1.40006081533995 There is nothing to convert for variables:: sage: x._convert(CC) sage: x._convert({'parent':CC}) x Note that the output is not meant to be in the in the given ring R. :: sage: (0.25 / (log(5.74 /x^0.9, 10))^2 / 4)._convert(QQ) sage: (0.25 / (log(5.74 /x^0.9, 10))^2 / 4)._convert({'parent':QQ}) 0.331368631904900/log(287/50/x^0.900000000000000)^2 sage: (0.25 / (log(5.74 /x^0.9, 10))^2 / 4)._convert(CC) sage: (0.25 / (log(5.74 /x^0.9, 10))^2 / 4)._convert({'parent':CC}) 0.331368631904900/log(5.74000000000000/x^0.900000000000000)^2 When converting to an exact domain, powers remain unevaluated:: sage: f = sqrt(2) * cos(3); f sqrt(2)*cos(3) sage: f._convert(int) sage: f._convert({'parent':int}) -0.989992496600445*sqrt(2) """ cdef GEx res = self._gobj.evalf(0, {'parent':R}) cdef GEx res = self._gobj.evalf(0, kwds) return new_Expression_from_GEx(self._parent, res) def _mpfr_(self, R): res._expr = self return res def _numerical_approx(self, prec=None, digits=None): def _numerical_approx(self, prec=None, digits=None, algorithm=None): """ Return a numerical approximation this symbolic expression as either a real or complex number with at least the requested prec = int((digits+1) * 3.32192) + 1 from sage.rings.real_mpfr import RealField R = RealField(prec) kwds = {'parent': R, 'algorithm': algorithm} cdef Expression x try: x = self._convert(R) x = self._convert(kwds) except TypeError: # numerical approximation for real number failed pass          # try again with complex R = R.complex_field() x = self._convert(R) kwds['parent'] = R.complex_field() x = self._convert(kwds) # we have to consider constants as well, since infinity is a constant # in pynac
• ## sage/symbolic/function.pyx

diff --git a/sage/symbolic/function.pyx b/sage/symbolic/function.pyx
 a sage: class my_function(SymbolicFunction): ...  def __init__(self): ...     SymbolicFunction.__init__(self, 'foo', nargs=2) ...  def _evalf_(self, x, y, parent=None): ...  def _evalf_(self, x, y, parent=None, algorithm=None): ...     return x*y*2r ...  def _conjugate_(self, x, y): ...     return x sage: u(y,x) 2*y sage: def evalf_f(self, x, parent=None): return int(6) sage: def evalf_f(self, x, **kwds): return int(6) sage: foo = function("foo", nargs=1, evalf_func=evalf_f) sage: foo.__getstate__() (2, 'foo', 1, None, {}, True, [None, "...", None, None, None, None, None, None, None, None, None])
• ## sage/symbolic/function_factory.py

diff --git a/sage/symbolic/function_factory.py b/sage/symbolic/function_factory.py
 a sage: f._mathematica_init_() 'Foo' sage: def evalf_f(self, x, parent=None): return x*.5r sage: def evalf_f(self, x, parent=None, algorithm=None): return x*.5r sage: g = function_factory('g',1,evalf_func=evalf_f) sage: g(2) g(2) 'Foo' sage: from sage.symbolic.function import pickle_wrapper sage: def evalf_f(self, x, parent=None): return 2r*x + 5r sage: def evalf_f(self, x, parent=None, algorithm=None): return 2r*x + 5r sage: def conjugate_f(self, x): return x/2r sage: nf = unpickle_function('g', 1, None, None, True, [None, pickle_wrapper(evalf_f), pickle_wrapper(conjugate_f)] + [None]*8) sage: nf sage: bar(x) bar(x) sage: def evalf_f(self, x, parent=None): return 6 sage: def evalf_f(self, x, parent=None, algorithm=None): return 6 sage: foo = function("foo", nargs=1, evalf_func=evalf_f) sage: foo(x) foo(x)
• ## sage/symbolic/integration/integral.py

diff --git a/sage/symbolic/integration/integral.py b/sage/symbolic/integration/integral.py
 a pass return None def _evalf_(self, f, x, a, b, parent=None): def _evalf_(self, f, x, a, b, parent=None, algorithm=None): """ Returns numerical approximation of the integral