[R] distances between points in R^3

baptiste Auguié ba208 at exeter.ac.uk
Mon Feb 4 20:10:15 CET 2008

```OK, I've come across a partial answer to the for loop problem in the
use of "dist()". However, my problem not only require to find the
distances between points, but also to store the vector differences,
and the normal to each face when i get the delaunay triangulation
sorted.

As i understand, the dist() function calls a C routine, is there a
straight-forward way to find / edit / modify it to serve this
specific purpose?

Thanks,

baptiste

On 3 Feb 2008, at 18:55, baptiste Auguié wrote:

> 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.
>
>
>
>
> baptiste
> _____________________________
>
> Baptiste Auguié
>
> Physics Department
> University of Exeter
> Exeter, Devon,
> EX4 4QL, UK
>
> Phone: +44 1392 264187
>
> http://newton.ex.ac.uk/research/emag
> http://projects.ex.ac.uk/atto
> ______________________________
>
>
>
>
>

_____________________________

Baptiste Auguié

Physics Department
University of Exeter