2
$\begingroup$

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]]
$\endgroup$
0

1 Answer 1

2
$\begingroup$

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[[1]] + 5 

enter image description here

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[[1]];
N[sum2]

4138. + 9.99201*10^-16 I

$\endgroup$
3
  • $\begingroup$ Thanks. Why when I try a/.a->2 +5 there is no problem at all? $\endgroup$
    – emnha
    Commented Mar 16, 2018 at 21:28
  • $\begingroup$ @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. $\endgroup$
    – kglr
    Commented Mar 16, 2018 at 21:39
  • $\begingroup$ Ah, got it. Thanks. $\endgroup$
    – emnha
    Commented Mar 16, 2018 at 21:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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