Freddy Mcloughlan
Freddy Mcloughlan

Reputation: 4496

Convert sympy Interval to set notation

How do I convert a solveset() result (an Interval) to a string of mathematical set notation?

Consider this example

>>> solveset("n^2 > 4", "n", Reals)
Union(Interval.open(-oo, -2), Interval.open(2, oo))

Is there a method to convert this Interval to a string; such that:

>>> x = Union(Interval.open(-oo, -2), Interval.open(2, oo))
>>> f(x)
'(−∞,−2) ∪ (2,∞)'

Upvotes: 2

Views: 301

Answers (1)

Davide_sd
Davide_sd

Reputation: 13150

pretty seems the to be the function your are looking for:

from sympy import *
from sympy.printing.pretty import pretty
var("n")
s = solveset("n^2 > 4", "n", Reals)
t = pretty(s)
print(type(t))
# <class 'str'>
print(t)
# (-∞, -2) ∪ (2, ∞)

Upvotes: 3

Related Questions