From 4e417596fc9a67b1834a0c24975424ff179b5ce5 Mon Sep 17 00:00:00 2001 From: Hsing Liu Date: Sat, 12 Apr 2014 15:31:57 +0800 Subject: [PATCH 1/2] first commit --- draft.R | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 draft.R diff --git a/draft.R b/draft.R new file mode 100644 index 00000000000..1d8100714c8 --- /dev/null +++ b/draft.R @@ -0,0 +1,52 @@ +makeCacheMatrix <- function(x = matrix()) { + ## Creates a list of functions used to cache/store + ## a matrix and its inverse + ## Usage example: + ## m <- makeCacheMatrix(matrix(1:4, 2, 2)) + ## ... + ## m$set(matrix(c(1, -1, 0, 1), 2, 2)) + + + # Inverse matrix is initiated with NULL value + inverse <- NULL + + # Set/get functions + set <- function(y) { + x <<- y + inverse <<- NULL + } + get <- function() x + setinverse <- function(solved) inverse <<- solved + getinverse <- function() inverse + + # Return the functions as a list + list(set = set, get = get, + setinverse = setinverse, + getinverse = getinverse) +} + +cacheSolve <- function(x, ...) { + ## Calculate the inverse of a matrix, using makeCacheMatrix + ## to "cache" the inverse. That is, if the inverse had + ## been calculated previously, then get the saved inverse + ## and return it. + ## + ## Usage example: + ## m <- makeCacheMatrix(...) + ## cacheSolve(m) + + + # Check if there is a previously calculated inverse + # If there is, then return the inverse (and exit function) + inverse <- x$getinverse() + if(!is.null(inverse)) { + message("Getting cached inverse matrix") + return(inverse) + } + + # Calculate and store inverse + data <- x$get() + inverse <- solve(data, ...) + x$setinverse(inverse) + inverse +} From 92c48c05f5af638f9e8b950ffb34ed2a6df3fd25 Mon Sep 17 00:00:00 2001 From: Hsing Liu Date: Sat, 12 Apr 2014 15:42:30 +0800 Subject: [PATCH 2/2] draft work integrated into cachematrix.R --- cachematrix.R | 51 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 47 insertions(+), 4 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..0e1585fd80c 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,58 @@ -## Put comments here that give an overall description of what your -## functions do -## Write a short comment describing this function +## makeCacheMatrix() creates a vector to cach a matrix and its inverse +## cacheSolve takes a cached matrix created by makeCacheMatrix and +## return its inverse + + +## makeCacheMatrix() creates a list of functions used to cache/store +## a matrix and its inverse +## Usage example: +## m <- makeCacheMatrix(matrix(1:4, 2, 2)) +## ... +## m$set(matrix(c(1, -1, 0, 1), 2, 2)) makeCacheMatrix <- function(x = matrix()) { + # Inverse matrix is initiated with NULL value + inverse <- NULL + + # Set/get functions + set <- function(y) { + x <<- y + inverse <<- NULL + } + get <- function() x + setinverse <- function(solved) inverse <<- solved + getinverse <- function() inverse + # Return the functions as a list + list(set = set, get = get, + setinverse = setinverse, + getinverse = getinverse) } -## Write a short comment describing this function +## cacheSolve calculates the inverse of a matrix, using makeCacheMatrix +## to "cache" the inverse. That is, if the inverse had been calculated +## previously, then get the saved inverse and return it. +## +## Usage example: +## m <- makeCacheMatrix(...) +## cacheSolve(m) cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' + + # Check if there is a previously calculated inverse + # If there is, then return the inverse (and exit function) + inverse <- x$getinverse() + if(!is.null(inverse)) { + message("Getting cached inverse matrix") + return(inverse) + } + + # Calculate and store inverse + data <- x$get() + inverse <- solve(data, ...) + x$setinverse(inverse) + inverse }