SCM

SCM Repository

[matrix] Annotation of /pkg/tests/simple.R
ViewVC logotype

Annotation of /pkg/tests/simple.R

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1319 - (view) (download)

1 : maechler 948 #### Currently a collection of simple tests
2 :     ## (since 'Matrix' takes long to load, rather have fewer source files!)
3 :    
4 : bates 346 library(Matrix)
5 : maechler 509
6 : maechler 948 source(system.file("test-tools.R", package = "Matrix"))# identical3() etc
7 :    
8 : maechler 1174 ### Matrix() ''smartness''
9 :     (d4 <- Matrix(diag(4)))
10 :     (z4 <- Matrix(0*diag(4)))
11 :     (o4 <- Matrix(1+diag(4)))
12 :     (m4 <- Matrix(cbind(0,rbind(6*diag(3),0))))
13 :     dm4 <- Matrix(m4, sparse = FALSE)
14 :     stopifnot(validObject(d4), validObject(z4), validObject(o4),
15 :     validObject(m4), validObject(dm4))
16 :     assert.EQ.mat(dm4, as(m4, "matrix"))
17 : maechler 1315 ## large sparse ones: these now directly "go sparse":
18 :     str(m0 <- Matrix(0, nrow=100, ncol = 1000))
19 :     str(l0 <- Matrix(FALSE, nrow=100, ncol = 200))
20 : maechler 1174
21 : maechler 948 ###-- Sparse Triangular :
22 :    
23 :     (t1 <- new("dtTMatrix", x= c(3,7), i= 0:1, j=3:2,
24 :     Dim= as.integer(c(4,4))))
25 :     stopifnot(validObject(t1),
26 :     validObject(t1c <- as(t1, "dtCMatrix")))
27 :     assert.EQ.mat(t1, as(t1c, "matrix"))
28 :    
29 :     ## from 0-diagonal to unit-diagonal {low-level step}:
30 :     tu <- t1 ; tu@diag <- "U"
31 :     tu
32 :     stopifnot(validObject(cu <- as(tu, "dtCMatrix")),
33 : maechler 1253 validObject(tu. <- as(cu, "dtTMatrix")),
34 :     ## NOT: identical(tu, tu.), # since T* is not unique!
35 :     identical(cu, as(tu., "dtCMatrix")),
36 :     validObject(t(cu)),
37 :     validObject(t(tu)))
38 : maechler 948 assert.EQ.mat(cu, as(tu,"matrix"), tol=0)
39 :    
40 :    
41 :     ###-- Numeric Dense: Crossprod & Solve
42 :    
43 : maechler 509 set.seed(123)
44 : maechler 1207 mm. <- mm <- Matrix(rnorm(500 * 150), nc = 150)
45 : maechler 509 stopifnot(validObject(mm))
46 : maechler 1207 xpx <- crossprod(mm)
47 :     stopifnot(identical(mm, mm.),# once upon a time, mm was altered by crossprod()
48 : maechler 873 validObject(xpx))
49 :     str(mm) # 'dge*"
50 :     str(xpx)# 'dpo*"
51 : bates 346 xpy <- crossprod(mm, rnorm(500))
52 :     res <- solve(xpx, xpy)
53 : maechler 873 str(xpx)# now with Cholesky factor
54 :     stopifnot(validObject(xpx),
55 :     validObject(xpy),
56 :     validObject(res))
57 :     stopifnot(all.equal(xpx %*% res, xpy, tol= 1e-12))
58 : maechler 509
59 : maechler 948 ###-- more solve() methods {was ./solve.R }
60 :    
61 :     ## first for "dgeMatrix" and all kinds of RHS :
62 :     (m6 <- 1 + as(diag(0:5), "dgeMatrix"))
63 :     rcond(m6)
64 :     I6 <- as(diag(6), "dgeMatrix")
65 :     stopifnot(all.equal(I6, m6 %*% solve(m6)),
66 :     all.equal(I6, solve(m6) %*% m6) )
67 :    
68 :     (i6 <- solve(m6, Matrix(1:6)))
69 :     stopifnot(identical(i6, as(cbind(c(-4, rep(1,5))), "dgeMatrix")),
70 : maechler 949 identical(i6, solve(m6, 1:6)),
71 : maechler 948 identical(i6, solve(m6, matrix(1:6))),
72 : maechler 949 identical(i6, solve(m6, matrix(c(1,2,3,4,5,6))))
73 : maechler 948 )
74 :    
75 :     ###-- row- and column operations {was ./rowcolOps.R }
76 :    
77 :     set.seed(321)
78 : maechler 956 m1k <- Matrix(round(rnorm(1000), 2), 50, 20)
79 :     m.m <- as(m1k, "matrix")
80 :     stopifnot(all.equal(colMeans(m1k), colMeans(m.m)),
81 :     all.equal(colSums (m1k), colSums (m.m)),
82 :     all.equal(rowMeans(m1k), rowMeans(m.m)),
83 :     all.equal(rowSums (m1k), rowSums (m.m)))
84 : maechler 948
85 :     ###-- Testing expansions of factorizations {was ./expand.R }
86 :    
87 :     (m1 <- round(Matrix(rnorm(25), 5), 2))
88 :     (lul <- expand(lu(m1)))
89 :     stopifnot(all.equal(as(m1, "matrix"),
90 :     as(lul$P %*% (lul$L %*% lul$U), "matrix")))
91 :    
92 :    
93 : maechler 956 ###-- kronecker for nonsparse uses Matrix(.):
94 :     stopifnot(is(kr <- kronecker(m1, m6), "Matrix"))
95 :     assert.EQ.mat(kr,
96 :     kronecker(as(m1, "matrix"),
97 :     as(m6, "matrix")),
98 :     tol = 0)
99 :     ## sparse:
100 :     (kt1 <- kronecker(t1, tu))
101 :     kt2 <- kronecker(t1c, cu)
102 :     ktf <- kronecker(as.matrix(t1), as.matrix(tu))
103 :    
104 :     assert.EQ.mat(kt1, ktf, tol= 0)
105 :     assert.EQ.mat(kt2, ktf, tol= 0)
106 :     ## but kt1 and kt2, both "dgT" are different since entries are not ordered!
107 :    
108 : maechler 1238 tm <- matrix(0, 8,8)
109 :     tm[cbind(c(1,1,2,7,8),
110 :     c(3,6,4,8,8))] <- c(2,-30,15,20,80)
111 :     (tM <- Matrix(tm)) ## dtC
112 :     (mM <- Matrix(m <- (tm + t(tm)))) ## dsC
113 :     mT <- as(mM, "dsTMatrix")
114 : maechler 1319 gC <- as(as(mT, "dgTMatrix"), "dgCMatrix")
115 :     ## Check that 'mT' and gC print properly :
116 :     pr.mT <- capture.output(mT)
117 :     nn <- unlist(strsplit(gsub(" +\\.", "", sub("^....", "", pr.mT[-(1:2)])), " "))
118 :     stopifnot(as.numeric(nn[nn != ""]) == m[m != 0],
119 :     capture.output(gC)[-1] == pr.mT[-1])
120 : maechler 1238 assert.EQ.mat(tM, tm, tol=0)
121 : maechler 1319 assert.EQ.mat(gC, m, tol=0)
122 : maechler 1238 assert.EQ.mat(mT, m, tol=0)
123 :     stopifnot(is(mM, "dsCMatrix"), is(tM, "dtCMatrix"),
124 :     ## coercions general <-> symmetric
125 :     identical(as(as(mM, "dgCMatrix"), "dsCMatrix"), mM),
126 :     identical(as(as(mM, "dgTMatrix"), "dsTMatrix"), mT),
127 :     identical(as(as(tM, "dgCMatrix"), "dtCMatrix"), tM)
128 :     )
129 :     eM <- eigen(mM) # works thanks to base::as.matrix hack in ../R/zzz.R
130 :     stopifnot(all.equal(eM$values,
131 :     { v <- c(162.462112512353, 30.0665927567458)
132 :     c(v, 15, 0, 0, 160-v[1], -15, -v[2])}, tol=1e-14))
133 :    
134 : maechler 974 ##--- symmetric -> pos.def. needs valid test:
135 : maechler 1238 m5 <- Matrix(diag(5) - 1)
136 :     if(FALSE) # FIXME: this happily "works" but MM thinks it shouldn't:
137 :     assertError(as(m5, "dpoMatrix"))
138 : maechler 956
139 : maechler 974
140 : maechler 956 ###-- logical sparse : ----------
141 :    
142 :     (lkt <- as(as(kt1, "dgCMatrix"), "lgCMatrix"))# ok
143 :     (clt <- crossprod(lkt))
144 : maechler 1238 if(FALSE) ## FIXME!
145 : maechler 956 crossprod(clt)
146 :     ## CHOLMOD error: matrix cannot be symmetric
147 :    
148 : maechler 961
149 : maechler 956 ### "d" <-> "l" for (symmetric) sparse :
150 : maechler 1228 data(KNex)
151 :     mm <- KNex$mm
152 : maechler 956 xpx <- crossprod(mm)
153 :     lxpx <- as(xpx, "lsCMatrix")
154 :     if(FALSE)
155 :     show(lxpx) ## gives error about "lsC" -> "lgT" coercion ..
156 :     ## The bug is actually from *subsetting* the large matrix:
157 :     if(FALSE) ## FIXME
158 :     r <- lxpx[1:2,]
159 :    
160 :     lmm <- as(mm, "lgCMatrix")
161 :     xlx <- crossprod(lmm)
162 : maechler 961 ## now A = lxpx and B = xlx should be close, but not quite the same
163 :     ## since <x,y> = 0 is well possible when x!=0 and y!=0 .
164 :     ## However, A[i,j] != 0 ==> B[i,j] != 0:
165 :     A <- as(as(lxpx, "lgCMatrix"), "lgTMatrix")
166 :     B <- as(as(xlx, "lgCMatrix"), "lgTMatrix")
167 :     ij <- function(a) a@i + ncol(a) * a@j
168 :     stopifnot(all(ij(A) %in% ij(B)))
169 : maechler 956
170 : maechler 974
171 :    
172 :    
173 :     cat('Time elapsed: ', proc.time(),'\n') # "stats"

root@r-forge.r-project.org
ViewVC Help
Powered by ViewVC 1.0.0  
Thanks to:
Vienna University of Economics and Business Powered By FusionForge