File tree Expand file tree Collapse file tree 1 file changed +34
-6
lines changed Expand file tree Collapse file tree 1 file changed +34
-6
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
1
+ # # These two functions generate a new "matrix" object and
2
+ # # introduce a cached inverse resultset to avoid the costly
3
+ # # computation of a matrix inverse.
3
4
4
- # # Write a short comment describing this function
5
+ # # Creates a set of lazy getter/setter functions for a matrix with
6
+ # # a cacheable inverse.
5
7
6
8
makeCacheMatrix <- function (x = matrix ()) {
7
-
9
+ i <- NULL
10
+
11
+ set <- function (y ) {
12
+ x <<- y
13
+ i <<- NULL
14
+ }
15
+
16
+ get <- function () x
17
+ setinverse <- function (inverse ) i <<- inverse
18
+ getinverse <- function () i
19
+
20
+ list (set = set , get = get ,
21
+ setinverse = setinverse ,
22
+ getinverse = getinverse )
8
23
}
9
24
10
25
11
- # # Write a short comment describing this function
26
+ # # Computes the inverse of the matrix. If the inverse has already
27
+ # # been calculated, the cached result will be retrieved.
12
28
13
29
cacheSolve <- function (x , ... ) {
14
- # # Return a matrix that is the inverse of 'x'
30
+ # # Return a matrix that is the inverse of 'x'
31
+ i <- x $ getinverse()
32
+
33
+ if (! is.null(i )) {
34
+ message(" getting cached data" )
35
+ return (i )
36
+ }
37
+
38
+ data <- x $ get()
39
+ i <- solve(data , ... )
40
+ x $ setinverse(i )
41
+
42
+ i
15
43
}
You can’t perform that action at this time.
0 commit comments