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
64 lines (51 loc) · 1.95 KB
/
cachematrix.R
File metadata and controls
64 lines (51 loc) · 1.95 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
## These functions accomplish the caching of a matrix inversion, which can be
## a costly operation. Using a previously computed inverse of a matrix
## instead of computing it again saves computation time.
##
## makeCacheMatrix can be used to obtain a special object of a matrix.
## cacheSolve uses a matrix object created by makeCachematrix to compute
## the inverse of the matrix and store the result in the object.
## If the inversion of the same matrix has been done before,
## the result will be pulled out of the cache.
## create a matrix object with the following functions:
## * set the value of the matrix
## * get the value of the matrix
## * set the value of the inverse of the matrix
## * get the value of the inverse of the matrix
makeCacheMatrix <- function(x = matrix()) {
# initialize the inverse of the matrix with NULL
inv <- NULL
# set stored matrix to new matrix y and reinitialize inverse
set <- function(y) {
x <<- y
inv <<- NULL
}
# get stored matrix
get <- function() x
# set inverse of matrix
setinv <- function(inv) inv <<- inv
# get inverse of matrix
getinv <- function() inv
# set list of operations
list(set = set, get = get, setinv = setinv, getinv = getinv)
}
## compute the inverse of a matrix object created by makeCacheMatrix
## if the inverse has already been calculated (and the matrix has not changed),
## then cacheSolve will retrieve the inverse from the cache.
cacheSolve <- function(x, ...) {
# get inverse of matrix object
inv <- x$getinv()
# if cached version of inverse is available, return that
if(!is.null(inv)) {
message("getting cached data")
return(inv)
}
# get the underlying matrix
data <- x$get()
# get the inverse of the matrix
inv <- solve(data, ...)
# store the inversed matrix in the cache
x$setinv(inv)
# return the calculated inverse matrix
inv
}