Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a large homogenous multivariate polynomial in, say, 5 variables $a,b,c,d,e$. As an example take the polynomial $$a^4+2abcd+a^2 b^2+e^4+cde^2.$$ Now I would like to replace $k$-th power of any variable by $c_k$, so the above example would become $$c_4+2c_1^4+c_2^2+c_4+c_1^2c_2.$$ At the moment I am using the CoefficientRules command which gives in the above example

coef={{4, 0, 0, 0, 0} -> 1, {2, 2, 0, 0, 0} -> 1, {1, 1, 1, 1, 0} -> 2, 
      {0, 0, 1, 1, 2} -> 1, {0, 0, 0, 0, 4} -> 1}.

Then I sum over the rules in this list by

Sum[coef[[All, 2]][[k]]*
  Product[c[coef[[All, 1]][[k, j]]], {j, 1, Length[coef[[All, 1]][[k]]]}], 
    {k, 1, Length[coef]}]

which is very inefficient. Any suggestions?

share|improve this question

3 Answers 3

up vote 2 down vote accepted
f1 = Module[{s}, (# /. _^p_ :> Subscript[s, p]) /. 
                                  Thread[Variables[#] -> Subscript[s, 1]] /. s -> #2] &

poly = a^4 + 2 a b c d + a^2 b^2 + e^4 + c d e^2;
f1[poly, c]

$2 c_1^4+c_2 c_1^2+c_2^2+2 c_4$

share|improve this answer

I'm not sure whether this works for your real case, but your example can be processed with a simple replacement:

poly = a^4 + 2 a b c d + a^2 b^2 + e^4 + c d e^2;

With[{varsQ = MemberQ[Variables[poly], #] &},
 poly /. {Power[a_Symbol?varsQ, k_Integer] :> ctmp[k]} 
      /. a_Symbol?varsQ :> ctmp[1] 
      /. ctmp[i_Integer] :> Subscript[c, i]
 ]

gives $$2 c_1^4+c_2 c_1^2+c_2^2+2 c_4$$

share|improve this answer
poly = a^4 + 2 a b c d + a^2 b^2 + e^4 + c d e^2;

coeffs = CoefficientRules[poly]

{{4, 0, 0, 0, 0} -> 1, {2, 2, 0, 0, 0} -> 1, {1, 1, 1, 1, 0} -> 2, {0, 0, 1, 1, 2} -> 1, {0, 0, 0, 0, 4} -> 1}

coeffsList = {Apply[
     Times, #1 /. {n_ /; n > 0 :> Subscript[c, n], 0 -> 1}], #2} & @@@ coeffs

$$ \left( \begin{array}{cc} c_4 & 1 \\ c_2^2 & 1 \\ c_1^4 & 2 \\ c_1^2 c_2 & 1 \\ c_4 & 1 \\ \end{array} \right) $$

Total[Times @@@ coeffsList]

$$ 2 c_1^4+c_2 c_1^2+c_2^2+2 c_4 $$

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.