From 363318f798cc89889fdc089828d941f02a4eb63a Mon Sep 17 00:00:00 2001 From: Ivan Nemytchenko Date: Sun, 26 Apr 2015 23:13:18 +0200 Subject: [PATCH] implemented task --- cachematrix.R | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..4cb38ef4bbc 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,31 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function +## This lets you to cache `solve` operation for your matrix +## Accepts matrix, returns list of functions makeCacheMatrix <- function(x = matrix()) { - + m <- NULL + set <- function(y) { + x <<- y + m <<- NULL + } + get <- function() x + setmatrix <- function(matrix) m <<- matrix + getmatrix <- function() m + list(set = set, get = get, + setmatrix = setmatrix, + getmatrix = getmatrix) } - -## Write a short comment describing this function - +## Accepts list of functions, and uses them to store result of `solve` +## operation into cache or get it from the cache. cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + m <- x$getmatrix() + if(!is.null(m)) { + message("getting cached data") + return(m) + } + data <- x$get() + m <- solve(data, ...) + x$setmatrix(m) + m } +