From e3abb1ad30f99db1897eb6652ab95de3d553e882 Mon Sep 17 00:00:00 2001 From: Stuart Woodward Date: Mon, 20 Apr 2015 15:40:21 +0900 Subject: [PATCH 1/4] Added comments for the functions defined. --- cachematrix.R | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..ccedf6c21f8 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,14 +1,15 @@ ## Put comments here that give an overall description of what your ## functions do -## Write a short comment describing this function +## makeCacheMatrix: This function creates a special "matrix" object that can cache its inverse. makeCacheMatrix <- function(x = matrix()) { } - -## Write a short comment describing this function +## cacheSolve: This function computes the inverse of the special "matrix" returned by makeCacheMatrix above. +## If the inverse has already been calculated (and the matrix has not changed), then the cachesolve should retrieve the +## inverse from the cache. cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' From 425a8562b0ab23d6227e594f06ca27c0ce006818 Mon Sep 17 00:00:00 2001 From: Stuart Woodward Date: Thu, 23 Apr 2015 11:12:37 +0900 Subject: [PATCH 2/4] Initial try..w --- cachematrix.R | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/cachematrix.R b/cachematrix.R index ccedf6c21f8..dd1a09c3a4e 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -5,6 +5,18 @@ makeCacheMatrix <- function(x = matrix()) { + i <- NULL + set <- function(y) { + x <<- y + <<- NULL + } + get <- function() x + setinverse <- function(inverse) i <<- inverse + getinverse <- function() i + list(set = set, get = get, + setinverse = setinverse, + getinverse = getinverse) + } ## cacheSolve: This function computes the inverse of the special "matrix" returned by makeCacheMatrix above. @@ -13,4 +25,15 @@ makeCacheMatrix <- function(x = matrix()) { cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' + + i <- x$getInverse() + if(!is.null(i)) { + message("getting cached data") + return(m) + } + data <- x$get() + i <- solve(data, ...) + x$setinverse(i) + i + } From db7ab09d9627436afc6fbf1e0b532ef675168a93 Mon Sep 17 00:00:00 2001 From: Stuart Woodward Date: Thu, 23 Apr 2015 12:22:46 +0900 Subject: [PATCH 3/4] Bug fixes... --- cachematrix.R | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index dd1a09c3a4e..099ea6983fc 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -8,14 +8,15 @@ makeCacheMatrix <- function(x = matrix()) { i <- NULL set <- function(y) { x <<- y - <<- NULL + i <<- NULL } get <- function() x setinverse <- function(inverse) i <<- inverse getinverse <- function() i list(set = set, get = get, setinverse = setinverse, - getinverse = getinverse) + getinverse = getinverse + ) } @@ -26,10 +27,10 @@ makeCacheMatrix <- function(x = matrix()) { cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' - i <- x$getInverse() + i <- x$getinverse() if(!is.null(i)) { message("getting cached data") - return(m) + return(i) } data <- x$get() i <- solve(data, ...) From 24963b3ea243e1997f1ee8836bd8ffe12f847c2b Mon Sep 17 00:00:00 2001 From: Stuart Woodward Date: Thu, 23 Apr 2015 12:38:27 +0900 Subject: [PATCH 4/4] Removed request for comments.. --- cachematrix.R | 3 --- 1 file changed, 3 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 099ea6983fc..cb2188b237b 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,6 +1,3 @@ -## Put comments here that give an overall description of what your -## functions do - ## makeCacheMatrix: This function creates a special "matrix" object that can cache its inverse. makeCacheMatrix <- function(x = matrix()) {