[R] distances between points in R^3
baptiste Auguié
ba208 at exeter.ac.uk
Sun Feb 3 19:55:30 CET 2008
Dear R helpers,
I'm trying to write a numerical scheme for a boundary integral method
to solve an electromagnetic problem. This requires the computation of
the distance between points at the surface of an object (a sphere, in
my example). Here is my code,
> require(rgl)
> r<-1
> size<-10
> theta<-seq(0,2*pi,length=size*2)
> phi<-seq(0,pi,length=size)
> pc = as.matrix(rbind(expand.grid(theta,phi)))
> x<- r* sin(pc[,2]) * cos(pc[,1])
> y<- r* sin(pc[,2]) * sin(pc[,1])
> z<- r* cos(pc[,2])
>
> plot3d(x, y, z, col=rainbow(1000), size=2,zlim=c(-1,1)) #
> scatterplot of points on a sphere
>
> df<- unique(rbind(x,y,z), MARGIN = 2 ) # removes duplicates in
> cartesian coordinates
> dimension <- dim(df)[1]
> matDistances <- array(data=0,dim=c(dimension,dimension))
>
> norm <- function(a) sqrt(a %*% a)
>
> for (ii in 1:dimension){
> for (jj in ii:dimension){
> matDistances[ii,jj]<- norm( df[,ii] - df[,jj])
> }
> }
This is both inefficient and ugly, I'll welcome any suggestion. In
particular:
- the location of the points on the sphere is not ideal (understand:
not uniformly distributed over the area): i looked into delaunayn
from the geometry package and qhull.com but at best I obtained a
random set of points on the sphere. It must be a most classical
problem –– generating a uniform distribution of points on a sphere––,
but i've set that problem aside for the moment.
- the double for loop over all the points cries out for a vectorized
solution, but i can't really think of any.
Many thanks in advance,
baptiste
_____________________________
Baptiste Auguié
Physics Department
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK
Phone: +44 1392 264187
http://newton.ex.ac.uk/research/emag
http://projects.ex.ac.uk/atto
More information about the R-help
mailing list