Re: Re: Re: Thread and MapThread
- To: mathgroup at smc.vnet.net
- Subject: [mg29903] Re: [mg29882] Re: [mg29870] Re: Thread and MapThread
- From: Andrzej Kozlowski <andrzej at tuins.ac.jp>
- Date: Wed, 18 Jul 2001 02:08:46 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
In general this is not a good idea. There is a reason why Thread evaluates its arguments and giving it the HoldFirst attribute will change its behaviour and make many constructions fail. Just compare these two (out of many possible) examples: Normal behaviour: In[1]:= Thread[Through[p[g,h][x]],g] Out[1]= g[p[x,h[x]]] After adding the HoldFirst attribute: SetAttributes[Thread, HoldFirst] In[3]:= Thread[Through[p[g,h][x]],g] Out[3]= p[g[x],h[x]] If you must change the attributes of Thread (or any built-in function) it is better to use the anonymous function Function[y, Thread[y], HoldFirst], which is equivalent to Thread with the HoldFirst attribute but does not affect the behaviour of Thread itself. -- Andrzej Kozlowski Toyama International University JAPAN http://platon.c.u-tokyo.ac.jp/andrzej/ http://sigma.tuins.ac.jp/~andrzej/ on 01.7.17 2:00 PM, Mianlai Zhou at lailai at nikhef.nl wrote: > There is another alternative to fulfill that: > > SetAttributes[Thread, HoldFirst]; > Thread[ ftest[{x1, x2, x3}, {y1, y2, y3}] ] > > Then it will give the answer you want: > > {{f1[x1], f2[y1]}, {f1[x2], f2[y2]}, {f1[x3], f2[y3]}} > > Enjoy it. > > Mianlai Zhou > Theory Group, NIKHEF > Amsterdam, The Netherlands > > On Sun, 15 Jul 2001, Allan Hayes wrote: > >> David, >> >> Here are the relevant evaluation steps of >> Thread[ftest[{x1, x2, x3}, {y1, y2, y3}]] >> >> Thread[ {f1[{x1, x2, x3}], f2[{y1, y2, y3}]}] >> (*ftest[...] evaluates before Thread acts*) >> >> {f1[{x1, x2, x3}], f2[{y1, y2, y3}]}] >> (*Thread does nothing since it has not, for example, been told to thread >> over f1}*) >> >> Compare >> >> Thread[ftest[{x1,x2,x3},{y1,y2,y3}],f1] >> >> f1[{{x1, x2, x3}, f2[{y1, y2, y3}]}] >> >> >> There are many ways of avoiding premature evaluation, here are a few >> >> Thread[Unevaluated[ftest[{x1,x2,x3},{y1,y2,y3}]]] (*caution:x1, ... are >> held*) >> >> {{f1[x1], f2[y1]}, {f1[x2], f2[y2]}, {f1[x3], f2[y3]}} >> >> >> Block[{ftest},Thread[ftest[{x1,x2,x3},{y1,y2,y3}]]] >> >> {{f1[x1], f2[y1]}, {f1[x2], f2[y2]}, {f1[x3], f2[y3]}} >> >> >> Evaluate[Thread[#[{x1,x2,x3},{y1,y2,y3}]]]&[ftest] >> >> {{f1[x1], f2[y1]}, {f1[x2], f2[y2]}, {f1[x3], f2[y3]}} >> >> -- >> Allan >> --------------------- >> Allan Hayes >> Mathematica Training and Consulting >> Leicester UK >> www.haystack.demon.co.uk >> hay at haystack.demon.co.uk >> Voice: +44 (0)116 271 4198 >> Fax: +44 (0)870 164 0565 >> > > >