0

I am new to sympy but want to solve the following problem:

I have multiple inequality constraints of the form

A < f(x) < B
C < g(x) < D
...

where A, B, C, D are just numbers. f and g are rational functions.

For instance I can get the following to work:

solve_rational_inequalities([[
   ((Poly(x-10000.00), Poly(1, x)), '>'), 
   ((Poly(x-100000.00), Poly(1, x)), '<'), 
   ((Poly((x/130000.00)-0.00), Poly(1, x)), '>'), 
   ((Poly((x/130000.00)-0.19), Poly(1, x)), '<')]])
Interval.open(10000, 24700)

Here, A = 10.000, B = 100.000, C = 0, D = 0.19, f(x) = x and g(x) = x/130.000. So this works.

Now, for another case, I have the function f(x) = 10100.00 / x.

If I just apply the recipe from above I get:

solve_rational_inequalities([[
    ((Poly((10100.00/x)-0.00), Poly(1, x)), '>'), 
    ((Poly((10100.00/x)-0.19), Poly(1, x)), '<')]])                     
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/oliverdemetz/Library/Python/2.7/lib/python/site-packages/sympy/solvers/inequalities.py", line 162, in solve_rational_inequalities
    numer_intervals = solve_poly_inequality(numer*denom, rel)
  File "/Users/oliverdemetz/Library/Python/2.7/lib/python/site-packages/sympy/solvers/inequalities.py", line 55, in solve_poly_inequality
    reals, intervals = poly.real_roots(multiple=False), []
  File "/Users/oliverdemetz/Library/Python/2.7/lib/python/site-packages/sympy/polys/polytools.py", line 3454, in real_roots
    reals = sympy.polys.rootoftools.CRootOf.real_roots(f, radicals=radicals)
  File "/Users/oliverdemetz/Library/Python/2.7/lib/python/site-packages/sympy/polys/rootoftools.py", line 196, in real_roots
    return cls._get_roots("_real_roots", poly, radicals)
  File "/Users/oliverdemetz/Library/Python/2.7/lib/python/site-packages/sympy/polys/rootoftools.py", line 565, in _get_roots
    raise PolynomialError("only univariate polynomials are allowed")
sympy.polys.polyerrors.PolynomialError: only univariate polynomials are allowed
olidem
  • 1,961
  • 2
  • 20
  • 45

2 Answers2

1

I just messed up rational functions with polynomials. What I was searching for was solve_poly_inequalities:

solve_poly_inequalities(((
    (Poly((10100.00/x)-0.00),'>'),
    (Poly((10100.00/x)-0.19),'<')
)))

BTW: can anyone tell me (as a phython beginner) why they use ((...)) for lists, although the manual explains square brackets [...] as list syntax???

Anyway, I finally solved my actual problem of inequality solving much more comfortably using the solveset command, see this other SO post

olidem
  • 1,961
  • 2
  • 20
  • 45
  • I don't recommend wrapping things in Poly just because SymPy complains if you don't. The answer by ralf htp explains how to correctly express denominators in rational inequalities. –  Jan 16 '18 at 03:09
  • Parentheses create a tuple (the double parentheses are just redundant and don't need to be there). Tuples and lists are more or less interchangeable. The only difference is that lists are mutable (can be changed in place), whereas tuples are not. – asmeurer Jan 17 '18 at 20:06
1

in the solve_rational_inequality() syntax the first polynomial ( poly ) is the numerator, the second polynomial is the denominator, so ( 1 / x ) is poly(1,x), poly(x) in this syntax :

solve_rational_inequalities([[((poly(-10100.00,x), poly(x)), '>'),((poly(-0.19*x+10100.00), poly(x)), '<')]])

(-oo, 0)

ralf htp
  • 9,149
  • 4
  • 22
  • 34
  • Yes that also was my first try. But then I have to split my input into nominator and denominator first. And I am just getting the inequality from my program. – olidem Jan 16 '18 at 07:03