       Re: Re: Problem overriding simple built-in functions

• To: mathgroup at smc.vnet.net
• Subject: [mg28854] Re: [mg28834] Re: Problem overriding simple built-in functions
• From: Andrzej Kozlowski <andrzej at tuins.ac.jp>
• Date: Wed, 16 May 2001 03:28:04 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

```Basically Allan Hayes had already anticipated and asnswered your question.
As he pointed out the problem you run into is caused by GCD's Flat
attribute. It may be still interesting to note however the difference
between these two cases:

In:=
\$RecursionLimit=25;
In:=
Unprotect[GCD];
In:=
ClearAttributes[GCD, Listable];
In:=
GCD[v_?VectorQ] := Apply[GCD, v]
In:=
VectorQ[{1,2}]
Out=
True
In:=
v = {2, 4}
Out=
{2,4}
In:=
GCD[v]
\$RecursionLimit::reclim: Recursion depth of 25 exceeded.
Out=
GCD[{2,4}]

By contrast consider the following:

In:=
Unprotect[GCD];

In:=
ClearAttributes[GCD, Listable];

In:=
GCD[v_List] := Apply[GCD, v]

In:=
v={2,4}

Out=
{2,4}

In:=
GCD[v]

Out=
GCD[{2,4}]

The difference is that in the second case there is no RecursionLimit
problem.

Roughly this is what happens. When you try to evaluate GCD[v]] the Flat
attribute causes this to be re-written as GCD[GCD[{2,4}]]. In the first case
Mathematica at this point checks if VectorQ[GCD[{2,4}] ] is true or not.
Again, trying to get a match it re-writes GCD[{2,4}] as GCD[GCD[{2,4}]] and
so on. Hence the recursion.

In the second case Mathematica only checks if the Head of the expression
GCD[{2,4}] is List. Since it is not there is no recursion.

--
Andrzej Kozlowski
Toyama International University
JAPAN

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

on 5/15/01 1:59 PM, Jean-Christophe Deschamps at jchd at worldnet.fr wrote:

>
>
> Thanks to those who took the pain to answer, pointing out that due the
> Listable attribute of GCD, the behavior was not surprising.
> I must admit my sloppiness in somehow making up the example in my previous
> post, living out many details like that. The corrected example below is more
> explicit about my question. I still must be missing something obvious, but I
> fail to see what.
>
>
>
> In:=
> Unprotect[GCD]
>
> Out=
> {"GCD"}
>
> In:=
> ClearAttributes[GCD, Listable]
>
> In:=
> GCD[v_?VectorQ] := Apply[GCD, v]
>
> In:=
> ?? GCD
>
> "GCD[n1, n2, ... ] gives the greatest common divisor of the integers ni."
>
> \!\(\*
> InterpretationBox[GridBox[{
> {\(Attributes[GCD] = {Flat, Orderless}\)},
> {" "},
> {GridBox[{
> {\(GCD[v_?VectorQ] := GCD @@ v\)}
> },
> GridBaseline->{Baseline, {1, 1}},
> ColumnWidths->0.999,
> ColumnAlignments->{Left}]}
> },
> GridBaseline->{Baseline, {1, 1}},
> ColumnAlignments->{Left}],
> Definition[ GCD],
> Editable->False]\)
>
> In:=
> v = {2, 4};
> VectorQ[v]
>
> Out=
> True
>
> In:=
> GCD[v]
>
> \$RecursionLimit::"reclim": "Recursion depth of \!\(256\) exceeded."
>
> Out=
> GCD[{2, 4}]
>
> In:=
> Apply[GCD, v]
>
> \$RecursionLimit::"reclim": "Recursion depth of \!\(256\) exceeded."
>
> \$RecursionLimit::"reclim": "Recursion depth of \!\(256\) exceeded."
>
> \$RecursionLimit::"reclim": "Recursion depth of \!\(256\) exceeded."
>
> General::"stop": "Further output of \!\(\$RecursionLimit :: \"reclim\"\) will
> \
> be suppressed during this calculation."
>
> Out=
> 2
>
>
> The answer finally pops up, but why the recursion? [Trace-ing of this has
> been left off here to conserve space.]
>
> LCM behaves as GCD in Mathematica 4.
>
>
> |> Consequently, define your own function in these circumstances
>
> No problem in this case: the scope of this code is very strictly limited and
> not any kind of a general setup.
>
> I was just wondering which new internal mecanism has been thrown in, that
> causes my own previous code to break pityfully. I believe it's worth
> understanding precisely why, in such a simple case, so as to benefit of the
> introduced goodies in possibly more subtle contexts where it might prove
> useful.
>
> JcD
>
>

```

• Prev by Date: Re: Problem overriding simple built-in functions
• Next by Date: Mathematica Optmization Programs
• Previous by thread: Re: Problem overriding simple built-in functions
• Next by thread: variables versus functions