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(Matrix) + library(FeatureHashing) + contr <- list( + Plant = contrasts(CO2$Plant, contrasts = FALSE), + Type = contrasts(CO2$Type, contrasts = FALSE), + Treatment = contrasts(CO2$Treatment, contrasts = FALSE) + ) + m1 <- sparse.model.matrix(~ ., CO2, contr, transpose = TRUE) + m2 <- m1 + class(m2) <- "CSCMatrix" + + r1 <- as.vector(m1[1,]) + r2 <- FeatureHashing:::.selectRow(m1, 1) + checkTrue(isTRUE(all.equal(r1, r2)), + "The result of C++ .selectRow is incorrect") + r3 <- m2[1.0, ] + checkTrue(isTRUE(all.equal(r1, r3)), + "The result of operator `[` is incorrect at first argument") + + r1 <- as.vector(m1[,1]) + r2 <- FeatureHashing:::.selectColumn(m1, 1) + checkTrue(isTRUE(all.equal(r1, r2)), + "The result of C++ .selectColumn is incorrect") + r3 <- m2[,1] + checkTrue(isTRUE(all.equal(r1, r3)), + "The result of `[` is incorrect at second argument") + + for(i in 1:10) { + j <- sample(1:nrow(m1), 1) + r1 <- as.vector(m1[j,]) + r2 <- FeatureHashing:::.selectRow(m1, j) + checkTrue(isTRUE(all.equal(r1, r2)), + "The result of C++ .selectRow is incorrect") + r3 <- m2[j,] + checkTrue(isTRUE(all.equal(r1, r3)), + "The result of operator `[` is incorrect at first argument") + + j <- sample(1:ncol(m1), 1) + r1 <- as.vector(m1[,j]) + r2 <- FeatureHashing:::.selectColumn(m1, j) + checkTrue(isTRUE(all.equal(r1, r2)), + "The result of C++ .selectColumn is incorrect") + r3 <- m2[,j] + checkTrue(isTRUE(all.equal(r1, r3)), + "The result of `[` is incorrect at second argument") + } + + r1 <- as.matrix(m1[1:2,]) + attr(r1, "dimnames") <- NULL + r2 <- FeatureHashing:::.selectRow(m1, 1:2) + checkTrue(isTRUE(all.equal(r1, r2))) + m2 <- m1 + class(m2) <- "CSCMatrix" + r3 <- m2[1:2, drop = FALSE] + checkTrue(isTRUE(all.equal(r1, local({ + retval <- as(as(r3, "dgCMatrix"), "matrix") + attr(retval, "dimnames") <- NULL + retval + })))) + checkTrue(isTRUE(all.equal(r1, as(r3, "matrix")))) + r4 <- m2[1:2,] + checkTrue(isTRUE(all.equal(r1, r4))) + + r1 <- as.matrix(m1[,1:2]) + attr(r1, "dimnames") <- NULL + r2 <- FeatureHashing:::.selectColumn(m1, 1:2) + checkTrue(isTRUE(all.equal(r1, r2))) + r3 <- m2[, 1:2, drop = FALSE] + checkTrue(isTRUE(all.equal(r1, local({ + retval <- as(as(r3, "dgCMatrix"), "matrix") + attr(retval, "dimnames") <- NULL + retval + })))) + checkTrue(isTRUE(all.equal(r1, as(r3, "matrix")))) + r4 <- m2[,1:2] + checkTrue(isTRUE(all.equal(r1, r4))) + + + r1 <- as.matrix(m1[i.set <- c(1, 3, 4, 16, 9),]) + attr(r1, "dimnames") <- NULL + r2 <- FeatureHashing:::.selectRow(m1, i.set) + checkTrue(isTRUE(all.equal(r1, r2))) + r3 <- m2[i.set, drop = FALSE] + checkTrue(isTRUE(all.equal(r1, local({ + retval <- as(as(r3, "dgCMatrix"), "matrix") + attr(retval, "dimnames") <- NULL + retval + })))) + checkTrue(isTRUE(all.equal(r1, as(r3, "matrix")))) + r4 <- m2[i.set,] + checkTrue(isTRUE(all.equal(r1, r4))) + + r1 <- as.matrix(m1[, j.set <- c(1, 3, 4, 16, 9)]) + attr(r1, "dimnames") <- NULL + r2 <- FeatureHashing:::.selectColumn(m1, j.set) + checkTrue(isTRUE(all.equal(r1, r2))) + r3 <- m2[, j.set, drop = FALSE] + checkTrue(isTRUE(all.equal(r1, local({ + retval <- as(as(r3, "dgCMatrix"), "matrix") + attr(retval, "dimnames") <- NULL + retval + })))) + checkTrue(isTRUE(all.equal(r1, as(r3, "matrix")))) + r4 <- m2[,j.set] + checkTrue(isTRUE(all.equal(r1, r4))) + + } Loading required package: RUnit [1] TRUE > > proc.time() user system elapsed 1.51 0.28 1.76