Skip to content

Commit c55ad78

Browse files
committed
Working solution for PA2 with a test suite
1 parent 7f657dd commit c55ad78

File tree

2 files changed

+75
-9
lines changed

2 files changed

+75
-9
lines changed

cachematrix-test.R

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
library("testthat")
2+
context("Calculate the inverse of a square matrix")
3+
4+
testData <- function(x = matrix()) {
5+
inverseX <- solve(x)
6+
y <- makeCacheMatrix(x)
7+
list(x = x, inverseX = inverseX, y = y)
8+
}
9+
10+
test_that("match the example output for this function", {
11+
data <- testData(matrix(1:4, 2, 2))
12+
expect_equal(cacheSolve(data$y), data$inverseX)
13+
expect_equal(data$y$getHits(), 0)
14+
expect_equal(cacheSolve(data$y), data$inverseX)
15+
expect_equal(data$y$getHits(), 1)
16+
17+
data <- testData(matrix(rnorm(16 * 16), 16, 16))
18+
expect_equal(cacheSolve(data$y), data$inverseX)
19+
expect_equal(cacheSolve(data$y), data$inverseX)
20+
expect_equal(cacheSolve(data$y), data$inverseX)
21+
expect_equal(data$y$getHits(), 2)
22+
})
23+

cachematrix.R

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,58 @@
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-
6-
makeCacheMatrix <- function(x = matrix()) {
1+
## Calculate the Inverse of a Matrix
2+
## Provide a caching mechanism to reduce computation costs
73

4+
## `makeCacheMatrix`: Create a matrix with a memoizable solution
5+
##
6+
## args:: *theMatrix* is just a matrix
7+
##
8+
## methods::
9+
## $setMatrix - will (re)set the original matrix and clear the memoized solution
10+
## $getMatrix - returns the original matrix
11+
## $setSolution - sets the calculated solution
12+
## $getSolution - returns the calculated solution
13+
## $cacheHit - increment the cache hit counter
14+
## $getHits - returns the number of times there was a cache "hit"
15+
## and we skipped recomputing the solution
16+
makeCacheMatrix <- function(theMatrix = matrix()) {
17+
cachedSolution <- NULL
18+
cacheHits <- 0
19+
20+
setMatrix <- function(newMatrix) {
21+
theMatrix <<- newMatrix
22+
cachedSolution <<- NULL
23+
cacheHits <<- 0
24+
}
25+
getMatrix <- function() theMatrix
26+
27+
setSolution <- function(solution) cachedSolution <<- solution
28+
29+
getSolution <- function() cachedSolution
30+
31+
cacheHit <- function() {
32+
cacheHits <<- cacheHits + 1
33+
}
34+
35+
getHits <- function() cacheHits
36+
37+
list(setMatrix = setMatrix, getMatrix = getMatrix,
38+
setSolution = setSolution, getSolution = getSolution,
39+
cacheHit = cacheHit, getHits = getHits)
840
}
941

1042

11-
## Write a short comment describing this function
12-
43+
## `cacheSolve`: Return a matrix that is the inverse of 'x'
44+
##
45+
## args:: *x* was created using `makeCacheMatrix`
46+
## The original matrix must be solveable, or else an error will raise
47+
##
48+
## Use the cached results if possible
1349
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
50+
solution <- x$getSolution()
51+
if(!is.null(solution)) {
52+
x$cacheHit()
53+
return(solution)
54+
}
55+
solution <- solve(x$getMatrix(), ...)
56+
x$setSolution(solution)
57+
solution
1558
}

0 commit comments

Comments
 (0)