[R] Integration in R

Rui Barradas ruipbarradas at sapo.pt
Tue Oct 2 20:27:03 CEST 2012


Hello,

Em 02-10-2012 19:18, Berend Hasselman escreveu:
> On 02-10-2012, at 20:01, Rui Barradas <ruipbarradas at sapo.pt> wrote:
>
>> Hello,
>>
>> Yes, it's possible to remove the loop. Since the loop is used to compute a running product and all we want is the final result, use the vectorized behavior of R and a final ?prod().
>> Seedup: another 2x. And 4x2 == 8 == 1 [decimal] order of magnitude.
>>
>>
>> lf2 <-function (x) {
>>    v<-1
>>    x1 <- x[1]
>>    x2 <- x[2]
>>    x3 <- x[3]
>>    x4 <- x[4]
>>    z1 <- exp(x1+x2*dose)
>>    z2 <- exp(x3+x4*dose)
>>    psi0<-1/((1+z1)*(1+z2))
>>    psi1<-z1*psi0
>>    v <- (psi0^y0)*(psi1^y1)*((1-psi0-psi1)^y2)
>>    return( prod(v) )
>> }
>>
>> lf2.c <- cmpfun(lf2)
>>
>> Hope this helps,
>
> Wonderful. It certainly does help.
> A single nitpick: the v <- 1 at the start of the function can now be removed.
Yes, I thought about removing it but in the end forgot to.
>
> I got a speedup of 7.5 compared to the very first version lf1.
My system is a Windows 7, R 2.15.1.

Rui Barradas

sessionInfo()
R version 2.15.1 (2012-06-22)
Platform: i386-pc-mingw32/i386 (32-bit)

locale:
[1] LC_COLLATE=Portuguese_Portugal.1252 LC_CTYPE=Portuguese_Portugal.1252
[3] LC_MONETARY=Portuguese_Portugal.1252 LC_NUMERIC=C
[5] LC_TIME=Portuguese_Portugal.1252

attached base packages:
[1] compiler  stats     graphics  grDevices utils     datasets methods
[8] base

other attached packages:
[1] rbenchmark_1.0.0 cubature_1.1-1

loaded via a namespace (and not attached):
[1] fortunes_1.5-0 tools_2.15.1
>
> Berend
>




More information about the R-help mailing list