函数的函数
比如说,我有一些函数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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
只是一个旁注。
当两个函数共享相同的参数,或者第二个函数的参数隐含在第一个函数中时,整个事情就更有意义了。
例如:
或者类似的东西
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:
Or something like
丹尼尔的答案是正确的 - 它回答了你应该问的问题!
但只是为了好玩,这里更接近您的问题实际提出的问题。
测试它:
定义另一个函数:
并给它第二个定义:
当然,事实上您没有将任何参数传递给
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.
test it:
Define another function:
and give it a second definition:
Of course, the fact that you don't pass any arguements to
f
ing[f_,d_]
will (in most cases) make this type of thing pretty pointless...在定义中,
将 a、b 和 c 视为函数调用中的第一个、第二个 和 第三个 参数,而不是符号
a
、b
或c
。与f := (#1 + #2 + #3)&
类似,对f
本身(不带参数)的调用不会给出所需的结果。也许您想做这样的事情:
现在,
f
关联为a
、b
和c,它们是全局符号,而不是函数参数。然后,
将给出
a + b + c + d
,您可以使用ReplaceAll
(或/.
)进行替换:给出
b +c+d+2x
。2 f
将给出2 (a+b+c)
,Sin[f]
将给出Sin[a+b+c ]
,fc
将给出a+b
等...In the definiton
look at a, b and c as the first, second and third argument at the function call and NOT symbols
a
,b
orc
. Similar as inf := (#1 + #2 + #3)&
, the call tof
itself (without arguments) will not give desired results.Perhaps you wanted to do something like this:
Now, the
f
is associated as a sum ofa
,b
andc
, which are global symbols and not function arguments. Then,will give
a + b + c + d
and you can useReplaceAll
(or/.
) for substitution:gives
b+c+d+2x
.2 f
will give2 (a+b+c)
,Sin[f]
will giveSin[a+b+c]
,f-c
will givea+b
etc...我也发现很难辨别您的意图,但这是我的解释:
这样,
g[f, x]
返回一个将其参数传递给f
的函数。I also I find it difficult to discern your intent, but this is my interpretation:
This way,
g[f, x]
returns a function which passes its arguments tof
.好吧,你的设置要求 g 是 a、b 和 c 的函数,因为你明确地使 fa 成为其函数。可以改为:
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:
Out[2]= a + b + c + d
Daniel Lichtblau