File tree Expand file tree Collapse file tree 1 file changed +28
-8
lines changed Expand file tree Collapse file tree 1 file changed +28
-8
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
+ # # Computes and caches the inverse of a square matrix.
2
+ # #
3
+ # # Two methods are exposed:
4
+ # # makeCacheMatrix creates or wraps a matrix to cache its inverse
5
+ # # cacheSolve returns the cached inverse if available, or computes and caches it otherwise.
5
6
7
+ # # Creates or wraps a matrix to cache its inverse
6
8
makeCacheMatrix <- function (x = matrix ()) {
7
-
9
+ i <- NULL
10
+ set <- function (y ) {
11
+ x <<- y
12
+ i <<- NULL
13
+ }
14
+ get <- function () x
15
+ setinverse <- function (inverse ) i <<- inverse
16
+ getinverse <- function () i
17
+ list (set = set , get = get ,
18
+ setinverse = setinverse ,
19
+ getinverse = getinverse )
8
20
}
9
21
10
22
11
- # # Write a short comment describing this function
12
-
23
+ # # Returns the cached inverse if available, or computes and caches it otherwise.
13
24
cacheSolve <- function (x , ... ) {
14
25
# # Return a matrix that is the inverse of 'x'
15
- }
26
+ inverse <- x $ getinverse()
27
+ if (! is.null(inverse )) {
28
+ message(" getting cached data" )
29
+ return (inverse )
30
+ }
31
+ data <- x $ get()
32
+ inverse <- solve(data , ... )
33
+ x $ setinverse(inverse )
34
+ inverse
35
+ }
You can’t perform that action at this time.
0 commit comments