-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
38 lines (33 loc) · 1.09 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
## These two functions create and calculate (and cache) the
## inverse of a square matrix
## The makeCacheMatrix function takes a matrix and wraps it
## in code to allow the inverse to be cached for future use
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL
set <- function(y) {
x <<- y
inv <<- NULL
}
get <- function() x
setinv <- function(inv1) inv <<- inv1
getinv <- function() inv
list(set = set, get = get,
setinv = setinv,
getinv = getinv)
}
## The cacheSolve function takes the wrapped matrix created using
## makeCacheMatrix and returns the inverse, calculating it if necessary
## i.e., if it is being called for the first time on this particular matrix
## instance.
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
inv <- x$getinv()
if(!is.null(inv)) {
message("getting cached data")
return(inv)
}
data <- x$get()
inv <- solve(data, ...)
x$setinv(inv)
inv
}