# Collecting terms in an expression [duplicate]

I have an expression:

(C1*R*(10*k12*k21 - 20*k11*k22 - k21^2*Cos[4*t] + 10*k11^2 + 10*k12^2 + 3*k21^2 + 10*k22^2 -
4*k12*k21*Cos[2*t] - 2*k12*k21*Cos[4*t] + 4*k11*k21*Sin[2*t] + 2*k11*k21*Sin[4*t] -
4*k21*k22*Sin[2*t] - 2*k21*k22*Sin[4*t]))/32


and I would like to factor this in terms of squares of the ks. (e.g., (k12^2 - k21^2), (k22 - k11)^2, (k12 + k21)^2, etc.)

If the trigonometric identities can be simplified, then that's perfect. Any help would be appreciated.

-

## marked as duplicate by Michael E2, Szabolcs, gpap, Artes, R. M.♦Mar 11 '14 at 0:53

From Daniel Lichtblau's answer, Working with symmetric polynomials, it's easy to do as much as is specified. I interpret "factor" to mean "write in terms of," but I'm not sure exactly what quadratic terms are implied.

subs = {q1 -> (k12^2 - k21^2), q2 -> (k22 - k11)^2, q3 -> (k12 + k21)^2};
polys = Subtract @@@ subs;
gb = GroebnerBasis[polys, {x, y}];

expr = (C1*
R*(10*k12*k21 - 20*k11*k22 - k21^2*Cos[4*t] + 10*k11^2 + 10*k12^2 +
3*k21^2 + 10*k22^2 - 4*k12*k21*Cos[2*t] - 2*k12*k21*Cos[4*t] +
4*k11*k21*Sin[2*t] + 2*k11*k21*Sin[4*t] - 4*k21*k22*Sin[2*t] -
2*k21*k22*Sin[4*t]))/32;
PolynomialReduce[expr, gb, {k11, k12, k21, k22}][[2]]


If you want Mathematica to find some way to express expr in terms of quadratic expressions in the ks, create a list qs of the possible forms you would consider acceptable and create variables q[i], i = 1, ... up to the number of forms in qs. Then the above method will find a solution, but it won't be unique.

ks = {k11, k12, k21, k22};
qs = Flatten@
MapThread[{#1^2 - #2^2, #1^2 + #2^2, (#1 - #2)^2, (#1 + #2)^2} &,
Transpose@Subsets[ks, {2}]];
subs = Thread[Array[q, Length@qs] -> qs];
polys = Subtract @@@ subs;
gb = GroebnerBasis[polys, {x, y}];

expr = (C1*
R*(10*k12*k21 - 20*k11*k22 - k21^2*Cos[4*t] + 10*k11^2 +
10*k12^2 + 3*k21^2 + 10*k22^2 - 4*k12*k21*Cos[2*t] -
2*k12*k21*Cos[4*t] + 4*k11*k21*Sin[2*t] + 2*k11*k21*Sin[4*t] -
4*k21*k22*Sin[2*t] - 2*k21*k22*Sin[4*t]))/32;
quadReduced = PolynomialReduce[expr, gb, {k11, k12, k21, k22}~Join~Array[q, Length@qs]][[2]]


Use

quadReduce /. subs


to get expr back.

-
thanks for your answer...it helps a lot..i was wondering if it may be possible to write the above expression in terms of squares of the k's without actually myself defining them (I have some other onerous expressions and I wanted to simplify them in this fashion -- i don't really know the end product of these). Any other advice would be much appreciated. thanks again – Stoc Mar 9 '14 at 2:08
Which squares of the k's? I don't see the pattern -- the three are all different. – Michael E2 Mar 9 '14 at 2:35
oh..i meant that the expressions themselves (i.e., those in subs) should be found automatically without me having to actually pre-define them -- for any expression...is this possible? it was just that i kind of knew the answer beforehand for this expression and so I put them in the question formulation, however, I have some other tedious expressions for which I do not know the end result...please let me know if I am not clear enough..thanks again – Stoc Mar 9 '14 at 15:34
@Stoc See if the second way does what you're after. You might want something in between, but I don't know how to do that automatically. If you have a basis in mind, you just have to be able to program it (for qs). – Michael E2 Mar 9 '14 at 17:06