Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiplying two functions

Tags:

function

r

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.

like image 272
BioBroo Avatar asked Dec 29 '13 07:12

BioBroo


People also ask

What happens when you multiply 2 functions?

When you multiply two functions together, you'll get a third function as the result, and that third function will be the product of the two original functions. For example, if you multiply f(x) and g(x), their product will be h(x)=fg(x), or h(x)=f(x)g(x). You can also evaluate the product at a particular point.

How do you multiply variables with functions?

Because of that, to multiply a number by an expression with variables, you just multiply the number with the number in front of the variable. Remember that when there isn't any number in front of , there's still an invisible 1 there, and you can multiply your number by 1.


1 Answers

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.

like image 198
Spacedman Avatar answered Oct 30 '22 06:10

Spacedman