Last active
August 29, 2015 14:12
-
-
Save nightuser/753752db26dbf9fc358a to your computer and use it in GitHub Desktop.
Toroidal (R)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
toroidal <- function(points, values) { | |
d <- ncol(points) | |
shifts <- sapply( | |
1:d, | |
function(x) { max(points[, x]) - min(points[, x]) } | |
) | |
directions <- c(0, 1, -1) | |
all_shifts <- expand.grid( | |
lapply( | |
as.list(shifts), | |
function(shift) { | |
shift * directions | |
}) | |
) | |
toroidal_points <- c() | |
for (i in 1:nrow(all_shifts)) { | |
shift <- unlist(all_shifts[i, ]) | |
toroidal_points <- rbind(toroidal_points, sweep(points, d, shift)) | |
} | |
toroidal_values <- rep(values, nrow(all_shifts)) | |
list(points = toroidal_points, values = toroidal_values) | |
} | |
d <- 2 | |
points <- c( | |
c(0, 0), | |
c(0, 2), | |
c(2, 0), | |
c(0.5, 0.5) | |
) | |
dim(points) = c(d, length(points) / d) | |
points <- t(points) | |
values <- c( | |
1, 1, 1, 4 | |
) | |
tor <- toroidal(points, values) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment