Hi, 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

0 |

11/7/2010 10:11:17 AM

On Nov 7, 2:11 am, niobe <ben.carb...@gmail.com> wrote: > Hi, > > 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_] := ...

0 |

11/8/2010 8:37:46 AM

Apologies, that was just a typo when copying to email and my function arguments are correct 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?

0 |

11/9/2010 8:54:09 AM

On Nov 9, 12:54 am, niobe <ben.carb...@gmail.com> wrote: > Apologies, that was just a typo when copying to email and my function > arguments are correct > > 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 ];

0 |

11/10/2010 11:27:45 AM

> Could you provide a x, y pair that reproduce this error? 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.

0 |

11/11/2010 11:11:39 AM