Skip to content

Commit 41e8580

Browse files
author
Jonathan Taylor
committed
completed assignment
1 parent 7f657dd commit 41e8580

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

cachematrix.R

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,36 @@
33

44
## Write a short comment describing this function
55

6-
makeCacheMatrix <- function(x = matrix()) {
7-
6+
makeCacheMatrix <- function(our_matrix = matrix()) {
7+
computed_inverse <- NULL
8+
9+
set <- function(new_matrix) {
10+
our_matrix <<- new_matrix #Update our matrix to the new one
11+
computer_inverse <<- NULL #Clear the cached value as the matrix has changed (so we recompute it)
12+
}
13+
14+
get <- function() our_matrix
15+
16+
setInverse <- function(inverted_matrix) computed_inverse <<- inverted_matrix
17+
18+
getInverse <- function() computed_inverse
19+
20+
list(set = set, get = get, setInverse = setInverse, getInverse = getInverse)
821
}
922

1023

1124
## Write a short comment describing this function
1225

1326
cacheSolve <- function(x, ...) {
1427
## Return a matrix that is the inverse of 'x'
28+
inverse <- x$getInverse()
29+
if(!is.null(inverse)) {
30+
message("getting cached data")
31+
return(inverse)
32+
}
33+
34+
data <- x$get()
35+
inverse <- solve(data) #Not using ... here we need to force defaults otherwise we can get different answers for the same input matrix!
36+
x$setInverse(inverse)
37+
inverse
1538
}

0 commit comments

Comments
 (0)