乘以两个函数 [英] Multiplying two functions
问题描述
有人知道如何在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)
pre>
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
使用
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 $调用
时调用c $ c>和h 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 integrateh
. I need to do things like this many times, so enteringh
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, includingCompose
which is like your case but constructsf(g(x))
rather thanf(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 theMultiply
function - this is because the argumentsa
andb
aren't evaluated when theMultiply
function is called - they only get evaluated when the returned function is called. If eitherf
org
is changed or deleted beforeh
is called, then without theforce
thenh
will get the value off
andg
at the timeh
is called, rather than the time it was defined. This can lead to some infuriatingly hard-to-find bugs.这篇关于乘以两个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!