4
4
5
5
6
6
# # Creates a "matrix" that can cache its own inverse.
7
+ # # Returns a list containing functions to view or edit the matrix and its
8
+ # # inverse.
7
9
makeCacheMatrix <- function (x = matrix ()) {
10
+
11
+ # # Inverse not yet computed
8
12
inv <- NULL
13
+ # # The setter function of the matrix
9
14
set <- function ( y ) {
10
15
x <<- y
16
+ # # The matrix just got a new value, so
17
+ # # the inverse is not yet known
11
18
inv <<- NULL
12
19
}
20
+ # # The getter function of the matrix
13
21
get <- function () x
22
+ # # The setter funcction of the inverse of the matrix
14
23
setinv <- function ( invv ) inv <<- invv
24
+ # # The getter function of the inverse of the matrix
15
25
getinv <- function () inv
26
+ # # Return the list that makes the matrix functions accessible
16
27
list ( set = set , get = get ,
17
28
setinv = setinv ,
18
29
getinv = getinv )
@@ -23,14 +34,16 @@ makeCacheMatrix <- function(x = matrix()) {
23
34
# # cache (if available), or by computing it.
24
35
# # ASSUME that the matrix supplied is always invertible.
25
36
cacheSolve <- function (x , ... ) {
26
- # # Return a matrix that is the inverse of 'x'
37
+
38
+ # # Get the stored inverse (if any) from 'x'
27
39
inv <- x $ getinv()
40
+ # # If the inverse is cached, return cached value
28
41
if ( ! is.null(inv ) ) {
29
42
message(" Getting cached data..." )
30
43
return ( inv )
31
- }
44
+ } # # Else get the data...
32
45
data <- x $ get()
33
- inv <- solve( data )
34
- x $ setinv( inv )
35
- inv
46
+ inv <- solve( data ) # # ... compute its inverse ...
47
+ x $ setinv( inv ) # # ... set the inverse in x's cache, and...
48
+ inv # # ... return the computed inverse.
36
49
}
0 commit comments