forked from rdpeng/ProgrammingAssignment2
/
cachematrix.R
51 lines (44 loc) · 1.03 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
## Create a matrix with functionality to cache inverse of a matrix to reduce computation time
## Cache Matrix object.
## Argument x is a regular R matrix
makeCacheMatrix <- function(x = matrix()) {
inverse <- NULL
set <- function(y)
{
x <<- y
inverse <<- NULL
}
get <- function()
{
x
}
set_inverse <- function(new_inverse)
{
inverse <<- new_inverse
}
get_inverse <- function()
{
inverse
}
list(set = set,
get = get,
set_inverse = set_inverse,
get_inverse = get_inverse)
}
## Returns inverse of a matrix
## Argument x is a cached matrix
## Calculates inverse if not already cached.
## If cached, returns inverse from cache
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
inverse <- x$get_inverse()
if (!is.null(inverse))
{
message("Getting cached data")
return(inverse)
}
original_matrix <- x$get()
inverse <- solve(original_matrix)
x$set_inverse(inverse)
inverse
}