Skip to content

Commit 5b4b0d2

Browse files
committed
Committing the assignment code
1 parent 7f657dd commit 5b4b0d2

File tree

2 files changed

+146
-9
lines changed

2 files changed

+146
-9
lines changed

cachematrix.R

Lines changed: 101 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,107 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
# Author:
2+
# Mostafa Abdelraouf <[email protected]>
3+
# Description:
4+
# Peer assessment assignment (Programming Assignment 2) submission
5+
# for JHU R programming course
6+
# https://class.coursera.org/rprog-016/human_grading/view/courses/973757/assessments/3/submissions
7+
#
8+
# Notes:
9+
# 1) I have strayed a bit from the requirements of the assignment for sake
10+
# of encapsulation and to not clutter the global namespace.
11+
# I have encapsulated the function that caches the inverse matrix inside the
12+
# returned object. The user of this object will not be required to explicitly call "cacheSolve".
13+
# Instead, if the user needs to get the inverse matrix he will just have to call
14+
# GetInverse(), it will calculate and cache the result and any further calls will be
15+
# served the cached inverse directly.
16+
# 2) This file uses, to the best of my ability, Google's R Style guide
17+
# http://google-styleguide.googlecode.com/svn/trunk/Rguide.xml
318

4-
## Write a short comment describing this function
19+
MakeCacheMatrix <- function(x=matrix()) {
20+
# Creates a matrix object that supports inverse matrix calculation and caching
21+
#
22+
# Args:
23+
# x: A matrix to build the object around,note that the matrix is assumed
24+
# to be invertable.
25+
#
26+
# Returns:
27+
# A list of functions (Or rather, an object) that can be used to modify
28+
# the matrix
529

6-
makeCacheMatrix <- function(x = matrix()) {
30+
31+
# Check if the x parameter is a matrix, if not, prints an error
32+
# message and exits
33+
if (!is.matrix(x)){
34+
message("Parameter supplied to makeMatrix() MUST be a matrix")
35+
return (NULL)
36+
}
737

8-
}
38+
original.matrix <- x
39+
inverse.matrix <- NULL # This will hold our inverse matrix
40+
41+
Set <- function(x) {
42+
# Sets the value of the internal matrix and invalidates the cache.
43+
#
44+
# Args:
45+
# x: A matrix to build the object around,note that the matrix is assumed
46+
# to be invertable.
47+
#
48+
# Returns:
49+
# True on success, false of failure
950

51+
# if y is not a matrix, we output an error message and make no changes to
52+
# the internal matrix
53+
if (!is.matrix(x)){
54+
message("Parameter supplied to Set() MUST be a matrix")
55+
return (FALSE)
56+
}
57+
58+
original.matrix <<- x # change the value of the internal matrix to "y"
59+
60+
# Notice that when resetting the internal matrix we invalidated any previously
61+
# stored "inverse" by setting it to null
62+
inverse.matrix <<- NULL
63+
64+
message("Resetting internal matrix, cache is now invalid")
65+
66+
TRUE
67+
}
1068

11-
## Write a short comment describing this function
69+
Get <- function(){
70+
# Gets the internal matrix
71+
#
72+
# Returns:
73+
# a copy of the internal matrix
74+
original.matrix
75+
}
1276

13-
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
15-
}
77+
GetInverse <- function(){
78+
# Calculates, caches and returns the inverse matrix of the internal matrix using (solve() function)
79+
#
80+
# Returns:
81+
# the inverse matrix of the internal matrix using (solve() function)
82+
83+
# Check to see if we have a valid "inverse matrix" cache for our matrix
84+
# if so, return it immediately
85+
if (!is.null(inverse.matrix)){
86+
message("Returning cache")
87+
return(inverse.matrix)
88+
}
89+
90+
# Otherwise, generate the inverse cache and store it internally in the "inverse.matrix" variable
91+
message("No cache found, generating cache")
92+
inverse.matrix <<- solve(original.matrix)
93+
94+
#return the inverse
95+
inverse.matrix
96+
97+
}
98+
99+
# This will return a list containing 3 functions that can be used to manipulate this particular
100+
# instance of the matrix
101+
list(
102+
Set = Set,
103+
Get = Get,
104+
GetInverse = GetInverse
105+
)
106+
107+
}

cachematrix_output.R

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Author:
2+
# Mostafa Abdelraouf <[email protected]>
3+
# Description:
4+
# Output of the Peer assessment assignment (Programming Assignment 2) submission
5+
# for JHU R programming course
6+
# https://class.coursera.org/rprog-016/human_grading/view/courses/973757/assessments/3/submissions
7+
#
8+
9+
source('cachematrix.R');
10+
11+
a <- c(100,50,90)
12+
b <- c(200,42,99)
13+
c <- c(77,56,34)
14+
15+
d <- c(105,33,80)
16+
e <- c(240,22,59)
17+
f <- c(78,33,39)
18+
19+
# Create our matrix
20+
m1 <- MakeCacheMatrix(rbind(a,b,c))
21+
message("Original Matrix")
22+
print(m1$Get())
23+
24+
# This call will not find a cached matrix and thus will generate it
25+
message("Inverted Matrix")
26+
print(m1$GetInverse())
27+
28+
# All subsequent calls to "getInverse" will use the cached inverse matrix
29+
print(m1$GetInverse())
30+
print(m1$GetInverse())
31+
print(m1$GetInverse())
32+
33+
# The following call will change the matrix and invalidate the cache
34+
m1$Set(rbind(d,e,f))
35+
message("Original Matrix")
36+
print(m1$Get())
37+
38+
# This call will not find a cached matrix and thus will generate it
39+
message("Inverted Matrix")
40+
print(m1$GetInverse())
41+
42+
# All subsequent calls to "getInverse" will use the cached matrix
43+
print(m1$GetInverse())
44+
print(m1$GetInverse())
45+
print(m1$GetInverse())

0 commit comments

Comments
 (0)