Skip to content

Commit f7ca2e8

Browse files
committed
Implement cached matrix and inverse solver
1 parent 7f657dd commit f7ca2e8

File tree

1 file changed

+79
-8
lines changed

1 file changed

+79
-8
lines changed

cachematrix.R

Lines changed: 79 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,86 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
3-
4-
## Write a short comment describing this function
1+
## Assignment 2
2+
## Implements a function that returns a matrix wrapper that caches the result of
3+
## the inverse calculations, and a function to return the inverse from such an object.
4+
## Also includes a test function.
55

6+
## A matrix wrapper that memoizes[0] the inverse calculation.
7+
## Returns a list containing getter/setter functions for the matrix
8+
## and its inverse.
9+
## 0 - https://en.wikipedia.org/wiki/Memoization
10+
##
11+
## Example
12+
## x <- matrix(rnorm(9), 3, 3)
13+
## x.cached <- makeCacheMatrix(x)
14+
## x.cached$get() # same as x
15+
## x.cached$getInverse() # Inverse of x, only calculated on the first call
616
makeCacheMatrix <- function(x = matrix()) {
7-
17+
cachedInverse <- NULL
18+
cachedMatrix <- x
19+
20+
# Setter function
21+
set <- function(y) {
22+
cachedMatrix <<- y
23+
cachedInverse <<- NULL # Inverse has changed, so invalidate cache
24+
}
25+
26+
# Getter function
27+
get <- function() cachedMatrix
28+
29+
# Setter function for the inverse
30+
setInverse <- function(inverse) cachedInverse <<- inverse
31+
32+
# Getter function for the inverse
33+
# implements a read through cache rather than requiring
34+
# the caller to know about internal details
35+
getInverse <- function(...) {
36+
if(is.null(cachedInverse)) {
37+
message("Cache miss - recalculating inverse")
38+
cachedInverse <<- solve(cachedMatrix, ...)
39+
}
40+
cachedInverse
41+
}
42+
43+
# Return a structure containing the getters and setters
44+
# Because of lexical scoping rules, they have a private copy of the cache
45+
list(set = set,
46+
get = get,
47+
setInverse = setInverse,
48+
getInverse = getInverse)
849
}
950

1051

11-
## Write a short comment describing this function
12-
52+
## Returns the inverse of a matrix previously wrapped by makeCacheMatrix
53+
## Note the wrapper is responsible for cache, so this is effectively a pass through
1354
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
55+
## Return a matrix that is the inverse of 'x'
56+
x$getInverse(...)
1557
}
58+
59+
test <- function() {
60+
a.normal <- matrix(rnorm(9), 3, 3)
61+
b.normal <- matrix(rnorm(9), 3, 3)
62+
63+
a.cached <- makeCacheMatrix(a.normal)
64+
b.cached <- makeCacheMatrix(b.normal)
65+
66+
if (identical(a.cached$get(), a.normal)) {
67+
print("Good, a is the same")
68+
}
69+
70+
if (identical(b.cached$get(), b.normal)) {
71+
print("Good, b is the same")
72+
}
73+
74+
if (identical(solve(a.normal), cacheSolve(a.cached))) {
75+
print("Good, a's inverse is correct")
76+
}
77+
78+
if (identical(solve(b.normal), cacheSolve(b.cached))) {
79+
print("Good, b's inverse is correct")
80+
}
81+
82+
if (identical(solve(a.normal), cacheSolve(a.cached))) {
83+
print("Good, a's inverse is still correct and you should not see a cache miss")
84+
}
85+
86+
}

0 commit comments

Comments
 (0)