diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..39c356f90f5 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,42 @@ ## Put comments here that give an overall description of what your ## functions do -## Write a short comment describing this function +## Takes matrix and returns special matrix which is list containing functions: +## get() - returns initial matrix +## set(y) - overwrites initial matrix with new one +## getsolved() - returns cached inverse of initial matrix +## setsolved() - caches inverse of matrix makeCacheMatrix <- function(x = matrix()) { - + s <- NULL + + get <- function() x + set <- function(y) { + x <<- y + s <<- NULL + } + + getsolved <- function() s + setsolved <- function(solved) s <<- solved + + list(get = get, + set = set, + getsolved = getsolved, + setsolved = setsolved) } - -## Write a short comment describing this function +## Takes special matrix and checks if it has cached inverse of matrix. +## If not it computes inverse of matrix and stores it in cache. +## It returns inverse of matrix as a result cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + solved <- x$getsolved() + + if (is.null(solved)) { + matrix <- x$get() + solved <- solve(matrix, ...) + x$setsolved(solved) + } + + solved }