# Errors while using replacement rules

I am trying to evaluate the sum below by using replacement rule but it doesn't work. I got some errors saying "...is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing".

Could anyone explain why the error happened and how to fix it?

sol = Solve[{a + b + c == 3, a^2 + b^2 + c^2 == 29, a*b*c == 11}, {a, b, c}];
sum = a^5 /. sol[[1,1]] + b^5 /. sol[[1,2]] + c^5 /. sol[[1,3]]


why the error happened

Plus has higher precedence than ReplaceAll(see: Operator Input Forms >> Operator Precedence), so addition is performed before replacement.

{#, Precedence @ #} & /@ {Plus, Rule, ReplaceAll}


{{Plus, 310.}, {Rule, 120.}, {ReplaceAll, 110.}}

A simpler example that generates the same error is

a^5 /. sol[] + 5 how to fix it

You can parenthesize the terms to make sure that ReplaceAll is performed before Plus:

sum = (a^5 /. sol[[1, 1]]) + (b^5 /. sol[[1, 2]]) + (c^5 /. sol[[1, 3]]);
N[sum]


4138. + 9.99201*10^-16 I

Alternatively, you can use the following which avoids precedence issues:

sum2 = Total[{a, b, c}^5] /. sol[];
N[sum2]


4138. + 9.99201*10^-16 I

• Thanks. Why when I try a/.a->2 +5 there is no problem at all? Mar 16, 2018 at 21:28
• @anhnha, a /. a->2 +5 the same as a/. a-> (2 +5) and gives, as expected 7. The case in your question is more like rule = a -> 2; a /. rule + 5 (or, a /. (a->2) +5), and this form gives an error.
– kglr
Mar 16, 2018 at 21:39
• Ah, got it. Thanks. Mar 16, 2018 at 21:43