From 67fd9a71e817ce60cb1fa2d112c074a228ef4064 Mon Sep 17 00:00:00 2001 From: Steve Knight Date: Mon, 15 Sep 2014 08:50:02 +0100 Subject: [PATCH 1/2] Update cachematrix.R Initial version --- cachematrix.R | 43 +++++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..fb875609d53 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,38 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function - +## Make a cache matrix with an initially unset inverse makeCacheMatrix <- function(x = matrix()) { - + # Inverse is initially null + i <- NULL + set <- function(y) { + # Set the data and the inverse into the parent scope + x <<- y + i <<- NULL + } + # Get the value of the matrix + get <- function() x + # Set the *cached* inverse of the matrix + setinv <- function(inv) i <<- inv + # Get the *cached* inverse of the matrix + getinv <- function() i + # Return a named-list with the data and functions embedded in it + list(set = set, get = get, + setinv = setinv, + getinv = getinv) } - -## Write a short comment describing this function - +## Memoize the solve function so as to provide a cached version of the inverse, if available, otherwise +## calculate, cache and return the inverse. cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + # If the inverse is already calculated return it + i <- x$getinv() + if (!is.null(i)) { + message("getting cached data") + return(i) + } + # Retrieve the data from the 'matrix' + data <- x$get() + # Solve the matrix to retrieve the inverse + i <- solve(data, ...) + x$setinv(i) + i } + From 6e30e64560f8227c1bf77f688e08fb6d3e1cca2c Mon Sep 17 00:00:00 2001 From: Steve Knight Date: Mon, 15 Sep 2014 08:51:32 +0100 Subject: [PATCH 2/2] Changed comments --- cachematrix.R | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index fb875609d53..b103d0f4918 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,4 +1,4 @@ -## Make a cache matrix with an initially unset inverse +## Make a cached 'matrix' with an initially unset inverse makeCacheMatrix <- function(x = matrix()) { # Inverse is initially null i <- NULL @@ -22,7 +22,7 @@ makeCacheMatrix <- function(x = matrix()) { ## Memoize the solve function so as to provide a cached version of the inverse, if available, otherwise ## calculate, cache and return the inverse. cacheSolve <- function(x, ...) { - # If the inverse is already calculated return it + # Get the inverse, if the inverse is already calculated return it i <- x$getinv() if (!is.null(i)) { message("getting cached data") @@ -30,8 +30,9 @@ cacheSolve <- function(x, ...) { } # Retrieve the data from the 'matrix' data <- x$get() - # Solve the matrix to retrieve the inverse + # Solve the matrix to retrieve the inverse, passing the additional options from the caller i <- solve(data, ...) + # Store the result x$setinv(i) i }