diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..625abeb3aa3 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,36 @@ -## Put comments here that give an overall description of what your -## functions do +## cache a matrix's inverse to avoid compute the inverse repeatedly -## Write a short comment describing this function +## this function creates a object which cache the matrix an it's inverse. makeCacheMatrix <- function(x = matrix()) { - + inverseMatrix <- NULL + set <- function(y){ + x <<- y + inverseMatrix <<- NULL + } + get <- function() x + setInverse <- function(inverse) inverseMatrix <<- inverse + getInverse <- function() inverseMatrix + + list(set = set, + get = get, + setInverse = setInverse, + getInverse = getInverse + ) } -## Write a short comment describing this function +## this function will generate the inverse from cache if possible cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + inverseMatrix <- x$getInverse() + if(!is.null(inverseMatrix)){ + message("getting cached data") + return(m) + } + oriMatrix <- x$get() + inverseMatrix <- solve(oriMatrix,...) + x$setInverse(inverseMatrix) + inverseMatrix } +