[R] Creating a "very long" function in R

David Winsemius dwinsemius at comcast.net
Sun Jun 1 01:24:32 CEST 2014


On May 31, 2014, at 3:17 PM, Yuriy Strilets wrote:

> Dear All,
> 
> 
> 
> I would appreciate if someone could give any hint/ provide a solution if
> it's possible in R to create such a function where many similar
> mathematical expressions organized in a given way.
> 
> For example, to create a function: f(x)=(...(((x^x)^x)^x)...^x)^x (x is
> encountered n times). Or another example:
> g(x)=sin(x)*sin(x^2)*sin(x^3)*...*sin(x^n).

You should look at:

?funprog


There are several example of repeated applications of a functional argument. Also composition of list of functions.

One instance:

Iterate <- function(f, n = 1)
    function(x) Reduce(Funcall, rep.int(list(f), n), x, right = TRUE)

Iterate(function(x) x^1.1, 30)(1.01)

-- 
David
> 
> 
> When we are interested in a computation of any specific value of that
> function given a particular value of x, there is no any issue, and
> corresponding function value is computed in a straightforward way. For
> example of f(x) defined above:
> 
> f<-function(x, n) {
> 
> p<-x
> 
> for (i in 1:n) {
> 
> p<-p^x
> 
> }
> 
> print(p)
> 
> }
> 
> 
> 
> The issue is when we want to use a function "in general" with no particular
> x given.
> 
> 
> The whole question can be converted to a very short question (solution for
> which one is assumed to be a solution for primary question):
> 
> - to integrate a function  f(x)=(...(((x^x)^x)^x)...^x)^x (x is encountered
> n times) on the interval (0,1).
> 
> 
> 
> Thanks a lot in advance for any ideas.
> 
> I really believe that this doable with R syntax.
> 
> 
> 
> Best Regards,
> 
> Yuriy
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list