Here is a minimal example:
s = 1 /(a + b[3]) + 1 /(2 a + b[3]);
s[[1]] = Sum[s[[1]] /. b[3] -> k, {k, 3}]
s[[4]]
s[[4]] = Sum[s[[4]] /. b[3] -> k, {k, 3}]
Output:
Out[143]= 1/(1 + a) + 1/(2 + a) + 1/(3 + a)
Out[144]= 1/(2 a + b[3])
Set::partw: Part 4 of (1/(1+a)+1/(2+a)+1/(3+a))+1/(2 a+b[3]) does not exist. >>
Out[145]= 1/(1 + 2 a) + 1/(2 + 2 a) + 1/(3 + 2 a)
I could only make the error occur when using Set
. As the above code shows, both s[[4]]
and Sum[s[[4]] /. b[3] -> k, {k, 3}]
are well defined.
I have been struggling with similar errors for days, can someone please explain exactly what is happening behind the scenes?
ReplacePart[s, 4 -> Sum[s[[4]] /. b[3] -> k, {k, 3}]]
$\endgroup$s=s
before evaluatings[[4]] = Sum[s[[4]] /. b[3] -> k, {k, 3}]
, the problem disappears $\endgroup$Part
. For ex:ReplaceParts[s,{}-> x]
returnss
unaltered. $\endgroup$s=s
does work, but why!? Thanks $\endgroup$ReplacePart
is a function, so you need to assign the result, as ins=ReplacePart...
. I think your issue is just an interesting gotcha -part ([[]])
works on fullform expressions, ands
vss=s
(which I noted when trying your code) gives the exact same fullform result. Beats me, perhaps L.S. or M.W. or other wizards will chime in. In any case, IMHOReplacePart
is the way to go working with expressions. $\endgroup$