forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
46 lines (39 loc) · 1.27 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
## Function to create a matrix whose inverse can be cached, as well as the
## function needed to retrieve the cached value
## Create a matrix object which supports caching of the matrix inverse
makeCacheMatrix <- function(x = matrix()) {
# Define an empty variable to hold the inverse
i <- NULL
# Function to set the contents of the matrix, clearing the cached value of
# the inverse in the process
set <- function(y) {
x <<- y
i <<- NULL
}
# Function to retrieve the matrix
get <- function() x
# Functions to get and set the inverse matrix
setinv <- function(inv) i <<- inv
getinv <- function() i
# Return a list containing the four getter and setter functions
list(set = set,
get = get,
setinv = setinv,
getinv = getinv)
}
## Return the inverse of the matrix, using the cached value if possible
cacheSolve <- function(x, ...) {
# Retrieve the cached value
i <- x$getinv()
# Return the cached value if it is valid
if(!is.null(i)) {
message("getting cached data")
return(i)
}
# Otherwise, calculate the inverse using solve() then cache and return the
# result
data <- x$get()
i <- solve(data, ...)
x$setinv(i)
i
}