1
- # # Put comments here that give an overall description of what your
2
- # # functions do
3
-
4
- # # Write a short comment describing this function
5
-
1
+ # # calculates a special 'matrix', represented by a list containing
2
+ # # the following four functions to:
3
+ # # 1) set the value of the matrix
4
+ # # 2) get the value of the matrix
5
+ # # 3) set the inverse of the matrix (i.e. cache the inverse)
6
+ # # 4) get the (cached) inverse of the matrix
6
7
makeCacheMatrix <- function (x = matrix ()) {
8
+
7
9
# private storage of inverse, initially set to NULL
8
10
# ... as inverse has not yet been calculcated
9
11
inverse <- NULL
10
12
11
13
# function allowing value of matrix to be reassigned.
12
14
# we reset inverse to NULL as inverse will need to be
13
- # recalculated for new matrix value
15
+ # recalculated for new matrix
14
16
set <- function (y ) {
15
17
x <<- y
16
18
inverse <<- NULL
17
19
}
18
20
19
- # getter method - retrieve current matrix value
21
+ # getter method - retrieve current matrix
20
22
get <- function () x
21
23
22
- # we'll use this to cache inverse after calculation
24
+ # setter to cache inverse after calculation
23
25
setinverse <- function (inv ) inverse <<- inv
24
26
25
- # getter for caculated inverse
27
+ # getter for pre- caculated inverse (may be null)
26
28
getinverse <- function () inverse
27
29
28
30
# return list of 4 functions defined above
@@ -32,17 +34,22 @@ makeCacheMatrix <- function(x = matrix()) {
32
34
}
33
35
34
36
35
- # # Write a short comment describing this function
36
-
37
+ # # Return a matrix that is the inverse of 'x'
37
38
cacheSolve <- function (x , ... ) {
38
- # # Return a matrix that is the inverse of 'x'
39
+ # retrieve cached inverse of x (may be null)
39
40
m <- x $ getinverse()
41
+
42
+ # if cached value is not null, use it, and print msg
40
43
if (! is.null(m )) {
41
44
message(" getting cached data" )
42
45
return (m )
43
46
}
47
+
48
+ # otherwise,retrieve matrix value, compute and store inverse
44
49
data <- x $ get()
45
50
m <- solve(data , ... )
46
51
x $ setinverse(m )
52
+
53
+ # return newly computed inverse
47
54
m
48
55
}
0 commit comments