From c9b424f3259c6f772fba5c989d661ea6fb76f5fb Mon Sep 17 00:00:00 2001 From: KevinFolan Date: Thu, 19 Jun 2014 14:00:46 -1000 Subject: [PATCH] Added first pass code Added comments and first pass at code --- cachematrix.R | 55 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 46 insertions(+), 9 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..7d6d3ac0bfe 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,52 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function +## Function makeCacheMatrix +## +## Purpose: +## Create a special "matrix" object that can cache its inverse. +## +## On Entry: +## x - matrix +## +## One Exit: +## returns an object that sotres a numeric vector and caches it inverse +## +## makeCacheMatrix <- function(x = 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) + } - -## Write a short comment describing this function - +## Function cacheSolve +## +## Purpose: +## Computes the inverse of the special "matrix" returned by makeCacheMatrix. +## If the inverse has already been calculated (and the matrix has not changed), then the cachesolve +## should retrieve the inverse from the cache. +## +## On Entry: +## Params: x matrix +## '...' to pass through to solve +## On Exit: +## Returns a matrix that is the inverse of 'x' +## cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + m <- x$getinverse() + if(!is.null(m)) { + message("getting cached data") + return(m) + } + data <- x$get() + m <- inverse(data, ...) + x$setinverse(m) + m }