Skip to content

Commit 7443ea5

Browse files
committed
Added functionality to the methods to calculate, cache, and retrieve matrix inverse
1 parent 7f657dd commit 7443ea5

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

cachematrix.R

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,33 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## small library of functions to handle caching of matrix computations
32

4-
## Write a short comment describing this function
3+
## Makes a matrix that will cache its inverse to save computation time
54

65
makeCacheMatrix <- function(x = matrix()) {
7-
6+
i <- NULL
7+
set <- function(y) {
8+
x <<- y
9+
i <<- NULL
10+
}
11+
get <- function() x
12+
setinverse <- function(inverse) i <<- inverse
13+
getinverse <- function() i
14+
list(set = set, get = get,
15+
setinverse = setinverse,
16+
getinverse = getinverse)
817
}
918

1019

11-
## Write a short comment describing this function
20+
## If inverse is cached, return it... otherwise calculate it, cache it, and then return it
1221

1322
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
23+
## Return a matrix that is the inverse of 'x'
24+
m <- x$getinverse()
25+
if(!is.null(m)) {
26+
message("getting cached inverse")
27+
return(m)
28+
}
29+
data <- x$get()
30+
m <- solve(data, ...)
31+
x$setinverse(m)
32+
m
1533
}

0 commit comments

Comments
 (0)