Re: Possible bug in Table and/or lists (Mathematica v.7)
- To: mathgroup at smc.vnet.net
- Subject: [mg108073] Re: [mg108053] Possible bug in Table and/or lists (Mathematica v.7)
- From: Leonid Shifrin <lshifr at gmail.com>
- Date: Mon, 8 Mar 2010 06:09:45 -0500 (EST)
- References: <201003070905.EAA01280@smc.vnet.net>
Hi Vijay, no, this is not a bug. First of all, in all your examples you use one-level indexing in assignments to a two-level structure, so you should always expect changes to entire rows (sublists) of your nested list. In the case when the value list has the same length as the target list (several rows of your matrix), the assignment is internally overloaded to be element-by-element, therefore you assign single 1-s to elements that were holding entire rows - thus your first result. The following will illustrate this effect in a simpler setting: In[1]:= Clear[list, a, b, c, d, e, f]; list = {{a, b}, {c, d}, {e, f}} Out[2]= {{a, b}, {c, d}, {e, f}} In[3]:= list[[1 ;; 2]] Out[3]= {{a, b}, {c, d}} In[4]:= list[[1 ;; 2]] = {first, second}; In[5]:= list Out[5]= {first, second, {e, f}} Here is what you should do to get what you probably were expecting: In[37]:= t = Table[1, {2*dim}, {2*dim}]; t[[-dim ;;]] = Table[1, {dim}, {dim}]; In[39]:= t Out[39]= {{1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1}, {1, 1, 1}, {1, 1, 1}, {1, 1, 1}} In the second case, the lengths are different, and when you have an assignment like yourlist[[i;;j]]=elem, and <elem> is not a list of length j-i+1, then all elements from i to j in <yourlist> are set to <elem> (this is another lesser-known property of Part-based assignments). Try this, for instance: In[40]:= test = Range[10] Out[40]= {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} In[41]:= test[[3 ;; 6]] = {1, 2} Out[41]= {1, 2} In[42]:= test Out[42]= {1, 2, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 7, 8, 9, 10} In your second example, all rows affected by your assignment are assigned a list of 4 elements (1-s) - thus your second result. This behavior may seem unusual at first, but in fact it is very useful at times, and has to do with the extended functionality of Part operating on regular structures like (sub)matrices. To find more about these issues, you should have a closer look at the documentation for the Part command, particularly its extended functionality. Regards, Leonid On Sun, Mar 7, 2010 at 1:05 AM, Vijay Kaul <vijaykaul at gmail.com> wrote: > I found this potential bug in Mathematica 7, running on Win XP and OS > X and it's easily reproducible. > > If I try the following: > > dim = 3; > t = Table[1, {2*dim}, {2*dim}]; > t // Grid > t[[-dim ;;]] = Table[1, {dim}]; > t // Grid > > I get unexpected results: the last three rows, rather than being each > a three element list, are, instead, single "1"s. On the other hand > > dim = 3; > t = Table[1, {2*dim}, {2*dim}]; > t // Grid > t[[-dim ;;]] = Table[1, {dim + 1}]; > t // Grid > > works as expected. > > In general, dimensions where the shorter lists are *not* half the > larger dimension seem to work just fine. I can also reproduce the > problem by creating a list of length 2N and then trying to fill half > of it with lists of length 2N and half with lists of length N. > > Maybe I'm missing something, but it baffled me and some others who > looked at it. > > Also, I'm not sure where or how to find a Mathematica bug-list, and I > didn't see a FAQ around here, but I'm relatively unexposed to usenet, > newsgroups, and Google groups, so I may have missed some obvious > stuff. > > So, with thanks and/or apologies in advance, > -Vijay > >
- References:
- Possible bug in Table and/or lists (Mathematica v.7)
- From: Vijay Kaul <vijaykaul@gmail.com>
- Possible bug in Table and/or lists (Mathematica v.7)