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
75 lines (55 loc) · 2.81 KB
/
Copy pathcachematrix.R
File metadata and controls
75 lines (55 loc) · 2.81 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
65
66
67
68
69
70
71
72
73
74
## Below are two R functions that create a special object to store a matrix
## and cache its inverse. It is assumed the matrix stored is invertible.
## The first function, makeCacheMatrix, creates a special object which is
## a list whose elements are functions and does the following:
## - set the value of the matrix
## - get the value of the matrix
## - set the value of the matrix's inverse
## - get the value of the matrix's inverse
## Note: The reference to parent (or enclosing) environment is also stored in the special object.
## The functions in list gain this rereferce when they are created during a call to makeCacheMatrix,
## The matrix of interest and its inverse are part of this referenced environment and
## their values are read or written when the respective function from the list is invoked.
makeCacheMatrix <- function(x = matrix()) {
matrixInv <- NULL # placeholder for storing inverse but nothing exists yet
set <- function(y) {
x <<- y # replaces matrix x in parent
matrixInv <<- NULL # nullify the inverse in cache whenever x changes
message("New matrix stored and cache cleared! Use cacheSolve to refresh cache.")
}
get <- function() {
x
}
setInv <- function(inverse) {
matrixInv <<- inverse #store the inverse matrix.
}
getInv <- function() {
matrixInv
}
## Return a list containing above functions
list(set = set, get = get, getInv = getInv, setInv = setInv)
}
## The cacheSolve function below computes the inverse of the matrix in the special object returned
## by makeCacheMatrix function above.
##
## It does a check first on the special "matrix" object to see if the inverse is already calculated.
##
## If the check passes (i.e. inverse is already calculaetd and matrix hasn't changed since last calc)
## then the inverse retrieved from the special object's cache is returned.
##
## Otherwise, the inverse is calculated using R's solve function and the result saved to the
## special object's cache.
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of matrix in special object 'x'
inverse <- x$getInv() # using function in list to get value of matrix's inverse
## inverse is not NULL means data in cache.
if(!is.null(inverse)) {
message("Retrieving cached data..")
return(inverse) # break execution here and return inverse matrix
}
# inverse is NULL, so calculate inverse and store with the object
data <- x$get() # using list function to get value of matrix
invData <- solve(data) #calculate inverse matrix
x$setInv(invData) # save into cache using setInv list function
invData # return inverse matrix
}