#29745·sympy

solver help for interesting (to me) problem

Author: deanm0000Created May 6, 2026Updated Sep 15, 2026
Labelssolvers.solveset

I stumbled on this bprp problem https://www.youtube.com/watch?v=Skl236rfDYs and I fed it to sympy which didn't do well with it.

It goes like this:

python
x = Symbol("x")
eq = 2 * sin(x) + -2 * sqrt(3) * cos(x) + sqrt(3) * tan(x) - 3

$\displaystyle 2 \sin{\left(x \right)} - 2 \sqrt{3} \cos{\left(x \right)} + \sqrt{3} \tan{\left(x \right)} - 3$

If I try to solveset(eq,x) it will just run for over an hour without finishing.

However if I manually do what he does then it solves it. I do it in 2 steps.

First is to convert tan to sin/cos

python
tan_identity = sin(x) / cos(x)
assert tan(x) == trigsimp(tan_identity)
eq2 = eq.subs(tan(x), tan_identity)

$\displaystyle 2 \sin{\left(x \right)} + \frac{\sqrt{3} \sin{\left(x \right)}}{\cos{\left(x \right)}} - 2 \sqrt{3} \cos{\left(x \right)} - 3$

I assume sympy is good at doing that by itself.

The next step is to treat 3 as $\displaystyle \sqrt{3}^{2}$ and manually factor out sin terms.

For that I substituted in a new variable as a place holder so it doesn't so eagerly turn $\displaystyle \sqrt{3}^{2}$ into 3 and then only later put the 3 back.

python
sin_terms = []
not_sin_terms = []
first_term, other_terms = eq2.as_coeff_add()
terms = [first_term]
terms.extend(list(other_terms))
for term in terms:
    if (coeff := term.as_coefficient(sin(x))) is None:
        not_sin_terms.append(term)
    else:
        sin_terms.append(coeff)


a = Symbol("a")
sin_factor = reduce(Add, sin_terms).subs(sqrt(3), a)
left_over = reduce(Add, not_sin_terms).subs(sqrt(3), a).subs(3, a**2)
left_over_factor = simplify(left_over / sin_factor)
eq3 = (sin_factor * (sin(x) + left_over_factor)).subs(a, sqrt(3))

$\displaystyle \left(2 + \frac{\sqrt{3}}{\cos{\left(x \right)}}\right) \left(\sin{\left(x \right)} - \sqrt{3} \cos{\left(x \right)}\right)$

Once the problem is represented this way solveset(eq3,x) finds the solution almost immediately.

I don't know anything about sympy's inner workings to even begin to understand how hard it might be to generalize this trick for the solver but just thought I'd present it incase someone else might.