MathGroup Archive 2001

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Problem overriding simple built-in functions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg28851] Re: Problem overriding simple built-in functions
  • From: Andrzej Kozlowski <andrzej at tuins.ac.jp>
  • Date: Tue, 15 May 2001 00:59:26 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

Well noticed Allan. Another case of "f-grouping" striking when you least
expect it. That suggests another way to "deal with the problem", (which, of
curse,I am  not  seriously proposing as a way to deal with the original
problem):

In[1]:=
Unprotect[GCD];

In[2]:=
ClearAttributes[GCD,Listable]

In[3]:=
SetAttributes[GCD,OneIdentity]

In[4]:=
GCD[x_List]:=GCD@@x

In[5]:=
GCD[{4,6}]

Out[5]=
2


-- 
Andrzej Kozlowski
Toyama International University
JAPAN

http://platon.c.u-tokyo.ac.jp/andrzej/
http://sigma.tuins.ac.jp/~andrzej/


on 01.5.14 8:24 PM, Allan Hayes at hay at haystack.demon.co.uk wrote:

> One way out of this difficulty is to define
> 
> MyGCD[x___]:= GCD@@Flatten[{x}]
> 
> This gives
> 
> MyGCD[{3,6}]
> 
> 3
> 
> 
> Another possibility is suggested at the end of the following discussion.
> 
> 
> Here is what seems to be happening with the examples that you give.
> 
> In Mathematica 4.1 we have
> 
> atts=Attributes[GCD]
> 
> {Flat,Listable,Orderless,Protected}
> 
> [though in Help Browser, Listable is not mentioned]
> 
> After
> 
> Unprotect[GCD];
> GCD[x_List]:=GCD@@x
> 
> I get, as you report,
> 
> GCD[{3,6}]
> 
> {3,6}
> 
> The explanation is that the attribute  Listable is used before any rules for
> GCD are looked for.
> So the main steps in the evaluation are
> 
> GCD[{3,6}]
> {GCD[3],GCD[6]}   because of the attribute  Listable
> {3,6}
> 
> That new assignment was never used.
> 
> With regard to
> 
> Apply[GCD,{3,6}]
> 
> 3
> 
> The mains steps are
> 
> Apply[GCD,{3,6}]
> GCD[3,6]
> 3
> 
> 
> HOWEVER: it is not sufficient to clear the attribute, Listable:
> 
> ClearAttributes[GCD, Listable];
> 
> We still get
> 
> GCD[{3,6}]
> 
> GCD[{3,6}]
> 
> This is due to the attribute  Flat:
> 
> Starting with GCD[{3,6}] a peculiarity of the attribute Flat is that
> x_ in GCD[x_List]:=GCD@@x is matched to a held GCD[{3,6}] in the
> expression
> GCD[GCD[{3,6}]] which evaluates to GCD[{3,6}] without further evaluation
> 
> But we can do something about this:
> 
> Clear the old definition:
> 
> GCD[x_List]:=.
> 
> Use instead
> 
> GCD[{x___}] := GCD[x]
> 
> Now  we get
> 
> GCD[{3,6}]
> 
> 3
> 
> --
> Allan
> ---------------------
> Allan Hayes
> Mathematica Training and Consulting
> Leicester UK
> www.haystack.demon.co.uk
> hay at haystack.demon.co.uk
> Voice: +44 (0)116 271 4198
> Fax: +44 (0)870 164 0565
> 
> "Jean-Christophe Deschamps" <jchd at worldnet.fr> wrote in message
> news:9dld4j$o9b at smc.vnet.net...
>> Hi,
>> 
>> 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.
>> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 



  • Prev by Date: Re: Problem overriding simple built-in functions
  • Next by Date: Plot3D
  • Previous by thread: Re: Problem overriding simple built-in functions
  • Next by thread: Re: Re: Problem overriding simple built-in functions