File: Comparisons.R

package info (click to toggle)
rmatrix 1.7-4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,096 kB
  • sloc: ansic: 97,203; makefile: 280; sh: 165
file content (166 lines) | stat: -rw-r--r-- 5,225 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
### R code from vignette source 'Comparisons.Rnw'

###################################################
### code chunk number 1: preliminaries
###################################################
options(width=75)
library(stats) # for R_DEFAULT_PACKAGES=NULL
library(utils) # ditto


###################################################
### code chunk number 2: modelMatrix
###################################################
data(Formaldehyde, package = "datasets")
str(Formaldehyde)
(m <- cbind(1, Formaldehyde$carb))
(yo <- Formaldehyde$optden)


###################################################
### code chunk number 3: naiveCalc
###################################################
solve(t(m) %*% m) %*% t(m) %*% yo


###################################################
### code chunk number 4: timedNaive
###################################################
system.time(solve(t(m) %*% m) %*% t(m) %*% yo)


###################################################
### code chunk number 5: catNaive
###################################################
dput(c(solve(t(m) %*% m) %*% t(m) %*% yo))
dput(unname(lm.fit(m, yo)$coefficients))


###################################################
### code chunk number 6: KoenNg
###################################################
library(Matrix)
data(KNex, package = "Matrix")
 y <- KNex$y
mm <- as(KNex$mm, "matrix") # full traditional matrix
dim(mm)
system.time(naive.sol <- solve(t(mm) %*% mm) %*% t(mm) %*% y)


###################################################
### code chunk number 7: crossKoenNg
###################################################
system.time(cpod.sol <- solve(crossprod(mm), crossprod(mm,y)))
all.equal(naive.sol, cpod.sol)


###################################################
### code chunk number 8: xpxKoenNg
###################################################
system.time(t(mm) %*% mm)


###################################################
### code chunk number 9: fullMatrix_crossprod
###################################################
fm <- mm
set.seed(11)
fm[] <- rnorm(length(fm))
system.time(c1 <- t(fm) %*% fm)
system.time(c2 <- crossprod(fm))
stopifnot(all.equal(c1, c2, tol = 1e-12))


###################################################
### code chunk number 10: naiveChol
###################################################
system.time(ch <- chol(crossprod(mm)))
system.time(chol.sol <-
            backsolve(ch, forwardsolve(ch, crossprod(mm, y),
                                       upper = TRUE, trans = TRUE)))
stopifnot(all.equal(chol.sol, naive.sol))


###################################################
### code chunk number 11: MatrixKoenNg
###################################################
mm <- as(KNex$mm, "denseMatrix")
class(crossprod(mm))
system.time(Mat.sol <- solve(crossprod(mm), crossprod(mm, y)))
stopifnot(all.equal(naive.sol, unname(as(Mat.sol,"matrix"))))


###################################################
### code chunk number 12: saveFactor
###################################################
xpx <- crossprod(mm)
xpy <- crossprod(mm, y)
system.time(solve(xpx, xpy))
system.time(solve(xpx, xpy)) # reusing factorization


###################################################
### code chunk number 13: SparseKoenNg
###################################################
mm <- KNex$mm
class(mm)
system.time(sparse.sol <- solve(crossprod(mm), crossprod(mm, y)))
stopifnot(all.equal(naive.sol, unname(as(sparse.sol, "matrix"))))


###################################################
### code chunk number 14: SparseSaveFactor
###################################################
xpx <- crossprod(mm)
xpy <- crossprod(mm, y)
system.time(solve(xpx, xpy))
system.time(solve(xpx, xpy))


###################################################
### code chunk number 15: sessionInfo
###################################################
toLatex(sessionInfo())


###################################################
### code chunk number 16: from_pkg_sfsmisc
###################################################

if(identical(1L, grep("linux", R.version[["os"]]))) { ##----- Linux - only ----

Sys.procinfo <- function(procfile)
{
  l2 <- strsplit(readLines(procfile),"[ \t]*:[ \t]*")
  r <- sapply(l2[sapply(l2, length) == 2],
              function(c2)structure(c2[2], names= c2[1]))
  attr(r,"Name") <- procfile
  class(r) <- "simple.list"
  r
}

Scpu <- Sys.procinfo("/proc/cpuinfo")
Smem <- Sys.procinfo("/proc/meminfo")
} # Linux only


###################################################
### code chunk number 17: Sys_proc_fake (eval = FALSE)
###################################################
## if(identical(1L, grep("linux", R.version[["os"]]))) { ## Linux - only ---
##  Scpu <- sfsmisc::Sys.procinfo("/proc/cpuinfo")
##  Smem <- sfsmisc::Sys.procinfo("/proc/meminfo")
##  print(Scpu[c("model name", "cpu MHz", "cache size", "bogomips")])
##  print(Smem[c("MemTotal", "SwapTotal")])
## }


###################################################
### code chunk number 18: Sys_proc_out
###################################################
if(identical(1L, grep("linux", R.version[["os"]]))) { ## Linux - only ---
 print(Scpu[c("model name", "cpu MHz", "cache size", "bogomips")])
 print(Smem[c("MemTotal", "SwapTotal")])
}