1
- # # Put comments here that give an overall description of what your
2
- # # functions do
1
+ # # These functions work as a pair to manage the storage of the
2
+ # # inverse of a matrix and avoid re-calculating it if that's
3
+ # # avoidable.
3
4
4
- # # Write a short comment describing this function
5
+
6
+ # # Can be used to create a wrapper object around a matrix with
7
+ # # the primary purpose being to hold the matrix and the inverse
8
+ # # of that matrix if the cacheSolve matrix has been used to
9
+ # # calculate it. The wrapper object, a list, also contains
10
+ # # functions to get and set the matrix and inverse stored within.
5
11
6
12
makeCacheMatrix <- function (dataMatrix = matrix ()) {
7
13
inv = NULL
@@ -17,17 +23,27 @@ makeCacheMatrix <- function(dataMatrix = matrix()) {
17
23
}
18
24
19
25
20
- # # Write a short comment describing this function
26
+ # # Used to retrieve or calculate the inverse of a matrix stored
27
+ # # within a wrapper list created by the makeCacheMatrix function.
21
28
22
29
cacheSolve <- function (matrixWrapper , ... ) {
30
+
31
+ # Retrieve the inverse stored in the wrapper object
23
32
inverse = matrixWrapper $ getInverse()
33
+
24
34
if (! is.null(inverse )) {
35
+ # Return the pre-calculated inverse
25
36
message(" getting cached data" )
26
37
return (inverse )
27
38
}
39
+
40
+ # No inverse was found, so calculate it
28
41
message(" calculating data" )
29
42
dataMatrix = matrixWrapper $ get()
30
43
inverse = solve(dataMatrix )
44
+
45
+ # Store the caluclated inverse for future use
31
46
matrixWrapper $ setInverse(inverse )
47
+
32
48
inverse
33
49
}
0 commit comments