File tree Expand file tree Collapse file tree 1 file changed +12
-13
lines changed Expand file tree Collapse file tree 1 file changed +12
-13
lines changed Original file line number Diff line number Diff line change 1
- # # Put comments here that give an overall description of what your
2
- # # functions do
1
+ # # cachematrix: Functions to cache matrix inversion
3
2
4
- # # Write a short comment describing this function
3
+ # # makeCacheMatrix sets up the scope for storing cache
5
4
6
5
makeCacheMatrix <- function (x = matrix ()) {
7
- m <- NULL
8
- set <- function (y ) {
6
+ m <- NULL # initialize cache to NULL
7
+ set <- function (y ) { # changing value invalidates cache
9
8
x <<- y
10
9
m <<- NULL
11
10
}
12
- get <- function () x
13
- setinverse <- function (inverse ) m <<- inverse
14
- getinverse <- function () m
11
+ get <- function () x # return original matrix
12
+ setinverse <- function (inverse ) m <<- inverse # set cache value to supplied value
13
+ getinverse <- function () m
15
14
list (set = set , get = get , setinverse = setinverse , getinverse = getinverse )
16
15
}
17
16
18
-
19
- # # Write a short comment describing this function
17
+ # # cacheSolve takes the list generated by makeCacheMatrix and either populates its cache
18
+ # # or returns the cached value
20
19
21
20
cacheSolve <- function (x , ... ) {
22
- m <- x $ getinverse()
21
+ m <- x $ getinverse() # returns NULL if not yet cached
23
22
if (! is.null(m )) {
24
- message(' cached inverse' )
23
+ message(' cached inverse' ) # report that returned value is from cache
25
24
return (m )
26
25
}
27
- m <- solve(x $ get())
26
+ m <- solve(x $ get()) # uses solve to get inverse of x
28
27
x $ setinverse(m )
29
28
m
30
29
}
You can’t perform that action at this time.
0 commit comments