I think replace doesn't match subexpressions in an Add, because it
isn't a subtree in the expression tree. This sort of thing needs to be
improved.
This workaround seems to work
In [23]: c = Wild('c', exclude=[sin(x), cos(x)])
In [24]: expr.replace(a*sympy.sin(b)**2+a*sympy.cos(b)**2 + c, a + c)
Out[24]: 54
However, putting the right thing in the exclude parameter is
important. Otherwise, you get stuff like
In [16]: c = Wild('c')
In [17]: expr.replace(a*sympy.sin(b)**2+a*sympy.cos(b)**2 + c, a + c)
Out[17]:
2 2
- 49⋅sin (x) - 49⋅cos (x) + 103
which is technically correct (49 + 5 = -49 + 103), but probably not
what you want.
Aaron Meurer
On Thu, Feb 18, 2016 at 4:43 AM, Paul Royik <[email protected]> wrote:
> I'm trying to simplify a*cos(b)**2+a*sin(b)**2 to a
>
> So, I write
>
> a = sympy.Wild('a')
> b = sympy.Wild('b')
> expr = 49*cos(x)**2+49*sin(x)**2 + 5
> expr.replace(a*sympy.sin(b)**2+a*sympy.cos(b)**2, a, exact=True)
>
>
> But this doesn't work. expr is unchanged.
>
> --
> 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 [email protected].
> To post to this group, send email to [email protected].
> 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/ad070b63-17ad-485f-b1e1-4ccd9c4cbc4f%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
--
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 [email protected].
To post to this group, send email to [email protected].
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/CAKgW%3D6J44CSE0JyU1FG%2BgRATrDv5gxY8OubtbV7%3Djm%2B6BSnZ7g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.