MathGroup Archive 2011

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

Search the Archive

Re: Overloading functions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg119154] Re: Overloading functions
  • From: DrMajorBob <btreat1 at austin.rr.com>
  • Date: Mon, 23 May 2011 06:25:50 -0400 (EDT)

"...no decision is made as to which f to send to MyOperator..."

(a) There is only one f (despite having two LHS patterns), and
(b) the pattern matching is done all at once.

"...and based on this looks for a rule for "f" that takes two variables.  
Is that correct?"

No.

Bobby

On Sun, 22 May 2011 05:57:41 -0500, Sam Takoy <sam.takoy at yahoo.com> wrote:

> Thanks Leonid.
>
> Let me paraphrase, so I can clarify my original confusion and see if I  
> got it
> right.
>
> I assume (am I wrong?) that MyOperator[f][x, y] associates left
> (MyOperator[f])[x, y]. At the time MyOperator[f] is called, no decision  
> is made
> as to which f to send to MyOperator, because what is sent to the  
> operator is
> just the symbol "f". Inside MyOperator, Mathematica sees  
> Derivative[1,0][f] and
> based on this looks for a rule for "f" that takes two variables.
>
> Is that correct?
>
> Thanks!
>
> Sam
>
>
>
> ________________________________
> From: Leonid Shifrin <lshifr at gmail.com>
> To: Sam Takoy <sam.takoy at yahoo.com>; mathgroup at smc.vnet.net
> Sent: Sat, May 21, 2011 7:20:51 PM
> Subject: [mg119130] Re: [mg119093] Overloading functions
>
> Sam,
>
> This is a misunderstanding. Mathematica will always use the symbol f.  
> Different
> definitions for f are different rules, and the analogy with function  
> overloading
> in other languages is only superficial (on the level of syntax only).  
> The choice
> of which rule to apply is then based on how Derivative works. Since you  
> call it
> as Derivative[1,0], it assumes 2 independent variables, and this  
> determines the
> way it calls f:
>
> In[23]:= Derivative[1,0][f][x,y]//Trace
>
> Out[23]= {{f^(1,0),{f[#1,#2],#1+#2},1&},(1&)[x,y],1}
>
> So, there is a single symbol with different rules. Which rule is used is
> determined once the expression f[args] is formed. It is as simple as  
> that.
>
> Regards,
> Leonid
>
>
>
> On Sat, May 21, 2011 at 3:50 AM, Sam Takoy <sam.takoy at yahoo.com> wrote:
>
> Hi,
>>
>> In the following code
>>
>> MyOperator[g_][x_, y_] = 4 + Derivative[1, 0][g][x, y];
>>
>> f[x_, y_] = x + y;
>> f[x_, y_, z_] = 2 x + 2 y + 2 z;
>>
>> MyOperator[f][x, y]
>>
>>
>> how does Mathematica know which f to send to MyOperator. Can someone
>> outline the formal decision tree that Mathematica follows?
>>
>> Thanks!
>>
>> Sam
>>
>>


-- 
DrMajorBob at yahoo.com


  • Prev by Date: Re: get a, b from numbers in the form a+b Pi
  • Next by Date: Re: Overloading functions
  • Previous by thread: Re: Overloading functions
  • Next by thread: Re: Overloading functions