Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #2635 > unrolled thread
| Started by | Leonid Shifrin <lshifr@gmail.com> |
|---|---|
| First post | 2011-05-23 10:26 +0000 |
| Last post | 2011-05-23 12:41 +0000 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
Re: Overloading functions Leonid Shifrin <lshifr@gmail.com> - 2011-05-23 10:26 +0000
Re: Overloading functions "p.ramsden" <p.ramsden@imperial.ac.uk> - 2011-05-23 12:41 +0000
| From | Leonid Shifrin <lshifr@gmail.com> |
|---|---|
| Date | 2011-05-23 10:26 +0000 |
| Subject | Re: Overloading functions |
| Message-ID | <irdcoa$3op$1@smc.vnet.net> |
Sam,
On Sun, May 22, 2011 at 2:57 PM, Sam Takoy <sam.takoy@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?
>
Yes, this is correct. From the Trace output in my previous answer you could
see that Derivative constructs
an anonymous pure function's body, with the number of arguments determined
by its specification ([1,0] in this case, so 2 variables). Therefore, it
constructs f[#1,#2], and it is at this moment that the 2-argument rule for
<f> fires, and f[#1,#2] gets rewritten into #1+#2 according to that rule.
Since #1 and #2 are used not yet in the context of pure functions, they are
as good the arguments as any.
In other words: there is only one principle at work here - for any rule to
apply, you need at some stage to form the expression of the form f[x1,x2]
etc. In this case, this is done in the implementation of Derivative. There
is nothing else at work here, everything follows directly from the
term-rewriting mechanism.
Cheers,
Leonid
>
> Thanks!
>
> Sam
>
>
>
> ________________________________
> From: Leonid Shifrin <lshifr@gmail.com>
> To: Sam Takoy <sam.takoy@yahoo.com>; mathgroup@smc.vnet.net
> Sent: Sat, May 21, 2011 7:20:51 PM
> Subject: Re: 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@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
> >
> >
>
[toc] | [next] | [standalone]
| From | "p.ramsden" <p.ramsden@imperial.ac.uk> |
|---|---|
| Date | 2011-05-23 12:41 +0000 |
| Message-ID | <irdkm3$14k$1@smc.vnet.net> |
| In reply to | #2635 |
You can see all this quite clearly, I think, if you simply type Derivative[1, 0][f] and Derivative[1, 0,0][f] which return 1 & and 2 & respectively.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web