From 2c893bf16ce8671529b843ba044cc5da9e702066 Mon Sep 17 00:00:00 2001 From: tim Lantz Date: Sun, 13 Apr 2014 17:36:26 -0400 Subject: [PATCH 1/3] adding cached matrix functions --- cachematrix.R | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..a4f099c4db0 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -4,12 +4,33 @@ ## Write a short comment describing this function makeCacheMatrix <- function(x = matrix()) { - + inv <- NULL + set <- function(omx) { + mx <<- omx + inv <<- NULL + } + get <- function() x + setinverse <- function(inverse) inv <<-inverse + getinverse <- function() inv + list( + set = set, + get = get + setinverse = setinverse, + getinverse = getinverse + ) } ## Write a short comment describing this function cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + inv <- x$getinverse() + if (!is.null(inv)) { + message('getting cached inverse') + } else { + data <- x$get() + inv <- solve(data, ...) + x$setinverse(inv) + } + inv } From 285282e1f114e947892fddc15f1b098c9acd6138 Mon Sep 17 00:00:00 2001 From: tim Lantz Date: Sun, 13 Apr 2014 17:36:49 -0400 Subject: [PATCH 2/3] fixing missing comma --- cachematrix.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cachematrix.R b/cachematrix.R index a4f099c4db0..ee4c11f6e4f 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -14,7 +14,7 @@ makeCacheMatrix <- function(x = matrix()) { getinverse <- function() inv list( set = set, - get = get + get = get, setinverse = setinverse, getinverse = getinverse ) From 288836f4847b579bc334272ab069b6c5360a80e2 Mon Sep 17 00:00:00 2001 From: tim Lantz Date: Sun, 13 Apr 2014 18:01:56 -0400 Subject: [PATCH 3/3] commenting and fixing bad variable name in set --- cachematrix.R | 50 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index ee4c11f6e4f..6f00d8da975 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,16 +1,35 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function +## Functions for wrapping a matrix with inverse caching functionality as specified by +## programming assignment 2 of the R Programming Coursera course. +## +## makeCacheMatrix wraps a regular matrix with cache goodness +## cacheSolve wrapper for solve() that leverages caching functionality +## +## Note: Google commenting style followed (function comments below first line). +## See http://google-styleguide.googlecode.com/svn/trunk/Rguide.xml. makeCacheMatrix <- function(x = matrix()) { + # Wraps a matrix with capabilities to cache its inverse. + # + # Args: + # x: An matrix (assumed to be invertible). Default is the empty matrix. + # + # Returns: + # A list with accessors for the matrix as follows. + # + # v.set(x) sets the underlying matrix (clears the cache) + # v.get() retrieves the underlying matrix + # v.setinverse(xp) sets the inverse cache + # v.getinverse() gets the the cached inverse or NULL if not set + + # inv will be the variable for storing the cached inverse inv <- NULL set <- function(omx) { - mx <<- omx + # the matrix has changed, wipe out the inverse as well + x <<- omx inv <<- NULL } get <- function() x - setinverse <- function(inverse) inv <<-inverse + setinverse <- function(inverse) inv <<- inverse getinverse <- function() inv list( set = set, @@ -20,17 +39,30 @@ makeCacheMatrix <- function(x = matrix()) { ) } - -## Write a short comment describing this function - cacheSolve <- function(x, ...) { + # Computers or fetches the cached inverse of x. + # + # Args: + # x: A wrapped matrix with caching functionality created with + # createCacheMatrix. + # ...: Extra arguments for solve. Note that these arguments will not get recheceked + # when accessing the cache. In other words two calls with different "..." args + # will get you the same matrix. This is consistent with the assignment vector + # example. + # + # Returns: + # The inverse of x's underlying matrix. inv <- x$getinverse() if (!is.null(inv)) { + # like the vector example, show a message when we get from cache + # inv already points to the cached inverse message('getting cached inverse') } else { + # compute inv and cache it data <- x$get() inv <- solve(data, ...) x$setinverse(inv) } inv } +