Re: Question about thread
- Subject: [mg2643] Re: [mg2581] Question about thread
 - From: hay at haystack.demon.co.uk (Allan Hayes)
 - Date: Thu, 30 Nov 1995 21:02:50 -0500
 - Approved: usenet@wri.com
 - Distribution: local
 - Newsgroups: wri.mathgroup
 - Organization: Wolfram Research, Inc.
 
sherod at boussinesq.Colorado.EDU (Scott Herod)
in [mg2581] Question about thread
writes
"I have a question about the way Thread behaves.  From the  
description in the _Mathematica_ book, I would expect it to return  
lists.  For example " (attached)
Scott,
1.Thread is not restricted to Lists
   Thread[f[h[1,2],h[3,4]], h ]  (*use 2nd place*)
	h[f[1, 3], f[2, 4]]
2.We have some control where the threading takes place
   Thread[f[x,g[y,z],h[a1,b1],h[a2,b2]],h,3] (*use 2nd & 3rd places*)
      h[f[x, g[y, z], a1, h[a2, b2]], f[x, g[y, z], b1, h[a2, b2]]]	
3.The behaviour of your example:
	
   blat[p_, q_] := Module[{}, Plus @@ (Variables[p]^q)]
   Thread[blat[{a+c,b+d},x]]
	 x    x    x    x
	a  + b  + c  + d
is due to blat being evaluated before Thread acts.
Here are some ways that this can be prevented.
   Thread[Unevaluated[blat[{a+c,b+d},x]]]
        x    x   x    x
      {a  + c , b  + d }
   ReleaseHold[Thread[Hold[blat][{a+c,b+d},x]]]
        x    x   x    x
      {a  + c , b  + d }
   Block[{blat},Thread[blat[{a+c,b+d},x]]]
        x    x   x    x
      {a  + c , b  + d }
Where it is suitable the first way is usually quickest. The last  
one depends on Literal[blat] being a symbol]
Allan Hayes
hay at haystack.demon.co.uk
********
Begin forwarded message:
>From: sherod at boussinesq.Colorado.EDU (Scott Herod)
>Subject: [mg2581] Question about thread
I have a question about the way Thread behaves.  From the description
in the _Mathematica_ book, I would expect it to return lists.  For
example
Mathematica 2.2 for Solaris
Copyright 1988-93 Wolfram Research, Inc.
License valid through 28 Nov 1995.
 -- Open Look graphics initialized --
In[1]:= Thread[f[{a,b},x]]
Out[1]= {f[a, x], f[b, x]}
But that doesn't always seem to be the case.
In[2]:= blat[p_, q_] := Module[{}, Plus @@ (Variables[p]^q)]
In[3]:= blat[a+b,x]
         x    x
Out[3]= a  + b
In[4]:= Thread[blat[{a+c,b+d},x]]
         x    x    x    x
Out[4]= a  + b  + c  + d
Why isn't Out[4] the same as
In[5]:= Outer[blat, {a+c, b+d} , {x}] // Flatten
          x    x   x    x
Out[5]= {a  + c , b  + d }
Scott Herod
Applied Mathematics
University of Colorado, Boulder
sherod at colorado.edu