Representing domains in SymPy

80 views
Skip to first unread message

Ben Lucato

unread,
Jul 24, 2013, 7:10:07 AM7/24/13
to sy...@googlegroups.com
We can represent domains on paper quite easily - for instance we can write x < 0, or alternatively x (epsilon symbol) R-, or even x (epsilon symbol) (-infinity, 0)

I looked around but couldn't really find that - is there a canonical way to be writing domains in SymPy?

Stefan Krastanov

unread,
Jul 24, 2013, 7:34:20 AM7/24/13
to sy...@googlegroups.com
It depends what exactly you want to do.

If you need it just for typography purposes (e.g. writing something in IPython notebook and wanting to print the expression) you are using sympy incorrectly. SymPy is not a typography library. (if you insists there are hacks to do it)

On the other hand quite frequently you need this for meaningful mathematics.

- if you want to work on polynomials and do certain operations (finding roots, etc) over a given field, you do this by specifying the field during the creation of the polynomial.

- there is some work in progress to be able to do the same for matrices, but it is not ready.

- in general, there is the assumption module. It is a bit of a mess, because we have an old and a new assumption module and we try to move to the new one. If all that you want is for abs(x) to automatically return x (or something similar) it suffices to define x as `x=Symbol('x', positive=True)`. There are a few other handles like `real` and `integer`.

- if you need something more general or more fancy, we may have it in some (possibly unfinished, mostly unused) form, but it goes deeper in SymPy so a more precise question will help us give you a more precise answer.


On 24 July 2013 13:10, Ben Lucato <ben.l...@gmail.com> wrote:
We can represent domains on paper quite easily - for instance we can write x < 0, or alternatively x (epsilon symbol) R-, or even x (epsilon symbol) (-infinity, 0)

I looked around but couldn't really find that - is there a canonical way to be writing domains in SymPy?

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Matthew Rocklin

unread,
Jul 24, 2013, 9:48:38 AM7/24/13
to sy...@googlegroups.com
SymPy can represent sets easily.  We do not have a standard class to couple sets to variables but it might be useful.  I had to deal with this problem when making sympy.stats.  Unfortunately I can't recommend the final implementation.  I'd love to have something cleaner within core sympy.  I think a first draft might be as simple as a pairing of variables with sets.

Matthew Rocklin

unread,
Jul 24, 2013, 9:49:56 AM7/24/13
to sy...@googlegroups.com
Stuff with sets:

In [1]: squares = TransformationSet(Lambda(x, x**2), S.Naturals)

In [2]: squares
Out[2]: 
⎧ 2        ⎫
⎨x  | x ∊ ℕ⎬
⎩          ⎭

Aaron Meurer

unread,
Jul 24, 2013, 12:17:55 PM7/24/13
to sy...@googlegroups.com
You can already write inequalities, and combine them using And and Or:

In [25]: x > 0
Out[25]: x > 0

In [26]: Or(x > 0, x < 1)
Out[26]: x > 0 ∨ x < 1

In [27]: Or(x > 0, x < 1).subs(x, 2)
Out[27]: True

If all you care about is getting it to work mathematically (i.e., with
subs), then you can get pretty far with this. It won't work with the
assumptions system, though, and it can get messy fast, especially if
you do care about printing.

I think what we need is just a Contains object, subclassing from
Boolean, which would work like Contains(x, Set), where Set is any set
from the sets module. It shouldn't be too hard to write something like
this. Basic functionality just needs to check set.contains for
evaluation, and implement basic pretty printing with ∈.

