diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..c822d724606 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,68 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function +# The two functions here work together to allow cached values for the inversion +# of a Matrix. The Matrix is created with makeCacheMatrix() which returns a List +# of functions to use when manipulating the Matrix. The cacheSolve(matrix) +# function will return the cached inversion or solve a new inversion if required. +# The makeCacheMatrix() function creates a special matrix which is really a +# list containing functions to: +# * get the value of the matrix +# * set the value of the matrix +# * get the value of the solved matrix +# * set the value of the solved matrix +# +# Returns: +# A List of functions used to manipulate the matrix +# +# Sample Use: +# x <- makeCacheMatrix() +# x$set(matrix(c(1,2,11 ,12), ncol=2, nrow=2, byrow=TRUE)) sets the matrix. +# x$get() returns the matrix data +# x$getsolve() returns the inverted matrix +# x$setsolve([inverted matrix]) sets, and caches, the inverted matrix. makeCacheMatrix <- function(x = matrix()) { - + # Intialize the solved matrix inversion variable + s <- NULL + # define the set() function to set the matrix value and re-set the inversion + set <- function(y) { + x <<- y + s <<- NULL + } + # define the get() function to return the matrix + get <- function() x + # define the setsolve() function to set the solved matrix value + setsolve <- function(slv) s <<- slv + # define the getsolve() function to get the solved matrix value + getsolve <- function() s + # return a List of functions to use when manipulating the matrix + list(set = set, get = get, + setsolve = setsolve, + getsolve = getsolve) } - -## Write a short comment describing this function - +# cacheSolve() inverts the matrix created with makeCacheMatrix(). +# It will first check to see if the inverted matrix has already been +# solved. If so, it returns the previously inverted matrix. Otherwise, +# it solves the inversion and saves it. +# +# Returns: +# An inverted matrix. +# +# Sample Use (x is created with the above makeCacheMatrix()): +# inversion <- cacheSolve(x) will return the cached inversion or solve new. cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + # Get the previously solved value + s <- x$getsolve() + # return the solved value if we have one. Give a message too. + if(!is.null(s)) { + message("getting cached data") + return(s) + } + # We need to solve the matrix inversion. So get the data.. + data <- x$get() + # Solve it.. + s <- solve(data, ...) + # and set the value + x$setsolve(s) + # Return a matrix that is the inverse of 'x' + s } diff --git a/cachematrix_test.R b/cachematrix_test.R new file mode 100644 index 00000000000..170345afd94 --- /dev/null +++ b/cachematrix_test.R @@ -0,0 +1,10 @@ +source('cachematrix.R') + +x <- makeCacheMatrix() +x$set(matrix(c(1,2,11,12), ncol=2, nrow=2, byrow=TRUE)) +message("Matrix:") +print(x$get()) +message("Solving...") +print(cacheSolve(x)) +message("and again...") +print(cacheSolve(x)) diff --git a/cachemean.R b/cachemean.R new file mode 100644 index 00000000000..92130cbcd96 --- /dev/null +++ b/cachemean.R @@ -0,0 +1,34 @@ +# The first function, makeVector creates a special "vector", which is really a list containing a function to +# +# set the value of the vector +# get the value of the vector +# set the value of the mean +# get the value of the mean + +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) +} + +# The following function calculates the mean of the special "vector" created with the above function. However, it first checks to see if the mean has already been calculated. If so, it gets the mean from the cache and skips the computation. Otherwise, it calculates the mean of the data and sets the value of the mean in the cache via the setmean function. + +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 +} diff --git a/cachemean_test.R b/cachemean_test.R new file mode 100644 index 00000000000..54e946e44c9 --- /dev/null +++ b/cachemean_test.R @@ -0,0 +1,13 @@ +source('cachemean.R') + +x <- makeVector() +print("Vector Empty") +print(cachemean(x)) +print("and again...") +print(cachemean(x)) +print('') +print("Vector 1:50") +x$set(1:50) +print(cachemean(x)) +print("and again...") +print(cachemean(x)) \ No newline at end of file