Re: Problem overriding simple built-in functions
- To: mathgroup at smc.vnet.net
- Subject: [mg28834] Re: Problem overriding simple built-in functions
- From: Jean-Christophe Deschamps <jchd at worldnet.fr>
- Date: Tue, 15 May 2001 00:59:05 -0400 (EDT)
- References: <9dnr16$stj@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
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