Skip to content

Commit c784caf

Browse files
committed
better commenting
1 parent 0b31e7f commit c784caf

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

cachematrix.R

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
3-
4-
## Write a short comment describing this function
5-
1+
## calculates a special 'matrix', represented by a list containing
2+
## the following four functions to:
3+
## 1) set the value of the matrix
4+
## 2) get the value of the matrix
5+
## 3) set the inverse of the matrix (i.e. cache the inverse)
6+
## 4) get the (cached) inverse of the matrix
67
makeCacheMatrix <- function(x = matrix()) {
8+
79
# private storage of inverse, initially set to NULL
810
# ... as inverse has not yet been calculcated
911
inverse <- NULL
1012

1113
# function allowing value of matrix to be reassigned.
1214
# we reset inverse to NULL as inverse will need to be
13-
# recalculated for new matrix value
15+
# recalculated for new matrix
1416
set <- function(y) {
1517
x <<- y
1618
inverse <<- NULL
1719
}
1820

19-
# getter method - retrieve current matrix value
21+
# getter method - retrieve current matrix
2022
get <- function() x
2123

22-
# we'll use this to cache inverse after calculation
24+
# setter to cache inverse after calculation
2325
setinverse <- function(inv) inverse <<- inv
2426

25-
# getter for caculated inverse
27+
# getter for pre-caculated inverse (may be null)
2628
getinverse <- function() inverse
2729

2830
# return list of 4 functions defined above
@@ -32,17 +34,22 @@ makeCacheMatrix <- function(x = matrix()) {
3234
}
3335

3436

35-
## Write a short comment describing this function
36-
37+
## Return a matrix that is the inverse of 'x'
3738
cacheSolve <- function(x, ...) {
38-
## Return a matrix that is the inverse of 'x'
39+
# retrieve cached inverse of x (may be null)
3940
m <- x$getinverse()
41+
42+
# if cached value is not null, use it, and print msg
4043
if(!is.null(m)) {
4144
message("getting cached data")
4245
return(m)
4346
}
47+
48+
# otherwise,retrieve matrix value, compute and store inverse
4449
data <- x$get()
4550
m <- solve(data, ...)
4651
x$setinverse(m)
52+
53+
# return newly computed inverse
4754
m
4855
}

0 commit comments

Comments
 (0)