R Under development (unstable) (2024-01-07 r85787 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > if (require(RUnit)) { + library(FeatureHashing) + + i <- sample(1:10, 10, TRUE) + x <- rnorm(10) + + i.bak <- integer(10) + x.bak <- numeric(10) + for(.i in seq_along(i)) { + i.bak[.i] <- i[.i] + x.bak[.i] <- x[.i] + } + i.bak + FeatureHashing:::pair_sort(i, x) + checkTrue(all.equal(x, x.bak[order(i.bak)]), + "The result of pair_result is incorrect") + checkTrue(all(diff(i) >= 0), + "The result of pair_result is incorrect") + + i <- sample(1:10, 10, TRUE) + x <- rnorm(10) + + FeatureHashing:::pair_sort(i, x) + i.bak <- integer(10) + x.bak <- numeric(10) + for(.i in seq_along(i)) { + i.bak[.i] <- i[.i] + x.bak[.i] <- x[.i] + } + + tmp <- FeatureHashing:::merge(i, x) + checkTrue(isTRUE(all.equal(i[1:tmp], sort(unique(i.bak)))), + "The result of C++ pair_sort and merge is incorrect") + checkTrue(isTRUE(all.equal(x[1:tmp], local({ + retval <- sapply(split(x.bak, i.bak), sum) + names(retval) <- NULL + retval + }))), + "The result of C++ pair_sort and merge is incorrect") + + contr <- list( + Plant = contrasts(CO2$Plant, contrasts = FALSE), + Type = contrasts(CO2$Type, contrasts = FALSE), + Treatment = contrasts(CO2$Treatment, contrasts = FALSE) + ) + + m4 <- hashed.model.matrix(~ ., data = CO2, hash.size = 2^4, + transpose = TRUE, is.dgCMatrix = FALSE) + + m5 <- as(m4, "dgCMatrix") + m6 <- hashed.model.matrix(~ ., data = CO2, hash.size = 2^4, + transpose = TRUE) + invisible(capture.output(print(m5))) + stopifnot(isTRUE(all.equal(dim(m4), dim(m5)))) + stopifnot(isTRUE(all.equal(sum(m4@x), sum(m5@x)))) + stopifnot(isTRUE(all.equal(dim(m4), dim(m6)))) + stopifnot(isTRUE(all.equal(sum(m4@x), sum(m6@x)))) + for(i in seq_len(dim(m4)[2])) { + e1 <- numeric(dim(m4)[2]) + e1[i] <- 1.0 + r4 <- m4 %*% e1 + r5 <- as.vector(m5 %*% e1) + r6 <- as.vector(m6 %*% e1) + stopifnot(isTRUE(all.equal(r4, r5))) + stopifnot(isTRUE(all.equal(r4, r6))) + } + + for(i in seq_len(dim(m4)[1])) { + e1 <- numeric(dim(m4)[1]) + e1[i] <- 1.0 + r4 <- e1 %*% m4 + r5 <- as.vector(e1 %*% m5) + r6 <- as.vector(e1 %*% m6) + stopifnot(isTRUE(all.equal(r4, r5))) + stopifnot(isTRUE(all.equal(r4, r6))) + } + } Loading required package: RUnit [[ suppressing 84 column names '1', '2', '3' ... ]] > > proc.time() user system elapsed 1.64 0.18 1.81