diff --git a/R/ind2subv.R b/R/ind2subv.R index 92b4943..7981170 100644 --- a/R/ind2subv.R +++ b/R/ind2subv.R @@ -21,7 +21,7 @@ ind2subv <- function(siz, ndx) { } if (all(siz == 2)) { - sub <- dec2bitv(ndx - 1, n) + sub <- t(vapply(ndx, function(x) dec2bitv(x - 1, n), c(0, 0, 0))) sub <- sub[, seq(n, 1, - 1)] + 1 return(sub) } @@ -31,7 +31,7 @@ ind2subv <- function(siz, ndx) { sub <- zeros(length(ndx), n) for (i in seq(n, 1, -1)) {# i'th digit sub[, i] <- floor(ndx / cp[i]) + 1 - ndx <- ndx %% cp(i) + ndx <- ndx %% cp[i] } return(sub) } diff --git a/tests/testthat/test-spatialMixture.R b/tests/testthat/test-spatialMixture.R index 47c63ca..d07881a 100644 --- a/tests/testthat/test-spatialMixture.R +++ b/tests/testthat/test-spatialMixture.R @@ -58,7 +58,14 @@ test_that("lakseKlitik() and subfunctions produce expected output", { expect_equal(dec2bitv(10, 1), c(1, 0, 1, 0)) expect_equal(dec2bitv(10, 5), c(0, 1, 0, 1, 0)) expect_equal(dec2bitv(10, 10), c(0, 0, 0, 0, 0, 0, 1, 0, 1, 0)) - # TODO: test ind2subv() + expect_equal( + ind2subv(c(2, 2, 2), 1:8), + matrix(c(rep(1:2, 4), rep(1:2, 2, each = 2), rep(1:2, 1, each = 4)), 8) + ) + expect_equal( + ind2subv(c(1, 2, 4, 3), 1:5), + matrix(c(rep(1, 5), 1, 2, 1, 2, 1, 1, 1, 2, 2, 3, rep(1, 5)), ncol = 4) + ) # TODO: test argmin() # TODO: test elim_order() # TODO: test triangulate()