Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map (/@) behavior

I guess this is something easy that I'm overlooking is a clear sign of illiteracy, but anyway.

How is that

(Map[Sign, LessEqual[x, y]]) === LessEqual[Sign[x], Sign[y]]
-> True  

But

(Map[Sign, LessEqual[-1, -100]]) == LessEqual[Sign[-1], Sign[-100]]
-> False  
like image 515
Dr. belisarius Avatar asked Dec 28 '22 01:12

Dr. belisarius


2 Answers

Using Trace on the lhs will help to show what has happened.

Trace[Map[Sign, LessEqual[-1, -100]]]

Out[2]= {{-1 <= -100, False}, Sign /@ False, False}

Notice that Map has no HoldXXX attributes.

Attributes[Map]

Out[3]= {Protected}

So the LessEqual evaluates before Map does anything. At which point you get

Map[Sign,False]

As False is an atomic expression, this just evaluates to False.

The rhs of course evaluates to True, since Sign[-1] and Sign[-100] are both -1.

Daniel Lichtblau Wolfram Research

like image 92
Daniel Lichtblau Avatar answered Jan 02 '23 14:01

Daniel Lichtblau


Look what happens when you do it in two steps:

In[1]:= LessEqual[-1,-100]
Out[1]= False

In[2]:= Map[Sign, False]
Out[2]= False

The second result there may be surprising, but it happens to be how the Map function works; if you use Map on an expression with length 0 (like the symbol False), it just returns that expression unchanged. Another example:

In[3]:= Map[f, "Pillsy"]
Out[3]= "Pillsy"

On the other hand, obviously

In[4]:= LessEqual[Sign[-1],Sign[-100]]
Out[4]= True
like image 30
Pillsy Avatar answered Jan 02 '23 16:01

Pillsy