File tree Expand file tree Collapse file tree 1 file changed +29
-9
lines changed Expand file tree Collapse file tree 1 file changed +29
-9
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
3
-
4
- # # Write a short comment describing this function
1
+ # # solution to Assignment: Caching the Inverse of a Matrix
5
2
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.
6
6
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 )
8
16
}
9
17
10
18
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
15
35
}
You can’t perform that action at this time.
0 commit comments