diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..d10f679ca5b 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,41 @@ -## Put comments here that give an overall description of what your -## functions do +## Functions representing caching implementation of +## calculating inverse of a matrix -## Write a short comment describing this function +## Returns an object (list), which is also cacheable +## matrix with getter/setter for both data and cache +## values makeCacheMatrix <- function(x = matrix()) { - + # checking the input + if (!is.matrix(x)) stop("This argument doesn't look like a matrix") + i <- NULL + set <- function(y) { + x <<- y + i <<- NULL + } + get <- function() x + # setter and getter for an inverse + setinv <- function(inv) i <<- inv + getinv <- function() i + list(set = set, get = get, + setinv = setinv, + getinv = getinv) } -## Write a short comment describing this function +## Calculates inverse of a matrix returned by makeCacheMatrix +## or returns cached value if it's present -cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' +cacheSolve <- function(inversible, ...) { + inv <- inversible$getinv() + # checking if inverce value is already in cache + if(!is.null(inv)) { + message("Getting cached inverse value") + return(inv) + } + # calculating inverse if cache is empty + data <- inversible$get() + inv <- solve(a=data, ...) + inversible$setinv(inv) + inv }