One issue is that there still isn't a very clear separation between
boolean and symbolic objects
(https://code.google.com/p/sympy/issues/detail?id=1887). And there are
of course issues with the assumptions system in general.

Aaron Meurer


On Wed, Jul 24, 2013 at 6:34 AM, Stefan Krastanov
<krastano...@gmail.com> wrote:

Matthew Rocklin

unread,
Jul 24, 2013, 12:31:23 PM7/24/13
to sy...@googlegroups.com
What are your thoughts on multivariate domains?  Is this just a composition of Contains objects and boolean operators?  Or would we have objects like Contains((x, y), Set) ?

Alan Bromborsky

unread,
Jul 24, 2013, 12:35:05 PM7/24/13
to sy...@googlegroups.com
Why not implement the following for domain definition. Let
f(x_1,...,x_n) be a real function of n real variables then if
f(x_1,...,x_n) > 0 the point [x_1,...,x_n] is outside the domain and if
f(x_1,...,x_n) <= 0 the point is inside the domain.

Aaron Meurer

unread,
Jul 24, 2013, 12:39:10 PM7/24/13
to sy...@googlegroups.com
To me, it's just an element of the cartesian product, which just
contains tuples, so yeah, Contains((x, y), ProductSet(A, B)). Or you
could write x*y > 0. I guess what we also need are some functions to
convert back and forth from set notation to relational notation.

Aaron Meurer

Matthew Rocklin

unread,
Jul 24, 2013, 12:53:58 PM7/24/13
to sy...@googlegroups.com
On Wed, Jul 24, 2013 at 11:39 AM, Aaron Meurer <asme...@gmail.com> wrote:
To me, it's just an element of the cartesian product

Note that not all domains will be Cartesian products.  Consider the triangle 0 < x < 1 and 0 < y < 1 and x + y < 1
 
, which just contains tuples, so yeah, Contains((x, y), ProductSet(A, B)).

This syntax passes the buck to sets.  I think that this is reasonable.
 
Or you
could write x*y > 0. I guess what we also need are some functions to
convert back and forth from set notation to relational notation.

This already exists to a certain extent for some (but not all) sets.

In [1]: Interval(0, 1).as_relational(x)
Out[1]: 0 ≤ x ∧ x ≤ 1

Aaron Meurer

unread,
Jul 24, 2013, 1:06:58 PM7/24/13
to sy...@googlegroups.com
On Wed, Jul 24, 2013 at 11:53 AM, Matthew Rocklin <mroc...@gmail.com> wrote:
>
> On Wed, Jul 24, 2013 at 11:39 AM, Aaron Meurer <asme...@gmail.com> wrote:
>>
>> To me, it's just an element of the cartesian product
>
>
> Note that not all domains will be Cartesian products. Consider the triangle
> 0 < x < 1 and 0 < y < 1 and x + y < 1 .

It is a subset of the product. All I meant is that the elements are
tuples. How you actually construct these sets for various shapes is
another question.

>
>>
>> , which just contains tuples, so yeah, Contains((x, y), ProductSet(A, B)).
>
>
> This syntax passes the buck to sets. I think that this is reasonable.
>
>>
>> Or you
>> could write x*y > 0. I guess what we also need are some functions to
>> convert back and forth from set notation to relational notation.
>
>
> This already exists to a certain extent for some (but not all) sets.
>
> In [1]: Interval(0, 1).as_relational(x)
> Out[1]: 0 ≤ x ∧ x ≤ 1

Oh great. That really should be Contains.as_relational.

Aaron Meurer

Ben Lucato

unread,
Jul 24, 2013, 10:27:02 PM7/24/13
to sy...@googlegroups.com
Yeah I initially thought of doing this - but say I want to do:

y = Symbol('y')
domain = And(-oo < y, y < oo)
print domain
>>> True

Which is unfortunate! Since having a domain over all real numbers is not uncommon


You received this message because you are subscribed to a topic in the Google Groups "sympy" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sympy/vMa2TEbdb-k/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sympy+un...@googlegroups.com.

To post to this group, send email to sy...@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy.
For more options, visit https://groups.google.com/groups/opt_out.





--
 
 
Ben Lucato
----------------------------------------------------------------------------------

Ben Lucato

unread,
Jul 24, 2013, 10:38:10 PM7/24/13
to sy...@googlegroups.com
What I'm doing in this case is writing simple functions, say y = log(2*x)/2 - 3 and then computing their inverse through a function I made myself. The usefulness in having domains is that when you compute an inverse of a function, the domain of the inverse is the range of the original. Likewise, the range of the inverse is the domain of the original.

Since it's for my own purposes I really could represent it with nested lists/tuples, using booleans to include a value or not:

[[(-sympy.oo, False), (-1, True)], [(0, False), (sympy.oo, True)]]

which represents (-inf, -1] U (0, inf)

which would work fine for my purposes. However I'm pretty sure with what I plan to do I'm going to be using domains a fair bit more in the future so if I have something that is going to work with what SymPy has planned then it may help.


--
You received this message because you are subscribed to a topic in the Google Groups "sympy" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sympy/vMa2TEbdb-k/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sympy+un...@googlegroups.com.

To post to this group, send email to sy...@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Matthew Rocklin

unread,
Jul 25, 2013, 8:39:36 AM7/25/13
to sy...@googlegroups.com
Since it's for my own purposes I really could represent it with nested lists/tuples, using booleans to include a value or not:

[[(-sympy.oo, False), (-1, True)], [(0, False), (sympy.oo, True)]]

which represents (-inf, -1] U (0, inf)

For this all you need is sets.  Note that they use False/True to mean openness, not inclusion.

In [4]: Interval(-oo, -1, True, False) + Interval(0, oo, True, True)
Out[4]: (-∞, -1] ∪ (0, ∞)
 
which would work fine for my purposes. However I'm pretty sure with what I plan to do I'm going to be using domains a fair bit more in the future so if I have something that is going to work with what SymPy has planned then it may help.

I suspect that we're overengineering things.  If all you need is to represent sets then SymPy should have you covered.  If you also need to attach each set to a particular symbol (like x or y) then we run into new territory.

Ben Lucato

unread,
Jul 25, 2013, 9:30:58 AM7/25/13
to sy...@googlegroups.com
Oh sick - intervals are exactly what I'm looking for. Thanks!

Aaron Meurer

unread,
Jul 25, 2013, 11:37:33 AM7/25/13
to sy...@googlegroups.com
Ah, that *might* be considered a bug. Our assumptions system(s) don't
really have a clear distinction between real numbers and extended real
numbers, unfortunately.

Aaron Meurer

Aaron Meurer

unread,
Jul 25, 2013, 11:39:10 AM7/25/13
to sy...@googlegroups.com
Yes, this probably wasn't clear enough in my previous responses, but
the sets module is very descriptive and well-developed (OK, there's
still work that needs to be done, but it's coming along nicely). If
you just want to represent a set, you can probably do it.

Aaron Meurer
Reply all
Reply to author
Forward
0 new messages