1
- # # Put comments here that give an overall description of what your
2
- # # functions do
1
+ # # A pair of functions which implement the encapsulation and caching of a matrix
2
+ # # and its inverse's computation.
3
+ # #
4
+ # # makeCacheMatrix is a factory function yielding an object providing access methods to
5
+ # # retrieve and assign the matrix and its inverse.
6
+ # #
7
+ # # cacheSolve takes an object obtained via makeCacheMatrix and returns the inverse if already computed;
8
+ # # otherwise the inverse is computed and cached by invoking setinv() on its argument prior to being
9
+ # # returned.
10
+ # #
11
+ # # It is important to note that the two functions are designed to be used together to reap the
12
+ # # caching benefit:
13
+ # # my_matrix <- makeCacheMatrix( matrix( <values>, <dimensions>)
14
+ # # cacheSolve( my_matrix)
15
+ # # my_matrix$getinv()
16
+ # #
17
+ # # Some care must be taken to invoke cacheSolve if changing the matrix value via set, prior to
18
+ # # invoking getinv.
19
+ # #
3
20
4
21
# # A factory function which yields an object encapsulating a square invertible matrix x,
5
22
# # with the following methods:
6
23
# #
7
24
# # get(), retrieves the current matrix; set( m) where m is assumed to be a square matrix;
8
25
# # getinv(), retrieves the inversion of x, or NULL if it has not been set.
9
26
# # setinv( inv) which assigns inv to the value returned by getinv(), but only if getinv() is NULL
10
- # # set( new_x), sets the square invertible matrix to be new_x, unless x is already assigned,
11
- # # in which case nothing is done.
27
+ # # set( new_x), assigns new_x to be the new matrix value, and ensures that if getinv() is the next
28
+ # # method invoked, NULL will be returned - see usage pattern at the top.
29
+ # #
30
+ # # If provided, x is assumed to be a square invertible matrix. Otherwise the default is a 1 by 1 empty matrix.
12
31
# #
13
- # # If provided, x is assumed to be a square invertible matrix and can't be changed in the returned object.
14
- # # Otherwise the default is a 1 by 1 empty matrix which can be overwritten by invoking set().
15
- # # Note that the matrix inversion computation is not itself provided, only its storage. Therefore,
16
- # # one should check for getinv() yielding NULL before using its value, and/or assign the inverse by calling
17
- # # x$setinv.
32
+ # # Note that the matrix inversion computation is not itself provided, only its storage. See usage pattern above.
18
33
19
34
makeCacheMatrix <- function (x = matrix ()) {
20
35
i <- NULL # the inverse of x
@@ -36,8 +51,9 @@ makeCacheMatrix <- function(x = matrix()) {
36
51
}
37
52
38
53
39
- # # Yields the inverse of the x$get(), computing it if necessary. In both cases, the inverse will
40
- # # be returned by future invocations of x$getinv().
54
+ # # Yields the inverse of the x$get(), computing it only if necessary.
55
+ # # If computed, the result is cached, i.e. future invocations of x$getinv() will return
56
+ # # the same value without computation until the next invocation of x$set.
41
57
42
58
cacheSolve <- function (x , ... ) {
43
59
@@ -46,6 +62,7 @@ cacheSolve <- function(x, ...) {
46
62
if ( ! is.null( cached ))
47
63
return ( cached )
48
64
65
+ message( " Computing inverse" );
49
66
# # Cache the result and yield
50
67
x $ setinv( solve(x $ get(), ... ))
51
68
x $ getinv()
0 commit comments