Sympy set: iterate over intervals

I need to manipulate some intervals of real numbers. I will mainly do unions and their crossroads. Thus, I always get sets of real numbers, which are the union of a finite number of intervals.

I am currently using sympy for python. My question is: if the Set simplex is given, is there a (good) way to iterate over its intervals?

One possibility is to use a set representation string that looks something like this:

(-oo, 5] U [7, 20] 

and then use regular expressions to unpack it.

Is there a nicer and easier way python?

+6
source share
1 answer

So, I will answer myself. I needed to use the args attributes of the Union class. This gives a tuple of sets whose union is considered:

 >>> union [2.0, 10.0) U [20.0, 30.0) U {1.0, 15.0, 17.0, 40.0} >>> union.args ([2.0, 10.0), [20.0, 30.0), {1.0, 15.0, 17.0, 40.0}) 
+7
source

Source: https://habr.com/ru/post/919491/


All Articles