Skip to content

Commit e5ec44f

Browse files
committed
Added descriptions for the two functions
1 parent 45d3529 commit e5ec44f

File tree

1 file changed

+57
-5
lines changed

1 file changed

+57
-5
lines changed

cachematrix.R

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,45 @@
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.
342

4-
## Write a short comment describing this function
543

644
makeCacheMatrix <- function(x = matrix()) {
745
inv <- NULL
@@ -18,10 +56,24 @@ makeCacheMatrix <- function(x = matrix()) {
1856
}
1957

2058

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+
2275

2376
cacheSolve <- function(x, ...) {
24-
## Return a matrix that is the inverse of 'x'
2577
inv <- x$getinverse()
2678
if(!is.null(inv)) {
2779
message("retriEving cached inverse")

0 commit comments

Comments
 (0)