Skip to content

Commit 3041c9e

Browse files
committed
Refactored code and added comment
1 parent 5fe2523 commit 3041c9e

File tree

1 file changed

+34
-11
lines changed

1 file changed

+34
-11
lines changed

cachematrix.R

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,62 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
# 2 functions to assist in caching the inverse of a matrix
32

4-
## Write a short comment describing this function
5-
6-
## TODO:
3+
# TODO:
74
# write guard clause against x != matrix
8-
# fix indentation to 4
5+
96
makeCacheMatrix <- function(x = matrix()) {
7+
# Wrap the matrix object provided and allow the caching of it's inverse
8+
#
9+
# Args:
10+
# x: a matrix object whose inverse can be calculated
11+
#
12+
# Returns:
13+
# A list of four functions (set, get, setInverse, getInverse)
14+
1015
inverse <- NULL
16+
17+
# replace the matrix with a new one
1118
set <- function(y) {
1219
x <<- y
1320
inverse <<- NULL
1421
}
22+
23+
# get the matrix object
1524
get <- function() x
25+
26+
# set the inverse of the matrix
1627
setInverse <- function(inverseMatrix) inverse <<- inverseMatrix
28+
29+
# get the saved inverse or NULL
1730
getInverse <- function() inverse
31+
32+
# return object
1833
list(set = set, get = get,
1934
setInverse = setInverse,
2035
getInverse = getInverse)
2136
}
2237

23-
24-
## Write a short comment describing this function
25-
2638
cacheSolve <- function(x, ...) {
27-
## Return a matrix that is the inverse of 'x'
39+
# Compute the inverse of the matrix provided by the makeCacheMatrix object
40+
#
41+
# Args:
42+
# x: makeCacheMatrix object
43+
#
44+
# Returns:
45+
# The inverse of the provided matrix
2846

2947
inverse <- x$getInverse()
48+
49+
# Returns the cached inverse if it is available
3050
if(!is.null(inverse)) {
3151
message("getting cached inverse matrix")
3252
return(inverse)
3353
}
3454

55+
# calculate the inverse
3556
data <- x$get()
3657
inverse <- solve(data, ...)
3758
x$setInverse(inverse)
59+
60+
# return the inverse
3861
inverse
39-
}
62+
}

0 commit comments

Comments
 (0)