1
- # # Put comments here that give an overall description of what your
2
- # # functions do
1
+ # 2 functions to assist in caching the inverse of a matrix
3
2
4
- # # Write a short comment describing this function
5
-
6
- # # TODO:
3
+ # TODO:
7
4
# write guard clause against x != matrix
8
- # fix indentation to 4
5
+
9
6
makeCacheMatrix <- function (x = matrix ()) {
7
+ # Wrap the matrix object provided and allow the caching of it's inverse
8
+ #
9
+ # Args:
10
+ # x: a matrix object whose inverse can be calculated
11
+ #
12
+ # Returns:
13
+ # A list of four functions (set, get, setInverse, getInverse)
14
+
10
15
inverse <- NULL
16
+
17
+ # replace the matrix with a new one
11
18
set <- function (y ) {
12
19
x <<- y
13
20
inverse <<- NULL
14
21
}
22
+
23
+ # get the matrix object
15
24
get <- function () x
25
+
26
+ # set the inverse of the matrix
16
27
setInverse <- function (inverseMatrix ) inverse <<- inverseMatrix
28
+
29
+ # get the saved inverse or NULL
17
30
getInverse <- function () inverse
31
+
32
+ # return object
18
33
list (set = set , get = get ,
19
34
setInverse = setInverse ,
20
35
getInverse = getInverse )
21
36
}
22
37
23
-
24
- # # Write a short comment describing this function
25
-
26
38
cacheSolve <- function (x , ... ) {
27
- # # Return a matrix that is the inverse of 'x'
39
+ # Compute the inverse of the matrix provided by the makeCacheMatrix object
40
+ #
41
+ # Args:
42
+ # x: makeCacheMatrix object
43
+ #
44
+ # Returns:
45
+ # The inverse of the provided matrix
28
46
29
47
inverse <- x $ getInverse()
48
+
49
+ # Returns the cached inverse if it is available
30
50
if (! is.null(inverse )) {
31
51
message(" getting cached inverse matrix" )
32
52
return (inverse )
33
53
}
34
54
55
+ # calculate the inverse
35
56
data <- x $ get()
36
57
inverse <- solve(data , ... )
37
58
x $ setInverse(inverse )
59
+
60
+ # return the inverse
38
61
inverse
39
- }
62
+ }
0 commit comments