乘以两个函数 [英] Multiplying two functions

查看:101
本文介绍了乘以两个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何在R中的两个数学函数上进行乘法或执行任何二进制运算吗?



我试图采用如下形式:

  f <-function(x){x + 2} 
g <-function(x){x}

,我想要 h = f * g ,最终整合 h 。我需要做很多次这样的事情,所以手动输入 h 并不是一个可行的选择。

解决方案

如果您要创建大量相乘函数,请创建一个乘数函数,该函数返回函数参数乘积的函数:

(b)
force(b)
函数(x){a(x) * b(x)}
}

然后您可以这样做:

  f <-function(x){x + 2} 
g <-function(x){x}
h =乘以(f,g)
h(1:5)
[1] 3 8 15 24 35
f(1:5)* g(1:5)
[1] 3 8 15 24 35

然后:

  h2 =乘法(f,f)
h2(1:5)
[1] 9 16 25 36 49
f(1:5) * f(1:5)
[1] 9 16 25 36 49

可以在任何函数中使用它:

  h3 =乘以(sqrt,sin)
h3(1:5)
[1] 0.841471 1.28594 1 0.244427 -1.513605 -2.144220
sqrt(1:5)* sin(1:5)
[1] 0.841471 1.285941 0.244427 -1.513605 -2.144220
pre>

使用 Multiply 函数创建的任何函数都将返回一个函数,该函数返回两个函数。

使用这样的函数进行编程通常非常有用。有一个R包, functional ,它有一些这种功能,包括 Compose ,就像你的情况一样但构造 f(g(x))而不是 f(x)* g(x)

  require(功能)
z =撰写(sqrt,sin)
z(1:5)
[ 1] 0.8414710 0.9877659 0.9870266 0.9092974 0.7867491
sin(sqrt(1:5))
[1] 0.8414710 0.9877659 0.9870266 0.9092974 0.7867491

请注意,其始终为圆括号(括号),因为这些仍然是函数。它们恰好是由其他函数创建的。



还要注意在<$ c中使用 force $ c> Multiply 函数 - 这是因为参数 a b 不是在调用 Multiply 函数时进行评估 - 只有在调用返回的函数时才会评估它们。如果在 h 之前更改或删除 f g 如果没有 force ,那么 h 会得到 f h 时调用c $ c>和 g ,而不是定义它的时间。这可能会导致一些难以发现的错误。


Does anyone know how to multiply, or perform any binary operation, on two mathematical functions in R?

I'm trying to take something like:

f<-function(x){x+2}
g<-function(x){x}

and I want h = f * g, eventually to integrate h. I need to do things like this many times, so entering h manually isn't a viable option.

解决方案

If you are going to be creating lots of multiplied functions, make a multiplier function that returns a function that is the product of its function arguments:

Multiply=function(a,b){
  force(a)
  force(b)
  function(x){a(x)*b(x)}
}

Then you can do:

 f<-function(x){x+2}
 g<-function(x){x}
 h=Multiply(f,g)
 h(1:5)
[1]  3  8 15 24 35
 f(1:5)*g(1:5)
[1]  3  8 15 24 35

And then:

h2=Multiply(f,f)
h2(1:5)
[1]  9 16 25 36 49
f(1:5)*f(1:5)
[1]  9 16 25 36 49

And you can use this with any function:

h3 = Multiply(sqrt,sin)
h3(1:5)
[1]  0.841471  1.285941  0.244427 -1.513605 -2.144220
sqrt(1:5)*sin(1:5)
[1]  0.841471  1.285941  0.244427 -1.513605 -2.144220

Any function you create with the Multiply function will be a function that returns the element-wise product of the two functions.

Programming with functions like this is often very useful. There's an R package, functional, that has some functions for this kind of thing, including Compose which is like your case but constructs f(g(x)) rather than f(x)*g(x):

require(functional)
z=Compose(sqrt,sin)
z(1:5)
[1] 0.8414710 0.9877659 0.9870266 0.9092974 0.7867491
sin(sqrt(1:5))
[1] 0.8414710 0.9877659 0.9870266 0.9092974 0.7867491

Note that its always round brackets (parentheses) because these are still functions. They just happen to have been created by other functions.

Note also the use of force in the Multiply function - this is because the arguments a and b aren't evaluated when the Multiply function is called - they only get evaluated when the returned function is called. If either f or g is changed or deleted before h is called, then without the force then h will get the value of f and g at the time h is called, rather than the time it was defined. This can lead to some infuriatingly hard-to-find bugs.

这篇关于乘以两个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