Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1999
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1999

[Date Index] [Thread Index] [Author Index]

Search the Archive

RE: Help to clarify 'Map', 'Apply', and 'Thread'.

  • To: mathgroup at smc.vnet.net
  • Subject: [mg15702] RE: [mg15626] Help to clarify 'Map', 'Apply', and 'Thread'.
  • From: "Ersek, Ted R" <ErsekTR at navair.navy.mil>
  • Date: Mon, 1 Feb 1999 14:54:19 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

Wen-Feng Shaw  wrote:

   The functions 'Map', 'Apply', 'Thread', and 'MapThread' are very 
useful. However, I am often confused by their functionalities. I often 
should stop to try if a certain expression is correct before coding it 
into my program. Could you help me to clarify them? Thanks a lot!
_________________________

Before I begin I have a question for others: The third argument of
Thread can be an integer (positive or negative) or a list of such
integers, but I don't what this might do for you.  If anyone knows
please explain.


First I clear all global variables.

In[1]:=
ClearAll["Global`*"];


Map is used to evaluate a function on each and every argument.

In[2]:=
Map[g,f[a1,a2,a3,a4]]
Out[2]=
f[g[a1],g[a2],g[a3],g[a4]]


The next line does the same thing using short hand notation.

In[3]:=
g/@f[a1,a2,a3,a4]
Out[3]=
f[g[a1],g[a2],g[a3],g[a4]]


Apply is used to change the head of an expression.

In[4]:=
Apply[g,f[a1,a2,a3,a4]]
Out[4]=
g[a1,a2,a3,a4]

The next line does the same thing using short hand notation.

In[5]:=
g@@f[a1,a2,a3,a4]
Out[5]=
g[a1,a2,a3,a4]

Note:
Map, Apply can take a level specification, but that's a complicated
subject and  could be discussed in another response.
___________________

You are familiar with the command to take the Transpose of a matrix. 
Right?

In[6]:=
Transpose[{{a1,a2,a3},{b1,b2,b3}}]
Out[6]=
{{a1,b1},{a2,b2},{a3,b3}}

Thread is analogous to Transpose.

In[7]:=
Thread[f[{a1,a2,a3},{b1,b2,b3}]]
Out[7]=
{f[a1,b1],f[a2,b2],f[a3,b3]}

The head at level 2 of the expression 'Thread' works on can be something
besides 'List', but you have to tell Thread what it is as a second
argument.

In[8]:=
Thread[f[g[a1,a2,a3],g[b1,b2,b3]],g] Out[8]=
g[f[a1,b1],f[a2,b2],f[a3,b3]]


The next line demonstrates that Thread is very flexible.

In[9]:=
Thread[f[x1,g[a1,a2,a3],x3],g]
Out[9]=
g[f[x1,a1,x3],f[x1,a2,x3],f[x1,a3,x3]]

Note:
As I said at the beginning the third argument of Thread can be an
integer (positive or negative) or a list of such integers, but I don't
know what this might do for you.
___________________

MapThread is like using 'Thread' followed by use of 'Map'.

In[10]:=
MapThread[f,{{a1,a2,a3,a4},{b1,b2,b3,b4},{c1,c2,c3,c4}}] Out[10]=
{f[a1,b1,c1],f[a2,b2,c2],f[a3,b3,c3],f[a4,b4,c4]}

The third argument given to MapThread tells it what level to use.  This
is a little hard to describe, but I will let you compare the results in
the next two examples and try to figure it out.  Also note the level
can be negative, but that's a little off the subject.  Negative "level"
could be addressed in another response.

In[11]:=
MapThread[f,{{{a1,a2,a3,a4},{b1,b2,b3,b4},{c1,c2,c3,c4}}}] Out[11]=
{f[{a1,a2,a3,a4}],f[{b1,b2,b3,b4}],f[{c1,c2,c3,c4}]}

In[12]:=
MapThread[f,{{{a1,a2,a3,a4},{b1,b2,b3,b4},{c1,c2,c3,c4}}},2] Out[12]=
{{f[a1],f[a2],f[a3],f[a4]},{f[b1],f[b2],f[b3],f[b4]},{f[c1],f[c2],f[c3],f[c4
]}}

Note:
You might also look at MapIndexed, MapAll, MapAt.  Using MapIndexed is a
little tricky and I could discuss that if anyone is interested.

Cheers,
Ted Ersek


  • Prev by Date: Re: Block vs. Module
  • Next by Date: Re: conditional pattern question
  • Previous by thread: Re: Block vs. Module
  • Next by thread: Re: Help to clarify 'Map', 'Apply', and 'Thread'.