Giacomo d'Antonio
Giacomo d'Antonio

Reputation: 2275

sympy set: iterate over intervals

I need to manipulate some intervals of real numbers. Basically I'll perform unions and intersections thereof. This way I always obtain sets of real numbers that are unions of a finite number of intervals.

At the moment I'm using sympy for python. My question is: given a sympy Set, is there a (nice) way to iterate over its intervals?

One possibility would be to use the repr string of the set, which looks something like this:

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

and then use regular expressions to unpack it.

Is there a nicer and more python way to do this?

Upvotes: 7

Views: 780

Answers (1)

Giacomo d'Antonio
Giacomo d'Antonio

Reputation: 2275

So, I'll answer myself. I needed to use the attribute args of the class Union. This gives a tuple of the Sets whose union is being 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})

Upvotes: 8

Related Questions