# [R] distances between points in R^3

From: baptiste Auguié <ba208_at_exeter.ac.uk>
Date: Sun, 3 Feb 2008 18:55:30 +0000

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.

baptiste

Baptiste Auguié

Physics Department
University of Exeter
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

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. Received on Mon 04 Feb 2008 - 07:40:47 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 05 Feb 2008 - 08:30:11 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.