From b37c927fe8aa5006c140f3ca3b9940a54198b441 Mon Sep 17 00:00:00 2001 From: happyB Date: Sun, 13 Apr 2014 17:34:45 +0100 Subject: [PATCH 1/4] Added a comment --- cachematrix.R | 1 + 1 file changed, 1 insertion(+) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..becb6ed17ae 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -2,6 +2,7 @@ ## functions do ## Write a short comment describing this function +# A function that can create a 'matrix' object that is capable of caching its own inverse makeCacheMatrix <- function(x = matrix()) { From 269d6c52627e9d9f5fe21eaac587768d65a02102 Mon Sep 17 00:00:00 2001 From: happyB Date: Sat, 26 Apr 2014 18:05:53 +0100 Subject: [PATCH 2/4] The frist change in this file, adding 2 letters --- cachematrix.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cachematrix.R b/cachematrix.R index becb6ed17ae..4bce63c4f63 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,5 +1,5 @@ ## Put comments here that give an overall description of what your -## functions do +## functions do BF ## Write a short comment describing this function # A function that can create a 'matrix' object that is capable of caching its own inverse From 5d6867e4425afc633e9960ccbb970a834fae806b Mon Sep 17 00:00:00 2001 From: happyB Date: Sat, 26 Apr 2014 18:54:55 +0100 Subject: [PATCH 3/4] R code fully implemented and commented --- cachematrix.R | 45 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 4bce63c4f63..5216cc60aae 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,16 +1,41 @@ -## Put comments here that give an overall description of what your -## functions do BF - -## Write a short comment describing this function -# A function that can create a 'matrix' object that is capable of caching its own inverse +## A matrix inversion is a complex mathematical operation +## (see e.g. http://www.purplemath.com/modules/mtrxinvr.htm). +## The following two functions are meant to allow the repeated determination of the result +## of a matrix inversion, BUT if an inverse of a matrix has been calculated once and the original +## matrix has not changed, the functions will return the CACHED inverse of the original matrix, +## thereby saving computation time. +# The makeCacheMatrix function creates a 'matrix' object, which in reality is a LIST containing a function +# that defines 4 different calls (set, get, setinverse, getinverse). This function expects to be passed a +# "real" matrix object. makeCacheMatrix <- function(x = matrix()) { - + i <- NULL + set <- function(y) { + x <<- y + i <<- NULL + } + get <- function() x + setinverse <- function(solved) i <<- solved + getinverse <- function() i + list(set = set, get = get, + setinverse = setinverse, + getinverse = getinverse) } - -## Write a short comment describing this function - +# The cacheSolve function calculates the inverse of the "matrix" created by the makeCacheMatrix function. +# It first checks whether the inverse has already been calculated for the original matrix, and if it has +# (without the original matrix having been re-set in the meantime) it simply returns the cached inverse +# of the matrix, without new calculation. If the inverse of the matrix is not stored in the +# makeCacheMatrix function, the cacheSolve function will calculate it and cache the result in the +# makeCacheMatrix function. cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + i <- x$getinverse() + if(!is.null(i)) { + message("getting cached data") + return(i) + } + data <- x$get() + i <- solve(data, ...) + x$setinverse(i) + i } From f2dbbd31175c5935657b726391d6bdfc098df41c Mon Sep 17 00:00:00 2001 From: happyB Date: Sat, 26 Apr 2014 21:37:24 +0100 Subject: [PATCH 4/4] R code fully implemented and commented - minor update --- cachematrix.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cachematrix.R b/cachematrix.R index 5216cc60aae..0e806992e11 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -15,7 +15,7 @@ makeCacheMatrix <- function(x = matrix()) { i <<- NULL } get <- function() x - setinverse <- function(solved) i <<- solved + setinverse <- function(inverse) i <<- inverse getinverse <- function() i list(set = set, get = get, setinverse = setinverse,