matrix - Euclidean distance between data sets in R using rdist from "fields" package -
i using rdist compute euclidean distances between matrix , itself:
> m = matrix(c(1,1,1,2,2,2,3,4,3),nrow=3, ncol=3) > m [,1] [,2] [,3] [1,] 1 2 3 [2,] 1 2 4 [3,] 1 2 3 library(fields) > rdist(m) [,1] [,2] [,3] [1,] 1e-10 1e+00 1e-10 [2,] 1e+00 1e-10 1e+00 [3,] 1e-10 1e+00 1e-10 what confuses me think should have 0s on diagonal (surely distance of vector 0?), , same reason should have 0s compares first , third row. value see instead (1e-10) looks way big numerical noise. what's going wrong?
edit: rdist package fields.
first of 1e-10 1*10^-10 0.0000000001, numericaly close 0 (as result of square rooting, actual error in computation of row of magnitude 1e-20). "too big"? well, library written in fortran, , focused on speed, quite acceptable. if analyze exact code, find out how computed:
# fields, tools spatial data # copyright 2004-2011, institute mathematics applied geosciences # university corporation atmospheric research # licensed under gpl -- www.gpl.org/licenses/gpl.html "rdist" <- function(x1, x2) { if (!is.matrix(x1)) x1 <- as.matrix(x1) if (missing(x2)) x2 <- x1 if (!is.matrix(x2)) x2 <- as.matrix(x2) d <- ncol(x1) n1 <- nrow(x1) n2 <- nrow(x2) par <- c(1/2, 0) temp <- .fortran("radbas", nd = as.integer(d), x1 = as.double(x1), n1 = as.integer(n1), x2 = as.double(x2), n2 = as.integer(n2), par = as.double(par), k = as.double(rep(0, n1 * n2)))$k return(matrix(temp, ncol = n2, nrow = n1)) } and exact answer hidden in fortran files (in radfun.f called radbas.f), can find line
if( dtemp.lt.1e-20) dtemp =1e-20 which treats small (even 0) values 1e-20, after taking square root results in 1e-10. seems motivation speed process using logarithm of value (as result, square rooting dividing 2), of course not defined 0.
Comments
Post a Comment