Map Behavior (/ @)

I think that this is something easy that I do not notice - a clear sign of illiteracy, but in any case.

Like this

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

But

 (Map[Sign, LessEqual[-1, -100]]) == LessEqual[Sign[-1], Sign[-100]] -> False 
+4
source share
2 answers

Using Trace on lhs will help show what happened.

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

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

Please note: the card does not have HoldXXX attributes.

 Attributes[Map] 

Out [3] = {Protected}

So, LessEqual evaluates before the card does something. At this point you get

 Map[Sign,False] 

Since False is an atomic expression, it is simply evaluated as False.

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

Daniel Lichtblow Wolfram Research

+7
source

See what happens when you do this in two steps:

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

The second result may be unexpected, but it turns out that the Map function works; if you use Map for an expression with a length of 0 (for example, the character False ), it simply returns that expression without changes. 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 
+3
source

Source: https://habr.com/ru/post/1338465/


All Articles