what does the pattern argument of rewrite do?

45 views
Skip to first unread message

Nathan Thern

unread,
Nov 1, 2016, 2:07:34 PM11/1/16
to sympy
I'm new to sympy and pouring through the documentation. I don't understand the "pattern" part of the argument to rewrite. The following portion of a session illustrates my question:

>>> f=sin(x)+cos(y)/gamma(z)
>>> f.rewrite(exp)
-I*(exp(I*x) - exp(-I*x))/2 + (exp(I*y)/2 + exp(-I*y)/2)/gamma(z)
>>> f.rewrite(sin,exp)
-I*(exp(I*x) - exp(-I*x))/2 + (exp(I*y)/2 + exp(-I*y)/2)/gamma(z)

Why does rewrite(sin,exp) also rewrite the cos(...) part of the expression when it has been explicitly given "sin" as the pattern to rewrite?

NT


Aaron Meurer

unread,
Nov 1, 2016, 2:18:00 PM11/1/16
to sy...@googlegroups.com
I never knew about this pattern argument to rewrite. My guess is that
there's a bug in there. According to the docstring, you're supposed to
call it as f.rewrite([sin,], exp), but for me that does the same
thing.

I would suggest using f.replace if you want targeted rewriting.

Aaron Meurer
> --
> 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 https://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/f7874739-0d2b-4259-9b3d-76ae958daf8a%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Nathan Thern

unread,
Nov 1, 2016, 2:26:07 PM11/1/16
to sympy
According to
f.rewrite(exp), f.rewrite(sin, exp), and f.rewrite([sin,], exp) are all valid.
I found that I can replace just the sin term with the following expression:

>>> f.replace(sin, lambda arg: sin(arg).rewrite(sin, exp))
-I*(exp(I*x) - exp(-I*x))/2 + cos(y)/gamma(z)

This seems to create the functionality I was expecting.
But I'm still not sure if I don't understand what the docs are saying about rewrite or if there's a bug.

Aaron Meurer

unread,
Nov 1, 2016, 2:53:06 PM11/1/16
to sy...@googlegroups.com
On Tue, Nov 1, 2016 at 2:26 PM, Nathan Thern <nth...@gmail.com> wrote:
> According to
> http://docs.sympy.org/latest/modules/core.html
> f.rewrite(exp), f.rewrite(sin, exp), and f.rewrite([sin,], exp) are all
> valid.
> I found that I can replace just the sin term with the following expression:
>
>>>> f.replace(sin, lambda arg: sin(arg).rewrite(sin, exp))
> -I*(exp(I*x) - exp(-I*x))/2 + cos(y)/gamma(z)
>
> This seems to create the functionality I was expecting.
> But I'm still not sure if I don't understand what the docs are saying about
> rewrite or if there's a bug.

I looked through the code and I think it's supposed to work like you
described (only rewriting the sin as exp). I'd have to debug it to see
why it isn't working.

Aaron Meurer
> https://groups.google.com/d/msgid/sympy/c8fdec88-74d0-4dc6-8736-cb5b5dfbcaeb%40googlegroups.com.

Shekhar Prasad Rajak

unread,
Nov 4, 2016, 2:26:24 AM11/4/16
to sympy
I tried to fix this issue in this PR : https://github.com/sympy/sympy/pull/11804

--
Shekhar
Reply all
Reply to author
Forward
0 new messages