Dear R Users I have a list of functions. Each function in the list is a function of single variable. I would like to create a function (of one variable) which represents the sum of all the functions in the list. So, if the functions in my list are f1(x),..,f5(x) then I would like a new function f(x)=f1(x)+f2(x)+...f5(x) Appreciate any suggestions on how to do this. I need the above f(x) function because I would like to minimise it with respect to x using the nlm function. Thanks.
> List <- list(abs, function(x)x*10, function(x)x*100) > f <- function(x)Reduce(`+`, lapply(List, function(func)func(x))) > f(-1:2)[1] -109 0 111 222 Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf > Of Onur Uncu > Sent: Thursday, December 19, 2013 11:05 AM > To: r-help at r-project.org > Subject: [R] A function which is a sum of other functions... > > > Dear R Users > > I have a list of functions. Each function in the list is a function of single variable. I would > like to create a function (of one variable) which represents the sum of all the functions in > the list. So, if the functions in my list are f1(x),..,f5(x) then I would like a new function > f(x)=f1(x)+f2(x)+...f5(x) > > Appreciate any suggestions on how to do this. > > I need the above f(x) function because I would like to minimise it with respect to x using > the nlm function. > > Thanks. > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
On Dec 19, 2013, at 11:05 AM, Onur Uncu wrote:> > Dear R Users > > I have a list of functions. Each function in the list is a function of single variable. I would like to create a function (of one variable) which represents the sum of all the functions in the list. So, if the functions in my list are f1(x),..,f5(x) then I would like a new function f(x)=f1(x)+f2(x)+...f5(x) > > Appreciate any suggestions on how to do this. > > I need the above f(x) function because I would like to minimise it with respect to x using the nlm function.> fbig <- function(x, f1=I, f2=I, f3=I, f4=I, f5=I){f1(x)+f2(x)+f3(x)+f4(x)+f5(x)}> fbig(2)[1] 10> fbig(2, exp)[1] 15.38906> fbig(2, exp, log)[1] 14.0822 Since "+" is vectorized this should not need to be wrapped in sapply as long as each function is itself vectorized. David Winsemius Alameda, CA, USA
My first thought was to use Reduce, but I think for this case that is a bit of overkill. You can have a vector or list of functions and just use sapply/lapply on the list of functions then sum the result. A quick example:> funs <- c(sin,cos,tan) > sapply( funs, function(f) f(pi/6) )[1] 0.5000000 0.8660254 0.5773503> sum(sapply( funs, function(f) f(pi/6) ))[1] 1.943376 Just wrap the above in a function with whatever options you want to use. If you need the functions to return vectors (of the same length) then you can still use sapply, but use rowSums, colSums, or apply on the result instead of sum. On Thu, Dec 19, 2013 at 12:05 PM, Onur Uncu <onuruncu at gmail.com> wrote:> > Dear R Users > > I have a list of functions. Each function in the list is a function of single variable. I would like to create a function (of one variable) which represents the sum of all the functions in the list. So, if the functions in my list are f1(x),..,f5(x) then I would like a new function f(x)=f1(x)+f2(x)+...f5(x) > > Appreciate any suggestions on how to do this. > > I need the above f(x) function because I would like to minimise it with respect to x using the nlm function. > > Thanks. > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Gregory (Greg) L. Snow Ph.D. 538280 at gmail.com