amurta@ipimar.pt
2002-Feb-01 15:49 UTC
[Rd] Different behaviour in Linux and Windows (PR#1293)
Hi
I have written a small function to calculate distances between pairs of
landmarks ('my.dist' bellow). I gave it to a colleague that works with
MSWindows and the function doesn't give the right answer:
R 1.4.1 for Linux:
> my.dist()
[1] 22 561
R 1.4.1 for Windows:
> my.dist()
[1] 22 560
Warning message:
Replacement length not a multiple of the elements to replace in
matrix(...)
FYI: S+ 2000 pro. rel.2 for Windows and S+ 6.0 for Linux both give the
same (wrong) answer as R 1.4.1 for Windows.
----------------------------------------------
xy3 <- matrix(round(100*runif(22*68)),22,68)
my.dist <- function(x = xy3){
landmarks.x <- as.matrix(x[, 1:34])
landmarks.y <- as.matrix(x[, 35:68])
x1 <- c()
x2 <- c()
y1 <- c()
y2 <- c()
for(i in 1:(ncol(landmarks.x) - 1)) {
x1 <- c(x1, rep(landmarks.x[, i], ncol(landmarks.x) - i))
x2 <- c(x2, c(landmarks.x[, - (1:i)]))
y1 <- c(y1, rep(landmarks.y[, i], ncol(landmarks.x) - i))
y2 <- c(y2, c(landmarks.y[, - (1:i)]))
}
distan <- sqrt((x1 - x2)^2 + (y1 - y2)^2)
comb <- prod(ncol(landmarks.x):1)/(2 * prod((ncol(landmarks.x) - 2):1))
dist.out <- matrix(distan, nrow(x), comb)
dim(dist.out)
}
--
Alberto G. Murta
IPIMAR - Institute of Fisheries and Sea Research
Avenida de Brasilia, 1449-006 Lisboa, Portugal
Tel:+351 213027062; Fax:+351 213015849; http://www.ipimar.pt/pelagicos/
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To:
r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
ripley@stats.ox.ac.uk
2002-Feb-01 16:16 UTC
[Rd] Different behaviour in Linux and Windows (PR#1293)
1) This is a random input, which might make it non-reproducible. 2) Solaris 2.7 gives 560 too. You appear to be trying to calculate 34!/2*32! in a *very* inefficient way, and then not rounding it. And your answer depends on comb being an integer. So use (34*33)/2 In summary: you have used an unstable algorithm, and the bug is using that algorithm. For only slightly large inputs it would fail altogether. On Fri, 1 Feb 2002 amurta@ipimar.pt wrote:> Hi > > I have written a small function to calculate distances between pairs of > landmarks ('my.dist' bellow). I gave it to a colleague that works with > MSWindows and the function doesn't give the right answer: > > R 1.4.1 for Linux: > > > my.dist() > [1] 22 561 > > R 1.4.1 for Windows: > > > my.dist() > [1] 22 560 > Warning message: > Replacement length not a multiple of the elements to replace in > matrix(...) > > > FYI: S+ 2000 pro. rel.2 for Windows and S+ 6.0 for Linux both give the > same (wrong) answer as R 1.4.1 for Windows. > > > ---------------------------------------------- > > xy3 <- matrix(round(100*runif(22*68)),22,68) > > my.dist <- function(x = xy3){ > > landmarks.x <- as.matrix(x[, 1:34]) > landmarks.y <- as.matrix(x[, 35:68]) > > x1 <- c() > x2 <- c() > y1 <- c() > y2 <- c() > > for(i in 1:(ncol(landmarks.x) - 1)) { > x1 <- c(x1, rep(landmarks.x[, i], ncol(landmarks.x) - i)) > x2 <- c(x2, c(landmarks.x[, - (1:i)])) > y1 <- c(y1, rep(landmarks.y[, i], ncol(landmarks.x) - i)) > y2 <- c(y2, c(landmarks.y[, - (1:i)])) > } > > distan <- sqrt((x1 - x2)^2 + (y1 - y2)^2) > comb <- prod(ncol(landmarks.x):1)/(2 * prod((ncol(landmarks.x) - 2):1)) > > dist.out <- matrix(distan, nrow(x), comb) > dim(dist.out) > } > > > -- > Alberto G. Murta > IPIMAR - Institute of Fisheries and Sea Research > Avenida de Brasilia, 1449-006 Lisboa, Portugal > Tel:+351 213027062; Fax:+351 213015849; http://www.ipimar.pt/pelagicos/ > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >-- Brian D. Ripley, ripley@stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._