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
3
18
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
5
29
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
+ }
7
37
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
9
50
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
+ }
10
68
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
+ }
12
76
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
+ }
0 commit comments