corvus
corvus

Reputation: 595

Simplifying Sets in Sympy

I want to know if there is a general method for reducing a sympy.set instance to its 'simplest' form, e.g. with a minimal number of set objects and minimal repetition of set elements, ideally nested in a linear fashion. For example:

>>> from sympy.abc import x,y
>>> from sympy import S
>>> from sympy.calculus.util import continuous_domain
>>>
>>>
>>> f = (1-x)/(2+x) - 3*(x-y)/(1+x+y)
>>>
>>> continuous_domain(f, x, S.Reals)
Union(Complement(Interval.open(-oo, -2), Union(Complement(Intersection(FiniteSet(-2, -y - 1), Reals), FiniteSet((y - 3)/4 - sqrt(y**2 + 22*y + 13)/4, (y - 3)/4 + sqrt(y**2 + 22*y + 13)/4)), Intersection(FiniteSet(-y - 1), Reals))), Complement(Interval.open(-2, oo), Union(Complement(Intersection(FiniteSet(-2, -y - 1), Reals), FiniteSet((y - 3)/4 - sqrt(y**2 + 22*y + 13)/4, (y - 3)/4 + sqrt(y**2 + 22*y + 13)/4)), Intersection(FiniteSet(-y - 1), Reals))))
>>>
>>> # Display the answer with pretty printing
>>> from sympy import init_printing
>>> init_printing()
>>> 
>>> continuous_domain(f, x, S.Reals)
⎛           ⎛                 ⎛                     ⎧            ________________             ________________ ⎫⎞⎞⎞   ⎛          ⎛                 ⎛                     ⎧            ________________             ________________ ⎫⎞⎞⎞
⎜           ⎜                 ⎜                     ⎪           ╱  2                         ╱  2              ⎪⎟⎟⎟   ⎜          ⎜                 ⎜                     ⎪           ╱  2                         ╱  2              ⎪⎟⎟⎟
⎜           ⎜                 ⎜                     ⎨ y - 3   ╲╱  y  + 22⋅y + 13   y - 3   ╲╱  y  + 22⋅y + 13  ⎬⎟⎟⎟   ⎜          ⎜                 ⎜                     ⎨ y - 3   ╲╱  y  + 22⋅y + 13   y - 3   ╲╱  y  + 22⋅y + 13  ⎬⎟⎟⎟
⎜(-∞, -2) \ ⎜(ℝ ∩ {-y - 1}) ∪ ⎜(ℝ ∩ {-2, -y - 1}) \ ⎪ ───── - ───────────────────, ───── + ─────────────────── ⎪⎟⎟⎟ ∪ ⎜(-2, ∞) \ ⎜(ℝ ∩ {-y - 1}) ∪ ⎜(ℝ ∩ {-2, -y - 1}) \ ⎪ ───── - ───────────────────, ───── + ─────────────────── ⎪⎟⎟⎟
⎝           ⎝                 ⎝                     ⎩   4              4             4              4          ⎭⎠⎠⎠   ⎝          ⎝                 ⎝                     ⎩   4              4             4              4          ⎭⎠⎠⎠


This strikes me as an extremely complex expression for the solution, which can also be written as a series of complements and unions:

>>> Reals - FiniteSet(-y-1) - FiniteSet(-2) + FiniteSet((y - 3)/4 - sqrt(y**2 + 22*y + 13)/4, (y - 3)/4 + sqrt(y**2 + 22*y + 13)/4)
                     ⎧        ________________             ________________     ⎫
                     ⎪       ╱  2                         ╱  2                  ⎪
                     ⎨ y   ╲╱  y  + 22⋅y + 13    3  y   ╲╱  y  + 22⋅y + 13    3 ⎬
(ℝ \ {-2, -y - 1}) ∪ ⎪ ─ - ─────────────────── - ─, ─ + ─────────────────── - ─ ⎪
                     ⎩ 4            4            4  4            4            4 ⎭

I have searched the documentation but have not found any methods to accomplish simplification of complex set expressions such as this one. Do any such methods exist? If not, how can I write a function which allows me to accomplish this?

Upvotes: 0

Views: 210

Answers (1)

Oscar Benjamin
Oscar Benjamin

Reputation: 14480

It seems this has improved on sympy master since the last release (1.5). With master I get

In [1]: from sympy.calculus.util import continuous_domain                                                                         

In [2]: x, y = symbols('x, y')                                                                                                    

In [3]: f = (1-x)/(2+x) - 3*(x-y)/(1+x+y)                                                                                         

In [4]: continuous_domain(f, x, S.Reals)                                                                                          
Out[4]: ((-∞, -2) \ (ℝ ∩ {-y - 1})) ∪ ((-2, ∞) \ (ℝ ∩ {-y - 1}))

You can improve this by declaring y to be real:

In [5]: x, y = symbols('x, y', real=True)                                                                                         

In [6]: f = (1-x)/(2+x) - 3*(x-y)/(1+x+y)                                                                                         

In [7]: continuous_domain(f, x, S.Reals)                                                                                          
Out[7]: ((-∞, -2) ∪ (-2, ∞)) \ {-y - 1}

Upvotes: 1

Related Questions