Skip to content
Prev 120839 / 398498 Next

code optimization tips

The quote "what is the problem you are trying to solve" is just part
of my signature.  I used to review projects for performance and
architecture and that was the first question I always asked them.

To pass the argument, if you notice the definition of apply:

apply(X, MARGIN, FUN, ...)

the ... are optional argument, so for your function:

sij <-function(rj,ri,k){
    rij=mod(rj-ri)
    cos_ij=rj[1]/rij
    sin_ij=rj[2]/rij
    A<-(1-1i*k*rij)*(3*cos_ij^2-1)*exp(1i*k*rij)/(rij^3)
    B<-k^2*sin_ij^2*exp(1i*k*rij)/rij
    sij<-A+B
}

you would call apply with the following:

s_ij<-apply(rj,2,sij, ri=ri, k=k)
On 7/23/07, baptiste Augui? <ba208 at exeter.ac.uk> wrote: