From 3790cd879e73ec8dc7afef912c97b1940116cf53 Mon Sep 17 00:00:00 2001 From: Fraser Hewson Date: Sun, 24 Aug 2014 12:26:48 +0200 Subject: [PATCH 1/5] Add .gitignore and project file --- .gitignore | 3 +++ ProgrammingAssignment2.Rproj | 13 +++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 .gitignore create mode 100644 ProgrammingAssignment2.Rproj diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000000..807ea251739 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.Rproj.user +.Rhistory +.RData diff --git a/ProgrammingAssignment2.Rproj b/ProgrammingAssignment2.Rproj new file mode 100644 index 00000000000..8e3c2ebc99e --- /dev/null +++ b/ProgrammingAssignment2.Rproj @@ -0,0 +1,13 @@ +Version: 1.0 + +RestoreWorkspace: Default +SaveWorkspace: Default +AlwaysSaveHistory: Default + +EnableCodeIndexing: Yes +UseSpacesForTab: Yes +NumSpacesForTab: 2 +Encoding: UTF-8 + +RnwWeave: Sweave +LaTeX: pdfLaTeX From f54b28ad9a75d8f9ca99831d49e67e5704d8477c Mon Sep 17 00:00:00 2001 From: Fraser Hewson Date: Sun, 24 Aug 2014 12:29:00 +0200 Subject: [PATCH 2/5] implemented makeCacheMatrix and cacheSolve --- cachematrix.R | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..3c1d664d5c9 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,40 @@ -## Put comments here that give an overall description of what your -## functions do +## The following functions allow for the creation of a matrix where the invers +## is cached after it has been calculated. This prevents the re-execution +## of a rather expensive operation. -## Write a short comment describing this function - -makeCacheMatrix <- function(x = matrix()) { +## makeCacheMatrix creates a 'special' matrix object that stores the +## matrix as well as its inverce. Use this matrix with cacheSolve() +makeCacheMatrix <- function(m.orig = matrix()) { + m.result <- NULL + set <- function(m.set) { + m.orig <<- m.set + m.result <<- NULL + } + get <- function() m.orig + setresult <- function(result) m.result <<- result + getresult <- function() m.result + list(set = set, get = get, + setresult = setresult, + getresult = getresult) } -## Write a short comment describing this function +## cacheSolve finds the inverse of the matrix created by makeCacheMatrix(). +## The inverse of the matrix is only calculated once. On subsequent calls, the +## cached result is used. cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + mcache <- NULL + mlist <- x + + m <- mlist$getresult() + if(!is.null(m)){ + print("using cached data") + return(m) + } + data <- mlist$get() + result <- solve(data, ...) + mlist$setresult(result) + result } From 50363f2e8dacaad48a1d9ee8afd0c1fc1e2ad266 Mon Sep 17 00:00:00 2001 From: Fraser Hewson Date: Sun, 24 Aug 2014 12:39:03 +0200 Subject: [PATCH 3/5] Change print to message --- cachematrix.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cachematrix.R b/cachematrix.R index 3c1d664d5c9..d5b101e25e2 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -30,7 +30,7 @@ cacheSolve <- function(x, ...) { m <- mlist$getresult() if(!is.null(m)){ - print("using cached data") + message("Using cached data") return(m) } data <- mlist$get() From 94afcbb87938612a09ad38cfbf6ff0377ca9b336 Mon Sep 17 00:00:00 2001 From: Fraser Hewson Date: Sun, 24 Aug 2014 12:50:47 +0200 Subject: [PATCH 4/5] Add comments --- cachematrix.R | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index d5b101e25e2..e6e6d5184ba 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -6,14 +6,18 @@ ## matrix as well as its inverce. Use this matrix with cacheSolve() makeCacheMatrix <- function(m.orig = matrix()) { - m.result <- NULL + m.result <- NULL #when created the result is not available set <- function(m.set) { - m.orig <<- m.set - m.result <<- NULL + m.orig <<- m.set #save the new matrix + m.result <<- NULL #clear the result } - get <- function() m.orig - setresult <- function(result) m.result <<- result - getresult <- function() m.result + + get <- function() m.orig #return the original + + setresult <- function(result) m.result <<- result #save a result matrix + + getresult <- function() m.result #return the result matrix + list(set = set, get = get, setresult = setresult, getresult = getresult) @@ -25,16 +29,17 @@ makeCacheMatrix <- function(m.orig = matrix()) { ## cached result is used. cacheSolve <- function(x, ...) { - mcache <- NULL - mlist <- x + mcache <- NULL #Cached matrix is initially null - m <- mlist$getresult() + m <- x$getresult() #Get the cached matrix and check if it exists if(!is.null(m)){ message("Using cached data") - return(m) + return(m) #Use and return the cached matrix if found } - data <- mlist$get() - result <- solve(data, ...) - mlist$setresult(result) - result + + # Cached result was not found... Calculate + data <- x$get() #Get the initial matrix + result <- solve(data, ...) #Solve it + x$setresult(result) #Cache it + result #Return it } From 6c032cbbd8fd6b2372dd6c0ccc7aec0e1b9f55cf Mon Sep 17 00:00:00 2001 From: Fraser Hewson Date: Sun, 24 Aug 2014 12:51:51 +0200 Subject: [PATCH 5/5] Assignment complete --- cachematrix.R | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cachematrix.R b/cachematrix.R index e6e6d5184ba..5e40648eeeb 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -43,3 +43,5 @@ cacheSolve <- function(x, ...) { x$setresult(result) #Cache it result #Return it } + +#done