From e1f23daedc5ef9f5b1976e9d73ba808e457eb132 Mon Sep 17 00:00:00 2001 From: Joel M Date: Sat, 19 Mar 2016 13:41:45 +0000 Subject: [PATCH 1/3] Initial Commit. --- cachematrix.R | 46 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..f05ed77d8e7 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,45 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function +## cachematrix() contains convenience functions for +## matrix storage and inversion. The matirx 'x' will have +## its inverse calculated once. If the inverse has already +## been calculated (and the matrix has not changed), then +## the cachesolve() should retrieve the inverse from the cache. +## This function creates a special "matrix" object +## that can cache its inverse makeCacheMatrix <- function(x = matrix()) { - + im <- NULL + + set <- function(y){ + x <- y + im <<- NULL + } + + get <- function() x + setmatrix <- function(solve) im <<- solve + getmatrix <- function() im + + list( set=set, get=get, + setmatrix=setmatrix, + getmatrix=getmatrix) } -## Write a short comment describing this function - +## 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' + ## This function assume that the matrix supplied is + ## always invertible. + im <- x$getmatrix() + if(!is.null(im)){ + message("getting cached data...") + return (im) + } + m <- x$get() + im <- solve(m, ...) + x$setmatrix(im) + ## Return a matrix that is the inverse of 'x' + im } From 0d22d72b84364958752432b0bb82b1a870bcbba8 Mon Sep 17 00:00:00 2001 From: Joel M Date: Sat, 19 Mar 2016 13:53:58 +0000 Subject: [PATCH 2/3] tidy up.. --- cachematrix.R | 1 - 1 file changed, 1 deletion(-) diff --git a/cachematrix.R b/cachematrix.R index f05ed77d8e7..0101d2f46ac 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -34,7 +34,6 @@ cacheSolve <- function(x, ...) { ## always invertible. im <- x$getmatrix() if(!is.null(im)){ - message("getting cached data...") return (im) } m <- x$get() From be9d12453f1803cbe650dd3b08dc4b7e8c37f4fd Mon Sep 17 00:00:00 2001 From: Joel M Date: Sat, 19 Mar 2016 19:12:50 +0000 Subject: [PATCH 3/3] final revision --- cachematrix.R | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cachematrix.R b/cachematrix.R index 0101d2f46ac..2d64a90c1df 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -32,13 +32,16 @@ makeCacheMatrix <- function(x = matrix()) { cacheSolve <- function(x, ...) { ## This function assume that the matrix supplied is ## always invertible. + im <- x$getmatrix() if(!is.null(im)){ + message("Getting cached data..") return (im) } - m <- x$get() + m <- x$get() im <- solve(m, ...) x$setmatrix(im) + ## Return a matrix that is the inverse of 'x' im }