Help on solving a trig. equation

402 views
Skip to first unread message

Dan

unread,
Aug 31, 2010, 7:25:18 AM8/31/10
to sympy
Hi folks,

I'm not an expect in Mathematica but I have managed to solve a trig.
expression in Wolfram Alpha first time round, http://bit.ly/cLVFcP. I
like to stay with Python as much as I can, but haven't been able to do
this using Sympy. Could you please point out where I'm going wrong in
the following?

var('gamma')
solve([1/2 - 3*cos(x)/4 + cos(x)**3/4 - gamma == 0])

Best,

Dan

Aaron S. Meurer

unread,
Aug 31, 2010, 11:48:38 AM8/31/10
to sy...@googlegroups.com
Well, unfortunately, it looks like that particular equation is not implemented. However, you should know a few things. First, don't use == for equality. That is strictly a boolean comparison operator from Python. See http://docs.sympy.org/gotchas.html#double-equals-signs.

Second, it is best to pass the variable that you want to solve for to solve. Otherwise, it tries to solve for all of them, and (I am assuming) you don't really care about solving for gamma.

Third, be careful about using 1/2. This will give you either the floating point number 0.5 or the integer 0, depending on if "from __future__ import division" was executed. See http://docs.sympy.org/gotchas.html#python-numbers-vs-sympy-numbers. The best workaround is to use S(1)/2 first, so that it becomes SymPy's Rational(1, 2) (sorry about these limitations, but that is the cost of developing over Python).

So doing it correctly, I get:

In [7]: solve(S(1)/2 - 3*cos(x)/4 + cos(x)**3/4 - gamma, x)
...
NotImplementedError: Unable to solve the equation(tsolve: at least one Function expected at this point

It should be able to do it but it can't. Chris, does this work better on any of the 1694 issues?

If you don't mind doing a little work on your own (because solve() currently isn't smart enough to do it itself), you can use this clever workaround to obtain the solutions:

In [13]: sols = solve(S(1)/2 - 3*y/4 + y**3/4 - gamma, y)

In [14]: print [solve(i, x) for i in [cos(x) - j for j in sols]]

[[acos(1/((1/2 - I*3**(1/2)/2)*(1 - 2*gamma + (-4*gamma + 4*gamma**2)**(1/2))**(1/3)) + (1 - 2*gamma + (-4*gamma + 4*gamma**2)**(1/2))**(1/3)/2 - I*3**(1/2)*(1 - 2*gamma + (-4*gamma + 4*gamma**2)**(1/2))**(1/3)/2)], [acos(1/((1/2 + I*3**(1/2)/2)*(1 - 2*gamma + (-4*gamma + 4*gamma**2)**(1/2))**(1/3)) + (1 - 2*gamma + (-4*gamma + 4*gamma**2)**(1/2))**(1/3)/2 + I*3**(1/2)*(1 - 2*gamma + (-4*gamma + 4*gamma**2)**(1/2))**(1/3)/2)], [acos(-1/(1 - 2*gamma + (-4*gamma + 4*gamma**2)**(1/2))**(1/3) - (1 - 2*gamma + (-4*gamma + 4*gamma**2)**(1/2))**(1/3))]]

Aaron Meurer

Dan

unread,
Aug 31, 2010, 7:36:27 PM8/31/10
to sympy
Aaron,

Many thanks for your clear explanations of the problem. Sympy is
awesome, you managed to get a result in the end, but I don't think
many users would have been able to get this one to work.

I'd be interested to know why it doesn't work straight off and why
your hack got a result.

Aaron S. Meurer

unread,
Aug 31, 2010, 9:53:29 PM8/31/10
to sy...@googlegroups.com
It doesn't work because our solve() is a little poor at solving more complex equations like that one right now. To be honest, you will find with SymPy that it is really good at some things and not so good at others, because everything is implemented by volunteers, who only implement things that they are interested in or need for something. The good news is that the list of things it is good at is always increasing and the list of things that it is bad at is always shrinking, and it has gotten pretty good at many things in the past few years.

So basically, what I did should be implemented (recognize that it is a polynomial in some function of x, in this case cos(x), then solve recursively), but it isn't. If you are interested in improving the algorithm, we are always welcome to contributions. Just ask here or on the IRC channel if you need help.

Aaron Meurer

Reply all
Reply to author
Forward
0 new messages