Maxima: Trigonometric simplification.




I have an expression which is a sum of N terms consisting of trigonometric 
expressions, eg: 

x1*cos(q1)*cos(q2) + x1*sin(q1)*sin(q2) + ...........

where x1 can itself be a product of expressions as described above.

I want to write a program using Maxima functions that examines all possible 
combinations of pairs of terms out of N terms, performs the factorization, 
and then simplifies using trigonometric identities, eg: 

x1*(cos(q1)*cos(q2) + sin(q1)*sin(q2)) -> x1*cos(q1-q2)

After  performing the possible simplifications (as above) to all combinations 
of pairs of terms, the program will select those pairs for which the 
stringlength of the simplified symbolic expression is the shortest, in order 
to form the simplified expression of the sum.

It seems useful if the program could somehow apply the above simplification 
procedure  recursively to take into account really complicated (nested) 
expressions.

I suspect that there might be an effective way to do the above using built-in 
Maxima functions, maybe map/fullmap (??), but its not exactly clear how to do 
this.

Any assistance would be much appreciated.

TIA

C. Frangos.