Skip to content

Commit 7299ca3

Browse files
committed
More comments to help understanding.
1 parent 288d939 commit 7299ca3

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

cachematrix.R

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,26 @@
44

55

66
## Creates a "matrix" that can cache its own inverse.
7+
## Returns a list containing functions to view or edit the matrix and its
8+
## inverse.
79
makeCacheMatrix <- function(x = matrix()) {
10+
11+
## Inverse not yet computed
812
inv <- NULL
13+
## The setter function of the matrix
914
set <- function( y ) {
1015
x <<- y
16+
## The matrix just got a new value, so
17+
## the inverse is not yet known
1118
inv <<- NULL
1219
}
20+
## The getter function of the matrix
1321
get <- function() x
22+
## The setter funcction of the inverse of the matrix
1423
setinv <- function( invv ) inv <<- invv
24+
## The getter function of the inverse of the matrix
1525
getinv <- function() inv
26+
## Return the list that makes the matrix functions accessible
1627
list( set = set, get = get,
1728
setinv = setinv,
1829
getinv = getinv )
@@ -23,14 +34,16 @@ makeCacheMatrix <- function(x = matrix()) {
2334
## cache (if available), or by computing it.
2435
## ASSUME that the matrix supplied is always invertible.
2536
cacheSolve <- function(x, ...) {
26-
## Return a matrix that is the inverse of 'x'
37+
38+
## Get the stored inverse (if any) from 'x'
2739
inv <- x$getinv()
40+
## If the inverse is cached, return cached value
2841
if ( !is.null(inv) ) {
2942
message("Getting cached data...")
3043
return( inv )
31-
}
44+
} ## Else get the data...
3245
data <- x$get()
33-
inv <- solve( data )
34-
x$setinv( inv )
35-
inv
46+
inv <- solve( data ) ## ... compute its inverse ...
47+
x$setinv( inv ) ## ... set the inverse in x's cache, and...
48+
inv ## ... return the computed inverse.
3649
}

0 commit comments

Comments
 (0)