From 5b4b0d2bfe0ed9e9c85c7f8be84a40535701f168 Mon Sep 17 00:00:00 2001 From: drdrsh Date: Thu, 18 Dec 2014 07:53:34 +0200 Subject: [PATCH 1/2] Committing the assignment code --- cachematrix.R | 110 +++++++++++++++++++++++++++++++++++++++---- cachematrix_output.R | 45 ++++++++++++++++++ 2 files changed, 146 insertions(+), 9 deletions(-) create mode 100644 cachematrix_output.R diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..2ec295cba01 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,107 @@ -## Put comments here that give an overall description of what your -## functions do +# Author: +# Mostafa Abdelraouf +# Description: +# Peer assessment assignment (Programming Assignment 2) submission +# for JHU R programming course +# https://class.coursera.org/rprog-016/human_grading/view/courses/973757/assessments/3/submissions +# +# Notes: +# 1) I have strayed a bit from the requirements of the assignment for sake +# of encapsulation and to not clutter the global namespace. +# I have encapsulated the function that caches the inverse matrix inside the +# returned object. The user of this object will not be required to explicitly call "cacheSolve". +# Instead, if the user needs to get the inverse matrix he will just have to call +# GetInverse(), it will calculate and cache the result and any further calls will be +# served the cached inverse directly. +# 2) This file uses, to the best of my ability, Google's R Style guide +# http://google-styleguide.googlecode.com/svn/trunk/Rguide.xml -## Write a short comment describing this function +MakeCacheMatrix <- function(x=matrix()) { + # Creates a matrix object that supports inverse matrix calculation and caching + # + # Args: + # x: A matrix to build the object around,note that the matrix is assumed + # to be invertable. + # + # Returns: + # A list of functions (Or rather, an object) that can be used to modify + # the matrix -makeCacheMatrix <- function(x = matrix()) { + + # Check if the x parameter is a matrix, if not, prints an error + # message and exits + if (!is.matrix(x)){ + message("Parameter supplied to makeMatrix() MUST be a matrix") + return (NULL) + } -} + original.matrix <- x + inverse.matrix <- NULL # This will hold our inverse matrix + + Set <- function(x) { + # Sets the value of the internal matrix and invalidates the cache. + # + # Args: + # x: A matrix to build the object around,note that the matrix is assumed + # to be invertable. + # + # Returns: + # True on success, false of failure + # if y is not a matrix, we output an error message and make no changes to + # the internal matrix + if (!is.matrix(x)){ + message("Parameter supplied to Set() MUST be a matrix") + return (FALSE) + } + + original.matrix <<- x # change the value of the internal matrix to "y" + + # Notice that when resetting the internal matrix we invalidated any previously + # stored "inverse" by setting it to null + inverse.matrix <<- NULL + + message("Resetting internal matrix, cache is now invalid") + + TRUE + } -## Write a short comment describing this function + Get <- function(){ + # Gets the internal matrix + # + # Returns: + # a copy of the internal matrix + original.matrix + } -cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' -} + GetInverse <- function(){ + # Calculates, caches and returns the inverse matrix of the internal matrix using (solve() function) + # + # Returns: + # the inverse matrix of the internal matrix using (solve() function) + + # Check to see if we have a valid "inverse matrix" cache for our matrix + # if so, return it immediately + if (!is.null(inverse.matrix)){ + message("Returning cache") + return(inverse.matrix) + } + + # Otherwise, generate the inverse cache and store it internally in the "inverse.matrix" variable + message("No cache found, generating cache") + inverse.matrix <<- solve(original.matrix) + + #return the inverse + inverse.matrix + + } + + # This will return a list containing 3 functions that can be used to manipulate this particular + # instance of the matrix + list( + Set = Set, + Get = Get, + GetInverse = GetInverse + ) + +} \ No newline at end of file diff --git a/cachematrix_output.R b/cachematrix_output.R new file mode 100644 index 00000000000..d859565dc8a --- /dev/null +++ b/cachematrix_output.R @@ -0,0 +1,45 @@ +# Author: +# Mostafa Abdelraouf +# Description: +# Output of the Peer assessment assignment (Programming Assignment 2) submission +# for JHU R programming course +# https://class.coursera.org/rprog-016/human_grading/view/courses/973757/assessments/3/submissions +# + +source('cachematrix.R'); + +a <- c(100,50,90) +b <- c(200,42,99) +c <- c(77,56,34) + +d <- c(105,33,80) +e <- c(240,22,59) +f <- c(78,33,39) + +# Create our matrix +m1 <- MakeCacheMatrix(rbind(a,b,c)) +message("Original Matrix") +print(m1$Get()) + +# This call will not find a cached matrix and thus will generate it +message("Inverted Matrix") +print(m1$GetInverse()) + +# All subsequent calls to "getInverse" will use the cached inverse matrix +print(m1$GetInverse()) +print(m1$GetInverse()) +print(m1$GetInverse()) + +# The following call will change the matrix and invalidate the cache +m1$Set(rbind(d,e,f)) +message("Original Matrix") +print(m1$Get()) + +# This call will not find a cached matrix and thus will generate it +message("Inverted Matrix") +print(m1$GetInverse()) + +# All subsequent calls to "getInverse" will use the cached matrix +print(m1$GetInverse()) +print(m1$GetInverse()) +print(m1$GetInverse()) From 4d10b0957bb5975231a46e20e331872fa9e90326 Mon Sep 17 00:00:00 2001 From: drdrsh Date: Thu, 18 Dec 2014 08:00:25 +0200 Subject: [PATCH 2/2] Added "cacheSolve()" function to comply with assignment requirement --- cachematrix.R | 14 +++++++++++++- cachematrix_output.R | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 2ec295cba01..d9830d1ee00 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -16,7 +16,7 @@ # 2) This file uses, to the best of my ability, Google's R Style guide # http://google-styleguide.googlecode.com/svn/trunk/Rguide.xml -MakeCacheMatrix <- function(x=matrix()) { +makeCacheMatrix <- function(x=matrix()) { # Creates a matrix object that supports inverse matrix calculation and caching # # Args: @@ -104,4 +104,16 @@ MakeCacheMatrix <- function(x=matrix()) { GetInverse = GetInverse ) +} + +cacheSolve <- function(x){ + # This function is here to comply with the assignment requirements, it merely wraps around GetInverse() function + # + # Args: + # x: A matrix object created with makeCacheMatrix + # + # Returns: + # the inverse matrix x + + x$GetInverse() } \ No newline at end of file diff --git a/cachematrix_output.R b/cachematrix_output.R index d859565dc8a..51fcab8b894 100644 --- a/cachematrix_output.R +++ b/cachematrix_output.R @@ -17,7 +17,7 @@ e <- c(240,22,59) f <- c(78,33,39) # Create our matrix -m1 <- MakeCacheMatrix(rbind(a,b,c)) +m1 <- makeCacheMatrix(rbind(a,b,c)) message("Original Matrix") print(m1$Get())