Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simplify works better than trigsimp #17778

Open
meganly opened this issue Oct 22, 2019 · 4 comments
Open

simplify works better than trigsimp #17778

meganly opened this issue Oct 22, 2019 · 4 comments
Labels

Comments

@meganly
Copy link
Contributor

meganly commented Oct 22, 2019

simplify does better than trigsimp in simplifying the following expression.

>>> from sympy import trigsimp, simplify, symbols, cos, sin, cot
>>> x = symbols('x')
>>> expr = 1/(sin(x)*cos(x))-cot(x)
>>> simplify(expr)
tan(x)
>>> trigsimp(expr)
-cot(x) + 2/sin(2*x)

As @asmeurer suspected, simplify writes the expression as a combined fraction first

>>> trigsimp(cancel(expr))
tan(x)

It seems that trigsimp should try cancel() first somewhere in here

tree = [identity,
(
TR3, # canonical angles
TR1, # sec-csc -> cos-sin
TR12, # expand tan of sum
lambda x: _eapply(factor, x, trigs),

@asmeurer
Copy link
Member

This would also be a good use case for the groebner method, but it doesn't seem to know about cot.

@meganly
Copy link
Contributor Author

meganly commented Oct 23, 2019

True, but the default trigsimp algorithm doesn't call the groebner method.

Would adding lambda x: _eapply(cancel, x, trigs), to the tree shown in the code above make trigsimp also try canceling? I'm a little confused how the code to construct the tree works.

@asmeurer
Copy link
Member

greedy tries the functions greedily to minimize the objective function which is defined above that code.

I think a problem that you would run into is that the objective is the number of trig functions (L) and then the total number of operations. But cancel actually increases the number of operations

>>> expr = 1/(sin(x)*cos(x))-cot(x)
>>> expr.count_ops()
6
>>> cancel(expr).count_ops()
11

So my guess from reading the code is that it wouldn't work. But I also didn't try it, so I could be wrong.

@asmeurer
Copy link
Member

cancel also increases the number of trig functions

>>> from sympy.simplify.fu import L
>>> L(expr)
3
>>> L(cancel(expr))
5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants