Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1b21e2a

Browse files
committed
Implementation and test cases
1 parent 7f657dd commit 1b21e2a

File tree

3 files changed

+108
-6
lines changed

3 files changed

+108
-6
lines changed

cachematrix.R

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,62 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## Defines functions that compute and cache the inverse of a matrix to improve
2+
## performance when the inverse is needed multiple times.
33

4-
## Write a short comment describing this function
4+
## Wraps the given matrix to enable the caching of its inverse. Returns a
5+
## list with the ability to get/set the matrix and get/set its inverse.
56

67
makeCacheMatrix <- function(x = matrix()) {
8+
inv <- NULL
79

8-
}
10+
## Set a new matrix, and be sure to reset the
11+
## cached inverse
12+
set <- function(y) {
13+
x <<- y
14+
inv <- NULL
15+
}
16+
17+
## Return the current matrix
18+
get <- function() x
19+
20+
## Cache the computed inverse
21+
setInverse <- function(i) {
22+
inv <<- i
23+
}
924

25+
## Return the cached inverse
26+
getInverse <- function() inv
27+
28+
## Return a list with the functions as properties
29+
list(
30+
set=set,
31+
get=get,
32+
setInverse=setInverse,
33+
getInverse=getInverse
34+
)
35+
}
1036

11-
## Write a short comment describing this function
37+
## Finds the inverse of a matrix constructed by makeCacheMatrix. Caches the
38+
## inverse and returns it. On repeat invocations, returns the cached value
39+
## without solving for the inverse again.
1240

1341
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
42+
## Check if the inverse has been computed and cached
43+
inv <- x$getInverse()
44+
45+
if(!is.null(inv)) {
46+
## If so, return it without recomputing it
47+
return(inv)
48+
}
49+
50+
## Fetch the matrix from its wrapping
51+
m <- x$get()
52+
53+
## Compute the inverse of the given matrix, passing through any additional
54+
## parameters given to the cacheSolve function
55+
inv <- solve(m, ...)
56+
57+
## Cache the inverse to avoid recomputing it next time
58+
x$setInverse(inv)
59+
60+
## Return the inverted matrix
61+
inv
1562
}

run_tests.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
library('testthat')
2+
3+
source('cachematrix.R')
4+
5+
test_dir('test', reporter = 'Summary')

test/test_cachematrix.R

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
## Defines unit tests for functions in cachematrix
2+
3+
context("cachematrix")
4+
5+
test_that("makeCacheMatrix() returns a list", {
6+
x <- makeCacheMatrix()
7+
expect_is(x, "list")
8+
})
9+
10+
test_that("makeCacheMatrix(m)$get returns the matrix", {
11+
m <- matrix(2,3)
12+
x <- makeCacheMatrix(m)
13+
expect_identical(x$get(), m)
14+
})
15+
16+
test_that("makeCacheMatrix(m)$set stores a new matrix", {
17+
x <- makeCacheMatrix()
18+
m <- matrix(2,3)
19+
x$set(m)
20+
expect_identical(x$get(), m)
21+
})
22+
23+
test_that("makeCacheMatrix(m) sets/gets the cached inverse", {
24+
x <- makeCacheMatrix()
25+
26+
expect_identical(x$getInverse(), NULL)
27+
28+
## bogus inverse, just for unit testing
29+
im <- matrix(1,1)
30+
x$setInverse(im)
31+
expect_identical(x$getInverse(), im)
32+
})
33+
34+
test_that("cacheSolve(x) uses the cached inverse", {
35+
x <- makeCacheMatrix()
36+
37+
## mock a cached inverse and expect to get it back
38+
im <- matrix(2,2)
39+
x$setInverse(im)
40+
expect_identical(cacheSolve(x), im)
41+
})
42+
43+
test_that("cacheSolve(x) caches a newly computed inverse", {
44+
## expected inverse
45+
im <- matrix(c(-2, 1, 1.5, -0.5), nrow=2)
46+
m <- matrix(1:4, nrow=2, ncol=2)
47+
x <- makeCacheMatrix(m)
48+
expect_equal(cacheSolve(x), im)
49+
})
50+

0 commit comments

Comments
 (0)