A few days ago I was giving my introductory course in Mathematica
programming and was about to explain the 'Listable' attribute of
functions, when on the spur of the moment I did something one should
never do and departed from my prepared plan. I just got the idea that a
good way to introduce listability would be to start with some very well
known function, remove the Listable attribute, and then demonstrate
that it would be really desirable to have it back. Of course I could
have defined my own function but at this point we had not yet got as
far as defining functions so I wanted to choose something very well
known. Without any tests my choice fall on Sqrt. Sqrt is of course
Listable:
In[1]:=
Sqrt[{4,9}]
Out[2]=
{2, 3}
One can check it's attributes explicitly: In[2]:=
Attributes[Sqrt]
Out[2]=
{Listable,NumericFunction,Protected}
Now we remove Listable:
In[3]:=
ClearAttributes[Sqrt,Listable]
Sqrt is no longer Listable:
In[4]:=
Attributes[Sqrt]
Out[4]=
{NumericFunction,Protected}
Or is it?
In[5]:=
Sqrt[{4,9}]
Out[5]=
{2,3}
This was a little embarrassing. I mumbled something to the effect that
one should never trust a computer program and replaced Sqrt with Sin.
Now everything worked as expected.
My question is: why on earth is Listablity "hard wired" into Sqrt but
not in other functions, like Sin. Log etc. Is this just a accidental
quirk (I wouldn't even call it a bug) which just proves my point about
"never trusting a computer program" or is there something "deep"
involved in this?
Andrzej Kozlowski
Professor of Mathematics
Toyama International University
Toyama, JAPAN
Of course one should never trust a computer program. Actually, this is
even written on page iv of the Mathematica book: "[...], users should
recognize that all complex software systems and their
documentation contain errors and omissions. [...]"
However, in your example there is no bug and Sqrt just changes to Power
:
In[1]:= ClearAttributes[Sqrt,Listable]
In[2]:= Sqrt[{a,b}]//Trace//FullForm
Out[2]//FullForm=
> List[HoldForm[Sqrt[List[a, b]]],
> HoldForm[Power[List[a, b], Rational[1, 2]]],
> HoldForm[List[Power[a, Rational[1, 2]], Power[b, Rational[1, 2]]]]]
In[3]:= ClearAttributes[Power,Listable]
In[4]:= Sqrt[{a,b}]
Out[4]= Sqrt[{a, b}]
Rolf Mertig
Mertig Research & Consulting
Mathematica training and programming Development and distribution of
FeynCalc Amsterdam, The Netherlands
http://www.mertig.com
>My question is: why on earth is Listablity "hard wired" into Sqrt but
>not in other functions, like Sin. Log etc. Is this just a accidental
>quirk (I wouldn't even call it a bug) which just proves my point about
>"never trusting a computer program" or is there something "deep"
>involved in this?
>
Sqrt[{x, y}]
{Sqrt[x], Sqrt[y]}
%//FullForm
List[Power[x,Rational[1,2]],Power[y,Rational[1,2]]]
Sqrt uses Power. To make Sqrt non-Listable you need to clear the
Listable attribute from both Sqrt and Power
ClearAttributes[{Sqrt, Power}, Listable]
Sqrt[{x, y}]
Sqrt[{x, y}]
SetAttributes[{Sqrt, Power}, Listable]
Bob Hanlon
Try TreeForm[Sqrt[x]] to see that Sqrt[x] is automatically converted to
x^(1/2). Since Power is a function of 2 arguments, I don't know if
"Listable" makes sense with it.
Regards,
Daniel CLEMENT
> ----- Original Message -----
> From: Andrzej Kozlowski [SMTP:and...@tuins.ac.jp]
> Sent: Friday, November 20, 1998, 8:14:22
> To: math...@smc.vnet.net
> Subject: [mg14836] Sqrt and Listability
>
> I have come across a mystery. Solving it is a matter of absolutely no
> importance, yet I would be grateful for any help in putting it to
rest.
>
[...]
>
> In[1]:=
> Sqrt[{4,9}]
> Out[2]=
> {2, 3}
>
> One can check it's attributes explicitly: In[2]:=
> Attributes[Sqrt]
> Out[2]=
> {Listable,NumericFunction,Protected}
>
> Now we remove Listable:
>
> In[3]:=
> ClearAttributes[Sqrt,Listable]
>
> Sqrt is no longer Listable:
>
> In[4]:=
> Attributes[Sqrt]
> Out[4]=
> {NumericFunction,Protected}
>
> Or is it?
>
> In[5]:=
> Sqrt[{4,9}]
> Out[5]=
> {2,3}
>
[...]
>
> My question is: why on earth is Listablity "hard wired" into Sqrt but
> not in other functions, like Sin. Log etc. Is this just a accidental
> quirk (I wouldn't even call it a bug) which just proves my point about
> "never trusting a computer program" or is there something "deep"
> involved in this?
>
> Andrzej Kozlowski
>
> Professor of Mathematics
> Toyama International University
> Toyama, JAPAN
>
>
> ----- End Of Original Message -----
Andrej:
Sqrt[x] is replaced by Power[x.1/2] which is computed; and Power is
Listable. Further, in output, Power[x.1/2] is changes to Sqrt[x].
With Sqrt Listable, the computation runs
Sqrt[{4,x}] -> {Sqrt[4],Sqrt[x]} -> {Power[4,1/2], Sqrt[x]} -> {2,
Sqrt[x]} -> {2, Power[x,1/2]} -> {2, Sqrt[x}
After ClearAttributes[Sqrt, Listable] we get
Sqrt[{4,x}] -> Power[{x,4},1/2} -> {Power[4,1/2], Power[x.1/2]} -> {2,
Power[x,1/2]} -> {2, Sqrt[x}
Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
www.haystack.demon.co.uk
h...@haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565