Skip to content

Commit cf4f02a

Browse files
committed
add comments and implementation for makeCacheMatrix and cacheSolve functions
1 parent e4eed41 commit cf4f02a

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

cachematrix.R

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,35 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
3-
4-
## Write a short comment describing this function
1+
## solution to Assignment: Caching the Inverse of a Matrix
52

3+
## creates a wrapper object around a matrix that can optionally store
4+
## its inverse. exposes setters and getters for both. invalidates
5+
## cache on set.
66
makeCacheMatrix <- function(x = matrix()) {
7-
7+
inverse <- NULL
8+
set <- function(y) {
9+
x <<- y
10+
inverse <<- NULL
11+
}
12+
get <- function() x
13+
setinv <- function(inv) inverse <<- inv
14+
getinv <- function() inverse
15+
list(set = set, get = get, setinv = setinv, getinv = getinv)
816
}
917

1018

11-
## Write a short comment describing this function
12-
13-
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
19+
## computes the inverse of a matrix with caching.
20+
## expects a wrapped matrix as returned from makeCacheMatrix. first
21+
## checks cache in the object passed in for the presence of the result
22+
## of a previous run. if found returns that, otherwise computes the
23+
## result and then stores it in the cache as well as returning it to
24+
## the caller
25+
cacheSolve <- function(x) {
26+
inverse <- x$getinv()
27+
if(!is.null(inverse)) {
28+
message("getting cached data")
29+
return(inverse)
30+
}
31+
data <- x$get()
32+
inverse <- solve(data)
33+
x$setinv(inverse)
34+
inverse
1535
}

0 commit comments

Comments
 (0)