From 6fec40a140ab2890e369bc842d1fb55b5324579b Mon Sep 17 00:00:00 2001 From: "Viacheslav V. Kovalevskyi" Date: Fri, 11 Apr 2014 20:57:17 +0100 Subject: [PATCH 1/3] creating methods makeCacheMatrix/cacheSolve --- cachematrix.R | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..6ca63d83183 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 +## functions calculating solve for matrix with caching. +## If solve already calculated for matrix it will be returned from cache +## If user set's new matrix, cache will be dropped -makeCacheMatrix <- function(x = matrix()) { +## makeCacheMatrix - create object with 4 fumctions: +## set - set matrix and delete cached solve +## get - get matrix +## setsolve - save solve to cache +## getsolve - load solve from cache +## input of function - matrix() +makeCacheMatrix <- function(x = matrix()) { + solve <- NULL + set <- function(y) { + matrix <<- y + solve <<- NULL + } + get <- function() x + setsolve <- function(y) solve <<- y + getsolve <- function() solve + list(set = set, + get = get, + setsolve = setsolve, + getsolve = getsolve) } -## Write a short comment describing this function - +## cacheSolve will try to read solve for matrix from cache and if fails it will calculate solve and save it to cache +## if it's read solve from cach it's print: "getting cached data" +## input - tuple that can be created with makeCacheMatrix function cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + s <- x$getsolve() + if(!is.null(s)) { + message("getting cached data") + return(s) + } + data <- x$get() + s <- solve(data, ...) + x$setsolve(s) + s } From 8189b3b58b6f7086e47ac6df59fa227521c2ab02 Mon Sep 17 00:00:00 2001 From: "Viacheslav V. Kovalevskyi" Date: Wed, 23 Apr 2014 20:42:08 +0100 Subject: [PATCH 2/3] small refactoring --- cachematrix.R | 48 ++++++++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 6ca63d83183..130af7d5fe5 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,23 +1,29 @@ - -## functions calculating solve for matrix with caching. +## functions calculating solve for matrix (with caching). ## If solve already calculated for matrix it will be returned from cache ## If user set's new matrix, cache will be dropped -## makeCacheMatrix - create object with 4 fumctions: -## set - set matrix and delete cached solve -## get - get matrix +## makeCacheMatrix - create object with 4 functions: +## set - set matrix (also deleting cached solve) +## get - get matrix (getting matrix) ## setsolve - save solve to cache ## getsolve - load solve from cache -## input of function - matrix() -makeCacheMatrix <- function(x = matrix()) { +## default input of function - matrix() +makeCacheMatrix <- function(matrix = matrix()) { solve <- NULL - set <- function(y) { - matrix <<- y + ## set new matrix and clear cahce + set <- function(new_matrix) { + matrix <<- new_matrix solve <<- NULL } - get <- function() x - setsolve <- function(y) solve <<- y + + ## get matrix + get <- function() matrix + + ## saving solve to cache + setsolve <- function(new_solve) solve <<- new_solve + + ## getting solve from cache getsolve <- function() solve list(set = set, get = get, @@ -26,17 +32,19 @@ makeCacheMatrix <- function(x = matrix()) { } -## cacheSolve will try to read solve for matrix from cache and if fails it will calculate solve and save it to cache -## if it's read solve from cach it's print: "getting cached data" +## cacheSolve will try to read matrix solve from cache and if fails it will calculate solve and save it to cache ## input - tuple that can be created with makeCacheMatrix function cacheSolve <- function(x, ...) { - s <- x$getsolve() - if(!is.null(s)) { + ## reading solve from cache + solve <- x$getsolve() + ## checking solve from cache + if(!is.null(solve)) { message("getting cached data") - return(s) + return(solve) } - data <- x$get() - s <- solve(data, ...) - x$setsolve(s) - s + matrix <- x$get() + solve <- solve(matrix, ...) + ## setting solve to cache + x$setsolve(solve) + solve } From 61fee420a972e85d94b9373d08084cff3f903493 Mon Sep 17 00:00:00 2001 From: "Viacheslav V. Kovalevskyi" Date: Wed, 23 Apr 2014 20:45:46 +0100 Subject: [PATCH 3/3] removing debug print --- cachematrix.R | 1 - 1 file changed, 1 deletion(-) diff --git a/cachematrix.R b/cachematrix.R index 130af7d5fe5..aee0553e2f5 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -39,7 +39,6 @@ cacheSolve <- function(x, ...) { solve <- x$getsolve() ## checking solve from cache if(!is.null(solve)) { - message("getting cached data") return(solve) } matrix <- x$get()