Re: unbelievable ...(continued)
- To: mathgroup at smc.vnet.net
- Subject: [mg16632] Re: unbelievable ...(continued)
- From: "Allan Hayes" <hay at haystack.demon.co.uk>
- Date: Fri, 19 Mar 1999 12:53:53 -0500
- References: <7cnl3i$e6f@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
My earlier postinf on this did not show how to do what Antoine wanted.
Here are some ideas - we are touching on some basic features!
Lets start with
a = A[1,2];
We have
x[[1]]=y; where x is a symbol, and, by using x =.. (not x:=...), we have
stored x = expr, where expr has a part 1, changes this stored assignment to
x = (expr with part 1 changed to y) and outputs y. Examples:
a[[1]]=3
3
a
A[3,2]
The conditions are essential, for example
A[[1,2]][[1]]=4;
Set::"setps":
"\!\(A \\[LeftDoubleBracket] \(1, 2\) \\[RightDoubleBracket]\) in \
assignment of part is not a symbol."
a
A[3,2]
Now for a function:
set1[x,y], below, where x is a symbol for which by x =.. (not x:=...) we
have stored x = expr, and expr has a part 1, changes this stored assignment
to x = (expr with part 1 changed to y) and outputs y.
We have to give set1 the attribute HoldFirst so that it passes only the name
and avoids the problem just illustrated.
SetAttributes[set1, HoldFirst]
set1[x_,y_] := x[[1]]=y
set1[a,4]
4
a
A[4,2]
Two more functions:
set2[x,y] and set3[x,y] below, where the value of x is an expression with
part 1,outputs result of setting first entry in the x to y
set2[x_,y_]:= Block[{t=x},t[[1]]=y;t]
set2[A[1,2],5]
A[5,2]
set3[x_,y_]:= ReplacePart[x,y,1]
set3[A[1,2],6]
A[6,2]
These evaluations did not change the value of a:
a
A[4,2]
Nor will set2 and set3 change the value of a in the following (a first of
all evaluates to A[4,2], then the evaluation is as abve)
set2[a,5]
A[5,2]
a
A[4,2]
set3[a,5]
A[5,2]
a
A[4,2]
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565