Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2001

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

Search the Archive

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[1]:=
$RecursionLimit=25;
In[2]:=
 Unprotect[GCD];
In[3]:=
 ClearAttributes[GCD, Listable];
In[4]:=
GCD[v_?VectorQ] := Apply[GCD, v]
In[5]:=
VectorQ[{1,2}]
Out[5]=
True
In[6]:=
v = {2, 4}
Out[6]=
{2,4}
In[7]:=
GCD[v]
$RecursionLimit::reclim: Recursion depth of 25 exceeded.
Out[7]=
GCD[{2,4}]

By contrast consider the following:


In[1]:=
 Unprotect[GCD];

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

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

In[4]:=
v={2,4}

Out[4]=
{2,4}

In[5]:=
GCD[v]

Out[5]=
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[1]:=
> Unprotect[GCD]
> 
> Out[1]=
> {"GCD"}
> 
> In[2]:=
> ClearAttributes[GCD, Listable]
> 
> In[3]:=
> GCD[v_?VectorQ] := Apply[GCD, v]
> 
> In[4]:=
> ?? 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[5]:=
> v = {2, 4};
> VectorQ[v]
> 
> Out[6]=
> True
> 
> In[7]:=
> GCD[v]
> 
> $RecursionLimit::"reclim": "Recursion depth of \!\(256\) exceeded."
> 
> Out[7]=
> GCD[{2, 4}]
> 
> In[8]:=
> 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[8]=
> 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[2]
  • Next by Date: Mathematica Optmization Programs
  • Previous by thread: Re: Problem overriding simple built-in functions
  • Next by thread: variables versus functions