
Hil Lyons
STAT 534
Final Project
1) R Code for compute.mst.and.diameter and fit.and.plot.pc
############################################################
#compute.mst.and.diameter
#
#This function calls a C function to do the MST and diameter
#calculations, wrapper_mst_and_diameter.
#As such, compute.mst.and.diameter does very little except
#clean up the output of the C function.
#
############################################################
compute.mst.and.diameter <- function(obs){
X1 <- as.vector(obs[,1])
X2 <- as.vector(obs[,2])
nobs <- length(X1)
MST.edge.vtx1 <- rep(0, nobs - 1)
MST.edge.vtx2 <- rep(0, nobs - 1)
diameter.indeces <- rep(0, nobs)
res <- .C("wrapper_mst_and_diameter", as.double(X1), as.double(X2),
as.integer(nobs), as.integer(MST.edge.vtx1), as.integer(MST.edge.vtx2),
as.integer(diameter.indeces))
MST.edges = cbind(res[[4]], res[[5]])
diameter = as.vector(res[[6]][res[[6]] > 0])
return(list(MST.edges = MST.edges, diameter = diameter))
}
############################################################
#diameter.distance is a simple function calculating the
#Euclidean length of the diameter. It is called from
#fit.and.plot.pc
#
############################################################
diameter.distance <- function(obs, diameter){
numpts <- length(diameter)
diam1 <- diameter[1:(numpts-1)]
diam2 <- diameter[2:numpts]
distance <- sum(sqrt((obs[diam2,1]-obs[diam1,1])^2 +
(obs[diam2,2]-obs[diam1,2])^2))
return(distance)
}
############################################################
#fit.and.plot.pc
#
#This function calls multiple functions that have been
#provided and developed over the course of the quarter.
#
#compute.mst.and.diameter (see above)
#position.inital.vertices
#fit.polyline (from HW1)
#
Hil Lyons Page 1 6/01/2006