[R] Need help writing a faster code

Ravi Varadhan rvaradhan at jhmi.edu
Thu Feb 1 17:08:00 CET 2007


Thank you, Dimitris and Robin.  

Dimitris - your solution(s) works very well.  Although my "g" function is a
lot more complicated than that in the simple example that I gave, I think
that I can use your idea of taking the whole matrix inside the function and
working directly with it.

Robin - using two applys doesn't make the code any faster, it just produces
a compact one-liner.

Best,
Ravi.
----------------------------------------------------------------------------
-------

Ravi Varadhan, Ph.D.

Assistant Professor, The Center on Aging and Health

Division of Geriatric Medicine and Gerontology 

Johns Hopkins University

Ph: (410) 502-2619

Fax: (410) 614-9625

Email: rvaradhan at jhmi.edu

Webpage:  http://www.jhsph.edu/agingandhealth/People/Faculty/Varadhan.html

 

----------------------------------------------------------------------------
--------

-----Original Message-----
From: Dimitris Rizopoulos [mailto:dimitris.rizopoulos at med.kuleuven.be] 
Sent: Thursday, February 01, 2007 10:33 AM
To: Ravi Varadhan
Cc: r-help at stat.math.ethz.ch
Subject: Re: [R] Need help writing a faster code

the following seems to be a first improvement:

m <- 2000
n <- 5000
A <- matrix(rnorm(2*m), ncol=2)
B <- matrix(rnorm(2*n), ncol=2)
W1 <- W2 <- matrix(0, m, n)

##############################
##############################

g1 <- function(x, y){
    theta <- atan((y[2] - x[2]) / (y[1] - x[1]))
    theta + 2*pi*(theta < 0)
}

invisible({gc(); gc()})
system.time(for (i in 1:m) {
    W1[i, ] <- apply(B, 1, y = A[i,], function(x, y) g1(y, x))
})

##############################

g2 <- function(x){
    out <- tB - x
    theta <- atan(out[2, ] / out[1, ])
    theta + 2*pi*(theta < 0)
}

tB <- t(B)
invisible({gc(); gc()})
system.time(for (i in 1:m) {
    W2[i, ] <- g2(A[i, ])
})

## or

invisible({gc(); gc()})
system.time(W3 <- t(apply(A, 1, g2)))

all.equal(W1, W2)
all.equal(W1, W3)


I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm


----- Original Message ----- 
From: "Ravi Varadhan" <rvaradhan at jhmi.edu>
To: <r-help at stat.math.ethz.ch>
Sent: Thursday, February 01, 2007 4:10 PM
Subject: [R] Need help writing a faster code


> Hi,
>
>
>
> I apologize for this repeat posting, which I first posted yesterday. 
> I would
> appreciate any hints on solving this problem:
>
>
>
> I have two matrices A (m x 2) and B (n x 2), where m and n are large
> integers (on the order of 10^4).  I am looking for an efficient way 
> to
> create another matrix, W (m x n), which can be defined as follows:
>
>
>
> for (i in 1:m){
>
> for (j in 1:n) {
>
> W[i,j] <- g(A[i,], B[j,])
>
> } }
>
> where g(x,y) is a function that takes two vectors and returns a 
> scalar.
>
>
>
> The following works okay, but is not fast enough for my purpose.  I 
> am sure
> that I can do better:
>
>
>
> for (i in 1:m) {
>
> W[i,] <- apply(B, 1, y=A[i,], function(x,y) g(y,x))
>
> }
>
>
>
> How can I do this in a faster manner? I attempted "outer", 
> "kronecker",
> "expand.grid", etc, but with no success.
>
>
>
> Here is an example:
>
>
>
> m <- 2000
>
> n <- 5000
>
> A <- matrix(rnorm(2*m),ncol=2)
>
> B <- matrix(rnorm(2*n),ncol=2)
>
> W <- matrix(NA, m, n)
>
>
>
> for (i in 1:m) {
>
> W[i,] <- apply(B, 1, y=A[i,], function(x,y) g(y,x))
>
> }
>
>
>
> g <- function(x,y){
>
> theta <- atan((y[2]-x[2]) / (y[1] - x[1]))
>
> theta + 2*pi*(theta < 0)
>
> }
>
>
>
> Thanks for any suggestions.
>
>
>
> Best,
>
> Ravi.
>
>
>
>
>
>
>
>
----------------------------------------------------------------------------
> -------
>
> Ravi Varadhan, Ph.D.
>
> Assistant Professor, The Center on Aging and Health
>
> Division of Geriatric Medicine and Gerontology
>
> Johns Hopkins University
>
> Ph: (410) 502-2619
>
> Fax: (410) 614-9625
>
> Email: rvaradhan at jhmi.edu
>
> Webpage: 
> http://www.jhsph.edu/agingandhealth/People/Faculty/Varadhan.html
>
>
>
>
----------------------------------------------------------------------------
> --------
>
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
> 


Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm



More information about the R-help mailing list