Re:Modification to Thread or MapThread
- To: mathgroup at smc.vnet.net
- Subject: [mg26419] Re:[mg26380] Modification to Thread or MapThread
- From: "Ersek, Ted R" <ErsekTR at navair.navy.mil>
- Date: Wed, 20 Dec 2000 00:21:41 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
Chris Johnson wanted to make Thread work a certain way. Thread and many other programming constructs in Mathematica are very versatile. In all the examples below any other head could have been used instead of Plus. The first example is the simplest way of using thread. In[1]:= Thread[{x1,x2,x3}+{y1,y2,y3}] Out[1]= {x1+y1,x2+y2,x3+y3} Unless Thread is given another argument indicating otherwise it threads over lists. That explains the result of the next example. In[2]:= Thread[{x1,x2,x3,x4}+h[a,b]] Out[2] {x1+h[a,b], x2+h[a,b], x3+h[a,b], x4+h[a,b]} In the next example nothing at level 1 is a List so we have to tell the kernel to thread over (g). We do that by giving Thread (g) as a second argument. In[3]:= Thread[g[x1,x2,x3]+h[a,b], g] Out[3]= g[x1+h[a,b], x2+h[a,b], x3+h[a,b]] In the next case we thread over (g) but only over the first 3 occurrences of (g). In[4]:= Thread[g[w1,w2]+g[x1,x2]+g[y1,y2]+g[z1,z2], g, 3] Out[4]= g[w1+x1+y1+g[z1,z2], w2+x2+y2+g[z1,z2]] In the next example we thread over the last 3 occurrences of (g). In[5]:= Thread[g[w1,w2]+g[x1,x2]+g[y1,y2]+g[z1,z2], g, -3] Out[5]= g[x1+y1+z1+g[w1,w2], x2+y2+z2+g[w1,w2]] Next we thread over the first occurrence through to the 2nd from the last occurrence of (g). In[6]:= Thread[g[w1,w2]+g[x1,x2]+g[y1,y2]+g[z1,z2], g, {1,-2}] Out[6]= g[w1+x1+y1+g[z1,z2], w2+x2+y2+g[z1,z2]] ------- Download lots of other Mathematica tips, tricks from http://www.verbeia.com/mathematica/tips/Tricks.html -------------------- Regards, Ted Ersek