From 262f47344c84e496e446ad5d03e4700d1c55e6fb Mon Sep 17 00:00:00 2001 From: Frank Murphy Date: Sat, 25 Jul 2015 12:08:58 -0500 Subject: [PATCH 1/2] Frank Murphy Programming assignment 2 --- .Rhistory | 0 README.md | 12 ++++++------ cachematrix.R | 39 ++++++++++++++++++++++++++++++++------- 3 files changed, 38 insertions(+), 13 deletions(-) create mode 100644 .Rhistory diff --git a/.Rhistory b/.Rhistory new file mode 100644 index 00000000000..e69de29bb2d diff --git a/README.md b/README.md index 7a8c502a4be..0afde47c11b 100644 --- a/README.md +++ b/README.md @@ -30,15 +30,15 @@ really a list containing a function to makeVector <- function(x = numeric()) { - m <- NULL - set <- function(y) { + m <- NULL # clear numeric vector m + set <- function(y) { # store y in x and NULL in m offsite. x <<- y m <<- NULL } - get <- function() x - setmean <- function(mean) m <<- mean - getmean <- function() m - list(set = set, get = get, + get <- function() x # function to get variable x + setmean <- function(mean) m <<- mean # Function to set m + getmean <- function() m # Function to return m + list(set = set, get = get, # returns a list of four functions setmean = setmean, getmean = getmean) } diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..d9c3817aec7 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,40 @@ -## Put comments here that give an overall description of what your -## functions do +## makeCacheMatrix contains four anonymous functions for solving, caching, and +## retrieving the inverse of a matrix. +## cacheSolve solves inverse or obtains cached matrix of the solved inverse -## Write a short comment describing this function +## makeCacheMatrix contains four anonymous functions for solving, caching, and +## retrieving the inverse of a matrix. makeCacheMatrix <- function(x = matrix()) { - + m <- NULL # Clear m at the beginning of the run + set <- function(y) { # create set anonymous function storing whatever is + x <<- y # passed to set into local variable x + m <<- NULL # clear local variable m + } + get <- function() x # get local variable x + # setsolve is an anonymous function to solve the inverse of the matrix that is passed + setsolve <- function(solve) m <<- solve + getsolve <- function() m #getsolve gets and returns cache of solved matrix + list(set = set, get = get, # return list of four functions + setsolve = setsolve, + getsolve = getsolve) } -## Write a short comment describing this function +## makeCacheMatrix contains four anonymous functions for solving, caching, and +## retrieving the inverse of a matrix. -cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' +cacheSolve <- function(x, ...) { # Receives matrix x and any arguments + ## Return a matrix that is the inverse of 'x' + # Run getsolve function on object x and assign value to m + m <- x$getsolve() + ## not null indicates data cached already + if(!is.null(m)) { + message("getting inverse matrix") + return(m) # Cached data retrieved with getsolve, so just return it + } + data <- x$get() # store calling variable x into local variable data + m <- solve(data, ...) # solve matrix data and assign into m + x$setsolve(m) # cache the solved matrix m into calling object x + m # return m } From 0555e24dbff6ff5f322dd3d96cc13d4d9c47edbb Mon Sep 17 00:00:00 2001 From: Frank Murphy Date: Sun, 26 Jul 2015 07:30:01 -0500 Subject: [PATCH 2/2] Gives credit to the program instructions --- cachematrix.R | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cachematrix.R b/cachematrix.R index d9c3817aec7..ecc9d4950ad 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -2,6 +2,9 @@ ## retrieving the inverse of a matrix. ## cacheSolve solves inverse or obtains cached matrix of the solved inverse +## *** Much of these functions have been adapted from the instructions for the +## problem at hand. + ## makeCacheMatrix contains four anonymous functions for solving, caching, and ## retrieving the inverse of a matrix.