Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #5168

Re: Bug with Sequence and Assignment by Part...

From David Bailey <dave@removedbailey.co.uk>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Bug with Sequence and Assignment by Part...
Date 2011-09-23 07:43 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <j5hdal$7gh$1@smc.vnet.net> (permalink)
References <j5f6ai$q8a$1@smc.vnet.net>

Show all headers | View raw


On 22/09/2011 12:31, BernieTheJet wrote:
> If one patches a list of items into a list using Sequence[], it
> appears that Mathemaitca doesn't properly update the data structure:
>
> temp = Range[3];
> temp[[2]] = Sequence @@ {a, a, a};
> temp
>
> as expected :
>      ->  {1, a, a, a, 3}
>
> FullForm[temp]
>    also looks right:
>     ->  List[1,a,a,a,3]
>
> As does:
> Length[temp]
>     ->  5
>
> Ok, so far all is fine.  But subsequent additions to the same place do
> not increase the length of temp, as one might expect:
> temp[[2]] = Sequence @@ {b, b, b};
> temp
> FullForm[temp]
>
>         ->  {1, b, b, b, 3}
> and  ->  List[1,b,b,b,3]
>
> And, despite the Length of temp being '5', in fact there is no way to
> put anything into position '5' of temp:
>
> Length[temp]
>     ->  5
> temp[[5]] = Sequence @@ {c, c, c}
>     ->  Set::partw: Part 5 of {1,Sequence[b,b,b],3} does not exist.>>
>
> Here we finally see what is going on, somewhere temp is being stored
> as {1,Sequence[b,b,b],3}, but all the functions that go to request
> info from it first perform the evaluation of Sequence hence returning
> the expected results.
>
> One can force Mathematica to sort things out with the otherwise meaningless:
> temp=temp
>
> Surely I am not the first to encounter this, and surely it constitutes
> at least one bug, (and surely it is shouldn't be in Mathematica 8!).  I mean,
> it seems to me that FullForm and Length should also detect the
> presence of Sequence and act differently than they do here.
>
> Bernard
>
Thanks for that really fascinating example!

I must say, when I first encountered Sequence as I learned Mathematica, 
I was a bit stunned because it didn't seem to fit the style of the rest 
of the Mathematica language. I tend to feel that if Sequence does what 
you want, use it, but it will never be a completely 'normal' function!

In your example, my guess is that there is some sort of efficiency issue 
here. If updating temp[[2]] could, in effect, change the rest of the 
temp array then the performance of updates of vectors might suffer.

Overall, I am glad Mathematica does not hide all its sneaky functions!

David Bailey
http://www.dbaileyconsultancy.co.uk

Back to comp.soft-sys.math.mathematica | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Bug with Sequence and Assignment by Part... BernieTheJet <berniethejet@gmail.com> - 2011-09-22 11:31 +0000
  Re: Bug with Sequence and Assignment by Part... "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> - 2011-09-23 07:48 +0000
  Re: Bug with Sequence and Assignment by Part... David Bailey <dave@removedbailey.co.uk> - 2011-09-23 07:43 +0000
  Re: Bug with Sequence and Assignment by Part... BernieTheJet <berniethejet@gmail.com> - 2011-09-25 09:42 +0000
  Re: Bug with Sequence and Assignment by Part... BernieTheJet <berniethejet@gmail.com> - 2011-09-26 08:15 +0000
  Re: Bug with Sequence and Assignment by Part... DrMajorBob <btreat1@austin.rr.com> - 2011-09-26 08:23 +0000
    Re: Bug with Sequence and Assignment by Part... Richard Fateman <fateman@cs.berkeley.edu> - 2011-09-27 00:10 +0000

csiph-web