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
48 lines (33 loc) · 1.08 KB
/
cachematrix.R
File metadata and controls
48 lines (33 loc) · 1.08 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
39
40
41
42
43
44
45
46
47
48
## these functions allow for calculating the inverse of a matrix and stores the final output
## this allows for data to be retrieved instead of recalculating when a matrix has already been previously calculated
## makeCacheMatrix() allows the user to create a matrix
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL
# setter and getter for original matrix
set <- function(y) {
x <<- y
inv <<- NULL
}
get <- function() x
# setter and getter for inverse
set_inverse <- function(solve) inv <<- solve
get_inverse <- function() inv
list(set = set, get = get,
set_inverse = set_inverse,
get_inverse = get_inverse)
}
## cacheSolve() finds the inverse of the matrix
## it gives the cached result if previously available
cacheSolve <- function(x, ...) {
## finds stored inverse and returns it if possible
inv <- x$get_inverse()
if(!is.null(inv)) {
message("Getting cached inverse matrix...")
return(inv)
}
## calculates new matrix if unavailable
data <- x$get()
inv <- solve(data, ...)
x$set_inverse(inv)
inv
}