From ebb7acb5c290ada278ac9f13b541258fff394a44 Mon Sep 17 00:00:00 2001 From: triharder Date: Tue, 22 Apr 2014 21:25:38 +0000 Subject: [PATCH 1/4] Update cachematrix.R --- cachematrix.R | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..4d47999b160 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,25 @@ -## Put comments here that give an overall description of what your -## functions do +## This second programming assignment will require you to write an R function is able to cache +## potentially time-consuming computations. For example, taking the mean of a numeric vector is +## typically a fast operation. However, for a very long vector, it may take too long to compute +## the mean, especially if it has to be computed repeatedly (e.g. in a loop). If the contents of +## a vector are not changing, it may make sense to cache the value of the mean so that when we +## need it again, it can be looked up in the cache rather than recomputed. In this Programming +## Assignment will take advantage of the scoping rules of the R language and how they can be +## manipulated to preserve state inside of an R object. -## Write a short comment describing this function +## This function creates a special "matrix" object that can cache its inverse. makeCacheMatrix <- function(x = matrix()) { } -## 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' + + return solve(x) } From 360ea402d23f8bdae7fb5cde3a44aed453ae1822 Mon Sep 17 00:00:00 2001 From: triharder Date: Wed, 23 Apr 2014 02:06:53 +0000 Subject: [PATCH 2/4] Update cachematrix.R --- cachematrix.R | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 4d47999b160..4a277c8b44a 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -10,7 +10,17 @@ ## This function creates a special "matrix" object that can cache its inverse. makeCacheMatrix <- function(x = matrix()) { - + m <- NULL + set <- function(y) { + x <<- y + m <<- NULL + } + get <- function() x + setInverse <- function(inverse) m <<- inverse + getInverse <- function() m + list(set = set, get = get, + setInverse = setInverse, + getInverse = getInverse) } @@ -20,6 +30,13 @@ makeCacheMatrix <- function(x = matrix()) { cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' - - return solve(x) + m <- getInverse() + if(!is.null(m)){ + message("Checking cache..." + return(m) + } + data <- x$get() + m <- solve(data,...) + x$setInverse(m) + m } From 420a994f0b002fca1f8eedb9404c9d0daa6c482f Mon Sep 17 00:00:00 2001 From: triharder Date: Wed, 23 Apr 2014 21:58:27 +0000 Subject: [PATCH 3/4] Update cachematrix.R --- cachematrix.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cachematrix.R b/cachematrix.R index 4a277c8b44a..945b0893787 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -36,7 +36,7 @@ cacheSolve <- function(x, ...) { return(m) } data <- x$get() - m <- solve(data,...) + m <- solve(data) x$setInverse(m) m } From b8fe311451469d5c46c788dce0a7149f41de610b Mon Sep 17 00:00:00 2001 From: triharder Date: Sat, 22 Aug 2015 22:30:18 -0400 Subject: [PATCH 4/4] Update cachematrix.R --- cachematrix.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cachematrix.R b/cachematrix.R index 945b0893787..00df4c3b87f 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -32,7 +32,7 @@ cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' m <- getInverse() if(!is.null(m)){ - message("Checking cache..." + message("Checking cache...") return(m) } data <- x$get()