Re: Strange Behaviour of ReplaceAll with Subscript (A bug or what?)

*To*: mathgroup at smc.vnet.net*Subject*: [mg113343] Re: Strange Behaviour of ReplaceAll with Subscript (A bug or what?)*From*: Andrea <btlgs2000 at gmail.com>*Date*: Sun, 24 Oct 2010 06:06:27 -0400 (EDT)*References*: <i9ufjj$rjf$1@smc.vnet.net>

On Oct 23, 1:06 pm, Thomas Dowling <thomasgdowl... at gmail.com> 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 ha= ve > 3 sublists. I can fix things using Replace, but should I have predicte= d > such a problem occurring? > > I would be interested in your thoughts, and in better workarounds (and in > better approaches). > > Thanks > > Thomas Dowling Hi Thomas, the behaviour of ReplaceAll is as expected. ReplaceAll try to match the pattern at any level (to the whole expression too) starting from the highest to the lowest. In the case of list2 the first match is with the whole expression cause actually it is a list of three elements (list themselves) as you specified in your pattern {x_,y_,z_}. You have to specify in the pattern that your elements are numbers. E.g. you can write list2 /. x : {_?NumberQ ..} :> Map[Subscript[a, #] &, x] so that the substitution apply only to list of at least one number