Skip to content

Commit 3e1012d

Browse files
committed
add comments
1 parent 752150b commit 3e1012d

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

cachematrix.R

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,42 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## A "matrix" that carries an embedded cached value of it's inverse
2+
3+
# the matrix is implemented as a "poor man's object" i.e. a record of
4+
# closures with a shared mutable environment.
5+
# the two functions set() and get() allow accessing and mutating the
6+
# embedded matrix object, while set.inverse and get.inverse allow
7+
# manipulation of the inverse object in the shared environment.
8+
#
9+
#
310

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

612
makeCacheMatrix <- function(x = matrix()) {
7-
cached.result <- NULL
13+
inverse <- NULL
814
set <- function(y) {
915
x <<- y
10-
cached.result <<- NULL
16+
inverse <<- NULL
1117
}
1218
get <- function() x
13-
set.cached.result <- function(result) cached.result <<- result
14-
get.cached.result <- function() cached.result
19+
set.inverse <- function(result) inverse <<- result
20+
get.inverse <- function() inverse
1521
list(set = set, get = get,
16-
set.cached.result = set.cached.result,
17-
get.cached.result = get.cached.result)
22+
set.inverse = set.inverse,
23+
get.inverse = get.inverse)
1824
}
1925

2026

21-
## Write a short comment describing this function
27+
# Cache solve can be used to get the inverse of a matrix
28+
# in a smart way by looking up the cached value and memoizing
29+
# it after a computation for future use.
2230

2331
cacheSolve <- function(matrix, ...) {
24-
res <- matrix$get.cached.result()
32+
res <- matrix$get.inverse()
2533
if(!is.null(res)) {
2634
message("getting cached data")
2735
return(res)
2836
}
2937
data <- matrix$get()
3038
res <- solve(data)
31-
matrix$set.cached.result(res)
39+
matrix$set.inverse(res)
3240
res
3341
}
3442

0 commit comments

Comments
 (0)