Re: Problem overriding simple built-in functions[2]

• To: mathgroup at smc.vnet.net
• Subject: [mg28861] Re: Problem overriding simple built-in functions[2]
• From: "Allan Hayes" <hay at haystack.demon.co.uk>
• Date: Wed, 16 May 2001 03:28:09 -0400 (EDT)
• References: <9dld4j\$o9b@smc.vnet.net> <9dqdrf\$4jr@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Apologies for the misposting of the following addendum (it went to Subject:
Re: column * row ??(2)).

I should have added the following alternative way of dealing with the
problem arising from the attribute Flat noted at the  end of my previous
posting on this topic [copied below].

ClearAttributes[GCD, {Listable, Protected}]
SetAttributes[GCD,{OneIdentity}]

GCD[x_List]:= GCD@@x

GCD[{3,6}]

3

Protect[GCD

--
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

"Allan Hayes" <hay at haystack.demon.co.uk> wrote in message
news:9dqdrf\$4jr at smc.vnet.net...
> 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
>
>
> Clear the old definition:
>
> GCD[x_List]:=.
>
>
> 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: Programming question about how build a fitting function
• Next by Date: Re: Re: Problem overriding simple built-in functions
• Previous by thread: Programming question about how build a fitting function
• Next by thread: Mathematica Optmization Programs