-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
38 lines (36 loc) · 1011 Bytes
/
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
# Create a special matrix and return its inverse
# makeCacheMatrix - Creates a special matrix object
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL
insert_matrix <- function(new_data) {
x <<- new_data
inv <<- NULL
}
get_data <- function() {
return(as.matrix(x))
}
set_inv <- function(inverse) {
inv <<- inverse
}
get_inv <- function() {
return(inv)
}
list(
insert_matrix = insert_matrix,
get_data = get_data,
set_inv = set_inv,
get_inv = get_inv
)
}
# cacheSolve - Returns inverse of the matrix created by makeCacheMatrix
cacheSolve <- function(x, ...) {
inv <- x$get_inv()
if (!is.null(inv)) {
message("returning inverse from cache...")
return(inv)
}
data <- x$get_data()
inv <- solve(data, ...)
x$set_inv(inv)
return(inv)
}