Re: Problem overriding simple built-in functions
- To: mathgroup at smc.vnet.net
- Subject: [mg28805] Re: [mg28796] Problem overriding simple built-in functions
- From: BobHanlon at aol.com
- Date: Mon, 14 May 2001 01:32:57 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
First, since GCD is Protected you cannot change the definition of GCD without
first using Unprotect[GCD]. However, in general, it is a bad idea to change
built-in functions since this could have unexpected side effects. For
instance, GCD is Listable.
Attributes[GCD]
{Flat, Listable, Orderless,
Protected}
I believe its being Listable precludes it behaving in the way in which you
expected. However, were the change to GCD to work it would break any code
which made use of its being Listable.
Consequently, define your own function in these circumstances
myGCD[x_List] := Apply[GCD, x];
myGCD[x__] := GCD[x];
GCD[3, 6] == myGCD[3, 6] == myGCD[{3, 6}]
True
Bob Hanlon
In a message dated 2001/5/13 3:31:50 AM, jchd at worldnet.fr writes:
>I'm told the following occurs under Mathematica v4.0:
>
>In[1]:= GCD[{3, 6}]
>Out[1]= {3, 6}
>In[2]:= GCD[x_List] := Apply[GCD, x]
>In[3]:= GCD[{3, 6}]
>Out[3]= GCD[{3, 6}]
>In[4]:= Apply[GCD, {3, 6}]
>Out[4]= 3
>
>Why is it so? It looks like the override is ignored, but why?
>
>The same exerpt seems to work fine under version 2.x.
>