1

I was trying to use Sympy, but i can't pass two side inequality equation. for e.g -3 < 2 *x + 1 < 5.

Is there any way of getting inequality solution using python?

7
  • Well, 2x will be invalid. What code did you try? Mar 22, 2017 at 12:13
  • i have updated my question actually there was typo , -3 < 2 * x + 1 < 5.
    – Viraj
    Mar 22, 2017 at 12:18
  • See my answer below. The code works. I'm not sure what you mean by "but i can't pass two side inequality equation". Mar 22, 2017 at 12:19
  • When I run type that inequality into python I get a boolean in return. Are you looking for a range of x values that satisfy the inequality? Mar 22, 2017 at 12:19
  • @IvanKelber Ooo, didn't think that's what he meant. I would have expected him to say that he's trying to solve the inequality. Mar 22, 2017 at 12:22

1 Answer 1

1

The formula -3 < 2*x + 1 < 5 abbreviates two inequalities into one:

-3 < 2*x + 1
     2*x + 1 < 5

In other words, it is a system of inequalities that you need to solve, using something like this:

solve_rational_inequalities([[
    ((-3, Poly(2*x + 1)), '<'),
    ((Poly(2*x + 1), 5),  '<')]])
2
  • From the documentation: from sympy.solvers.inequalities import solve_rational_inequalities Mar 22, 2017 at 12:35
  • Thanks, that's what I had but I briefly considered changing to poly_inequality (but munged the code)
    – alexis
    Mar 22, 2017 at 12:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.