R Under development (unstable) (2024-10-16 r87241 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. > #### Thanks to the manipulation in base namespace, see ../R/zzz.R , > #### all the functions (in 'base' or namespaces that import it) > #### starting with something like > #### " x <- as.matrix(x) " or " X <- as.array(X) " > #### will work for 'Matrix'-matrices > > ## for R_DEFAULT_PACKAGES=NULL : > library(stats) > library(utils) > > library(Matrix) > > data(KNex, package = "Matrix") > mm <- KNex$mm > str(m1 <- mm[1:500, 1:200]) Formal class 'dgCMatrix' [package "Matrix"] with 6 slots ..@ i : int [1:500] 0 2 25 27 163 165 1 3 4 6 ... ..@ p : int [1:201] 0 6 10 14 18 22 27 31 35 39 ... ..@ Dim : int [1:2] 500 200 ..@ Dimnames:List of 2 .. ..$ : NULL .. ..$ : NULL ..@ x : num [1:500] 0.277 0.277 0.277 0.277 0.277 ... ..@ factors : list() > m11 <- m1[1:100, 1:20] > ## These now work thanks to using our as.matrix(): > str(D1 <- dist(m11)) 'dist' num [1:4950] 0.572 0 0.572 0.572 0.434 ... - attr(*, "Size")= int 100 - attr(*, "Diag")= logi FALSE - attr(*, "Upper")= logi FALSE - attr(*, "method")= chr "euclidean" - attr(*, "call")= language dist(x = m11) > str(rs <- apply(m1, 1, sum)) num [1:500] 0.277 0.5 0.277 0.5 0.5 ... > > stopifnot(identical(kappa(Matrix(2:5, 2)), + kappa(matrix(2:5, 2)))) > ## used to seg.fault, PR#7984, > ## because qr() was calling the wrong as.matrix() > > ## also matplot() or pairs(). > > ## a regression test for as.matrix.dist(.) still working > data(USJudgeRatings, package = "datasets") > stopifnot(c(43, 43) == dim(as.matrix(d <- dist(USJudgeRatings)))) > > m <- Matrix(0:5, 3, 2) > (m2 <- Matrix(diag(c(3,1)))) 2 x 2 diagonal matrix of class "ddiMatrix" [,1] [,2] [1,] 3 . [2,] . 1 > (m3 <- crossprod(t(m))) # <- that's an S4 method; nothing "base" 3 x 3 Matrix of class "dpoMatrix" [,1] [,2] [,3] [1,] 9 12 15 [2,] 12 17 22 [3,] 15 22 29 > > str( svd(m) ) List of 3 $ d: num [1:2] 7.35 1 $ u: num [1:3, 1:2] 0.393 0.561 0.729 -0.824 -0.137 ... $ v: num [1:2, 1:2] 0.275 0.962 0.962 -0.275 > str( lapply(eigen(m3), zapsmall)) List of 2 $ values : num [1:3] 54 1 0 $ vectors: num [1:3, 1:3] -0.393 -0.561 -0.729 0.824 0.137 ... > > ### outer() used to work thanks to as.array() -- up to R 2.2.1 > ## no longer, because the definition of outer has changed -- FIXME? > ## Whould work by providing an as.vector(.) method > ## *and* is.array(.) \-> TRUE which may be too strong > ##--> For %o%: "need" to make outer(.,.) an S3 generic > ## *and* provide Matrix S3 methods > ## stopifnot(identical(outer(m, m2), > ## outer(as(m,"matrix"), as(m2,"matrix"))), > ## identical(outer(m3, m2), > ## outer(as(m3,"matrix"), as(m2,"matrix")))) > > > cat('Time elapsed: ', proc.time(),'\n') # for ``statistical reasons'' Time elapsed: 1.18 0.15 1.28 NA NA > > proc.time() user system elapsed 1.18 0.15 1.28