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?
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})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With