How to solve multivariate inequalities with python and sympy?

sloan picture sloan · Jun 11, 2013 · Viewed 8.6k times · Source

I'm quite new using python and Sympy... And got a problem to solve multivariate inequalities using sympy.

Let's say I have a lot of functions in a file which look like this :

    cst**(sqrt(x)/2)/cst
    exp(sqrt(cst*x**(1/4)))
    log(log(sqrt(cst + exp(x))))
    (y**(1/4) + y)**cst
    sqrt(y/log(x))/cst
    sqrt(cst**log(cst) + x)
    (y**2)**(x/4)
    sqrt(y*sqrt(cst**y))
    log(sqrt(2)*sqrt(cst)*x)

I need to derivate them, set the value of the constant and check if, for each functions f,

    df/dx > 0
    df/dy < 0 

With x in [0, +oo) and y in [0, 1].

To derivate i use :

    dx = diff(f, x)
    dy = diff(f, y)

Then when i try :

    cst = 2 #(for example) 
    solve(dx > 0) 

I got this error :

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/usr/local/lib/python2.7/dist-packages/sympy/solvers/solvers.py", line 634, in solve
symbols=symbols)
    File "/usr/local/lib/python2.7/dist-packages/sympy/solvers/inequalities.py", line 374, in reduce_inequalities
    raise NotImplementedError("only univariate inequalities are supported")
    NotImplementedError: only univariate inequalities are supported

But if i try that :

    x=Symbol('x', real=True, postive=True, nonzero=True)
    y=Symbol('y', real=True, postive=True, nonzero=True)
    solve(x**2+y > 0)

I got :

    True

Which is good and workable answer. Is there anyway to solve multivariate inequalities and always get an workable answer?

For example I would like to get : solve(x**2-y>0) Or(x>-sqrt(y), x>sqrt(y))

Answer

Krastanov picture Krastanov · Jun 11, 2013

When trying to solve this with SymPy you get a pretty clear error message: NotImplementedError: only univariate inequalities are supported. Be aware that this means that the SymPy team will be very happy if you contribute an algorithm that solves this problem.

Now that it is clear that sympy.solve is not powerful enough you can try another approach. Recently (in 0.7.2) an implicit plotting routine was added to sympy that can plot where an expression evaluates to True. Regretfully it is only a numeric solution, not a symbolic one that you can get from solve but it might be enough:

enter image description here

From the image you can see that there is only a single line where the expression changes sign, so solving for expr==0 might give you what you want. And this is indeed the case:

enter image description here