solving inequality in sympy

2019-06-27 13:22发布

i want to solve the following inequality in sympy

(10000 / x) - 1 < 0

so I issue the command

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

as result I get

[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 because of the -1.

Thanks in advance!

标签: python sympy
2条回答
欢心
2楼-- · 2019-06-27 13:41

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))
查看更多
ゆ 、 Hurt°
3楼-- · 2019-06-27 13:54

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')
查看更多
登录 后发表回答