8000 Update commit #1 · TracerMAK/ProgrammingAssignment2@ba55433 · GitHub
[go: up one dir, main page]

Skip to content

Commit ba55433

Browse files
committed
Update commit rdpeng#1
1 parent c991241 commit ba55433

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

cachematrix.R

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,21 @@ makeCacheMatrix <- function(x = matrix()) {
2020
## Creates a global key/value map cache where the key is the hash value of the
2121
## matrix and the value is the inverse of that matrix. The intention is to
2222
## 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.
2424
cacheSolve <- function(x, ...) {
2525
if (!exists("inversemap")) {
2626
inversemap <<- list(matrix())
2727
}
2828

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]])
3839
x$getinverse()
3940
}

0 commit comments

Comments
 (0)
0