File tree Expand file tree Collapse file tree 1 file changed +32
-9
lines changed Expand file tree Collapse file tree 1 file changed +32
-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
+ # # makeCacheMatrix - create a matrix wrapper whose inverse is cacheable
2
+ # # cacheSolve - invert a matrix; use cached result if available
5
3
4
+ # # This method returns a wrapper for a matrix for which the inverse is
5
+ # # cacheable
6
6
makeCacheMatrix <- function (x = matrix ()) {
7
-
7
+ inverse <- NULL
8
+ set <- function ( y )
9
+ {
10
+ x <<- y
11
+ inverse <<- NULL
12
+ }
13
+ get <- function ()
14
+ {
15
+ x
16
+ }
17
+ setInverse <- function ( i )
18
+ {
19
+ inverse <<- i
20
+ }
21
+ getInverse <- function ()
22
+ {
23
+ inverse
24
+ }
25
+ list ( set = set , get = get , setInverse = setInverse , getInverse = getInverse )
8
26
}
9
27
10
-
11
- # # Write a short comment describing this function
12
-
28
+ # # This method calculates the inverse of matrix x
13
29
cacheSolve <- function (x , ... ) {
14
- # # Return a matrix that is the inverse of 'x'
30
+ candidate <- x $ getInverse()
31
+ if ( ! is.null( candidate ) ) {
32
+ candidate
33
+ } else {
34
+ inverse <- solve( x $ get() )
35
+ x $ setInverse( inverse )
36
+ inverse
37
+ }
15
38
}
You can’t perform that action at this time.
0 commit comments