From d0afc59cb1a335ab95ae12f9d7386fc44628b789 Mon Sep 17 00:00:00 2001 From: Maxim Konovalov Date: Sat, 22 Aug 2015 15:43:11 +0300 Subject: [PATCH] assignment2 commit --- cachematrix.R | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..65fc96c7f06 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -4,12 +4,29 @@ ## Write a short comment describing this function makeCacheMatrix <- function(x = matrix()) { - + mi = NULL + + getMatrix <- function() x + setMatrix <- function(matrix) { + mi <<- NULL; + x <<- matrix + } + + getCachedInverse <- function() mi + setCachedInverse <- function(inv) mi <<- inv + list(getMatrix = getMatrix, setMatrix = setMatrix, getCachedInverse = getCachedInverse, setCachedInverse = setCachedInverse) } ## Write a short comment describing this function cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + cached <- x$getCachedInverse() + if (!is.null(cached)) { + message("Guys, we have a cache hit here! What a miracle!") + return(cached) + } + inv <- solve(x$getMatrix(), ...) + x$setCachedInverse(inv) + inv }