From 8c6cc3f4b26107b15598db2b152397abf92a8f21 Mon Sep 17 00:00:00 2001 From: Sasha Egorov Date: Mon, 23 Feb 2015 02:32:36 +0900 Subject: [PATCH 1/3] makeCacheMatrix added --- cachematrix.R | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..6305bdae265 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,12 +1,38 @@ -## Put comments here that give an overall description of what your -## functions do +# Hi! I'm the guy from Ruby Planet! And I love clear code. +# It was painfull for me to read initial version, so I've refactored all names. +# Please Enjoy R and this MOOC. If you are interested in detailed information +# please read detailed comment at the end of code. +# Thanks for your review! -## Write a short comment describing this function +makeCacheMatrix <- function(local_matrix = matrix()) { + # By default local_matrix is the empty matrix -makeCacheMatrix <- function(x = matrix()) { + # Initializing variable supposed for cached solution + cached_solution <- NULL -} + set_matrix <- function(arg) { + local_matrix <<- arg + cached_solution <<- NULL + } + get_matrix <- function() local_matrix + + # Setting cached solution + set_solved <- function(solution) cached_solution <<- solution + # Getting cached solution + get_solved <- function() cached_solution + # Return list of functions list of functions: + # - `set_matrix` - set the value of the matrix + # - `get_matrix` - get the value of the matrix + # - `set_solved` - set the value of the inverted matrix + # - `get_solved` - get the value of the inverted matrix + + list( + set_matrix = set_matrix, # ...$set_matrix + get_matrix = get_matrix, # ...$get_matrix + set_solved = set_solved, + get_solved = get_solved) +} ## Write a short comment describing this function From ce1049ef94585c3dedd7a3d56955d8fa1f72ac56 Mon Sep 17 00:00:00 2001 From: Sasha Egorov Date: Mon, 23 Feb 2015 03:08:54 +0900 Subject: [PATCH 2/3] cacheSolve added --- cachematrix.R | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 6305bdae265..ba003f21f1b 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -34,8 +34,20 @@ makeCacheMatrix <- function(local_matrix = matrix()) { get_solved = get_solved) } -## Write a short comment describing this function - -cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' +## Consumer of makeCacheMatrix +cacheSolve <- function(cache_matrix, ...) { + # Get "solved" matrix + # If solution isn't cached it will be null + solution <- cache_matrix$get_solved() + if(!is.null(solution)) { + # If solution is cached it will be returned + message("Getting cached matrix") + return(solution) + } else { + # Solve it... + solution <- solve(cache_matrix$get_matrix(), ...) + # Put in cache just before return + cache_matrix$set_solved(solution) + return(solution) + } } From 72e9d7915ff6e039fc2ad3d8946ac09d11a5e703 Mon Sep 17 00:00:00 2001 From: Sasha Egorov Date: Mon, 23 Feb 2015 03:18:55 +0900 Subject: [PATCH 3/3] Small changes --- cachematrix.R | 48 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index ba003f21f1b..63f65aa9194 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,31 +1,29 @@ # Hi! I'm the guy from Ruby Planet! And I love clear code. -# It was painfull for me to read initial version, so I've refactored all names. +# It was painful for me to read initial version, so I've refactored all names. # Please Enjoy R and this MOOC. If you are interested in detailed information # please read detailed comment at the end of code. # Thanks for your review! -makeCacheMatrix <- function(local_matrix = matrix()) { - # By default local_matrix is the empty matrix +makeCacheMatrix <- function(original_matrix = matrix()) { + # By default original_matrix is the empty matrix # Initializing variable supposed for cached solution cached_solution <- NULL + # Setting/Getting original matrix set_matrix <- function(arg) { - local_matrix <<- arg + original_matrix <<- arg cached_solution <<- NULL } - get_matrix <- function() local_matrix + get_matrix <- function() original_matrix - # Setting cached solution + # Setting/Getting cached solution set_solved <- function(solution) cached_solution <<- solution - # Getting cached solution get_solved <- function() cached_solution # Return list of functions list of functions: - # - `set_matrix` - set the value of the matrix - # - `get_matrix` - get the value of the matrix - # - `set_solved` - set the value of the inverted matrix - # - `get_solved` - get the value of the inverted matrix + # - `get_matrix`/`set_matrix` get/set the value of the original matrix + # - `get_solved`/`set_solved` get/set the value of the inverted matrix list( set_matrix = set_matrix, # ...$set_matrix @@ -51,3 +49,31 @@ cacheSolve <- function(cache_matrix, ...) { return(solution) } } + +## But how to check if this works ... and really caches? +## First of all create matrix. + +## This small matrix will *just* show you the message... +## There is no difference in sence of time for such small matrix +# m <- matrix(rnorm(16), nrow = 4) + +## This will give metioned difference... +# m <- matrix(rnorm(16000000), nrow = 4000) + +## Assign vector with function to `mv` variable +# mv <- makeCacheMatrix(m) + +## Call cacheSolve() with `mv` as argument +# cacheSolve(mv) + +## You will get result with delay: +# ... +# [n, ] -1.0133707610 0.5960284005 -4.543966e-01 +# [n+1,] 1.7333438756 0.1842437258 3.645269e-01 + +## Call it again cacheSolve() with `mv` as argument +## You will get result from cache. Here is output: +# Getting cached matrix +# ... +# [n, ] -1.0133707610 0.5960284005 -4.543966e-01 +# [n+1,] 1.7333438756 0.1842437258 3.645269e-01