File tree Expand file tree Collapse file tree 1 file changed +11
-8
lines changed Expand file tree Collapse file tree 1 file changed +11
-8
lines changed Original file line number Diff line number Diff line change 4
4
# #Create a matrix whose inverse can be cached
5
5
makeCacheMatrix <- function (x = matrix ()) {
6
6
inverse <- NULL
7
+
8
+ # #Assigns a new matrix and invalidates the inverse cache
7
9
set <- function (y ) {
8
10
x <<- y
9
11
inverse <<- NULL
10
12
}
13
+
11
14
get <- function () x
12
15
setInverse <- function (i ) inverse <<- i
13
16
getInverse <- function () inverse
@@ -16,14 +19,14 @@ makeCacheMatrix <- function(x = matrix()) {
16
19
17
20
# # Returns the cached inverse of matrix 'x'. If inverse of 'x' is not cached, it is calculated and cached.
18
21
cacheSolve <- function (x , ... ) {
19
- # # Return a matrix that is the inverse of 'x'
22
+
23
+ # #Calculate and cache inverse if necessary
20
24
inverse <- x $ getInverse()
21
- if (! is.null(inverse )) {
22
- message(" getting cached data" )
23
- return (inverse )
25
+ if (is.null(inverse )) {
26
+ data <- x $ get()
27
+ inverse <- solve(data , ... )
28
+ x $ setInverse(inverse )
24
29
}
25
- data <- x $ get()
26
- inverse <- solve(data , ... )
27
- x $ setInverse(inverse )
28
- inverse
30
+
31
+ x $ getInverse()
29
32
}
You can’t perform that action at this time.
0 commit comments