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

bool, relational: added as_set method #2781

Merged
merged 1 commit into from
Jan 19, 2014
Merged

bool, relational: added as_set method #2781

merged 1 commit into from
Jan 19, 2014

Conversation

hargup
Copy link
Contributor

@hargup hargup commented Jan 16, 2014

as_set is the inverse method for existing as_relational
method for sets. as_set converts a univariate boolean or
relational expression to real set.

Example:

In [1]: from sympy import And, Or

In [2]: from sympy.abc import x

In [3]: And(x<2, x>-2).as_set()
Out[3]: (-2, 2)

In [4]: (x**2 > 4).as_set()
Out[4]: (-oo, -2) U (2, oo)

@mrocklin
Copy link
Member

Cool!

What is the behavior of the following?

And(x > 0, y > 1).as_set()

Note that Set.as_relational doesn't handle this either. I just want to make sure that we raise a suitable error or that the answer is not ambiguous.

"""
Rewrite logic operators and relationals in terms of real sets.

>>> from sympy import false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

insert

Examples
========

(even for a single test spell it with an S)

@asmeurer
Copy link
Member

Should this be implemented using a _as_set to dispatch. @mrocklin what's the advantage of doing it that way rather than just using as_set directly?

@hargup
Copy link
Contributor Author

hargup commented Jan 17, 2014

What is the behavior of the following?

And(x > 0, y > 1).as_set()

At the time you commented it was giving the wrong result as (0, oo). Now I have modified the code to raise a suitable error. I have also added a XFAIL to test to remind us of future work.

@skirpichev
Copy link
Contributor

At the time you commented it was giving the wrong result as (0, oo)

No. But, perhaps, it does matter in which order I have run the above statements.

Now I have modified the code to raise a suitable error.

Which one? I don't see any difference:

In [1]: And(x>0, y>1).as_set()
Out[1]: (1, ∞)

In [2]: Or(x>0, y>1).as_set()
Out[2]: (0, ∞)

@hargup
Copy link
Contributor Author

hargup commented Jan 17, 2014

At the time you commented it was giving the wrong result as (0, oo)

No. But, perhaps, it does matter in which order I have run the above statements.

Sorry but I couldn't understand your comment. What I meant was that the code was buggy the time Matthew asked for the result of And(x>0, y>1).as_set(). Then I amended the commit to fix it.

The code on my system is raising the correct error (with an unintentional typo). I don't know why we are getting different results. See the lines of code which raises error here: https://github.com/sympy/sympy/pull/2781/files#diff-cf47a176c69dd7d5e3225858891e091cR288

from sympy import *

from sympy.abc import x, y

And(x>0, y>1).as_set()
---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-3-c4a8716c2ceb> in <module>()
----> 1 And(x>0, y>1).as_set()

/home/hargup/arbit_sympy_testing/sympy/logic/boolalg.pyc in as_set(self)
    286             return Intersection(*[arg.as_set() for arg in self.args])
    287         else:
--> 288             raise NotImplementedError("Sorry, And.as_set has not yet been"
    289                                       " implemented for mutivariate"
    290                                       " expressions")

NotImplementedError: Sorry, And.as_set has not yet been implemented for mutivariate expressions

Or(x>0, y>1).as_set()
---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-4-9d432be651cb> in <module>()
----> 1 Or(x>0, y>1).as_set()

/home/hargup/arbit_sympy_testing/sympy/logic/boolalg.pyc in as_set(self)
    348             return Union(*[arg.as_set() for arg in self.args])
    349         else:
--> 350             raise NotImplementedError("Sorry, Or.as_set has not yet been"
    351                                       " implemented for mutivariate"
    352                                       " expressions")

NotImplementedError: Sorry, Or.as_set has not yet been implemented for mutivariate expressions

@skirpichev
Copy link
Contributor

On Fri, Jan 17, 2014 at 05:00:39AM -0800, Harsh Gupta wrote:

The code on my system is raising the correct error (with an unintentional
typo). I don't know why we are getting different results.

Sorry. That was my bad, perhaps, I've not run git fetch.

@mrocklin
Copy link
Member

Should this be implemented using a _as_set to dispatch. @mrocklin what's the advantage of doing it that way rather than just using as_set directly?

I mostly followed tradition. The benefits I've seen though are shared docstrings and the ability to enforce code at the superclass level (see Set.contains)

`as_set` is the inverse method for existing `as_relational`
method for sets. `as_set` converts a univariate boolean or
relational expression to real set.

Example:

In [1]: from sympy import And, Or

In [2]: from sympy.abc import x

In [3]: And(x<2, x>-2).as_set()
Out[3]: (-2, 2)

In [4]: (x**2 > 4).as_set()
Out[4]: (-oo, -2) U (2, oo)
@hargup
Copy link
Contributor Author

hargup commented Jan 18, 2014

There was a typo in 'multivariate', I have fixed it.

@hargup
Copy link
Contributor Author

hargup commented Jan 18, 2014

Travis is being timed out. Is there anything that I do about it?

smichr added a commit that referenced this pull request Jan 19, 2014
bool, relational: added as_set method
@smichr smichr merged commit 4936b65 into sympy:master Jan 19, 2014
@smichr
Copy link
Member

smichr commented Jan 19, 2014

Thanks. This is in.

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

Successfully merging this pull request may close these issues.

5 participants