Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complement of interval? #15

Open
kenahoo opened this issue Mar 2, 2017 · 5 comments
Open

Complement of interval? #15

kenahoo opened this issue Mar 2, 2017 · 5 comments

Comments

@kenahoo
Copy link

kenahoo commented Mar 2, 2017

Is there a way to take the complement of an interval object?

Strictly speaking I think it's not possible since you define all your intervals to be unions of closed simple intervals, so the complements would be unions of open simple intervals, which are not representable in your scheme.

But if you define complement(x) to be the smallest object representable in your scheme whose union with x produces the entire extended real line; or equivalently, the union of the classical complement of x with x.extrema(), then it's in your scheme and it would work for me.

@taschini
Copy link
Owner

taschini commented Mar 5, 2017

You are perfectly right.

I should be able to add a x.closed_complement() method returning the closure of the complement of x, which, as you said, it would be equal to the union of the set-theoretic complement of x and x.extrema.

@taschini
Copy link
Owner

taschini commented Mar 5, 2017

Alternatively, since the set-theoretic complement of an instance of the Interval class is not representable with an instance of the Interval class, I could define a method that simply returns a list of pairs. The reason this might be preferrable is illustrated by:

>>> x = interval([1, 2], [3])
>>> x.closed_complement()
interval([-inf, 1.0], [2.0, inf])

>>> x.complement()
[(-inf, 1.0), (2.0, 3.0), (3.0, inf)]

@dangom
Copy link

dangom commented May 23, 2017

Landed here looking for exactly this functionality.
Any suggestions on how to implement it?

@kenahoo
Copy link
Author

kenahoo commented May 23, 2017

Here's how I'm doing it, in a new subclass of interval:

class MyInterval(interval):
    def complement(self) -> 'MyInterval':
        chain = itertools.chain(self, [[inf, None]])

        out = []
        prev = [None, neg_inf]
        for this in chain:
            if prev[1] != this[0]:
                out.append([prev[1], this[0]])
            prev = this

        return self.__class__(*out)

@langelgjm
Copy link

Thanks @kenahoo this was exactly what I needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants