3

Why ReplaceAll does not work in this situation?

id = {{1, 0}, {0, 1}};
sx = {{0, 1}, {1, 0}};
sy = {{0, -I}, {I, 0}};
sz = {{1, 0}, {0, -1}};
s = {id, sx, sy, sz};
p[t_] := {p0[t], p1[t], p2[t], 
  p3[t]}; 

In[1]:= 
Sum[p[1][[i]]*s[[i]].sx.s[[i]], {i, 1, 4}] /. p[1] -> {1, 0, 0, 0}

Out[1]= {{0, 
  p0[1] + p1[1] - p2[1] - p3[1]}, {p0[1] + p1[1] - p2[1] - p3[1], 0}}

The output is right, only the replacement of the terms in vector p[t] does not take place.

xzczd
  • 65,995
  • 9
  • 163
  • 468
Agnieszka
  • 677
  • 4
  • 13
  • 1
    Strongly related: https://mathematica.stackexchange.com/a/128438/1871 There should be more. – xzczd Jul 19 '19 at 11:15

1 Answers1

7

Because

p[1] -> {1, 0, 0, 0}

evaluates to

{p0[1], p1[1], p2[1], p3[1]} -> {1, 0, 0, 0}

and the left side of this rule does not appear in your expression. What you want is,

MapThread[Rule, {p[1], {1, 0, 0, 0}}]

which evaluates to the list of rules

{p0[1] -> 1, p1[1] -> 0, p2[1] -> 0, p3[1] -> 0}

Using ReplaceAll with that list of rules gives the result,

{{0, 1}, {1, 0}}
Andrew Norton
  • 847
  • 4
  • 13