11

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!

Tenten Ponce
  • 2,436
  • 1
  • 13
  • 40
olidem
  • 1,961
  • 2
  • 20
  • 45

2 Answers2

15

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).gens
(1/x,)
smichr
  • 16,948
  • 2
  • 27
  • 34
  • Perfect! Thank you! It's a pity that sympy has such a bad documentation. I have come across solveset several times, but never managed to understand its docs. Now it's clear. Also the reason that I was attracted of `solve_poly_inequality` was its `solve_poly_inequalities` variant for systems of inequalities. Thanks again! – olidem Jan 16 '18 at 17:21
  • @OlliD-Metz if you have an idea on how that part of the documentation can be improved any contribution would almost certainly be most welcomed (no matter how small). – Bjoern Dahlgren Jan 18 '18 at 22:34
  • 1
    Well, I did not find a basic documentation of the `Poly` command. So, if someone is missing the basics, everything that follows can only be very complicated. If you search the web for "sympy solve inequality", you end up here: http://docs.sympy.org/latest/modules/solvers/inequalities.html, with no remark that one should better just use solveset (which is also much more comfortable) – olidem Jan 19 '18 at 13:12
3

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))
Morten Jensen
  • 5,818
  • 3
  • 43
  • 55
Tai
  • 7,684
  • 3
  • 29
  • 49