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
38 lines (35 loc) · 1.05 KB
/
cachematrix.R
File metadata and controls
38 lines (35 loc) · 1.05 KB
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
# This file implements two functions: makeCacheMatrix and cacheSolve.
# Together they provide a way to construct a matrix object that caches
# its inverse computation, provinding faster access to it.
# makeCacheMatrix creates an object that represents a matrix that the
# inverse operation is cached for faster computations.
makeCacheMatrix <- function(x = matrix()) {
x_inv <- NULL
set <- function(x_new) {
x <<- x_new
x_inv <<- NULL
}
get <- function() {
x
}
set_inverse <- function(x_inv_new) {
x_inv <<- x_inv_new
}
get_inverse <- function() {
x_inv
}
list(set = set, get = get, set_inverse = set_inverse, get_inverse = get_inverse)
}
# cacheSolve solves the inverse of the matrix x and caches its results
# so that later requests are not calculated again
cacheSolve <- function(x, ...) {
xi <- x$get_inverse()
if(!is.null(xi)) {
message("getting cached inverse matrix")
return(xi)
}
x_data <- x$get()
xi <- solve(x_data)
x$set_inverse(xi)
xi
}