From 13486c680be83bd24d281093c81b06fe1dc49f36 Mon Sep 17 00:00:00 2001 From: FallenAzazel Date: Thu, 10 Apr 2014 15:53:14 +0100 Subject: [PATCH 1/3] Update cachematrix.R --- cachematrix.R | 46 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..8fe2a8d7a91 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,53 @@ -## Put comments here that give an overall description of what your -## functions do +## Coursera - R Programming by Roger D. Peng +## Programming Assignment 2 - Peer Reviewed -## Write a short comment describing this function +## Utilising R, this assignment aims to using caching techniques to speed up CPU costly calculations, in this +## case calculations on matrices. This involves the use of two functions - +## 'makeCacheMatrix' This function creates a special "matrix" object that can cache its inverse +## 'cacheSolve' This function 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. + +## Objects 'x' will be the matrix and 'inv' its solved inverse matrix + +## After creating the special matrix object, it will cache its inverse. makeCacheMatrix <- function(x = matrix()) { + inv <- NULL + set <- function(y) { + x <<- y + inv <<- NULL + } + get <- function() x + set_inv <- function(solve) inv <<- solve + get_inv <- function() inv + list(set = set, get = get, + set_inv = set_inv, + get_inv = get_inv) + + + } -## Write a short comment describing this function +## This function will check to see if the solution to the inverse matrix 'x' has already been cached, if it has then +## will return the cached data, if not will go ahead and calculate the inverse, cache it and return it. + cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' + + + #Checks to see if the solution to the inverse of the matrix object x is already cached + inv <- x$get_inv() + if(!is.null(inv)) { + message("getting cached inverse matrix data") + return(inv) + } + data <- x$get() + inv <- solve(data, ...) + x$set_inv(inv) + inv + } From 207a2791c6cb784d0d62c65eaad7d314fec2ffd0 Mon Sep 17 00:00:00 2001 From: FallenAzazel Date: Thu, 10 Apr 2014 15:54:18 +0100 Subject: [PATCH 2/3] Update cachematrix.R --- cachematrix.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cachematrix.R b/cachematrix.R index 8fe2a8d7a91..61958757c5b 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -10,7 +10,7 @@ ## Objects 'x' will be the matrix and 'inv' its solved inverse matrix -## After creating the special matrix object, it will cache its inverse. +## After creating the special matrix object, this function will cache its inverse. makeCacheMatrix <- function(x = matrix()) { From 6f027eb3ff35fcd885ddde1b24838a3269c6b28b Mon Sep 17 00:00:00 2001 From: FallenAzazel Date: Fri, 11 Apr 2014 11:35:49 +0100 Subject: [PATCH 3/3] Update cachematrix.R --- cachematrix.R | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 61958757c5b..31217b25d43 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -4,7 +4,7 @@ ## Utilising R, this assignment aims to using caching techniques to speed up CPU costly calculations, in this ## case calculations on matrices. This involves the use of two functions - ## 'makeCacheMatrix' This function creates a special "matrix" object that can cache its inverse -## 'cacheSolve' This function computes the inverse of the special "matrix" returned by makeCacheMatrix. +## 'cacheSolve' This function 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. @@ -26,8 +26,6 @@ makeCacheMatrix <- function(x = matrix()) { set_inv = set_inv, get_inv = get_inv) - - } @@ -36,7 +34,7 @@ makeCacheMatrix <- function(x = matrix()) { cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + ## Return a matrix that is the inverse of 'x' #Checks to see if the solution to the inverse of the matrix object x is already cached