From f970b7d7b12f72657717a7afdfe6e5c2faffcbbb Mon Sep 17 00:00:00 2001 From: Trent Albright Date: Sat, 22 Nov 2014 15:20:50 -0500 Subject: [PATCH] completion of assignment --- cachematrix.R | 63 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 52 insertions(+), 11 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..053fcd4b5cf 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,56 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function - -makeCacheMatrix <- function(x = matrix()) { +## This module provides inverse operations for a matrix, but +## improves performance by caching intermediary results +## This function creates a special "matrix" object that can cache its inverse, +## using the supported functions attached to the matrix object. +## +## create matrix: amatrix = makeCacheMatrix(matrix(c(1,2,3,4), nrow=2, ncol=2)) +## get matrix: amatrix$get() +## set matrix: amatrix$set(matrix(c(0,5,99,66), nrow=2, ncol=2)) +## inverse of matrix: amatrix$getinverse() +makeCacheMatrix <- function(self.matrix = matrix()) { + self.inv <- NULL + set <- function(newmatrix) { + self.matrix <<- newmatrix + self.inv <<- NULL + } + get <- function() { + self.matrix + } + getinverse <- function() { + self.inv + } + setinverse <- function(inverse) { + self.inv <<- inverse + } + 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' +## This function computes the inverse of the special "matrix" returned by +## vmakeCacheMatrix above. If the inverse has already been calculated (and +## the matrix has not changed), then cacheSolve retrieves the inverse from +## the cache. +cacheSolve <- function(cachematrx, ...) { + inverse <- cachematrx$getinverse() + if(!is.null(inverse)) { + message("getting cached data") + return(inverse) + } + data <- cachematrx$get() + inverse <- solve(data, ...) + cachematrx$setinverse(inverse) + inverse } + +# mtrx = makeCacheMatrix(matrix(c(1,2,3,4), nrow=2, ncol=2)) +# mtrx$get() +# cacheSolve(mtrx) +# mtrx$getinverse() +# cacheSolve(mtrx) +# mtrx$set(matrix(c(0,5,99,66), nrow=2, ncol=2)) +# mtrx$get() +# cacheSolve(mtrx) +# mtrx$getinverse() +# cacheSolve(mtrx)