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

MathGroup Archive 2007

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

Search the Archive

Re: ClearAll[f]; f[x_] := x^2; f[y_] :=y^4; (*What is:*) f[2]

  • To: mathgroup at smc.vnet.net
  • Subject: [mg84386] Re: ClearAll[f]; f[x_] := x^2; f[y_] :=y^4; (*What is:*) f[2]
  • From: Szabolcs Horvát <szhorvat at gmail.com>
  • Date: Thu, 20 Dec 2007 16:51:35 -0500 (EST)
  • References: <fkcueh$5d9$1@smc.vnet.net>

cebailey wrote:
> ClearAll[f]; f[x_] := x^2; f[y_] :=y^4; (*What is:*) f[2]
> 
> Evaluating this line in Mathematica 5.2 or Mathematica 6 returns 16. This makes sense, because the second definition replaces the first, as we can see when ?f returns:
> Global`f
> f[y_]:=y^4
> 
> But in _A_Physicist's_Guide_to_Mathematica_ on p.314, Patrick Tam shows an example like this returning the other answer, 4, defined in the first definition. He then demonstrates that ?f returns:
> Global`f
> f[x_] := x^2
> f[y_]:= y^4
> He says his book was developed with Mathematica 2.2 and a prerelease of Mathematica 3 and is compatible with both.
> 
> He goes on to explain:
> "Contrary to expectation, Mathematica used the first definition. The ? operator reveals that Mathematica stores both definitions in the global rule base, giving higher priority to the first definition. (This problem cannot, perhaps, be called a bug because developers of Mathematica are well aware of this design flaw, which is quite difficult to mend....)"
> 
> What is he talking about? Did Mathematica  2.2 and 3 treat this differently? If earlier versions worked in this surprising way, there must have been a reason - what was it? Was it changed to prevent surprises like this example? Did changing it create other unfortunate consequences? Was Tam just wrong? Or do I misunderstand?
> 

I haven't used any Mathematica versions before 4.0, but it seems that 
Mathematica just wasn't smart enough to figure out that the patterns 
f[x_] and f[y_] are really the same.

You can still fool it into thinking that equivalent patterns are 
different, for example try

g[x_?Positive] := x^2
g[x_ /; Positive[x]] := x^2

Here, both definitions will be stored.

As explained in the documentation, when several definitions are attached 
to a symbol, Mathematica tries to figure out which one is "more 
specific" than the others, and use that first when evaluating 
expressions.  (For example, f[0] = 1 is more specific than f[x_] := 
f[x-1]*x, because it matches "less frequently".)  But there is no 
precise definition of what "more specific" or "more general" is.  When 
the system cannot decide about this, it simply uses the definitions in 
the order they were given.  Even in this ordering of definitions, there 
are differences between Mathematica versions.

-- 
Szabolcs


  • Prev by Date: Re: ClearAll[f]; f[x_] := x^2; f[y_] :=y^4; (*What is:*) f[2]
  • Next by Date: Re: Speeding Up Realtime Visualization
  • Previous by thread: Re: ClearAll[f]; f[x_] := x^2; f[y_] :=y^4; (*What is:*) f[2]
  • Next by thread: Re: ClearAll[f]; f[x_] := x^2; f[y_] :=y^4; (*What is:*) f[2]