File tree Expand file tree Collapse file tree 1 file changed +25
-7
lines changed Expand file tree Collapse file tree 1 file changed +25
-7
lines changed Original file line number Diff line number Diff line change 1
1
# # Put comments here that give an overall description of what your
2
2
# # functions do
3
3
4
- # # Write a short comment describing this function
5
-
4
+ # # Creates a matrix that will cache its inverse, a costly operation
6
5
makeCacheMatrix <- function (x = matrix ()) {
7
-
6
+ inv <- NULL
7
+ list (
8
+ set = function (y ) {
9
+ x <<- y
10
+ inv <<- NULL
11
+ },
12
+ get = function () x ,
13
+ setInv = function (new_inv ) inv <<- new_inv ,
14
+ getInv = function () inv
15
+ )
8
16
}
9
17
10
-
11
- # # Write a short comment describing this function
12
-
18
+ # Find the inverse of the passed matrix and return it and cache it in the passed param
19
+ # note that x must be a value returned from 'makeCachedMatrix'
20
+ # The following function calculates the inverse of the special matrix
21
+ # created with 'makeCachedMatrix' However, it first checks to see if the
22
+ # inverse has already been calculated. If so, it gets the inverse from the
23
+ # cache and skips the computation. Otherwise, it calculates the inverse of
24
+ # the matrix and sets the value using the setInv function on x
13
25
cacheSolve <- function (x , ... ) {
14
- # # Return a matrix that is the inverse of 'x'
26
+ inv <- x $ getInv()
27
+ if (! is.null(inv )) {
28
+ return (inv )
29
+ }
30
+ inv <- solve(x $ get())
31
+ x $ setInv(inv )
32
+ inv
15
33
}
You can’t perform that action at this time.
0 commit comments