4

Given a sympy symbolic function, for example

x=symbols("x")
h=sin(x) 

when one calls

h.subs(x, mpf('1.0000000000000000000000000000000000000000000001')) 

sympy returns a floating point number. The return value is not an mpf.

Is there a convenient way to get sympy to evaluate symbolic standard functions (like exponential and trig functions), using arbitrary precision?

Ryan Budney
  • 308
  • 1
  • 6
  • 16

1 Answers1

6

To evaluate an expression, use expr.evalf(), which takes a precision as the first argument (the default is 15). You can substitute expressions using expr.evalf(prec, subs=subs_dict).

>>> sin(x).evalf(50, subs={x: Float('1.0')})
0.84147098480789650665250232163029899962256306079837
asmeurer
  • 86,894
  • 26
  • 169
  • 240
  • Thanks, with a little modification your answer does what I'm looking for. I need to generally change your Float('') creation to an mpf('') creation but that compiles as well. – Ryan Budney Jan 11 '16 at 17:12
  • Second comment: it appears a sympy Float is ?essentially? an mpf type. So my first comment was neither here nor there. – Ryan Budney Jan 11 '16 at 17:36
  • Yes, Float is a wrapper around mpf, which is necessary so that it works correctly with SymPy functions. If you want to work with mpmath functions and types directly another option is to use `lambdify(x, sin(x), "mpmath")` which will convert the SymPy expression into a mpmath function, which can then be called with an mpf. – asmeurer Jan 13 '16 at 15:35