# How to understand the difference between Sqrt and Log after ClearAll them

I came across this post, here I change the last line of the code

Unprotect[Sqrt];
Sqrt[x_] = "blahblah";
Protect[Sqrt];
Sqrt[2]


"blahblah"

Unprotect[Sqrt];
ClearAll[Sqrt];
Protect[Sqrt];
Sqrt[{1, 2, 3}]


{1, Sqrt[2], Sqrt[3]}

And I do the same for Log

Unprotect[Log];
Log[x_] = "blahblah";
Protect[Log];
Log[2]


"blahblah"

Unprotect[Log];
ClearAll[Log];
Protect[Log];
Log[{1, 2}]


Log[{1, 2}]

Since I've ClearAll the definitions and attributes of Sqrt and Log, and neither of them have Listable attributes.

Attributes[{Sqrt, Log}]


{{Protected}, {Protected}}

So why is Sqrt still Listable while Log is not? Do I misunderstand something?

Leaving aside the wisdom of modifying System functions this is an interesting question.

I would not have been surprised to see this behavior had the input list been packed as I have already learned about low-level optimizations on packed arrays. See:

However that does not appear to be the issue here since (typed in) {1, 2, 3} is not packed. Apparently the "listable" behavior of Sqrt is not (only) implemented in terms of the attribute Listable. As an example here is function without Listable that still threads over lists:

f[x_List] := f /@ x

f[{1, 2, 3}]

{f[1], f[2], f[3]}


Quoting:

There are two types of listability - the one in built-in functions, which pushes threading into the kernel and is fast, and the top-level one (setting Listable for some functions by the user).

You can not achieve built-in listability by simply setting a Listable attribute. The reason is that, while the end result is the same - automatic threading over lists, the underlying mechanisms to achieve it are different for built-ins vs user-defined. When a built-in Listable function (particularly numerical) is passed lists as arguments, it dispatches to the special branch which internally runs the loop, and returns a list of results. So, for a built-in function, Listable is rather a signal to pick the internal branch which deals with lists automatically.

It seems that the absence of Listable does not prevent this "internal branch" from being used.

• I think in this case, you make in Listable,because trace it will give me {f[{1,2,3}],f/@{1,2,3},{f[1],f[2],f[3]}}. – luyuwuli Jan 12 '15 at 13:33
• @luyuwuli But f does not have the Listable attribute, it only behaves (somewhat) like it does, and that was my point. – Mr.Wizard Jan 12 '15 at 13:34
• According to your edit, did you mean that there are lower-level map function implemented in the kernel so that it can auto-thread even without the Listable attributes? – luyuwuli Jan 12 '15 at 13:37
• @luyuwuli I am not the one who wrote that, Leonid Shifrin is, but as I understand it, yes. – Mr.Wizard Jan 12 '15 at 13:39
• I always understood the Listable attribute in these cases as mainly a courtesy to user code rather than a requirement for proper evaluation. After all, if a function behaves as if Listable, it's not really helpful if the attribute is missing, but you can of course always have a function with specialized definitions for different types of inputs regardless of that. Or, perhaps Sqrt implements an explicit loop over depth-1 lists, but not for higher-dimensional structures, so that it is part specialized, and part top-level Listable. This would be a reasonable optimization, IMO. – Oleksandr R. Jan 12 '15 at 13:41