On Sat, 15 Nov 2008, kathie wrote:
>
> Dear R users...
>
>
> I made the R-code for this triple summation computation
>
> http://www.nabble.com/file/p20517134/a.jpg
>
> -------------------------------------------------
> Here is my code..
>
>
> x=seq(.1,1,.1); l=10
> y=seq(1,10); m=10
> z=seq(.1,1,.1); n=10
>
> sum(sapply(1:l, function(i) {sum(sapply(1:m, function(j) {sum(sapply(1:n,
> function(k){exp(x[i]*y[j]*z[k] )/gamma(y[j]+1)}))^(1.5) }))}))
>
> -------------------------------------------------
>
> In fact, this is a part of optimization and in my real data the number of x
> is about 5000. So, I guess if it is vectorized, then the running time could
> be shortened.
>
> How could I vectorize this? or any suggestion will be greatly appreciated.
gamma and exp ARE vectorized. Do all the computations in the inner most
loop in a single call, then sum those, then raise to 1.5, then sum the
result
something like
> foo <- function(x,y,z) {exp( x * y * z )/gamma( y+1 )}
> dat <- expand.grid( x=x, y=y, z=z )
> res <- do.call( foo, dat )
> sum( rowSums( matrix( res, nc= n) )^1.5 )
[1] 1090.613
HTH,
Chuck
>
> Kathryn Lord
> --
> View this message in context:
http://www.nabble.com/make-a-triple-summation-more-efficient-tp20517134p20517134.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>
Charles C. Berry (858) 534-2098
Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901