I have a multivariate polynomial x
. I get coefficients of various monomials using CoefficientRules
, which returns a list of replacement rules. I now want to apply a function, say ^2
on these coefficients.
My current method is as follows:
c = CoefficientRules[Expand[x]];
Table[{c[[i]][[1]] -> c[[i]][[2]]^2}, {i, 1, Length[c]}]
Is this the conventional way to do it? I find myself using Table
all the time for iterating, but is there a better way to do this, say using Thread
or Map
?
More generally, when is it advisable to use Table
? Most of my data is usually an instance of a (multidimensional) List
and I find myself constructing iterators over them.
I looked at these questions and suspect that my method is probably not optimal, but I can't say if the other methods are better than Table
.
Thanks!
Update:
In[64]:= Mean[First /@ Table[ClearSystemCache[];
AbsoluteTiming[Table[c[[i]][[1]] -> c[[i]][[2]]^2, {i,1,Length[c]}];], {692}]]
Out[64]= 0.000386799
In[63]:= Mean[First /@ Table[ClearSystemCache[]; AbsoluteTiming[c /. HoldPattern[a_ -> b_] :> a -> b^2;], {1067}]]
Out[63]= 0.000161383
In[61]:= Mean[First /@ Table[ClearSystemCache[]; AbsoluteTiming[MapAt[#1^2 &, c, {All, 2}];], {1336}]]
Out[61]= 0.000140475
In[58]:= f = # -> #2^2 & @@@ # &;
In[60]:= Mean[First /@ Table[ClearSystemCache[]; AbsoluteTiming[f[c];], {1627}]]
Out[60]= 0.000175706
MapAt
seems to have won it. The syntax seems natural enough.