How do you reduce compound inequalities in sympy? (4 < x - 8 < 10)

133 Views Asked by At

Using sympy solve() or reduce_inequalities() works fine for non compound inequalities like 2 - 6 * x <= -4 but it cannot do compound ones.

Is there a different sympy function or a different import that can solve a compound inequality? I can't seem to find anything in the sympy docs or on Google.

2

There are 2 best solutions below

1
vineet singh On

solveset() function can solve both simple and compound inequalities.

For example, to solve the compound inequality (2 - 6x <= -4) AND (3x + 1 > 4), you can use the following code:

from sympy import *
x = Symbol('x')

ineq1 = Eq(2 - 6*x, -4)
ineq2 = Eq(3*x + 1, 4)

solveset(And(ineq1, ineq2), x)

you can use linsolve() function to solve system of linear equations and inequalities.

linsolve([2 - 6*x - 4, 3*x + 1 - 4], x)
0
Kevin Schnaubelt On

As others have mentioned...

4 < x - 8 < 10

is the same as...

4 < x - 8 AND x - 8 < 10

So I'm simply doing...

solve(4 < x - 8)
solve(x - 8 < 10)

and just combining the results. So...

(12 < x) & (x < 18) or (12,18) or 12 < x < 18

*EDIT User Stef also pointed out that you can give reduce_inequalities() these two equations as two arguments!

reduce_inequalities((4 < x-8, x-8 < 10))