Re: Nested Lists, Definition and Evaluation.
- To: mathgroup at smc.vnet.net
- Subject: [mg59756] Re: [mg59729] Nested Lists, Definition and Evaluation.
- From: "benshimo at bgumail.bgu.ac.il" <bsyehuda at gmail.com>
- Date: Sat, 20 Aug 2005 03:13:45 -0400 (EDT)
- References: <200508190831.EAA27442@smc.vnet.net>
- Reply-to: benshimo at bgumail.bgu.ac.il
- Sender: owner-wri-mathgroup at wolfram.com
You do not list based examples to explore this
try (from a fresh kernel)
b=c;
c=8;
b
?b
and you will see that since c is not defined before the assignment is
similar to a delayed one, that is, b is set to c and to the value of c
if instead you change the order (again, clear variables or start a fresh
kernel) then
c=8;
b=c;
b
?b
and you will notice the difference here
if you type b:=c in th second case, then it will be equivalent to the first
case.
Similar things happen with your lists.
In addition
list[[i,j]][[m,n]] is identical in the returned value to list[[i,j,m,n]]
but for setting new values you need the second option (a single pair of
double square brackets)
You can check this simple example
list=Array[1&,{2,2,2,2}];
list[[2,2,2,2,]]=33;
?list
list[[2,2]][[2,2]]=44
and you will get an error message.
If you type
Trace[list[[2,2,2,2,]]]
and compare it to
Trace[list[[2,2]][[2,2]]]
you will notice that the difference is that in the first case the position
of the element is defined once, and this can be used for assignments.
In the second case l[[2,2]] is evaluated first, returns the corresponding
sublist (and NOT a VARIABLE) so it cannot be assigned a value.
I hope this helps
regards
yehuda
On 8/19/05, Alexander <beginning.physst at mail.ru> wrote:
>
> Hello!
>
> Here is simple example of defining list:
>
> b = Table[Symbol["a" <> ToString[i] <> ToString[j]], {i, 2}, {j, 2}];
> a11 = IdentityMatrix[2];
> b[[1,1]][[1,1]]
>
> Output will be number 1.
>
> Then we type another command:
> b[[1,1]][[1,1]]=2
>
> wich cause error message.
>
> If we type ?b the structure of b can be seen:
> Global`b
> b = {{a11, a12}, {a21, a22}}
>
> And I wonder, how to change elements of aij from b? It's interesing
> question about how mathematica think things and how evaluate them.
>
> So, the questions are:
> 1) Why code above produce error message?
> 2) What difference between b[[1,1]][[1,1]] and
> b[[1,1,1,1]] notation?
>
> Here is an example concerning the second question:
> This code doesn't produce an error:
>
> a11 = IdentityMatrix[2];
> b = Table[Symbol["a" <> ToString[
> i] <> ToString[j]], {i, 2}, {j, 2}];
> b[[1, 1, 1, 1]] = 3
>
> but this does:
>
> a11 = IdentityMatrix[2];
> b = Table[Symbol["a" <> ToString[
> i] <> ToString[j]], {i, 2}, {j, 2}];
> b[[1, 1]][[1, 1]] = 3
>
> Why?
>
> Sorry for my obscure speech, and thanks for your answers!
>
>
- References:
- Nested Lists, Definition and Evaluation.
- From: "Alexander" <beginning.physst@mail.ru>
- Nested Lists, Definition and Evaluation.