forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
72 lines (64 loc) · 2.15 KB
/
cachematrix.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#' These functions demonstrate the use of caching to prevent
#' repetition of possibly costly function calls.
#' Michael Powe
#' 20210926T073826
#' https://github.com/nyambol/ProgrammingAssignment2.git
#' d85bb1690ad919e6f54970f5b050ac6ea8afad79
#' Create a cached value in the parent environment. This overcomes the
#' lexical scope by keeping the cached value in a separate
#' environment. This function also sets attributes on the cached
#' matrix that are used by the `cacheSolve' function.
#'
#' @param x A square matrix
#' @examples
#' m <- matrix(sample(1:20,100,replace=TRUE),ncol=10)
#' makeCacheMatrix(m)
#'
makeCacheMatrix <- function(x = matrix()) {
ifelse(dim(x)[1] != dim(x)[2], return(message("Must be a square matrix")), "")
inv <- NULL
set <- function(y) {
x <<- y
inv <<- NULL
}
get <- function() x
setInverse <- function(inverseMatrix) inv <<- inverseMatrix
getInverse <- function() inv
list(set = set, get = get,
setInverse = setInverse,
getInverse = getInverse)
}
#' Retrieve or set a cached value for the inverse of a given matrix.
#' If a cached value is found, return it. Otherwise, calculate the
#' inverse, store it in the cache and return it. This function only
#' works with matrices created through the `makeCacheMatrix'
#' function. These have the attributes necessary to access the cache.
#'
#' @param x A square matrix created through `makeCacheMatrix'
#' @return inv The inverse of x
#' @examples
#' cm <- makeCacheMatrix(matrix(sample(1:20,100,replace=TRUE),ncol=10))
#' cacheSolve(cm)
#'
#' m <- matrix(sample(1:20, 100, replace=TRUE), ncol=10)
#' cacheSolve(m)
#' Error: $ operator is invalid for atomic vectors
#'
#' solve(m) == cacheSolve(cm)
#' Using cached data
#' [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#' [1,] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
#' [...]
cacheSolve <- function(x) {
# check for a cached inverse
inv <- x$getInverse()
if(!is.null(inv)) {
message("Using cached data")
return(inv)
}
# no cache, create it and return the inverse
mat <- x$get()
inv <- solve(mat)
x$setInverse(inv)
inv
}