Skip to content

Commit af442db

Browse files
committed
cached inverse calculation
1 parent 7f657dd commit af442db

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

cachematrix.R

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,35 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## Calculate the inverse of a matrix. Use a cache structure to optimize computations
2+
##
33

4-
## Write a short comment describing this function
4+
## Construct that stores a matrix and its inverse. If the inverse is not stored NULL is returned
55

66
makeCacheMatrix <- function(x = matrix()) {
7-
7+
inv <- NULL
8+
set <- function(y){
9+
x <<- y
10+
inv <<- NULL
11+
}
12+
13+
get <- function() x
14+
setInverse <- function(toCache) inv <<- toCache
15+
getInverse <- function() inv
16+
17+
list(set=set, get=get, setInverse=setInverse, getInverse=getInverse)
818
}
919

1020

11-
## Write a short comment describing this function
21+
## Returns the inverse of a matrix. The result is cached.
1222

1323
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
24+
inverse <- x$getInverse()
25+
if(!is.null(inverse)){
26+
message("Cached Inverse")
27+
return(inverse);
28+
}
29+
30+
original <- x$get()
31+
inverse <- solve(original)
32+
x$setInverse(inverse)
33+
34+
inverse
1535
}

0 commit comments

Comments
 (0)