[R] more efficient double summation...

Giovanni Petris GPetris at uark.edu
Fri Aug 29 20:39:15 CEST 2008


You can cut execution time by a factor 2 simply using the fact that the
double summation is symmetric in the indices j and k:

2 * sum(sapply(1:(m-1), function(k){sum(sapply((k-1):m,
  function(j){x[k]*x[j]*dnorm((mu[j]+mu[k])/sqrt(sig[k]+sig[j]))/sqrt(sig[k]+sig[j])}))}))
  + sum(x^2*dnorm((2*mu)/sqrt(2*sig))/ sqrt(2*sig))

Best,
Giovanni Petris

> Date: Thu, 28 Aug 2008 21:47:38 -0700 (PDT)
> From: kathie <kathryn.lord2000 at gmail.com>
> Sender: r-help-bounces at r-project.org
> Precedence: list
> 
> 
> Dear R users...
> 
> I made the R-code for this double summation computation
> 
> 
> http://www.nabble.com/file/p19213599/doublesum.jpg 
> 
> -------------------------------------------------
> Here is my code..
> 
> 
> sum(sapply(1:m, function(k){sum(sapply(1:m,
> function(j){x[k]*x[j]*dnorm((mu[j]+mu[k])/sqrt(sig[k]+sig[j]))/sqrt(sig[k]+sig[j])}))}))
> 
> 
> -------------------------------------------------
> 
> In fact, this is a part of optimization. I think if it is changed more
> efficiently, then the running time could be shortened.
> 
> How could I change this to more efficiently? Any suggestion will be greatly
> appreciated.
> 
> Kathryn Lord
> -- 
> View this message in context: http://www.nabble.com/more-efficient-double-summation...-tp19213599p19213599.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.
> 
> 

-- 

Giovanni Petris  <GPetris at uark.edu>
Associate Professor
Department of Mathematical Sciences
University of Arkansas - Fayetteville, AR 72701
Ph: (479) 575-6324, 575-8630 (fax)
http://definetti.uark.edu/~gpetris/



More information about the R-help mailing list