File tree Expand file tree Collapse file tree 1 file changed +25
-2
lines changed Expand file tree Collapse file tree 1 file changed +25
-2
lines changed Original file line number Diff line number Diff line change 3
3
4
4
# # Write a short comment describing this function
5
5
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 )
8
21
}
9
22
10
23
11
24
# # Write a short comment describing this function
12
25
13
26
cacheSolve <- function (x , ... ) {
14
27
# # 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
15
38
}
You can’t perform that action at this time.
0 commit comments