Skip to content

Commit e45771a

Browse files
committed
Elaborate on the documentation a bit more
1 parent b58f543 commit e45771a

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

cachematrix.R

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,68 @@
11
# ------------------------------------------------------------------------------
22
# Helper functions to compute and cache the inverse of a matrix (i.e. "solve" a
3-
# matrix).
3+
# matrix). Computing the inverse of a matrix can be a time-consuming operation,
4+
# therefore it's useful to be able to cache the computation so the value can be
5+
# used multiple times without running the computation again.
46
#
57
# Note: we assume a square matrix (i.e. one that is invertible) is always
6-
# provided when using these functions. Therefore there is no erro handling for
8+
# provided when using these functions. Therefore there is no error handling for
79
# cases where this is not true.
810
#-------------------------------------------------------------------------------
911

1012

1113
# Given an initial matrix, this method returns a special wrapper, in the form of
12-
# a list with functions that can be used to:
14+
# a list, with functions that can be used to:
1315
# - set the underlying matrix again
1416
# - get the underlying matrix
15-
# - set the inverse of the underlying matrix
16-
# - get th inverse of the underlying matrix
17+
# - set the inverse of the underlying matrix (which caches this value internally)
18+
# - get the inverse of the underlying matrix (which uses the cached value)
1719
makeCacheMatrix <- function(x = matrix()) {
18-
i <- NULL # Cache value for the inverse matrix of `x`
19-
20+
i <- NULL # Cache for the inverse matrix of `x`
21+
2022
# Setter method, if we want to overwrite the underlying matrix `x`
2123
set <- function(y) {
2224
x <<- y
23-
i <<- NULL
25+
i <<- NULL # Reset the cache
2426
}
25-
27+
2628
# Getter method, to access the underlying matrix `x`
2729
get <- function() {
2830
x
2931
}
30-
32+
3133
# Set the inverse of the underlying matrix `x`, into the cache
3234
setinverse <- function(inverse) {
3335
i <<- inverse
3436
}
35-
37+
3638
# Get the inverse of the underlying matrix `x`, from the cache
3739
# Note: this will return `NULL` if the cache hasn't been set yet
3840
getinverse <- function() {
3941
i
4042
}
41-
43+
4244
list(set = set, get = get,
4345
setinverse = setinverse,
4446
getinverse = getinverse)
4547
}
4648

4749

4850
# Use this method to "solve" (i.e. get the inverse) of a matrix, doing so in a
49-
# way that caches the very first computation of the inverse and uses the cache
50-
# subsequently.
51+
# way that caches the very first computation of the inverse computation, and
52+
# uses the cache for subsequent access.
5153
#
52-
# The argument `x` MUST be the special wrapper obtained from `makeCacheMatrix`.
54+
# The argument `x` MUST be a special wrapper obtained from `makeCacheMatrix`.
5355
cacheSolve <- function(x, ...) {
5456
## Return a matrix that is the inverse of 'x'
55-
57+
5658
# Try to get the cached value
5759
i <- x$getinverse()
5860
if(!is.null(i)) {
5961
message("getting cached data")
6062
return(i)
6163
}
62-
63-
# … otherwise compute the inverse and cache
64+
65+
# … otherwise compute the inverse and cache it
6466
data <- x$get()
6567
i <- solve(data, ...)
6668
x$setinverse(i)

0 commit comments

Comments
 (0)