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+ # #
320
421# # A factory function which yields an object encapsulating a square invertible matrix x,
522# # with the following methods:
623# #
724# # get(), retrieves the current matrix; set( m) where m is assumed to be a square matrix;
825# # getinv(), retrieves the inversion of x, or NULL if it has not been set.
926# # 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.
1231# #
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.
1833
1934makeCacheMatrix <- function (x = matrix ()) {
2035 i <- NULL # the inverse of x
@@ -36,8 +51,9 @@ makeCacheMatrix <- function(x = matrix()) {
3651}
3752
3853
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.
4157
4258cacheSolve <- function (x , ... ) {
4359
@@ -46,6 +62,7 @@ cacheSolve <- function(x, ...) {
4662 if ( ! is.null( cached ))
4763 return ( cached )
4864
65+ message( " Computing inverse" );
4966 # # Cache the result and yield
5067 x $ setinv( solve(x $ get(), ... ))
5168 x $ getinv()
0 commit comments