|
| 1 | + |
| 2 | +## Copyright (C) 2010 - 2019 Dirk Eddelbuettel, Romain Francois and Kevin Ushey |
| 3 | +## |
| 4 | +## This file is part of Rcpp. |
| 5 | +## |
| 6 | +## Rcpp is free software: you can redistribute it and/or modify it |
| 7 | +## under the terms of the GNU General Public License as published by |
| 8 | +## the Free Software Foundation, either version 2 of the License, or |
| 9 | +## (at your option) any later version. |
| 10 | +## |
| 11 | +## Rcpp is distributed in the hope that it will be useful, but |
| 12 | +## WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +## GNU General Public License for more details. |
| 15 | +## |
| 16 | +## You should have received a copy of the GNU General Public License |
| 17 | +## along with Rcpp. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | +.runThisTest <- Sys.getenv("RunAllRcppTests") == "yes" |
| 20 | + |
| 21 | +if (!.runThisTest) exit_file("Skipping, set 'RunAllRcppTests=yes' to run.") |
| 22 | + |
| 23 | +library(Rcpp) |
| 24 | +sourceCpp("cpp/Matrix.cpp") |
| 25 | + |
| 26 | +# test.List.column <- function() { |
| 27 | +x <- matrix( 1:16+.5, nc = 4 ) |
| 28 | +res <- runit_Row_Column_sugar( x ) |
| 29 | +target <- list(x[1,], x[,1], x[2,], x[,2], x[2,] + x[,2]) |
| 30 | +expect_equal( res, target, info = "column and row as sugar" ) |
| 31 | + |
| 32 | + |
| 33 | +# test.NumericMatrix <- function(){ |
| 34 | +x <- matrix( 1:16 + .5, ncol = 4 ) |
| 35 | +expect_equal( matrix_numeric(x), sum(diag(x)), info = "matrix indexing" ) |
| 36 | + |
| 37 | +y <- as.vector( x ) |
| 38 | +expect_error( matrix_numeric(y) , info = "not a matrix" ) |
| 39 | + |
| 40 | +# test.CharacterMatrix <- function(){ |
| 41 | +x <- matrix( letters[1:16], ncol = 4 ) |
| 42 | +expect_equal( matrix_character(x), paste( diag(x), collapse = "" ) ) |
| 43 | + |
| 44 | +# test.GenericMatrix <- function( ){ |
| 45 | +g <- function(y){ |
| 46 | + sapply( y, function(x) seq(from=x, to = 16) ) |
| 47 | +} |
| 48 | +x <- matrix( g(1:16), ncol = 4 ) |
| 49 | +expect_equal( matrix_generic(x), g(diag(matrix(1:16,ncol=4))), info = "GenericMatrix" ) |
| 50 | + |
| 51 | +# test.IntegerMatrix.diag <- function(){ |
| 52 | +expected <- matrix( 0L, nrow = 5, ncol = 5 ) |
| 53 | +diag( expected ) <- 1L |
| 54 | +expect_equal( matrix_integer_diag(), expected, info = "IntegerMatrix::diag" ) |
| 55 | + |
| 56 | +# test.CharacterMatrix.diag <- function(){ |
| 57 | +expected <- matrix( "", nrow = 5, ncol = 5 ) |
| 58 | +diag( expected ) <- "foo" |
| 59 | +expect_equal( matrix_character_diag(), expected, info = "CharacterMatrix::diag" ) |
| 60 | + |
| 61 | +# test.NumericMatrix.Ctors <- function(){ |
| 62 | +x <- matrix(0, 3, 3) |
| 63 | +expect_equal( matrix_numeric_ctor1(), x, info = "matrix from single int" ) |
| 64 | + |
| 65 | +x <- matrix(0, 3, 3) |
| 66 | +expect_equal( matrix_numeric_ctor2(), x, info = "matrix from two int" ) |
| 67 | + |
| 68 | +# test.IntegerVector.matrix.indexing <- function(){ |
| 69 | +x <- matrix( 1:16, ncol = 4 ) |
| 70 | +expect_equal( integer_matrix_indexing(x), sum(diag(x)), info = "matrix indexing" ) |
| 71 | + |
| 72 | +expect_equal( diag(integer_matrix_indexing_lhs(x)), 2*0:3, info = "matrix indexing lhs" ) |
| 73 | + |
| 74 | +y <- as.vector( x ) |
| 75 | +expect_error( integer_matrix_indexing_lhs(y) , info = "not a matrix" ) |
| 76 | + |
| 77 | +# test.NumericMatrix.row <- function(){ |
| 78 | +x <- matrix( 1:16 + .5, ncol = 4 ) |
| 79 | +expect_equal( runit_NumericMatrix_row( x ), sum( x[1,] ), info = "iterating over a row" ) |
| 80 | + |
| 81 | +# test.NumericMatrix.row.const <- function(){ |
| 82 | +x <- matrix( 1:16 + .5, ncol = 4 ) |
| 83 | +expect_equal( runit_NumericMatrix_row_const( x ), sum( x[1,] ), info = "iterating over a row" ) |
| 84 | + |
| 85 | + |
| 86 | +# test.CharacterMatrix.row <- function(){ |
| 87 | +m <- matrix( letters, ncol = 2 ) |
| 88 | +expect_equal( runit_CharacterMatrix_row(m), paste( m[1,], collapse = "" ), info = "CharacterVector::Row" ) |
| 89 | + |
| 90 | +# test.CharacterMatrix.row.const <- function(){ |
| 91 | +m <- matrix( letters, ncol = 2 ) |
| 92 | +expect_equal( runit_CharacterMatrix_row_const(m), paste( m[1,], collapse = "" ), info = "CharacterVector::Row" ) |
| 93 | + |
| 94 | +# test.List.row <- function(){ |
| 95 | +m <- lapply( 1:16, function(i) seq(from=1, to = i ) ) |
| 96 | +dim( m ) <- c( 4, 4 ) |
| 97 | +expect_equal( runit_GenericMatrix_row( m ), 1 + 0:3*4, info = "List::Row" ) |
| 98 | + |
| 99 | +# test.List.row.const <- function(){ |
| 100 | +m <- lapply( 1:16, function(i) seq(from=1, to = i ) ) |
| 101 | +dim( m ) <- c( 4, 4 ) |
| 102 | +expect_equal( runit_GenericMatrix_row_const( m ), 1 + 0:3*4, info = "List::Row" ) |
| 103 | + |
| 104 | +# test.NumericMatrix.column <- function(){ |
| 105 | +x <- matrix( 1:16 + .5, ncol = 4 ) |
| 106 | +expect_equal( runit_NumericMatrix_column( x ), sum( x[,1] ) , info = "iterating over a column" ) |
| 107 | + |
| 108 | +# test.NumericMatrix.column.const <- function(){ |
| 109 | +x <- matrix( 1:16 + .5, ncol = 4 ) |
| 110 | +expect_equal( runit_NumericMatrix_column_const( x ), sum( x[,1] ) , info = "iterating over a column" ) |
| 111 | + |
| 112 | +# test.NumericMatrix.cumsum <- function(){ |
| 113 | +x <- matrix( 1:8 + .5, ncol = 2 ) |
| 114 | +expect_equal( runit_NumericMatrix_cumsum( x ), t(apply(x, 1, cumsum)) , info = "cumsum" ) |
| 115 | + |
| 116 | +# test.CharacterMatrix.column <- function(){ |
| 117 | +m <- matrix( letters, ncol = 2 ) |
| 118 | +expect_equal( runit_CharacterMatrix_column(m), paste( m[,1], collapse = "" ), info = "CharacterVector::Column" ) |
| 119 | + |
| 120 | +# test.CharacterMatrix.column.const <- function(){ |
| 121 | +m <- matrix( letters, ncol = 2 ) |
| 122 | +expect_equal( runit_CharacterMatrix_column_const(m), paste( m[,1], collapse = "" ), info = "CharacterVector::Column" ) |
| 123 | + |
| 124 | +# test.List.column <- function(){ |
| 125 | +m <- lapply( 1:16, function(i) seq(from=1, to = i ) ) |
| 126 | +dim( m ) <- c( 4, 4 ) |
| 127 | +expect_equal( runit_GenericMatrix_column( m ), 1:4, info = "List::Column" ) |
| 128 | + |
| 129 | +# test.List.column.const <- function(){ |
| 130 | +m <- lapply( 1:16, function(i) seq(from=1, to = i ) ) |
| 131 | +dim( m ) <- c( 4, 4 ) |
| 132 | +expect_equal( runit_GenericMatrix_column_const( m ), 1:4, info = "List::Column" ) |
| 133 | + |
| 134 | +# test.NumericMatrix.colsum <- function( ){ |
| 135 | +probs <- matrix(1:12,nrow=3) |
| 136 | +expect_equal( runit_NumericMatrix_colsum( probs ), t(apply(probs,1,cumsum)) ) |
| 137 | + |
| 138 | +# test.NumericMatrix.rowsum <- function( ){ |
| 139 | +probs <- matrix(1:12,nrow=3) |
| 140 | +expect_equal( runit_NumericMatrix_rowsum( probs ), apply(probs,2,cumsum) ) |
| 141 | + |
| 142 | +# test.NumericMatrix.SubMatrix <- function( ){ |
| 143 | +target <- rbind( c(3,4,5,5), c(3,4,5,5), 0 ) |
| 144 | +expect_equal( runit_SubMatrix(), target, info = "SubMatrix" ) |
| 145 | + |
| 146 | +# test.NumericMatrix.opequals <- function() { |
| 147 | +m <- matrix(1:4, nrow=2) |
| 148 | +expect_equal(m, matrix_opequals(m)) |
| 149 | + |
| 150 | +# test.NumericMatrix.rownames.colnames.proxy <- function() { |
| 151 | +m <- matrix(as.numeric(1:4), nrow = 2) |
| 152 | +runit_rownames_colnames_proxy(m, letters[1:2], LETTERS[1:2]) |
| 153 | +expect_equal(rownames(m), letters[1:2]) |
| 154 | +expect_equal(colnames(m), LETTERS[1:2]) |
| 155 | +expect_error(runit_rownames_colnames_proxy(m, letters[1:3], letters[1:3])) |
| 156 | +expect_error(runit_rownames_colnames_proxy(m, letters[1:2], NULL)) |
| 157 | + |
| 158 | +m <- matrix(as.numeric(1:9), nrow = 3) |
| 159 | +runit_rownames_proxy(m) |
| 160 | +expect_equal(rownames(m), c("A", "B", "C")) |
| 161 | +expect_equal(colnames(m), NULL) |
| 162 | + |
| 163 | +# test.NumericMatrix.no.init <- function() { |
| 164 | +m <- runit_no_init_matrix() |
| 165 | +expect_equal(m, matrix(c(0, 1, 2, 3), nrow = 2)) |
| 166 | + |
| 167 | +# test.NumericMatrix.no.init.ctor <- function() { |
| 168 | +m <- runit_no_init_matrix_ctor() |
| 169 | +expect_equal(m, matrix(c(0, 1, 2, 3), nrow = 2)) |
| 170 | + |
| 171 | +# test.NumericMatrix.no.init.ctor.nrow <- function() { |
| 172 | +nrow <- runit_no_init_matrix_ctor_nrow() |
| 173 | +expect_equal(nrow, 2L) |
| 174 | + |
| 175 | +# test.NumericMatrix.const.Column <- function(){ |
| 176 | +m <- matrix(as.numeric(1:9), nrow = 3) |
| 177 | +res <- runit_const_Matrix_column(m) |
| 178 | +expect_equal( m[,1], m[,2] ) |
| 179 | + |
| 180 | +# test.IntegerMatrix.accessor.with.bounds.checking <- function() { |
| 181 | +m <- matrix(seq(1L, 12, by=1L), nrow=4L, ncol=3L) |
| 182 | +expect_equal(mat_access_with_bounds_checking(m, 0, 0), 1) |
| 183 | +expect_equal(mat_access_with_bounds_checking(m, 1, 2), 10) |
| 184 | +expect_equal(mat_access_with_bounds_checking(m, 3, 2), 12) |
| 185 | +expect_error(mat_access_with_bounds_checking(m, 4, 2) , info = "index out of bounds not detected" ) |
| 186 | +expect_error(mat_access_with_bounds_checking(m, 3, 3) , info = "index out of bounds not detected" ) |
| 187 | +expect_error(mat_access_with_bounds_checking(m, 3, -1) , info = "index out of bounds not detected" ) |
| 188 | +expect_error(mat_access_with_bounds_checking(m, -1, 2) , info = "index out of bounds not detected" ) |
| 189 | +expect_error(mat_access_with_bounds_checking(m, -1, -1) , info = "index out of bounds not detected" ) |
| 190 | + |
| 191 | +# test.IntegerMatrix.transpose <- function() { |
| 192 | +M <- matrix(1:12, 3, 4) |
| 193 | +expect_equal(transposeInteger(M), t(M), info="integer transpose") |
| 194 | +rownames(M) <- letters[1:nrow(M)] |
| 195 | +expect_equal(transposeInteger(M), t(M), info="integer transpose with rownames") |
| 196 | +colnames(M) <- LETTERS[1:ncol(M)] |
| 197 | +expect_equal(transposeInteger(M), t(M), info="integer transpose with row and colnames") |
| 198 | + |
| 199 | +# test.NumericMatrix.transpose <- function() { |
| 200 | +M <- matrix(1.0 * (1:12), 3, 4) |
| 201 | +expect_equal(transposeNumeric(M), t(M), info="numeric transpose") |
| 202 | +rownames(M) <- letters[1:nrow(M)] |
| 203 | +expect_equal(transposeNumeric(M), t(M), info="numeric transpose with rownames") |
| 204 | +colnames(M) <- LETTERS[1:ncol(M)] |
| 205 | +expect_equal(transposeNumeric(M), t(M), info="numeric transpose with row and colnames") |
| 206 | + |
| 207 | +# test.CharacterMatrix.transpose <- function() { |
| 208 | +M <- matrix(as.character(1:12), 3, 4) |
| 209 | +expect_equal(transposeCharacter(M), t(M), info="character transpose") |
| 210 | +rownames(M) <- letters[1:nrow(M)] |
| 211 | +expect_equal(transposeCharacter(M), t(M), info="character transpose with rownames") |
| 212 | +colnames(M) <- LETTERS[1:ncol(M)] |
| 213 | +expect_equal(transposeCharacter(M), t(M), info="character transpose with row and colnames") |
| 214 | + |
| 215 | +# test.Matrix.Scalar.op <- function() { |
| 216 | +M <- matrix(c(1:12), 3, 4) |
| 217 | +expect_equal(matrix_scalar_plus(M, 2), M + 2, info="matrix + scalar") |
| 218 | +expect_equal(matrix_scalar_plus2(M, 2), 2 + M, info="scalar + matrix") |
| 219 | +expect_equal(matrix_scalar_divide(M, 2), M / 2, info="matrix / scalar") |
| 220 | +expect_equal(matrix_scalar_divide2(M, 2), 2 / M, info="scalar / matrix") |
| 221 | + |
| 222 | +## 23 October 2016 |
| 223 | +## eye function |
| 224 | +# test.Matrix.eye <- function() { |
| 225 | +expect_equal(dbl_eye(3), diag(1.0, 3, 3), info = "eye - numeric") |
| 226 | +expect_equal(int_eye(3), diag(1L, 3, 3), info = "eye - integer") |
| 227 | +expect_equal(cx_eye(3), diag(1.0 + 0i, 3, 3), info = "eye - complex") |
| 228 | + |
| 229 | +## diag(TRUE, 3, 3) was registering as |
| 230 | +## a numeric matrix on Travis for some reason |
| 231 | +mat <- matrix(FALSE, 3, 3) |
| 232 | +diag(mat) <- TRUE |
| 233 | +expect_equal(lgl_eye(3), mat, info = "eye - logical") |
| 234 | + |
| 235 | +## ones function |
| 236 | +# test.Matrix.ones <- function() { |
| 237 | +expect_equal(dbl_ones(3), matrix(1.0, 3, 3), info = "ones - numeric") |
| 238 | +expect_equal(int_ones(3), matrix(1L, 3, 3), info = "ones - integer") |
| 239 | +expect_equal(cx_ones(3), matrix(1.0 + 0i, 3, 3), info = "ones - complex") |
| 240 | +expect_equal(lgl_ones(3), matrix(TRUE, 3, 3), info = "ones - logical") |
| 241 | + |
| 242 | +## zeros function |
| 243 | +# test.Matrix.zeros <- function() { |
| 244 | +expect_equal(dbl_zeros(3), matrix(0.0, 3, 3), info = "zeros - numeric") |
| 245 | +expect_equal(int_zeros(3), matrix(0L, 3, 3), info = "zeros - integer") |
| 246 | +expect_equal(cx_zeros(3), matrix(0.0 + 0i, 3, 3), info = "zeros - complex") |
| 247 | +expect_equal(lgl_zeros(3), matrix(FALSE, 3, 3), info = "zeros - logical") |
| 248 | + |
| 249 | +# test.Matrix.diagfill <- function() { |
| 250 | +expect_equal(num_diag_fill(diag(1.0, 2, 4), 0.0), matrix(0.0, 2, 4), info = "diagonal fill - case: n < p") |
| 251 | +expect_equal(num_diag_fill(diag(1.0, 4, 2), 0.0), matrix(0.0, 4, 2), info = "diagonal fill - case: n > p") |
| 252 | +expect_equal(num_diag_fill(diag(1.0, 3, 3), 0.0), matrix(0.0, 3, 3), info = "diagonal fill - case: n = p") |
| 253 | + |
| 254 | +m <- matrix("", 2, 4) |
| 255 | +diag(m) <- letters[1:2] |
| 256 | + |
| 257 | +expect_equal(char_diag_fill(m, ""), matrix("", 2, 4), info = "diagonal fill - char") |
0 commit comments