Search code examples
pythonalgorithmsetlinear-algebraintersection

taking intersection over a continous range of values in python


I have a domain of values on which I want to take intersection. for example. 0 <= n1 < 2*pi and -pi <= n2 < pi now the n1 intersection n2 would be [0,pi). The ranges of n1 and n2 can be anything. I want to know if there is an easy way to do this. I am currently using nested if-else to do this. I also want to know if their intersection is null if there is no overlap.


Solution

  • In module sympy there is a class Set and several subclasses, including Interval.

    from sympy import Interval, Intersection, pi
    
    n1 = Interval.Ropen(0, 2*pi)
    n2 = Interval.Ropen(-pi, pi)
    
    n3 = Intersection(n1, n2)
    
    print(n3)
    # Interval.Ropen(0, pi)
    

    Relevant documentation: sympy Sets