Skip to content

Commit 65de45b

Browse files
committed
* Comment the solution
1 parent fac1c72 commit 65de45b

File tree

1 file changed

+57
-23
lines changed

1 file changed

+57
-23
lines changed

cachematrix.R

Lines changed: 57 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,66 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
3-
4-
## Write a short comment describing this function
1+
# cachematrix.R implements a special type of data structure (implemented as a list)
2+
# that caches the results of the solve() R function. Implemented as two
3+
# functions, makeCacheMatrix() acts as a constructor for this data structure
4+
# and cacheSolve() checks the data structure returning the cached
5+
# solution if available and solving and caching if not.
56

7+
# makeCacheMatrix() constructs a caching data structure for a matrix. Intended
8+
# to be used with the cacheSolve() function defined below.
9+
#
10+
# Arguments:
11+
# x -- a matrix that is assumed to be invertible
12+
#
13+
# Returns:
14+
# A list that contains a placeholder for the cached value (initialized
15+
# to NULL) and functions that can set the cached value and get the
16+
# cached value.
617
makeCacheMatrix <- function(x = matrix()) {
718

8-
m <- NULL
9-
get <- function() x
10-
setinverse <- function(inverse) m <<- inverse
11-
getinverse <- function() m
19+
# Intialize the cached value
20+
m <- NULL
1221

13-
list(get = get,
14-
setinverse = setinverse,
15-
getinverse = getinverse)
16-
}
22+
# A function to get the given matrix
23+
get <- function() x
24+
25+
# A function that caches the inverse matrix (result from solve())
26+
setinverse <- function(inverse) m <<- inverse
1727

28+
# A function to return the cached value
29+
getinverse <- function() m
1830

19-
## Write a short comment describing this function
31+
# Return our data structure as a list, binding the
32+
# above functions to it
33+
list(get = get,
34+
setinverse = setinverse,
35+
getinverse = getinverse)
36+
}
2037

38+
# cacheSolve() computes and caches the inverse of the given
39+
# matrix 'x'. If the cached value is available, that is
40+
# returned otherwise solve() is called on the matrix, the
41+
# return from resolve() cached and then returned.
42+
#
43+
# Arguments:
44+
# x -- a data structure returned from makeCacheMatrix()
45+
#
46+
# Returns:
47+
# The inversion of the matrix contained in 'x'. The
48+
# matrix contained in 'x' is assumed to be invertible.
2149
cacheSolve <- function(x, ...) {
22-
## Return a matrix that is the inverse of 'x'
23-
m <- x$getinverse()
24-
if (!is.null(m)) {
25-
message("Getting cached data")
26-
return(m)
27-
}
28-
data <- x$get()
29-
m <- solve(data)
30-
x$setinverse(m)
31-
m
50+
51+
# Get the cached value and return it
52+
# if it's been set (not NULL)
53+
m <- x$getinverse()
54+
if (!is.null(m)) {
55+
message("Getting cached data")
56+
return(m)
57+
}
58+
59+
# The result has not been cached, get
60+
# the original matrix, call solve() on it
61+
# cache the result and return the result
62+
data <- x$get()
63+
m <- solve(data)
64+
x$setinverse(m)
65+
m
3266
}

0 commit comments

Comments
 (0)