From c014972cb1483cbf644a889ebc8cdd2dcb3dcc4b Mon Sep 17 00:00:00 2001 From: Sohail Munir Khan Date: Tue, 12 May 2015 21:15:45 +1000 Subject: [PATCH 1/3] Added RStudio project as well as the support for Git. It created a .gitignore that works well for R projects --- .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 2f815bf459f4edb3e6d2f702b70ea0fa586bd6e5 Mon Sep 17 00:00:00 2001 From: Sohail Munir Khan Date: Tue, 12 May 2015 21:16:39 +1000 Subject: [PATCH 2/3] First commit to create inverse of matrix as well as cache it for future use (when / if needed) --- cachematrix.R | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..93a7c61d6eb 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -4,12 +4,31 @@ ## Write a short comment describing this function makeCacheMatrix <- function(x = matrix()) { - + cachedMatrixInverse <- NULL + set <- function(y) { + x <<- y + cachedMatrixInverse <<- NULL + } + get <- function() x + setMatrixInverse <- function(inv) cachedMatrixInverse <<- inv + getMatrixInverse <- function() cachedMatrixInverse + list(set = set, get = get, + setMatrixInverse = setMatrixInverse, + getMatrixInverse = getMatrixInverse) } ## Write a short comment describing this function cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + ## Return a matrix that is the inverse of 'x' + matrixInverse <- x$getMatrixInverse() + if(!is.null(matrixInverse)) { + message("found cached matrix inverse") + return(matrixInverse) + } + matrix <- x$get() + calculatedMatrixInverse <- solve(matrix) + x$setMatrixInverse(calculatedMatrixInverse) + calculatedMatrixInverse } From cf775bc703fe2f4dd3b7f6b21eeb66f7184149cb Mon Sep 17 00:00:00 2001 From: Sohail Munir Khan Date: Tue, 12 May 2015 23:04:36 +1000 Subject: [PATCH 3/3] Comments for the entire file. A usage section with short comments for each of the two functions: makeCacheMatrix and cacheSolve --- cachematrix.R | 60 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 43 insertions(+), 17 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 93a7c61d6eb..1423b8e1274 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,34 +1,60 @@ -## Put comments here that give an overall description of what your -## functions do +## Couple of functions to optimize inversing matrixes as it could be fairly +## computational work to find the inverse of a large matrix. Usage below: +## +## [Inspired by Johanna Tikanmäki COMMUNITY TA usage for cachemean on +## https://class.coursera.org/rprog-014/forum/thread?thread_id=105] +## +## cMatrix <- makeCacheMatrix(m) #first a cache aware matrix where 'm' is the +## #matrix you would like to use as input +## cacheSolve(cMatrix) #calculate Inverse (solve) of the matrix +## cacheSolve(cMatrix) #this will return the cached matrix inverse +## cMatrix$set(new_m) #cMatrix can be set to a new input matrix ('new_m') +## cacheSolve(cMatrix) #calculate Inverse of new_m for the first time +## cacheSolve(cMatrix) #this will return the cached inverse +## cacheSolve(cMatrix) #same result as above; still cached -## Write a short comment describing this function -makeCacheMatrix <- function(x = matrix()) { - cachedMatrixInverse <- NULL +## Create a cached aware matrix that that be reused with new matrixes as well + +makeCacheMatrix <- function(m = matrix()) { + cachedMatrixInverse <- NULL #cached matrix inverse value if set set <- function(y) { - x <<- y - cachedMatrixInverse <<- NULL + m <<- y #set new value for raw matrix 'm' set + #at makeCacheMatrix function level using + #<<- + cachedMatrixInverse <<- NULL #clear cached as 'm' has changed at the + #parent frame of this function } - get <- function() x + get <- function() m #get raw matrix 'm' + + #functions below: set & get cached matrix value for makeCacheMatrix's + #cacheMatrixInverse variable setMatrixInverse <- function(inv) cachedMatrixInverse <<- inv getMatrixInverse <- function() cachedMatrixInverse + + #return a list containing various functions that can be used to: + #get raw matrix 'm' + #set raw matrix that will clear matrix inverse as well + #getMatrixInverse to get cachedMatrixInverse + #setMatrixInverse to set cachedMatrixInverse list(set = set, get = get, setMatrixInverse = setMatrixInverse, getMatrixInverse = getMatrixInverse) } -## Write a short comment describing this function +## Solve (Inverse) and cache matrix. Has to be used as shown in the usage +## section. 'x' has to be an object returned by makeCacheMatrix function cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' - matrixInverse <- x$getMatrixInverse() - if(!is.null(matrixInverse)) { - message("found cached matrix inverse") - return(matrixInverse) + matrixInverse <- x$getMatrixInverse() #try retrieving cached value + if(!is.null(matrixInverse)) { #if we find the value + message("found cached matrix inverse") #message we have cached value + return(matrixInverse) #return from this function } - matrix <- x$get() - calculatedMatrixInverse <- solve(matrix) - x$setMatrixInverse(calculatedMatrixInverse) + matrix <- x$get() #get actual matrix data + calculatedMatrixInverse <- solve(matrix) #use solve from {base} package + x$setMatrixInverse(calculatedMatrixInverse) #set cached value for x + ## Return a matrix that is the inverse of 'x' calculatedMatrixInverse }