I am looking for recommendations for math software that can do manipulations with trigonometric polynomials. Basically, I have a bunch of trigonometric expressions like $$K \cos{\left(\alpha + \chi_1 - \chi_2 - \phi_1 + \phi_2 \right)} + K \cos{\left(\alpha - \phi_1 + \phi_2 - \psi_1 + \psi_2 \right)} .$$ Here $K$, $r$ and $\alpha$ are parameters and $\phi_1, \phi_2, \psi_1, \psi_2, \chi_1, \chi_2$ are variables. A quite usual approach to my further analysis would be to expand it into products of sines and cosines of variables and group terms with the same powers. For example, $K \cos{\left(\alpha + \chi_1 - \chi_2 - \phi_1 + \phi_2 \right)}$ expands to $$K \sin{\left(\alpha \right)} \sin{\left(\chi_1 \right)} \sin{\left(\chi_2 \right)} \sin{\left(\phi_1 \right)} \cos{\left(\phi_2 \right)} - K \sin{\left(\alpha \right)} \sin{\left(\chi_1 \right)} \sin{\left(\chi_2 \right)} \sin{\left(\phi_2 \right)} \cos{\left(\phi_1 \right)} - K \sin{\left(\alpha \right)} \sin{\left(\chi_1 \right)} \sin{\left(\phi_1 \right)} \sin{\left(\phi_2 \right)} \cos{\left(\chi_2 \right)} - K \sin{\left(\alpha \right)} \sin{\left(\chi_1 \right)} \cos{\left(\chi_2 \right)} \cos{\left(\phi_1 \right)} \cos{\left(\phi_2 \right)} + K \sin{\left(\alpha \right)} \sin{\left(\chi_2 \right)} \sin{\left(\phi_1 \right)} \sin{\left(\phi_2 \right)} \cos{\left(\chi_1 \right)} + K \sin{\left(\alpha \right)} \sin{\left(\chi_2 \right)} \cos{\left(\chi_1 \right)} \cos{\left(\phi_1 \right)} \cos{\left(\phi_2 \right)} + K \sin{\left(\alpha \right)} \sin{\left(\phi_1 \right)} \cos{\left(\chi_1 \right)} \cos{\left(\chi_2 \right)} \cos{\left(\phi_2 \right)} - K \sin{\left(\alpha \right)} \sin{\left(\phi_2 \right)} \cos{\left(\chi_1 \right)} \cos{\left(\chi_2 \right)} \cos{\left(\phi_1 \right)} + K \sin{\left(\chi_1 \right)} \sin{\left(\chi_2 \right)} \sin{\left(\phi_1 \right)} \sin{\left(\phi_2 \right)} \cos{\left(\alpha \right)} + K \sin{\left(\chi_1 \right)} \sin{\left(\chi_2 \right)} \cos{\left(\alpha \right)} \cos{\left(\phi_1 \right)} \cos{\left(\phi_2 \right)} + K \sin{\left(\chi_1 \right)} \sin{\left(\phi_1 \right)} \cos{\left(\alpha \right)} \cos{\left(\chi_2 \right)} \cos{\left(\phi_2 \right)} - K \sin{\left(\chi_1 \right)} \sin{\left(\phi_2 \right)} \cos{\left(\alpha \right)} \cos{\left(\chi_2 \right)} \cos{\left(\phi_1 \right)} - K \sin{\left(\chi_2 \right)} \sin{\left(\phi_1 \right)} \cos{\left(\alpha \right)} \cos{\left(\chi_1 \right)} \cos{\left(\phi_2 \right)} + K \sin{\left(\chi_2 \right)} \sin{\left(\phi_2 \right)} \cos{\left(\alpha \right)} \cos{\left(\chi_1 \right)} \cos{\left(\phi_1 \right)} + K \sin{\left(\phi_1 \right)} \sin{\left(\phi_2 \right)} \cos{\left(\alpha \right)} \cos{\left(\chi_1 \right)} \cos{\left(\chi_2 \right)} + K \cos{\left(\alpha \right)} \cos{\left(\chi_1 \right)} \cos{\left(\chi_2 \right)} \cos{\left(\phi_1 \right)} \cos{\left(\phi_2 \right)}.$$
This is a simple example, however it already shows that coefficients of these polynomials are not just integers: they are also symbolic expressions, like $K \sin \alpha$. For example, if expressions are more complex, simple expansion won't help, I have to group up terms for further analysis.
Before I was using Python's SymPy to do expansions and term rewriting. However, I haven't found a way to collect terms as I want. For example, in a simplest expression
$$ A \cos{\left(\alpha \right)} + B \sin{\left(\alpha \right)} \cos{\left(\alpha \right)} + C \cos{\left(\alpha \right)} $$
I expect terms to be gathered like this
$$ (A+C) \cos{\left(\alpha \right)} + B \sin{\left(\alpha \right)} \cos{\left(\alpha \right)} ,$$
but using sympy.collect(expr, [sympy.cos(alpha), sympy.sin(alpha)])
or sympy.collect(expr, [sympy.cos(alpha), sympy.sin(alpha)], sympy.factor)
leads to
$$ \left(A + B \sin{\left(\alpha \right)} + C\right) \cos{\left(\alpha \right)},$$
which is not desirable. Exactly the same happens to regular polynomials too: collecting terms is greedy. The trigonometric problem could be converted to a problem of ordinary multivariate polynomials by substituting $\cos \theta = \frac{1-T^2}{1+T^2}$ and $\sin \theta = \frac{2T}{1+T^2}$, so I am okay with software that can group polynomials in the way that I need.
I would be glad to hear what are the alternatives to SymPy or how that could be done in SymPy if you've encountered this problem. Thanks in advance!
P.S. Special thanks if you know an open-source solution for this or can point to a tutorial where it is shown that a software can do this!
P.P.S. I also have big hopes for open-source software like Maxima, Reduce, Axiom. I'm currently skimming their tutorials, but haven't found if they can do what I'm asking.
In Wolfram Mathematica 12.0 writing:
you get:
since this is the standard factorization rule present in practically every CAS.
On the other hand, not all CAS have a function like:
through which to obtain:
also applicable to more complicated cases:
But perhaps the most interesting thing is to be able to introduce custom rules like:
Of course, it takes some time to get enough confidence.