From 93def9748017930a2d98611b7c47ec1dd5ead5e9 Mon Sep 17 00:00:00 2001 From: Zhenis Beisekov Date: Sun, 29 Sep 2019 17:18:29 +0200 Subject: [PATCH 1/3] Update cachematrix.R Implementation of caching inverse matrix. --- cachematrix.R | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..408c9e5008a 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,35 @@ ## Put comments here that give an overall description of what your ## functions do -## Write a short comment describing this function +## makeCacheMatrix creates a list, which caches inversed matrix makeCacheMatrix <- function(x = matrix()) { - + inv <- NULL + set <- function(y) { + x <<- y + inv <<- NULL + } + get <- function() x + setinv <- function(inverse) inv <<- inverse + getinv <- function() inv + list(set = set, get = get, + setinv = setinv, + getinv = getinv) } -## Write a short comment describing this function +## cacheSolve returns inverse matrix for x, which is 'cachematrix'. +## if there is no cached inverse matrix, it calculates new one. cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + ## Return a matrix that is the inverse of 'x' + + m <- x$getinv() + if(!is.null(m)) { + message("getting cached data") + return(m) + } + inv <- solve(x$get(), diag(nrow(x$get())), ...) + x$setinv(inv) + inv } From 0f8002d9a8dfd40808720a3bab2d35cbe1f109d5 Mon Sep 17 00:00:00 2001 From: Zhenis Beisekov Date: Mon, 30 Sep 2019 19:24:24 +0200 Subject: [PATCH 2/3] More comments to cachematrix.R Adding more comments, so it can pass the peers' review. --- cachematrix.R | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 408c9e5008a..f84a8900b28 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,17 +1,20 @@ ## Put comments here that give an overall description of what your ## functions do -## makeCacheMatrix creates a list, which caches inversed matrix +## makeCacheMatrix creates an object, which keeps both +## original matrix and inverse one. makeCacheMatrix <- function(x = matrix()) { - inv <- NULL - set <- function(y) { + inv <- NULL ## variable to keep inverse matrix. + ## set original matrix. Note: it does not calculate the inverse matrix + ## by default. + set <- function(y) { x <<- y inv <<- NULL } - get <- function() x - setinv <- function(inverse) inv <<- inverse - getinv <- function() inv + get <- function() x ## get original matrix + setinv <- function(inverse) inv <<- inverse ## set inverse matrix + getinv <- function() inv ## get inverse matrix list(set = set, get = get, setinv = setinv, getinv = getinv) @@ -25,11 +28,20 @@ cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' m <- x$getinv() - if(!is.null(m)) { + + ## check if there is the previously set value, + ## because calculating inverse matrix might be expensive. + if(!is.null(m)) { message("getting cached data") return(m) } - inv <- solve(x$get(), diag(nrow(x$get())), ...) - x$setinv(inv) - inv + + m <- x$get() + + ## calculate inverse matrix only if the original one is not null + if (!is.null(m)) { + m <- solve(m, ...) ## reusing m, so we can return its value + x$setinv(m) + } + m } From 60df672d8b2611b8ea6bb13403b43d2384f85810 Mon Sep 17 00:00:00 2001 From: Zhenis Beisekov Date: Fri, 4 Oct 2019 13:07:24 +0200 Subject: [PATCH 3/3] Ignoring artifacts of R --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000000..ae05230ef93 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ + +.DS_Store +.Rhistory