forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
46 lines (42 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
## makeCacheMatrix is a function that create a "special" matrix. This matrix is
# a list that stores several fucntions that allow a user to (1) set the value of
# that matrix, (2) call the matrix, (3) set the inverse of the matrix, and
# (4) call the inverse of the matrix.
## cacheSolve caculates the inverse of special matrix constructed with
# makeCacheMatrix. cacheSolve checks to see if the inverse has already been
# calculated, if it has is pulls the inverse for the cache, if it has not the
# invese is caculated and stored in the special matrix.
# B must be a numerical, square matrix.
makeCacheMatrix <- function(B = matrix()){
iB <- NULL
set <- function(Y) {
B <<- Y
iB <<- NULL
}
get <- function () B
setinv <- function(y) {
iB <<- y
}
getinv <- function () iB
list( set = set, get = get, setinv = setinv, getinv = getinv)
}
cacheSolve <- function(B, ...){
iB <- B$getinv()
if(!is.null(iB)){
message("getting cached data")
return(iB)
}
data <- B$get()
iB <- solve(data)
B$setinv(iB)
iB
}
## example
A = matrix(
c(2, 4, 1, 5),
nrow=2,
ncol=2)
test = makeCacheMatrix(A)
cacheSolve(test)
# check that the inverse is cached, and cached data called.
cacheSolve(test)