Matlab 到 R
# start d2xy
d2xy <- function (m, d)
{
m <- as.integer(m)
d <- as.integer(d)
n <- 2^m
x <- 0
y <- 0
t <- d
s <- 1
while ( s < n ){
rx <- floor ( t / 2 ) %% 2
if ( rx == 0 ){
ry <- t %% 2
} else {
ry <- bitwXor(as.integer(t), as.integer(rx)) %% 2
}
xy <- rot ( s, x, y, rx, ry )
x <- xy['x'] + s * rx
y <- xy['y'] + s * ry
t <- floor ( t / 4 )
s <- s * 2
}
return(c(x = x, y = y))
}
# end d2xy
# start rot
rot <- function(n, x, y, rx, ry)
{
n <- as.integer(n)
x <- as.integer(x)
y <- as.integer(y)
rx <- as.integer(rx)
ry <- as.integer(ry)
if ( ry == 0 ){
if ( rx == 1 ){
x <- n - 1 - x
y <- n - 1 - y
}
t <- x
x <- y
y <- t
}
return(c(x = x, y = y))
}
# end rot
在 R 中测试上述函数
# vectorize our translated R function
d2xy_R <- Vectorize(d2xy, c('m', 'd'))
rm(d2xy)
将 matlab 与 R 翻译代码与 matlab 函数进行比较
set.seed(1L)
m <- 2
d <- 5
xx <- runif(n = m*d, min = 0, max = 1)
mat_R <- d2xy_R(m = m, d = 1:d)
mat_R
# [,1] [,2] [,3] [,4] [,5]
# x.x 1 1 0 0 0
# y.y 0 1 1 2 3
将mat_R
输出与matlab
输出进行比较。两者相同,因此翻译没有问题。
mat_R <- mat_R + 1
coord2D_R <- matrix(xx[mat_R], nrow = m, ncol = d)
rownames(coord2D_R) <- c('x', 'y')
coord2D_R
# [,1] [,2] [,3] [,4] [,5]
# x 0.3721239 0.3721239 0.2655087 0.2655087 0.2655087
# y 0.2655087 0.3721239 0.3721239 0.5728534 0.9082078
绘制希尔伯特曲线
set.seed(1L)
m <- 2
d <- 50
xx <- runif(n = m*d, min = 0, max = 1)
mat_R <- d2xy_R(m = m, d = 1:d)
mat_R <- mat_R + 1
coord2D_R <- matrix(xx[mat_R], nrow = m, ncol = d)
rownames(coord2D_R) <- c('x', 'y')
plot(t(coord2D_R), type = 'l', col = 'red')
将 matlab 和 R 翻译代码与@hrbrmstr 的 github hilbert 包进行比较
从 hrbrmstr github hilbert 包中获取hilbert.cpp文件
library('Rcpp')
sourceCpp("hilbert.cpp") # compile C++ functions in hilbert.cpp file
d2xy_Rcpp <- d2xy
rm(d2xy)
mat_Rcpp <- matrix(nrow = m, ncol = d)
rownames(mat_Rcpp) <- c('x', 'y')
for(i in seq_len(d)){ # for loop is introduced, because unlike the R translated code, the Rcpp function is not vectorized
xy <- d2xy_Rcpp(n = m, d = i)
mat_Rcpp['x', i] <- xy['x']
mat_Rcpp['y', i] <- xy['y']
}
mat_Rcpp
# [,1] [,2] [,3] [,4] [,5]
# [1,] 0 1 1 0 0
# [2,] 1 1 0 0 1
将mat_Rcpp
输出与mat_R
输出进行比较matlab
。它与它们不匹配,因此此包中可能存在错误或提供的 matlab 代码存在问题。
mat_Rcpp <- mat_Rcpp + 1
coord2D_Rcpp <- matrix(xx[mat_Rcpp], nrow = m, ncol = d)
rownames(coord2D_Rcpp) <- c('x', 'y')
coord2D_Rcpp
# [,1] [,2] [,3] [,4] [,5]
# x 0.2655087 0.3721239 0.3721239 0.2655087 0.2655087
# y 0.3721239 0.3721239 0.2655087 0.2655087 0.3721239
使用 hrbrmstr 的 hilbert 包对 matlab 到 R 翻译的代码进行基准测试
library('microbenchmark')
set.seed(1L)
m <- 2
d <- 5
xx <- runif(n = m*d, min = 0, max = 1)
microbenchmark(d2xy_R(m = m, d = d), # matlab to R translation
d2xy_Rcpp(n = m, d = d), # @hrbrmstr - hilbert github package
times = 100000)
# Unit: microseconds
# expr min lq mean median uq max neval
# d2xy_R(m = m, d = d) 169.382 177.534 192.422166 180.252 184.780 94995.239 1e+05
# d2xy_Rcpp(n = m, d = d) 2.718 4.530 7.309071 8.606 9.512 2099.603 1e+05