solving inequality in sympy

怎甘沉沦 提交于 2019-12-23 13:25:17

问题


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!


回答1:


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')



回答2:


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))


来源:https://stackoverflow.com/questions/48284093/solving-inequality-in-sympy

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