将 sympy Interval 转换为设置符号

Convert sympy Interval to set notation

如何将 solveset() 结果(Interval)转换为数学集合符号字符串?

考虑这个例子

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

有没有办法把这个Interval转换成字符串;这样:

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

pretty 似乎是您正在寻找的功能:

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, ∞)