expand(trig=True) doesn't like difference of angles

39 views
Skip to first unread message

G B

unread,
Sep 19, 2012, 9:55:30 PM9/19/12
to sy...@googlegroups.com
I'm still new to sympy, so I might be doing something stupid, but I think there's a problem here:

In [1]: from sympy import *
In [2]: x,y=symbols('x,y')
In [3]: cos(x+y).expand(trig=True)
Out[3]: -sin(x)*sin(y) + cos(x)*cos(y)

So far so good.  but then:
In [4]: cos(x-y).expand(trig=True)

Leads to a long traceback that culminates with: AttributeError: 'Mul' object has no attribute '_eval_expand_trig'

For giggles, I also tried:
In [5]: cos(x + (-y)).expand(trig=True)

Which fails in the same way.  I can post the full traceback if it helps.

Aaron Meurer

unread,
Sep 19, 2012, 10:01:48 PM9/19/12
to sy...@googlegroups.com
Ah, that is a bug. You should report that at
http://code.google.com/p/sympy/issues/. The reason it happens is that
it tries to do the expansion recursively, but sin(-x) becomes -sin(x).
The fix is not too hard either, if you want to give it a shot.

The work around is to do the expansion with +y and then do
expr.subs(y, -y) at the end.

By the way, x - y and x + -y are exactly the same thing in SymPy.
Both are represented internally as Add(x, Mul(-1, y)).

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/sympy/-/-Z8Y7YC_PuEJ.
> To post to this group, send email to sy...@googlegroups.com.
> To unsubscribe from this group, send email to
> sympy+un...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/sympy?hl=en.

G B

unread,
Sep 19, 2012, 11:49:04 PM9/19/12
to sy...@googlegroups.com
Oh, so that's where the broken Mul comes from...  =)  Makes sense, of course, but I was looking at my code saying "there's no multiplication!".

I've submitted the issue.

G B

unread,
Sep 20, 2012, 4:36:33 AM9/20/12
to sy...@googlegroups.com
I seem to have fixed my problem by making changing in sympy/functions/elementary/trigonometric.py this definition in class sin (and making a corresponding change in class cos).  The change was to tell sin/cos not evaluate in their constructors.

I haven't run any tests on this except for my code where I found the problem.



    def _eval_expand_trig(self, **hints):
        from sympy import expand_mul
        arg = self.args[0]
        x = None
        if arg.is_Add: # TODO, implement more if deep stuff here
            # TODO: Do this more efficiently for more than two terms
            x, y = arg.as_two_terms()
#GCB 120920 added ",evaluate=False" to the next 4 lines
            sx = sin(x,evaluate=False)._eval_expand_trig()
            sy = sin(y,evaluate=False)._eval_expand_trig()
            cx = cos(x,evaluate=False)._eval_expand_trig()
            cy = cos(y,evaluate=False)._eval_expand_trig()
            return sx*cy + sy*cx
        else:
            n, x = arg.as_coeff_Mul(rational=True)
            if n.is_Integer: # n will be positive because of .eval
                # canonicalization

                if n.is_odd:
                    return (-1)**((n - 1)/2)*C.chebyshevt(n, sin(x))
                else:
                    return expand_mul((-1)**(n/2 - 1)*cos(x)*C.chebyshevu(n -
                        1, sin(x)), deep=False)
        return sin(arg)



On Wednesday, September 19, 2012 7:02:11 PM UTC-7, Aaron Meurer wrote:

Aaron Meurer

unread,
Sep 22, 2012, 10:14:01 PM9/22/12
to sy...@googlegroups.com

G B

unread,
Sep 23, 2012, 4:18:03 AM9/23/12
to sy...@googlegroups.com
Great! I'd seen the fixed notice on the issue, but wasn't sure what the patch was. Glad to hear that I was a bit of help.

Cheers—
Greg
Reply all
Reply to author
Forward
0 new messages