forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
59 lines (46 loc) · 1.28 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
52
53
54
55
56
57
58
59
# Returns an 'inverse cacheable' matrix. The matrix value and the inverse are
# stored in closures
#
# Args:
# x: the matrix that we want to cache
#
# Returns:
# A list of four functions:
# set(data) - sets the value of the matrix
# get() - returns the value of the matrix (set as x from above)
# setinverse(data) - sets the cached inverse value of the matrix
# getinverse() - gets the cached inverse value of the matrix,
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)
}
# Solves the inverse of an 'inverse cacheable' matrix. If the value has
# already been computed, we use the cached value
#
# Args:
# x: the inverse cacheable matrix, of the type created by makeCacheMatrix()
#
# Returns:
# A matrix that is the inverse of the underlying matrix of the provided
# cacheable matrix
cacheSolve <- function(x, ...) {
inv <- x$getinverse()
if(!is.null(inv)) {
message("inverse is cached")
return(inv)
}
matr <- x$get()
inv <- solve(matr, ...)
x$setinverse(inv)
inv
}