From aab5d400fad0937c5420e8e5260296bb6349b749 Mon Sep 17 00:00:00 2001 From: spymark Date: Sun, 20 Apr 2014 14:38:31 +0100 Subject: [PATCH 01/11] Just trying out adding another script --- script.R | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 script.R diff --git a/script.R b/script.R new file mode 100644 index 00000000000..5f16a81c200 --- /dev/null +++ b/script.R @@ -0,0 +1,29 @@ +setwd("D:\\Coursera\\R programming\\progass2") + +makeVector <- function(x = numeric()) { + m <- NULL + set <- function(y) { + x <<- y + m <<- NULL + } + get <- function() x + setmean <- function(mean) m <<- mean + getmean <- function() m + list(set = set, get = get, + setmean = setmean, + getmean = getmean) +} + + + +cachemean <- function(x, ...) { + m <- x$getmean() + if(!is.null(m)) { + message("getting cached data") + return(m) + } + data <- x$get() + m <- mean(data, ...) + x$setmean(m) + m +} \ No newline at end of file From 197994646a952cd3f931a4818bd6c4c9479612d3 Mon Sep 17 00:00:00 2001 From: spymark Date: Thu, 24 Apr 2014 20:06:23 +0100 Subject: [PATCH 02/11] testing --- test.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 test.md diff --git a/test.md b/test.md new file mode 100644 index 00000000000..e69de29bb2d From 4dfd3c94f3966bb5790c6f363b272c7f711b927c Mon Sep 17 00:00:00 2001 From: spymark Date: Thu, 24 Apr 2014 20:24:49 +0100 Subject: [PATCH 03/11] Delete test.md --- test.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 test.md diff --git a/test.md b/test.md deleted file mode 100644 index e69de29bb2d..00000000000 From 2d801d8b3154d42dd6393b2d4cb8769edcb11696 Mon Sep 17 00:00:00 2001 From: spymark Date: Thu, 24 Apr 2014 22:48:46 +0100 Subject: [PATCH 04/11] Delete script.R --- script.R | 29 ----------------------------- 1 file changed, 29 deletions(-) delete mode 100644 script.R diff --git a/script.R b/script.R deleted file mode 100644 index 5f16a81c200..00000000000 --- a/script.R +++ /dev/null @@ -1,29 +0,0 @@ -setwd("D:\\Coursera\\R programming\\progass2") - -makeVector <- function(x = numeric()) { - m <- NULL - set <- function(y) { - x <<- y - m <<- NULL - } - get <- function() x - setmean <- function(mean) m <<- mean - getmean <- function() m - list(set = set, get = get, - setmean = setmean, - getmean = getmean) -} - - - -cachemean <- function(x, ...) { - m <- x$getmean() - if(!is.null(m)) { - message("getting cached data") - return(m) - } - data <- x$get() - m <- mean(data, ...) - x$setmean(m) - m -} \ No newline at end of file From ea5a824bfe9328de6fdea65769deb28d37dbfade Mon Sep 17 00:00:00 2001 From: spymark Date: Thu, 24 Apr 2014 22:53:02 +0100 Subject: [PATCH 05/11] started --- cachematrix.R | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..595f93dbcb9 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -4,6 +4,11 @@ ## Write a short comment describing this function makeCacheMatrix <- function(x = matrix()) { + x_inv <- NULL + + set <- function(y) { + x <<- y + x_inv <<- NULL } From 0d48be10ab6ba3af18985b1a77565f90cc2fd5ec Mon Sep 17 00:00:00 2001 From: spymark Date: Thu, 24 Apr 2014 22:59:43 +0100 Subject: [PATCH 06/11] attempt on makeCacheMatrix --- cachematrix.R | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/cachematrix.R b/cachematrix.R index 595f93dbcb9..dc58cec5942 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -9,6 +9,17 @@ makeCacheMatrix <- function(x = matrix()) { set <- function(y) { x <<- y x_inv <<- NULL + } + + get <- function() x + + set_inv <- function(inv) x_inv <<- inv + + get_inv <- function() x_inv + + list(set = set, get = get, + set_inv = set_inv, + get_inv = get_inv) } From a1f1d4ddcfbfff917d983c417448c7f3bda30929 Mon Sep 17 00:00:00 2001 From: spymark Date: Thu, 24 Apr 2014 23:15:12 +0100 Subject: [PATCH 07/11] I think that should do it --- cachematrix.R | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/cachematrix.R b/cachematrix.R index dc58cec5942..a26aa550ea7 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -28,4 +28,16 @@ makeCacheMatrix <- function(x = matrix()) { cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' + + x_inv <- x$get_inv() + if(!is.null(x_inv)) { + message("getting cached data") + return(x_inv) + } + data <- x$get() + x_inv <- solve(data, ...) + x$set_inv(x_inv) + x_inv + + } From 2069a5d9876300ea4a90ab24363f842b54dca8ae Mon Sep 17 00:00:00 2001 From: spymark Date: Thu, 24 Apr 2014 23:28:39 +0100 Subject: [PATCH 08/11] need to add comments --- cachematrix.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cachematrix.R b/cachematrix.R index a26aa550ea7..58d320f7cd1 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -35,7 +35,7 @@ cacheSolve <- function(x, ...) { return(x_inv) } data <- x$get() - x_inv <- solve(data, ...) + x_inv <- solve(data) x$set_inv(x_inv) x_inv From 748b58e70af23c4e9fd8004e2b41ae655603b71b Mon Sep 17 00:00:00 2001 From: spymark Date: Sat, 26 Apr 2014 19:52:21 +0100 Subject: [PATCH 09/11] Adding comments --- cachematrix.R | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cachematrix.R b/cachematrix.R index 58d320f7cd1..de1e7fd38fd 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -3,6 +3,8 @@ ## Write a short comment describing this function +## This function calculates the inverse of a matrix +## It is assumed that the input matrix is inversible makeCacheMatrix <- function(x = matrix()) { x_inv <- NULL @@ -25,7 +27,9 @@ makeCacheMatrix <- function(x = matrix()) { ## Write a short comment describing this function - +##This function returns and prints the inverse of an input matrix. +##If the inverse has been previously calculated it serves the cached result, otherwise +## It is assumed that the input matrix is inversible cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' From 4b2552aeab5ec264826621701e1da05f1a986516 Mon Sep 17 00:00:00 2001 From: spymark Date: Sat, 26 Apr 2014 20:00:35 +0100 Subject: [PATCH 10/11] adding more comments --- cachematrix.R | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index de1e7fd38fd..43dfe121815 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,9 +1,10 @@ ## Put comments here that give an overall description of what your ## functions do -## Write a short comment describing this function -## This function calculates the inverse of a matrix +## makeCacheMatrix creates a special "matrix" object that can cache its inverse. It initially sets the cached inverse to NULL. It also creates +## a set/get function for the input matrix, and set/get functions for the inverse of the input matrix. +## Ultimately a list of the above functions is returned. ## It is assumed that the input matrix is inversible makeCacheMatrix <- function(x = matrix()) { x_inv <- NULL @@ -25,10 +26,8 @@ makeCacheMatrix <- function(x = matrix()) { } - -## Write a short comment describing this function -##This function returns and prints the inverse of an input matrix. -##If the inverse has been previously calculated it serves the cached result, otherwise +## cacheSolve returns and prints the inverse of an input matrix. +## If the inverse has been previously calculated it serves the cached result, otherwise ## It is assumed that the input matrix is inversible cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' From 90e450478d6a1a0c6b64251d82a8e9e5a77b6d49 Mon Sep 17 00:00:00 2001 From: spymark Date: Sat, 26 Apr 2014 21:57:56 +0100 Subject: [PATCH 11/11] Added comments everywhere and execution example code --- cachematrix.R | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 43dfe121815..bde31066bd3 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -2,24 +2,27 @@ ## functions do -## makeCacheMatrix creates a special "matrix" object that can cache its inverse. It initially sets the cached inverse to NULL. It also creates -## a set/get function for the input matrix, and set/get functions for the inverse of the input matrix. -## Ultimately a list of the above functions is returned. +## makeCacheMatrix takes matrix as input argument and creates a list of functions about that input argument matrix ## It is assumed that the input matrix is inversible makeCacheMatrix <- function(x = matrix()) { + ## start by setting the cached value to NULL. x_inv <- NULL + ## creating the set() function that initializes the input matrix and sets the inverse value to NULL set <- function(y) { x <<- y x_inv <<- NULL } - + ## creating the get() function that just returns the input matrix get <- function() x + ## creating the set_inv() function that can set the value for the inverse set_inv <- function(inv) x_inv <<- inv + ## creating the get_inv() function that just returns the value of the inverse get_inv <- function() x_inv + ## return a list of all the functions created list(set = set, get = get, set_inv = set_inv, get_inv = get_inv) @@ -27,20 +30,41 @@ makeCacheMatrix <- function(x = matrix()) { } ## cacheSolve returns and prints the inverse of an input matrix. -## If the inverse has been previously calculated it serves the cached result, otherwise -## It is assumed that the input matrix is inversible +## The input expected is list of functions from makeCacheMatrix, so that cachSolve can use these functions to either calculate the inverse +## or retrieve an already calculated and cached version of the inverse cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + ## Return a matrix that is the inverse of 'x' + ## retreiving cached value for the inverse using makeCachedMatrix's function get_inv() x_inv <- x$get_inv() + + ## if there is a cached value, return that and print an appropriate message if(!is.null(x_inv)) { message("getting cached data") return(x_inv) } + ## otherwise (if there is no cached inverse) get the actual input matrix using get() from makeCacheMatrix data <- x$get() + + ## calculate it's inverse at this point x_inv <- solve(data) + + ## set the cached value to the just calculated value for the inverse x$set_inv(x_inv) + + ## return the inverse x_inv } + + +## Execution example +# a <- matrix(c(1,2,3,3,4,4,5,5,5),nrow=3,ncol=3) +# df <- makeCacheMatrix(a) +# df$get() +# df$get_inv() #this returns null +# cacheSolve(df) #this calculates inv ,stores and prints it +# df$get_inv() # now inv is not null, but the previous calculated value +# cacheSolve(df) # now +