# Unexpected result from Map [closed]

I'm using Mathematica 8.0. Is the following output a bug or do I miss something?

f[a_] = 2 a;
Map[f,1+x]


gives $2+2x$ as expected, but

Map[f,x]


results in the output $x$

-

## closed as off-topic by Daniel Lichtblau, Jens, rasher, m_goldberg, KubaJun 5 at 23:32

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Daniel Lichtblau, Jens, rasher, m_goldberg, Kuba
If this question can be reworded to fit the rules in the help center, please edit the question.

Please do not use the bugs tag when posting new questions. It will be added if the problem is confirmed to be a bug by the community. In the majority of the cases (like here), the problem is not due to a bug. –  Szabolcs Jun 5 at 14:53
In addition to the answers below you might want to get in the habit of evaluating AtomQ if in doubt about the structure of things. e.g. AtomQ[x] –  Mike Honeychurch Jun 5 at 22:30

Why don't you use?

f@x


and

f@(1 + x)


Also possible:

Map[f, x, {0}]


Further:

Map[f, 1 + x, {0}]


--> 2 (1 + x)

Map[f, 1 + x, {1}]


--> 2 + 2 x

-

When experimenting with Map (do check the examples under this link), it's better not to define the function you're mapping. If it's not defined, it won't evaluate and it's easier to see what's going on.

Map[f, 1+x] is Map[f, Plus[1,x]] with a different notation. So you get Plus[f[1], f[x]], i.e. f[1]+f[2].

Map[f, x] returns x because x is an atomic object without structure. It does not have an "inner part" where f could be inserted.

To clarify, what Map does is insert f at level 1 in the expression. If there's no level 1, it won't do anything.

Examples:

f /@    x    --->          x

f /@   {x}   --->     { f[ x ] }

f /@  {{x}}  --->    { f[ {x} ] }

f /@ {{{x}}} --->   { f[ {{x}} ] }


The first of these may seem slightly less intuitive. It's what happens in your case.

-
thanks, very instructive. one could also mention "Trace". –  eldo Jun 5 at 15:10

In your first example, Map maps the function f over Plus, applying f to each argument. In the second case this is not possible, because there is just an x. What is your expected result? Maybe this?

Map[f, {x}]
(* {2 x} *)

-

You might find some other functions clarifying:

g[a_] := Sin[a]; Map[g,1+x]

(* Returns Sin[1]+Sin[x] *)

h[a_] := Sqrt[a]; Map[h,1+x]

(* Returns 1+Sqrt[x] *)


etcetera. I think you are being mislead by the coincidence that $2(1+x) = 2 + 2x$.

-
At least your "h" is mislead by the coincidence that you use the "1". Try it with "2" :) –  eldo Jun 5 at 21:10