Transformations on sets/intervals/unions

29 views
Skip to first unread message

Ben Lucato

unread,
Jul 29, 2013, 8:24:11 PM7/29/13
to sy...@googlegroups.com
Howdy, say I do:

domain = Interval(-pi, 0, True, True) + Interval(0, pi/2, True, True)


I could hope to do a transformation to dilate this domain by a factor 2 from the origin, like so:


new_domain = domain.replace(lambda expr: expr.is_Real, lambda expr: 2*expr)
OR:
new_domain = TransformationSet(Lambda(x, 2*x), domain)

in either case, checking:

3*pi/4 in new_domain

returns False.

I may be using TransformationSet incorrectly - I just looked in the docs and it looked like something relevant. Perhaps it only works for sets? 

Matthew Rocklin

unread,
Jul 29, 2013, 8:34:48 PM7/29/13
to sy...@googlegroups.com
This is what I get.  Can you verify that things don't work for you?  This was done in sympy/master.

In [1]: domain = Interval(-pi, 0, True, True) + Interval(0, pi/2, True, True)

In [2]: new_domain = TransformationSet(Lambda(x, 2*x), domain)

In [3]: domain
Out[3]: 
⎛   π⎞          
⎜0, ─⎟ ∪ (-π, 0)
⎝   2⎠          

In [4]: new_domain
Out[4]: 
⎧          ⎛   π⎞          ⎫
⎨2⋅x | x ∊ ⎜0, ─⎟ ∪ (-π, 0)⎬
⎩          ⎝   2⎠          ⎭

In [5]: 3*pi/4 in new_domain
Out[5]: True



--
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.
 
 

Ben Lucato

unread,
Jul 29, 2013, 10:37:31 PM7/29/13
to sy...@googlegroups.com
Thanks - I updated to the git master and now the TransformationSet works. Is there some way to take this new TransformationSet object and rewrite it as a Union/Interval?

i.e. 

get this:

Interval(-2*pi, 0, True, True) + Interval(0, pi, True, True)

Matthew Rocklin

unread,
Jul 29, 2013, 11:18:54 PM7/29/13
to sy...@googlegroups.com
At some point we might do a simplify sort of treatment for sets.

In the meantime something like the following could work in the case of monotonic functions.  In general we know how to transform Intervals and FiniteSets.  We know that unions and intersections will compose well with these transformations.  That's all you really need.

This should eventually be broken out into methods on the various classes:

def transform_set(x, expr, set):
    """ Transform a set by an expression

    >>> domain = Interval(-pi, 0, True, True) + Interval(0, pi/2, True, True)
    (0, pi/2) U (-pi, 0)

    >>> transform(x, 2*x, domain)
    (0, pi) U (-2*pi, 0)

    >>> transform(x, x**2, domain)
    (0, pi**2)
    """
    if isinstance(set, Union):
        return Union(transform_set(x, expr, arg) for arg in set.args)
    if isinstance(set, Intersection):
        return Intersection(transform_set(x, expr, arg) for arg in set.args)
    z = Dummy('z', real=True)
    f = Lambda(x, expr)
    if isinstance(set, Interval):
        # TODO: manage left_open and right_open better
        left, right = f(set.left), f(set.right)
        return Interval(Min(left, right), Max(left, right),
                        set.left_open, set.right_open)
    if isinstance(set, FiniteSet):
        return FiniteSet(map(f, set))

Ben Lucato

unread,
Jul 30, 2013, 8:14:52 AM7/30/13
to sy...@googlegroups.com
Awesome! You are a gentleman and a scholar, Matthew Rocklin.

Thanks for letting me know there is no way to do this in the current sympy.


--
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/WlG3XirFAzM/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
----------------------------------------------------------------------------------

Matthew Rocklin

unread,
Jul 30, 2013, 9:33:59 AM7/30/13
to sy...@googlegroups.com
Really I just like working on sets.  They're fairly simple and fun to play with.  Here is a pull request adding a `transform` method to sets

https://github.com/sympy/sympy/pull/2340

Matthew Rocklin

unread,
Aug 1, 2013, 8:43:55 AM8/1/13
to sy...@googlegroups.com
I'm renaming TransformationSet to ImageSet (I think this is a more precise name).  


2340 is in by the way but the interface will change shortly to 

>>> Interval(0, 1).image(x, 2*x)
[0, 2]

Matthew Rocklin

unread,
Aug 9, 2013, 7:16:56 AM8/9/13
to sy...@googlegroups.com
The API has changed in a recent PR


>>> imageset(x, 2*x, Interval(0, 1)
[0, 2]
Reply all
Reply to author
Forward
0 new messages