Re: Strange Behaviour of ReplaceAll with Subscript (A bug or what?)
- To: mathgroup at smc.vnet.net
- Subject: [mg113339] Re: Strange Behaviour of ReplaceAll with Subscript (A bug or what?)
- From: Peter Breitfeld <phbrf at t-online.de>
- Date: Sun, 24 Oct 2010 06:05:40 -0400 (EDT)
- References: <i9ufjj$rjf$1@smc.vnet.net>
Thomas Dowling wrote: > Hello, > > I have come across some strange behaviour with ReplaceAll when creating > subscripted variables, > which I am not able to explain. > > The Problem: > > I have lists like the following: > > list1 = Partition[Range[6], {3}] > > Out[5]={{1,2,3},{4,5,6}} > > list2 = Partition[Range[9], {3}] > list3 = Partition[Range[12], {3}] > > I wish to convert to a list of indexed variables which I do as follows: > > replist={Subscript[a,x],Subscript[a,y],Subscript[a,z]}; > > list1/.{x_,y_,z_}-> replist > > This produces the expected output > > {{Subscript[a, 1],Subscript[a, 2],Subscript[a, 3]},{Subscript[a, > 4],Subscript[a, 5],Subscript[a, 6]}} > > The same applies to list3 > > list3/.{x_,y_,z_}-> replist > > However, **list2** produces the following > > list2/.{x_,y_,z_}-> replist > > {Subscript[a, {1,2,3}],Subscript[a, {4,5,6}],Subscript[a, {7,8,9}]} > > No such problem arises with Power or with Times (but the same problem > occurs with Superscript) > > list2/.{x_,y_,z_}-> {Power[a,x],Power[a,y],Power[a,z]} > > gives as output > {{a,a^2,a^3},{a^4,a^5,a^6},{a^7,a^8,a^9}} > > A Workaround: > > There is an easy solution using Replace and specifying the level > > Replace[list2, {x_,y_,z_}-> replist, {1}] > > giving the expected output: > > {{Subscript[a, 1],Subscript[a, 2],Subscript[a, 3]},{Subscript[a, > 4],Subscript[a, 5],Subscript[a, 6]},{Subscript[a, 7],Subscript[a, > 8],Subscript[a, 9]}} > > > The problem arises when the number of entries in the ReplaceAll list > (ie,{x_,y_,z_}) is the same as the number of sublists in the target list > > (The same problem occurs with the following) > > Partition[Range[16], {4}]/.{w_,x_,y_,z_}-> > {Subscript[a,w],Subscript[a,x],Subscript[a,y],Subscript[a,z]} > > Is this a bug or is such behaviour expected (and if so, what am I missing?) > > > Is thre a way of telling ReplaceAll what level to use (so that it ALWAYS > produces the > expected result)? > > The more general problem I have is that lists of this type are generated by > a program, and everything is well unless the generated list happens to have > 3 sublists. I can fix things using Replace, but should I have predicted > such a problem occurring? > > I would be interested in your thoughts, and in better workarounds (and in > better approaches). > > Thanks > > Thomas Dowling > > You can do the following This function makes indices to lists like {1,3,5} indList[v_?VectorQ]:=Subscript[a,#]&/@v Apply it to nested lists: indList[vl:{_?VectorQ ..}]:=Map[indList,vl,{1}] Then you get: In:= indList[list1] Out={{Subscript[a, 1], Subscript[a, 2], Subscript[a, 3]}, {Subscript[a,4],Subscript[a, 5], Subscript[a, 6]}} In:=indList[list2] Out={{Subscript[a, 1], Subscript[a, 2], Subscript[a, 3]}, {Subscript[a, 4], Subscript[a, 5], Subscript[a, 6]}, {Subscript[a, 7], Subscript[a, 8], Subscript[a, 9]}} -- _________________________________________________________________ Peter Breitfeld, Bad Saulgau, Germany -- http://www.pBreitfeld.de