Skip to content

Instantly share code, notes, and snippets.

@nightuser
Last active August 29, 2015 14:12
Show Gist options
  • Save nightuser/753752db26dbf9fc358a to your computer and use it in GitHub Desktop.
Save nightuser/753752db26dbf9fc358a to your computer and use it in GitHub Desktop.
Toroidal (R)
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