# Replacing a subset of variables in a polynomial [closed]

Let's say I have a polynomial in n variables, e.g. x^3 + xyz + z^2, where n=3 in this case. How can I replace all of the variables in the equation with a certain number?

I tried things like

eq /. Variables[eq] -> ConstantArray[5,Length[Variables[eq]]]


but for some reason that didn't work.

I figure that you somehow need to create a list of rules such as:

{ x -> 5, y -> 5, z -> 5 }


but I'm not sure how to do that.

Bonus question: how could I replace just some subset of the variables? E.g. I give the input {1,3} and it would replace x and z by 5, but not y.

## closed as off-topic by Daniel Lichtblau, m_goldberg, Henrik Schumacher, MarcoB, José Antonio Díaz NavasFeb 16 at 11:37

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Daniel Lichtblau, m_goldberg, Henrik Schumacher, MarcoB, José Antonio Díaz Navas
If this question can be reworded to fit the rules in the help center, please edit the question.

• You may be looking for Thread[Variables[eq] -> ConstantArray[...]] – MarcoB Feb 14 at 4:14

Try

eq=x^3 + x*y*z + z^2;
rules=Map[#->5&,Variables[eq]];
eq/.rules

(*which returns 275*)


To answer your bonus question I would need to know how you decided it was x and z to replace, but not y.

In continuation to Bills answer, If selective replacement is required, store all the variables first and select the variables based on the input and make the rules.

For example, may not be efficient,

eq=x^3 + x*y*z + z^2;
var=Variables[eq];
rules=Table[var[[i]]->5,{i,{1,3}}];
eq/.rules