Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solving Inequalities in Sympy

Tags:

python

sympy

I tried to solve the following inequality in sympy:

(10000 / x) - 1 < 0

So I issued the command:

solve_poly_inequality( Poly((10000 / x) - 1 ), '<')

However, I got:

[Interval.open(-oo, 1/10000)]

However, my manual computations give either x < 0 or x > 10000.

What am I missing? Due to the -1, I cannot represent it as a rational function.

Thanks in advance!

like image 894
olidem Avatar asked Jan 16 '18 14:01

olidem


2 Answers

You are using a low-level solving routine. I would recommend using the higher-level routines solve or solveset, e.g.

>>> solveset((10000 / x) - 1 < 0, x, S.Reals)
(−∞, 0) ∪ (10000, ∞)

The reason that your attempt is right but looks wrong is that you did not specify the generator to use so Poly used 1/x as its variable (let's call it g) so it solved the problem 1000*g - 1 < 0...which is true when g is less than 1/1000 as you found.

You can see this generator identification by writing

>>> Poly(1000/x - 1)
Poly(1000*1/x - 1, 1/x, domain='ZZ')
like image 106
smichr Avatar answered Oct 15 '22 01:10

smichr


10000/x-1 is not a polynomial in x but a polynomial in 1/x. Rather, 10000/x-1 is a rational function in x. While you may try to put Poly(1000*1/x - 1, x, domain='ZZ'), there will be errors

PolynomialError: 1/x contains an element of the generators set

because by definition 10000/x-1 cannot be a polynomial in x. Thus, you just cannot do the computation with this.

You can also try to following or other solvers.

from sympy.solvers.inequalities import reduce_rational_inequalities
from sympy import Poly
from sympy.abc import x
reduce_rational_inequalities([[10000/x - 1 < 0]], x)
((-oo < x) & (x < 0)) | ((10000 < x) & (x < oo))
like image 3
Tai Avatar answered Oct 15 '22 03:10

Tai