1
- # # Put comments here that give an overall description of what your
2
- # # functions do
1
+ # The two functions below - makeCacheMatrix and cacheSolve - work in conjuction
2
+ # to calculate the inverse of a matrix. makeCacheMatrix stores (i.e. caches) a
3
+ # matrix and the inverse and provides a set of functions to set and retrieve
4
+ # these two variables. cacheSolve does the actual math to calculate the matrix
5
+ # inverse. In addition, it checks the cache and retrieves the inverse if it
6
+ # already exists.
7
+ #
8
+ # Here is a typical way of using these two functions together:
9
+ # mx <- matrix(sample(10, 100, replace = TRUE), 10, 10) --- define a matrix
10
+ # mx.1 <- makeCacheMatrix(mx) --- pass mx to the first function
11
+ # mx.inv <- cacheSolve(mx.1) --- calculate or retrieve cached inverse
12
+ #
13
+ #
14
+ #
15
+ #
16
+ # MakeCacheMatrix: This function
17
+ # 1. caches two variables, a matrix 'x' and its inverse 'inv',
18
+ # 2. provides four functions (let's call them caching functions'); two of them
19
+ # to set and retieve the orignal matrix and another pair to set and retrieve
20
+ # the inverse matrix.
21
+ #
22
+ # The cached varibles 'x' and 'inv' are stored in the enclosing environment of
23
+ # the four caching functions. This way the variable values are preserved even
24
+ # after exiting the caching functions. In order to save 'x' and 'inv' into the
25
+ # enclosing environment the two functions 'set' and 'setinvers' use the special
26
+ # assignment operator '<<-'.
27
+ #
28
+ # Another important point is to recognize that the four caching functions are
29
+ # placed inside the function makeCacheMatrix. Due to the lexical scoping in R,
30
+ # the enclosing environment of the four caching functions is the environment
31
+ # that is active when they are created, which is the evaluation environment of
32
+ # makeCacheMatrix. This is imporant for understanding of what happens when
33
+ # makeCacheMatrix gets invoked.
34
+ #
35
+ # When we call makeCacheMatrix and pass a matrix to it, the matrix gets assigned
36
+ # to the value 'x' and written to the evaluation environment (in line 1 of the
37
+ # fuction); next in line 2 'inv' (which is the cache for the matrix invers) is
38
+ # set to NULL. The rest of the makeCacheMatrix is to define the four caching
39
+ # functions (which don't get evaluated at this point) and to wrap them into a
40
+ # list. Since 'x' and 'inv' already exist when the four caching functions get
41
+ # created, 'x' and 'inv' become attributes of their enclosing environment.
3
42
4
- # # Write a short comment describing this function
5
43
6
44
makeCacheMatrix <- function (x = matrix ()) {
7
45
inv <- NULL
@@ -18,10 +56,24 @@ makeCacheMatrix <- function(x = matrix()) {
18
56
}
19
57
20
58
21
- # # Write a short comment describing this function
59
+ # The main purpose of the cacheSolve function is to calculate an inverse matrix
60
+ # or retrieve it in case it already exists in the cache.
61
+ #
62
+ # The cacheSolve function requires as an input the output of makeCacheMatrix
63
+ # (that is a list of function closures see use example above).
64
+ #
65
+ # The first step in the function body is to retrieve the cached value for the
66
+ # inverse matrix 'inv' using the getinverse funtion. If 'inv' is not NULL, the
67
+ # cached inverse is returned.
68
+ #
69
+ # If 'inv' is NULL the original matrix is retrieved and assigned to the variable
70
+ # 'data'. Next the inverse matrix gets calculated (using the function solve) and
71
+ # saved back into the cache using setinverse. Finally, the inverse matrix is
72
+ # returned.
73
+
74
+
22
75
23
76
cacheSolve <- function (x , ... ) {
24
- # # Return a matrix that is the inverse of 'x'
25
77
inv <- x $ getinverse()
26
78
if (! is.null(inv )) {
27
79
message(" retriEving cached inverse" )
0 commit comments