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
- # # Put comments here that give an overall description of what your
2
- # # functions do
3
-
4
- # # Write a short comment describing this function
5
-
6
1
makeCacheMatrix <- function (x = matrix ()) {
7
-
2
+ # This function creates a special "matrix" object that can cache its inverse.
3
+ inverse <- NULL
4
+ set <- function (y ){
5
+ x <<- y
6
+ inverse <<- NULL
7
+ }
8
+ get <- function () x
9
+ setinverse <- function (inv ) inverse <<- inv
10
+ getinverse <- function () inverse
11
+ list (set = set , get = get , setinverse = setinverse , getinverse = getinverse )
8
12
}
9
13
10
14
11
- # # Write a short comment describing this function
12
15
13
16
cacheSolve <- function (x , ... ) {
14
17
# # Return a matrix that is the inverse of 'x'
18
+ # This function computes the inverse of the special "matrix" returned by makeCacheMatrix above.
19
+ # If the inverse has already been calculated (and the matrix has not changed),
20
+ # then the cachesolve should retrieve the inverse from the cache.
21
+ inverse <- x $ getinverse()
22
+ if (! is.null(inverse )){
23
+ message(" getting cached data" )
24
+ return (inverse )
25
+ }
26
+ data <- x $ get()
27
+ inverse <- solve(data , ... )
28
+ # ellipses in case we want to pass additional arguments to solve()
29
+ # note the ellipses in the function def that implies this functionality
30
+ # is desired
31
+ x $ setinverse(inverse ) # set it for caching
32
+ inverse # return
15
33
}
You can’t perform that action at this time.
0 commit comments