Home>

I am programming with python3. I imported the sympy module and used the solve function to output multiple inequality solutions. I want to find and output the common range of multiple solutions. Specifically, I want to find the common range of the Solved1 variable and Solved2 variable in the source code.

Error message

The inequality solution is an Or variable. However, even if I looked up the methods and functions for retrieving or rewriting the value of the Or type variable, I could not understand it at all.

Applicable source code
import sympy as sp
x = sp.Symbol ('x')
Solved1 = sp.solve ([x ** 2 + x-6>0], [x])
Solved2 = sp.solve ([x ** 2 + x-12<0], [x])

I couldn't find any information that could be searched on the Internet, whether it was badly checked.

Supplemental information (FW/tool version etc.)

If there is no way to extract or rewrite it, it would be a bit impolite to think that you must create a program that solves the inequality from scratch without using the solve function. I would be happy if you could give me advice. Thank you.

Jupyter Notebook 6.0.1

  • Answer # 1

    When you search for "real interval intersection set", there are two ways to calculate the intersection of real interval with sympy. (There may be other

    Set interval type with Interval and take intersection with&

    Use intersection as coordinates (not tried)

    Here's what I wrote in

    1. I feel a bit overwhelming, so it's just a reference level ...

    import sympy as sp
    x = sp.Symbol ('x')
    sol1 = sp.solve ([x ** 2 + x-6], [x])
    sol2 = sp.solve ([x ** 2 + x-12], [x])
    # Stop inequality and convert to interval with the following process
    interval1_1 = sp.Interval (-sp.oo, sol1 [0] [0])
    interval1_2 = sp.Interval (sol1 [1] [0], sp.oo)
    print (interval1_1, interval1_2)
    # Interval (-oo, -3) Interval (2, oo)
    interval2 = sp.Interval (sol2 [0] [0], sol2 [1] [0])
    print (interval2)
    # Interval (-4, 3)
    print (interval1_1&interval2, interval1_2&interval2)
    # Interval (-4, -3) Interval (2, 3)

Trends