       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:

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

First I clear all global variables.

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

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

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

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

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

Apply is used to change the head of an expression.

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

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

In:=
g@@f[a1,a2,a3,a4]
Out=
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:=
Transpose[{{a1,a2,a3},{b1,b2,b3}}]
Out=
{{a1,b1},{a2,b2},{a3,b3}}

In:=
Out=
{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:=
g[f[a1,b1],f[a2,b2],f[a3,b3]]

The next line demonstrates that Thread is very flexible.

In:=
Out=
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.
___________________

In:=
{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:=
{f[{a1,a2,a3,a4}],f[{b1,b2,b3,b4}],f[{c1,c2,c3,c4}]}

In:=
{{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'.