From 9cc33e18aa86a7d60fbb305e4afc1d5e969c5c8d Mon Sep 17 00:00:00 2001 From: ribery77 Date: Tue, 19 Aug 2014 14:30:15 +0300 Subject: [PATCH 1/2] Revert "More typos" This reverts commit 7f657dd22ac20d22698c53b23f0057e1a12c09b7. --- README.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 7a8c502a4be..81bba09417d 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,16 @@ ### Introduction This second programming assignment will require you to write an R -function that is able to cache potentially time-consuming computations. -For example, taking the mean of a numeric vector is typically a fast +function that 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 you 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. +Programming Assignment you 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. ### Example: Caching the Mean of a Vector @@ -76,8 +76,8 @@ Write the following functions: that can cache its inverse. 2. `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 - `cacheSolve` should retrieve the inverse from the cache. + already been calculated (and the matrix has not changed), then the + `cachesolve` should retrieve the inverse from the cache. Computing the inverse of a square matrix can be done with the `solve` function in R. For example, if `X` is a square invertible matrix, then From 02d5dba497ba70265c3a9f3f35ebae8435999283 Mon Sep 17 00:00:00 2001 From: ribery77 Date: Tue, 19 Aug 2014 17:08:00 +0300 Subject: [PATCH 2/2] Caching the Inverse of a Matrix first version --- cachematrix.R | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..b958dd83038 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -4,7 +4,20 @@ ## Write a short comment describing this function makeCacheMatrix <- function(x = matrix()) { - + ## Creates a list of functions that + ## can cache the inverse of a 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) + } @@ -12,4 +25,26 @@ makeCacheMatrix <- function(x = matrix()) { cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' + + m <- x$getInverse() + if ( ! is.null(m)) { + print("getting cached data") + return(m) + } + m <- solve(x$get()) + x$setInverse(m) + m } + +## Test cases + +a <- makeCacheMatrix(matrix(1:4,2)) +a$get() +a$getInverse() +a$set(matrix(5:8,2)) +a$get() +cacheSolve(a) +cacheSolve(a) +a$getInverse() +b = a$getInverse() +a$get() %*% b