Re: part assigned sequence behavior puzzling
- To: mathgroup at smc.vnet.net
- Subject: [mg105257] Re: part assigned sequence behavior puzzling
- From: "Sjoerd C. de Vries" <sjoerd.c.devries at gmail.com>
- Date: Wed, 25 Nov 2009 06:21:10 -0500 (EST)
- References: <heimgd$58a$1@smc.vnet.net>
I don't think you're wrong. I was just as puzzled with this behaviour as you. I'd say it's a bug. Just look at the following : Using extra temporary variables works as expected: In[283]:= tmp = Range[15]; tmp[[7]] = Sequence @@ Range[2]; tmp2 = tmp; tmp2[[7]] = Sequence @@ Range[2]; tmp3 = tmp2; tmp3[[7]] = Sequence @@ Range[2]; tmp3 Out[289]= {1, 2, 3, 4, 5, 6, 1, 2, 2, 2, 8, 9, 10, 11, 12, 13, 14, 15} Using different locations in tmp works as expected: In[295]:= tmp = Range[15]; tmp[[3]] = Sequence @@ Range[2]; tmp[[5]] = Sequence @@ Range[2]; tmp[[7]] = Sequence @@ Range[2]; tmp Out[299]= {1, 2, 1, 2, 4, 1, 2, 6, 1, 2, 8, 9, 10, 11, 12, 13, 14, 15} Temporarily storing tmp somewhere else works as expected: In[311]:= tmp = Range[15]; tmp[[7]] = Sequence @@ Range[2]; tmp2 = tmp; tmp = 0; tmp = tmp2; tmp[[7]] = Sequence @@ Range[2]; tmp2 = tmp; tmp = 0; tmp = tmp2; tmp[[7]] = Sequence @@ Range[2]; tmp Out[321]= {1, 2, 3, 4, 5, 6, 1, 2, 2, 2, 8, 9, 10, 11, 12, 13, 14, 15} My original hypothesis of what happens here was that Mathematica optimizes assignments and remembers what it last assigned to a location in an array. If it is the same, it does nothing. This would work for all elements except Sequence. This hypothesis is falsified by the following tests in which I changed the ranges: In[392]:= tmp = Range[15]; tmp[[7]] = Sequence @@ Range[2]; tmp[[7]] = Sequence @@ Range[3]; tmp[[7]] = Sequence @@ Range[4]; tmp Out[396]= {1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 8, 9, 10, 11, 12, 13, 14, 15} In[397]:= tmp = Range[15]; tmp[[7]] = Sequence @@ Range[4]; tmp[[7]] = Sequence @@ Range[3]; tmp[[7]] = Sequence @@ Range[2]; tmp Out[401]= {1, 2, 3, 4, 5, 6, 1, 2, 8, 9, 10, 11, 12, 13, 14, 15} As you can see, only the last range sticks. A new hypothesis would be to assume that Mathematica stores lists as a table of pointers to the list members and that a Sequence not causes the list to get extra pointers but that the Sequence is still invisible lurking there with a single pointer pointing to it. Copying the array may flatten out this structure, so that every member now gets his own pointer: In[402]:= tmp = Range[15]; tmp[[7]] = Sequence @@ Range[2]; tmp = tmp; tmp[[7]] = Sequence @@ Range[2]; tmp = tmp; tmp[[7]] = Sequence @@ Range[2]; tmp = tmp; tmp Out[406]= {1, 2, 3, 4, 5, 6, 1, 2, 2, 2, 8, 9, 10, 11, 12, 13, 14, 15} This seems to confirm the hypothesis. The problem seems to be in Set, as Part works OK: In[476]:= tmp = Range[15]; tmp[[7]] = Sequence @@ Range[2]; {tmp[[7]], tmp[[8]], tmp[[9]]} tmp[[8]] = Sequence @@ Range[2]; {tmp[[7]], tmp[[8]], tmp[[9]]} tmp[[9]] = Sequence @@ Range[2]; {tmp[[7]], tmp[[8]], tmp[[9]]} tmp Out[477]= {1, 2, 8} Out[478]= {1, 2, 1} Out[479]= {1, 2, 1} Out[480]= {1, 2, 3, 4, 5, 6, 1, 2, 1, 2, 1, 2, 10, 11, 12, 13, 14, 15} Could you report this to support at wolfram.com? Cheers -- Sjoerd On Nov 25, 9:31 am, mkr <mileskra... at gmail.com> wrote: > I am puzzled by the following behavior: > > tmp = Range[15] > tmp[[7]] = Sequence @@ Range[2]; > tmp[[7]] = Sequence @@ Range[2]; > tmp[[7]] = Sequence @@ Range[2]; > tmp > > yields > > {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15} > {1, 2, 3, 4, 5, 6, 1, 2, 8, 9, 10, 11, 12, 13, 14, 15} > > I would have expected the repeated assignment to have a repeated > effect, thus obtaining > > {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15} > {1, 2, 3, 4, 5, 6, 1, 2, 2, 2, 8, 9, 10, 11, 12, 13, 14, 15} > > Where/why am I wrong?