函数的函数

发布于 2024-10-31 00:22:48 字数 217 浏览 1 评论 0原文

比如说,我有一些函数f[a_,b_,c_]=a+b+c。现在我需要定义另一个函数g[f_,d_]=f+d,这里的f应该替换为f的定义代码>,结果为a+b+c+d

我该怎么做?我尝试了 g[f_,d_]=f+d/.f->Definition[f],但这不起作用。

Say, I have some function f[a_,b_,c_]=a+b+c. Now I need to define another function,g[f_,d_]=f+d, where the f here should be replaced by the definition of f, resulting in a+b+c+d.

How do I do this? I tried g[f_,d_]=f+d/.f->Definition[f], however that didn't work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

瑾夏年华 2024-11-07 00:22:49

只是一个旁注。

当两个函数共享相同的参数,或者第二个函数的参数隐含在第一个函数中时,整个事情就更有意义了。

例如:

f[x_] := 1 + 1/x;

g[h_, x_] := h[x]^x;

Limit[g[f, x], x -> +Infinity]  
-> E

或者类似的东西

f[x_] := EuclideanDistance[x, #] &

g[f_, x_] := If[f[2 x]@x < 3, x, 2 x]

g[f, 3]
->6

g[f,2]
->2

Just an aside note.

The whole thing makes more sense when both functions share the same arguments, or the arguments for the second function are implicit in the first.

For example:

f[x_] := 1 + 1/x;

g[h_, x_] := h[x]^x;

Limit[g[f, x], x -> +Infinity]  
-> E

Or something like

f[x_] := EuclideanDistance[x, #] &

g[f_, x_] := If[f[2 x]@x < 3, x, 2 x]

g[f, 3]
->6

g[f,2]
->2
-柠檬树下少年和吉他 2024-11-07 00:22:49

丹尼尔的答案是正确的 - 它回答了你应该问的问题!

但只是为了好玩,这里更接近您的问题实际提出的问题。

f[a_,b_,c_] := a + b + c

g[f_, d_] := Catch[Module[{dv},
  Check[dv = DownValues[f], Throw[$Failed], {DownValues::sym}];
  Switch[Length[dv],
         0, Print["No DownValues"]; $Failed,
         1, dv[[1, 2]] + d,
         _?(# > 1 &), Print["More than one DownValue - using the first!"]; 
                      dv[[1, 2]] + d,
         _, Print["huh?!"]; $Failed]]]

测试它:

In[3]:= g[f, d]
Out[3]= a + b + c + d

定义另一个函数:

ff[a_, b_, c_] := a b c

In[5]:= g[ff, d]
Out[5]= a b c + d

并给它第二个定义:

In[6]:= ff[a_, b_] := a + b
In[7]:= DownValues[ff]
Out[7]= {HoldPattern[ff[a_,b_,c_]] :> a b c, HoldPattern[ff[a_,b_]] :> a+b}

In[8]:= g[ff, d]
During evaluation of In[8]:= More than one DownValue - using the first!
Out[8]= a b c + d

当然,事实上您没有将任何参数传递给 g[f_,d_] 中的 f (在大多数情况下)会让这种事情变得毫无意义......

Daniel's answer is the right one - it answers what your question should have asked!

But just for fun here's something closer to what your question actually asked.

f[a_,b_,c_] := a + b + c

g[f_, d_] := Catch[Module[{dv},
  Check[dv = DownValues[f], Throw[$Failed], {DownValues::sym}];
  Switch[Length[dv],
         0, Print["No DownValues"]; $Failed,
         1, dv[[1, 2]] + d,
         _?(# > 1 &), Print["More than one DownValue - using the first!"]; 
                      dv[[1, 2]] + d,
         _, Print["huh?!"]; $Failed]]]

test it:

In[3]:= g[f, d]
Out[3]= a + b + c + d

Define another function:

ff[a_, b_, c_] := a b c

In[5]:= g[ff, d]
Out[5]= a b c + d

and give it a second definition:

In[6]:= ff[a_, b_] := a + b
In[7]:= DownValues[ff]
Out[7]= {HoldPattern[ff[a_,b_,c_]] :> a b c, HoldPattern[ff[a_,b_]] :> a+b}

In[8]:= g[ff, d]
During evaluation of In[8]:= More than one DownValue - using the first!
Out[8]= a b c + d

Of course, the fact that you don't pass any arguements to f in g[f_,d_] will (in most cases) make this type of thing pretty pointless...

Spring初心 2024-11-07 00:22:49

在定义中,

 f[a_, b_, c_] := a+b+c

将 a、b 和 c 视为函数调用中的第一个第二个第三个 参数,而不是符号 abc。与 f := (#1 + #2 + #3)& 类似,对 f 本身(不带参数)的调用不会给出所需的结果。

也许您想做这样的事情:

f := a+b+c

现在,f 关联为 abc,它们是全局符号,而不是函数参数。然后,

f+d

将给出a + b + c + d,您可以使用ReplaceAll(或/.)进行替换:

f + d /. a->2x

给出b +c+d+2x2 f 将给出 2 (a+b+c)Sin[f] 将给出 Sin[a+b+c ]fc 将给出 a+b 等...

In the definiton

 f[a_, b_, c_] := a+b+c

look at a, b and c as the first, second and third argument at the function call and NOT symbols a,b or c. Similar as in f := (#1 + #2 + #3)&, the call to f itself (without arguments) will not give desired results.

Perhaps you wanted to do something like this:

f := a+b+c

Now, the f is associated as a sum of a, b and c, which are global symbols and not function arguments. Then,

f+d

will give a + b + c + d and you can use ReplaceAll (or /.) for substitution:

f + d /. a->2x

gives b+c+d+2x. 2 f will give 2 (a+b+c), Sin[f] will give Sin[a+b+c], f-c will give a+b etc...

诗笺 2024-11-07 00:22:49

我也发现很难辨别您的意图,但这是我的解释:

f[a_, b_, c_] := a + b + c
g[f_, d_] := f[##] + d &;

g[f, 3]

  (* Out=  f[##1] + 3 &     *)

%[q, r, s]

  (* Out=  3 + q + r + s    *)

g[f, 5][1, 2, 3]

  (* Out=  11    *)

这样,g[f, x] 返回一个将其参数传递给 f 的函数。

I also I find it difficult to discern your intent, but this is my interpretation:

f[a_, b_, c_] := a + b + c
g[f_, d_] := f[##] + d &;

g[f, 3]

  (* Out=  f[##1] + 3 &     *)

%[q, r, s]

  (* Out=  3 + q + r + s    *)

g[f, 5][1, 2, 3]

  (* Out=  11    *)

This way, g[f, x] returns a function which passes its arguments to f.

过去的过去 2024-11-07 00:22:48

好吧,你的设置要求 g 是 a、b 和 c 的函数,因为你明确地使 fa 成为其函数。可以改为:

f[a_,b_,c_]=a+b+c;

g[f_,a_,b_,c_,d_] = f[a,b,c]+d

Out[2]= a + b + c + d

Daniel Lichtblau

Well, your setup requires that g be a function of a, b, and c, since you explicitly made f a function thereof. Could instead do:

f[a_,b_,c_]=a+b+c;

g[f_,a_,b_,c_,d_] = f[a,b,c]+d

Out[2]= a + b + c + d

Daniel Lichtblau

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文