Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python sympy (inequality OR relational) to (set OR interval)

How to I take the result from a (inequality / relational) object and make it into a (set / interval) object?

import sympy
import sympy.solvers
#import sympy.solvers.inequalities
from sympy.solvers.inequalities import reduce_rational_inequalities 

x = sympy.Symbol('x')

ExampleInequalities1 = [[x**2 <= 1]]
ResultDomain1 = reduce_rational_inequalities(ExampleInequalities1, x)
print 'ResultDomain1', ResultDomain1

Gives me:

>>> And(-1 <= x, x <= 1)

The above result could be represented as:

>>> sympy.Interval(-1, 1)

Which is a Sympy Set Interval object. (which lets me use intersection, uniion, complement etc...).

How do I perform this transformation?

like image 948
D Adams Avatar asked Oct 31 '25 21:10

D Adams


1 Answers

Use the as_set method:

>>> And(-1 <= x, x <= 1).as_set()
[-1, 1]

You can go back with as_relational

>>> Interval(-1, 1).as_relational(x)
(-1 <= x) & (x <= 1)
like image 109
asmeurer Avatar answered Nov 02 '25 11:11

asmeurer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!