# Re: [R] make a triple summation more efficient

From: Charles C. Berry <cberry_at_tajo.ucsd.edu>
Date: Sat, 15 Nov 2008 10:13:12 -0800

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
> 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

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. Received on Sat 15 Nov 2008 - 18:15:26 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 15 Nov 2008 - 18:30:26 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.