From c419bb81fb4cda34fc2e804e99fa1cd935187996 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20Skaar?= Date: Sun, 22 Mar 2015 22:06:22 +0100 Subject: [PATCH 1/2] Assignment solution and example code --- cachematrix.R | 36 +++++++++++++++++++++++++++++++----- example.R | 28 ++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 5 deletions(-) create mode 100644 example.R diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..4eb3387c263 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,41 @@ -## Put comments here that give an overall description of what your -## functions do -## Write a short comment describing this function +## This file is the solution to programming assignment 2 and contains 2 functions. + +## Make a list of functions that work on a captured varible to store a matrix and a cached version of the inverse of the same matrix. +## No computation in this funxtion, only getter & setter functions that work on the captured variable. makeCacheMatrix <- function(x = matrix()) { + ## Store the cached inverse matrix "inside" this closure + cache <- NULL + + ## Getter / setter functions to return + get <- function() x + getInverse <- function() cache + setInverse <- function(m) cache <<- m + + ## return a list of functions, like the example code + list(get = get, getInverse = getInverse, setInverse = setInverse) } -## Write a short comment describing this function +## Solve the inverse of a matrix, using the closue we created with makeCacheMatrix +## If the cache is set return that. If not, solve() for matrix and return the result after setting the cache cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + + inv <- x$getInverse() # # the varibale we will return after setting it to the correct matrix + + if(is.null(inv)){ + # Nothing cached, solve and fill the cache + matrix <- x$get() + inv <- solve(matrix) + x$setInverse(inv) + } else { + # Got cached result, nothing to do + message("getting cached data") + } + + inv + } diff --git a/example.R b/example.R new file mode 100644 index 00000000000..b72e1edf91a --- /dev/null +++ b/example.R @@ -0,0 +1,28 @@ + +## Use the example from the assignment + +makeVector <- function(x = numeric()) { + m <- NULL + set <- function(y) { + x <<- y + m <<- NULL + } + get <- function() x + setmean <- function(mean) m <<- mean + getmean <- function() m + list(set = set, get = get, + setmean = setmean, + getmean = getmean) +} + +cachemean <- function(x, ...) { + m <- x$getmean() + if(!is.null(m)) { + message("getting cached data") + return(m) + } + data <- x$get() + m <- mean(data, ...) + x$setmean(m) + m +} \ No newline at end of file From 8dff0e845726ca8ba7f46832de910583359e450f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20Skaar?= Date: Sun, 22 Mar 2015 22:11:11 +0100 Subject: [PATCH 2/2] Typos --- cachematrix.R | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 4eb3387c263..7b65b497b48 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,30 +1,33 @@ -## This file is the solution to programming assignment 2 and contains 2 functions. +## This file is the solution to programming assignment 2 and contains two functions. -## Make a list of functions that work on a captured varible to store a matrix and a cached version of the inverse of the same matrix. -## No computation in this funxtion, only getter & setter functions that work on the captured variable. + +## Make a list of functions that work on a captured varible. +## Store a matrix and a cached version of the inverse of the same matrix. +## No computation in this function, only getter & setter functions that work on the captured variable. makeCacheMatrix <- function(x = matrix()) { - ## Store the cached inverse matrix "inside" this closure + # Store the cached inverse matrix "inside" this closure (stored in the environment) cache <- NULL - ## Getter / setter functions to return + # Getter / setter functions to return get <- function() x getInverse <- function() cache setInverse <- function(m) cache <<- m - ## return a list of functions, like the example code + # Return a list of functions, like the example code list(get = get, getInverse = getInverse, setInverse = setInverse) } -## Solve the inverse of a matrix, using the closue we created with makeCacheMatrix -## If the cache is set return that. If not, solve() for matrix and return the result after setting the cache +## Solve the inverse of a matrix, using the closure we created with makeCacheMatrix +## If the cache is set return that. +## If not, solve() for matrix and return the result after setting the cache cacheSolve <- function(x, ...) { - inv <- x$getInverse() # # the varibale we will return after setting it to the correct matrix + inv <- x$getInverse() # the varibale we will return after setting it to the correct matrix if(is.null(inv)){ # Nothing cached, solve and fill the cache