sympy and mpmath give “TypeError: cannot create mpf” when using the erf() function within solveset()

梦想与她 提交于 2019-12-02 07:07:03

问题


I have 4 input variables (floats):

  • Xmax
  • Xmin
  • percentage
  • mode

and I want to solve the following (rather long) equation for s:

> (1/2+1/2*erf((log(Xmax)-(log(mode)+s**2))/(sqrt(2)*s))-(1/2+1/2*erf((log(Xmin)-(log(mode)+s**2))/(sqrt(2)*s))) - percentage == 0

I want to use mpmath and sympy to solve the equation, but it gives me the following error message:

TypeError: cannot create mpf from 0.707106781186547*(-s**2 - 0.287682072451781)/s

My code is as follows:

from mpmath import erf, log, sqrt
from sympy import Symbol, solveset, S

percentage = 0.95
mode = 2
Xmin = 1.
Xmax = 1.5
s = Symbol('s')

eqn = (1/2+1/2*erf((log(Xmax)-(log(mode)+s**2))/(sqrt(2)*s))-(1/2+1/2*erf((log(Xmin)-(log(mode)+s**2))/(sqrt(2)*s))) - percentage)

solveset(eqn, s, domain=S.Reals)

mpf is the float type created by mpmath.

I think I narrowed down the problem to the erf() function, it returns

EmptySet()

when i run

solveset(log(Xmax) - (log(mode) + s ** 2), s, domain=S.Reals)

I cannot figure out what to try next, any help would be appreciated!

I initially thought it was an issue with the math but the equation is solved successfully in matlab, so the problem is probably coming from sympy or mpmath.


回答1:


Losing the mpmath import of erf, and use the sympy version resolves your error.

from sympy import Symbol, solveset, S, erf, log, sqrt

percentage = 0.95
mode = 2
Xmin = 1.
Xmax = 1.5
s = Symbol('s', real=True)

eqn = (1/2+1/2*erf((log(Xmax)-(log(mode)+s**2))/(sqrt(2)*s))-(1/2+1/2*erf((log(Xmin)-(log(mode)+s**2))/(sqrt(2)*s))) - percentage)

solveset(eqn, s)

Note also:

  • you don't have to import log and sqrt from mpmath. It won't make a difference to your result here to get them from sympy
  • you can specify the Real domain on the variable s, saves you doing so on the solveset call(s).

Example of further usage is in the package tests here if you need it.



来源:https://stackoverflow.com/questions/37759214/sympy-and-mpmath-give-typeerror-cannot-create-mpf-when-using-the-erf-functi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!