I am trying to use ReplacePart in a function definition. The function
works using =, but not :=
newX= ReplacePart[x,{y[[1, 1]], y[[2, 1]]} -> x[[y[[1, 1]], y[[2,
1]]]] - y[[2, 2]]]
.. works
update[x, y] := ReplacePart[
x,
{y[[1, 1]], y[[2, 1]]} -> x[[y[[1, 1]], y[[2, 1]]]] - y[[2, 2]]
]
.. SetDelayed::write: Tag List in <snip> is Protected.
x and y are just integer lists. The problem seems to be Mathematica
trying to replace the head (List) of the rule in ReplacePart... even
though this is just correct syntax for replace part.
I tried using RuleDelayed but no change. Many functions use rules in
their syntax so this must be a general issue I haven't come across
yet.
cheers
B
The two arguments in the definition of update are not patterns:
update[x, y] := ...
Try: update[x_, y_] := ...
update[x_, y_] := ReplacePart[
x,
{y[[1, 1]], y[[2, 1]]} -> x[[y[[1, 1]], y[[2, 1]]]] - y[[2, 2]]
]
.. SetDelayed::write: Tag List in <snip> is Protected.
Has no-one else encountered this issue?
Could you provide a x, y pair that reproduce this error?
I would avoid using ReplacePart. If I understand your example
correctly...
y is a ragged array: {{row}, {col0, col1}}
And you're performing the following operation: x[[row, col0]] - x[[row, col1]]
update2[x_, {{row_}, {col0_, col1_}}] := Module[{temp = x},
temp[[row, col0]] -= temp[[row, col1]];
temp
];
x={{7, 9, 0, 0, 10}, {4, 0, 3, 10, 9}, {4, 9, 0, 5, 0}, {0, 2, 1, 8,
5}, {2, 5, 3, 4, 4}, {1, 0, 4, 0, 0}, {1, 10, 6, 8, 9}}
y={{4, 5}, {3, 1}, {2, 6}}
> I would avoid using ReplacePart. If I understand your example
> correctly...
>
> y is a ragged array: {{row}, {col0, col1}}
Think of x as a lists of quantities and y as rules for adjusting them.
So in this example of y, decrement x[[4,3]] by 2, increment x[[5,3]]
by 2, increment x[[4,1]] by 6, decrement x[[5,1]] by 6,
But the point is that the operation works when not delayed. It seems
like the way command is being parsed is the problem as Mathematica is
thinking that the ReplacePart syntax is actual an operation on one of
the arguments. I am hoping there is a way to force the correct
interpretation.