File tree Expand file tree Collapse file tree 1 file changed +11
-10
lines changed Expand file tree Collapse file tree 1 file changed +11
-10
lines changed Original file line number Diff line number Diff line change @@ -20,20 +20,21 @@ makeCacheMatrix <- function(x = matrix()) {
20
20
# # Creates a global key/value map cache where the key is the hash value of the
21
21
# # matrix and the value is the inverse of that matrix. The intention is to
22
22
# # calculate the inverse of an unique matrix one time, as long as the
23
- # # 'inversemap' object persists.
23
+ # # 'inversemap' object persists. Multiple inverses can be cached this way.
24
24
cacheSolve <- function (x , ... ) {
25
25
if (! exists(" inversemap" )) {
26
26
inversemap <<- list (matrix ())
27
27
}
28
28
29
- inverse <- x $ getinverse()
30
- if (is.null(inverse )) {
31
- key <- digest(x $ get())
32
- if (is.null(inversemap [[key ]])) {
33
- print(" Updating cache..." )
34
- inversemap [[key ]] <<- solve(x $ get())
35
- }
36
- x $ setinverse(inversemap [[key ]])
37
- }
29
+ key <- digest(x $ get())
30
+ inverse <- inversemap [[key ]]
31
+
32
+ if (! is.null(inverse )) {
33
+ message(" Getting cached inverse" )
34
+ return (inverse )
35
+ }
36
+ message(" Updating cache" )
37
+ inversemap [[key ]] <<- solve(x $ get())
38
+ x $ setinverse(inversemap [[key ]])
38
39
x $ getinverse()
39
40
}
You can’t perform that action at this time.
0 commit comments