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
40 lines (37 loc) · 1.38 KB
/
cachematrix.R
File metadata and controls
40 lines (37 loc) · 1.38 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
## The following functions contain the solution to Programming Assignment 2.
## They can be used to calculate the inverse of an invertible matrix using
## a thin wrapper for the matrix that caches the result of the calculation.
## Creates a wrapper of the given matrix that can cache its inverse.
## Warning: using wrapper$set(y) will change the underlying matrix and
## discard the cached inverse, but modifying the wrapped matrix
## manually can lead to an inconsistent state!
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL
set <- function(y) {
x <<- y
inv <<- NULL
}
get <- function() x
setinverse <- function(inverse) inv <<- inverse
getinverse <- function() inv
list(
set = set,
get = get,
setinverse = setinverse,
getinverse = getinverse
)
}
## Takes a matrix wrapper created with makeCacheMatrix(x) and calculates the
## inverse matrix unless it has already been calculated and cached. Any
## additional arguments will be passed to the solve() function.
## Warning: if the inverse matrix has already been calculated and cached,
## solve() won't be invoked again, so the additional arguments will be ignored!
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
inv <- x$getinverse()
if (is.null(inv)) {
inv <- solve(x$get(), ...)
x$setinverse(inv)
}
inv
}