Skip to content

Commit 6b29f72

Browse files
committed
add comments
1 parent 63d6df1 commit 6b29f72

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

cachematrix.R

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## cachematrix: Functions to cache matrix inversion
32

4-
## Write a short comment describing this function
3+
## makeCacheMatrix sets up the scope for storing cache
54

65
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
98
x <<- y
109
m <<- NULL
1110
}
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
1514
list(set = set, get = get, setinverse=setinverse, getinverse=getinverse)
1615
}
1716

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
2019

2120
cacheSolve <- function(x, ...) {
22-
m <- x$getinverse()
21+
m <- x$getinverse() # returns NULL if not yet cached
2322
if (!is.null(m)) {
24-
message('cached inverse')
23+
message('cached inverse') # report that returned value is from cache
2524
return(m)
2625
}
27-
m <- solve(x$get())
26+
m <- solve(x$get()) # uses solve to get inverse of x
2827
x$setinverse(m)
2928
m
3029
}

0 commit comments

Comments
 (0)