Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sympy set: iterate over intervals

Tags:

python

sympy

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?

like image 532
Giacomo d'Antonio Avatar asked Jul 02 '12 10:07

Giacomo d'Antonio


1 Answers

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})
like image 147
Giacomo d'Antonio Avatar answered Oct 07 '22 18:10

Giacomo d'Antonio