forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.Rhistory
336 lines (336 loc) · 10 KB
/
.Rhistory
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
load(cachematrix.R)
library(cachematrix.R)
library("cachematrix.R")
load("cachematrix.R")
load("cachematrix.R")
load("cachematrix.R")
## Put comments here that give an overall description of what your
## functions do
## Write a short comment describing this function
## This function requires matrix as an input, but generates list of functions to access the data using Lexical scoping
## Use of Lexical scoping enables it to cache values to save processing time and memory in certain cases
makeCacheMatrix <- function(x = matrix()) {
## object inv set to NULL , which is current in context of this function
inv <- NULL
set <- function(y) {
## object mx and inv assigned, but not in the current context so they are available outside this function (Lexical scoping). Note the <<- operator
mx <<- y
inv <<- NULL
}
## This returns value of mx , which was set using above function but still available outside where it was defined
get <- function() mx
## This sets value of inv, which was defined above but available outside where it was defined
setinv <-function(invm) inv <<- invm
## This returns value of inv, as it is available outside where it was defined using the <<- operator
getinv <- function() inv
## This returns a list of function defined above
list(set-set,get=get,setinv=setinv,getinv=getinv)
}
## This function takens the list of functions and associated cache variables created by makeCacheMatrix function above
## It then returns inverse of the matrix if it was already calculated and cached, else it calculates then sets the cache value and returns the inverted matrix
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
## This executes getinv() function from the list of functions to get cached value of inverted matrix
inv <- x$getinv()
## If the cache value is available it returns it, else it calculates
if(!is.null(inv)) {
message("getting cached data")
return(inv)
}
## If cache value of inverted matrix is not available, it get calculated below
## This gets cached matrix through the get() function in the input list of functions
data <-x$get()
## This creates inverse of the matrix stored in data
inv <- solve(data,...)
## This sets the cache value of inverse matrix, for future reuse
X$setinv(inv)
## Returns inverse of the matrix x
inv
}
ls()
mt1 <- matrix(c(1,3,2,2),2,2)
mtc1 <- makeCacheMatrix(mt1)
class(mt1)
## Put comments here that give an overall description of what your
## functions do
## Write a short comment describing this function
## This function requires matrix as an input, but generates list of functions to access the data using Lexical scoping
## Use of Lexical scoping enables it to cache values to save processing time and memory in certain cases
makeCacheMatrix <- function(mx = matrix()) {
## object inv set to NULL , which is current in context of this function
inv <- NULL
set <- function(y) {
## object mx and inv assigned, but not in the current context so they are available outside this function (Lexical scoping). Note the <<- operator
mx <<- y
inv <<- NULL
}
## This returns value of mx , which was set using above function but still available outside where it was defined
get <- function() mx
## This sets value of inv, which was defined above but available outside where it was defined
setinv <-function(invm) inv <<- invm
## This returns value of inv, as it is available outside where it was defined using the <<- operator
getinv <- function() inv
## This returns a list of function defined above
list(set-set,get=get,setinv=setinv,getinv=getinv)
}
## This function takens the list of functions and associated cache variables created by makeCacheMatrix function above
## It then returns inverse of the matrix if it was already calculated and cached, else it calculates then sets the cache value and returns the inverted matrix
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
## This executes getinv() function from the list of functions to get cached value of inverted matrix
inv <- x$getinv()
## If the cache value is available it returns it, else it calculates
if(!is.null(inv)) {
message("getting cached data")
return(inv)
}
## If cache value of inverted matrix is not available, it get calculated below
## This gets cached matrix through the get() function in the input list of functions
data <-x$get()
## This creates inverse of the matrix stored in data
inv <- solve(data,...)
## This sets the cache value of inverse matrix, for future reuse
X$setinv(inv)
## Returns inverse of the matrix x
inv
}
ls()
mtc1 <- makeCacheMatrix(mt1)
rm(ls(list.ALL=TRUE))
rm(ls())
ls()
mt1
mtc1 <- makeCacheMatrix(mt1)
## Following functions show use of Lexical Scoping to enable caching functionality in R.
## Caching could save processing and memory in certain situations.
## This function requires matrix as an input, but generates list of functions to access the data using Lexical scoping
## Use of Lexical scoping enables it to cache values to save processing time and memory in certain cases
makeCacheMatrix <- function(mx = matrix()) {
## object inv set to NULL , which is current in context of this function
inv <- NULL
set <- function(y) {
## object mx and inv assigned, but not in the current context so they are available outside this function (Lexical scoping). Note the <<- operator
mx <<- y
inv <<- NULL
}
## This returns value of mx , which was set using above function but still available outside where it was defined
get <- function() mx
## This sets value of inv, which was defined above but available outside where it was defined
setinv <-function(invm) inv <<- invm
## This returns value of inv, as it is available outside where it was defined using the <<- operator
getinv <- function() inv
## This returns a list of function defined above
list(set-set,get=get,setinv=setinv,getinv=getinv)
}
## This function takens the list of functions and associated cache variables created by makeCacheMatrix function above
## It then returns inverse of the matrix if it was already calculated and cached, else it calculates then sets the cache value and returns the inverted matrix
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
## This executes getinv() function from the list of functions to get cached value of inverted matrix
inv <- x$getinv()
## If the cache value is available it returns it, else it calculates
if(!is.null(inv)) {
message("getting cached data")
return(inv)
}
## If cache value of inverted matrix is not available, it get calculated below
## This gets cached matrix through the get() function in the input list of functions
data <-x$get()
## This creates inverse of the matrix stored in data
inv <- solve(data,...)
## This sets the cache value of inverse matrix, for future reuse
X$setinv(inv)
## Returns inverse of the matrix x
inv
}
mtc1 <- makeCacheMatrix(mt1)
makeCacheMatrix()
makeCacheMatrix
mtc1 <- makeCacheMatrix(mt1)
makeCacheMatrix <- function(mx = matrix()) {
inv <- NULL
set <- function(y) {
mx <<- y
inv <<- NULL
}
get <- function() mx
setinv <- function(invm) inv <<- invm
getinv <- function() inv
list(set-set,get=get,setinv=setinv,getinv=getinv)
}
cacheSolve <- function(x, ...) {
inv <- x$getinv()
if(!is.null(inv)) {
message("getting cached data")
return(inv)
}
data <-x$get()
inv <- solve(data,...)
X$setinv(inv)
inv
}
ls()
mtc1 <- makeCacheMatrix(mt1)
version()
version
rm(list=ls())
ls()
makeCacheMatrix <- function(mx = matrix()) {
inv <- NULL
set <- function(y) {
mx <<- y
inv <<- NULL
}
get <- function() mx
setinv <- function(invm) inv <<- invm
getinv <- function() inv
list(set-set,get=get,setinv=setinv,getinv=getinv)
}
cacheSolve <- function(x, ...) {
inv <- x$getinv()
if(!is.null(inv)) {
message("getting cached data")
return(inv)
}
data <-x$get()
inv <- solve(data,...)
X$setinv(inv)
inv
}
mt1<- matrix(c(1,3,2,2),2,2)
mt1
mct1 <- makeCacheMatrix(mt1)
makeCacheMatrix <- function(mx = matrix()) {
inv <- NULL
set <- function(y) {
mx <<- y
inv <<- NULL
}
get <- function() mx
setinv <- function(invm) inv <<- invm
getinv <- function() inv
list(set-set,get=get,setinv=setinv,getinv=getinv)
}
cacheSolve <- function(x, ...) {
inv <- x$getinv()
if(!is.null(inv)) {
message("getting cached data")
return(inv)
}
data <-x$get()
inv <- solve(data,...)
X$setinv(inv)
inv
}
mct1 <- makeCacheMatrix(mt1)
load("C:/Users/kunal/Documents/.RData")
ls()
mt1
mv1
mv1 <- makeCacheMatrix((mt1)
)
mv1
ls()
rm(list=ls())
ls()
ls()
function(mx=matrix()) {
inv <- NULL
set <- function(y) {
mx <<-y
inv <<- NULL
}
get <- function() mx
setinv <- function(invm) inv <<- invm
getinv <- function() inv
list(set=set, get=get,setinv=setinv,getinv=getinv)
}
function(x,...) {
inv<-x$getinv()
if(!is.null(inv)) {
message("getting cached data")
return(inv)
}
data <- x$get()
inv <- solve(data,...)
x$setinv(inv)
inv
}
ls()
ls()
function(mx=matrix()) {
inv <- NULL
set <- function(y) {
mx <<-y
inv <<- NULL
}
get <- function() mx
setinv <- function(invm) inv <<- invm
getinv <- function() inv
list(set=set, get=get,setinv=setinv,getinv=getinv)
}
function(x,...) {
inv<-x$getinv()
if(!is.null(inv)) {
message("getting cached data")
return(inv)
}
data <- x$get()
inv <- solve(data,...)
x$setinv(inv)
inv
}
ls()
load("cachematrix.R")
ls()
load("cachematrix.R")
source("cachematrix.R")
mtx <- matrix(c(1,3,2,2),2,2)
mtx
cmtx <- makeCacheMatrix(mtx)
cacheSolve(cmtx)
ccmtx
## Put comments here that give an overall description of what your
## functions do
## Write a short comment describing this function
makeCacheMatrix <- function(mx = matrix()) {
inv <- NULL
set <- function(y) {
mx <<-y
inv <<- NULL
}
get <- function() mx
setinv <- function(invm) inv <<- invm
getinv <- function() inv
list(set=set, get=get,setinv=setinv,getinv=getinv)
}
## Write a short comment describing this function
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
inv<-x$getinv()
if(!is.null(inv)) {
message("getting cached data")
return(inv)
}
data <- x$get()
inv <- solve(data,...)
x$setinv(inv)
inv
}
cmtx <- makeCacheMatrix(mtx)
cmtx
cacheSolve(cmtx)
cacheSolve(cmtx)
source("cachematrix.R")
ls()
mtx
source("cachematrix.R")
ls()
mtx <- matrix(c(1,3,2,2),2,2)
mtx
cmtx <- makeCacheMatrix(mtx)
cmtx
cmtx$get()
cacheSolve(cmtx)
cacheSolve(cmtx)
mtx*cacheSolve(cmtx)==cacheSolve(cmtx)*mtx