forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcachematrix.R
More file actions
39 lines (35 loc) · 977 Bytes
/
Copy pathcachematrix.R
File metadata and controls
39 lines (35 loc) · 977 Bytes
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
## Two functions that perform memoization of
## matrix inversion.
## Creates a vector consisting of a:
## 1. set function (containing a matrix and its inverse)
## 2. get function
## 3. store_inverse function
## 4. get_inverse function
makeCacheMatrix <- function(x = matrix()) {
i <- NULL
set <- function(y) {
x <<- y
i <<- NULL
}
get <- function() x
store_inverse <- function(inverse) i <<- inverse
get_inverse <- function() i
list(set = set, get = get,
store_inverse = store_inverse,
get_inverse = get_inverse)
}
## Return a matrix that is the inverse of 'x'
## (either a cached copy, or a recently computed copy)
cacheSolve <- function(x, ...) {
# check if the inverse is already stored...
i <- x$get_inverse()
if(!is.null(i)) {
message("getting cached data")
return(i)
}
# ... if not, compute the inverse and store it for the future
data <- x$get()
i <- solve(data, ...)
x$store_inverse(i)
i
}