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

Time:01-30

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.

CodePudding user response:

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)

CodePudding user response:

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
  • Related