forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
56 lines (41 loc) · 2.03 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
## makeCacheMatrix specifies a matrix which will be stored at parent level.
## It contains a solve() function to find the inverse of the matrix passed to it.
## cacheSolve checks for the presence of the inverse in parent cache. If the
## inverse was not cached, it uses the solve method in makeCacheMatrix to find it.
## Creates a special "matrix" object that can cache its inverse
makeCacheMatrix <- function(x = matrix()) {
mInv<-matrix() ##initialize a local, empty inverse
mOrig <- x
setInv<- function(x2) { # calculate the inverse of the original matrix
mInv<<- solve(x2) # cache the inverse
}
getInv<-function() mInv # return the inverse
setmOrig <- function(x3) { # cache the original matrix
mOrig <<- x3
}
getmOrig <- function() mOrig # return the original matrix
# list functions to be constructed
list(getInv = getInv, setInv = setInv,
getmOrig = getmOrig, setmOrig = setmOrig)
}
##*************** END OF FUNCTION
cacheSolve <- function(MCM, ...) { # makeCachematrix will be passed to this function
## Return a matrix that is the inverse of the original matrix passed to it
orig <- MCM$getmOrig # store local copy of original matrix
# check for original matrix change
orig <- MCM$getmOrig() # store local copy of original matrix
if(orig == MCM$mOrig) { # if a match is found
# check for cached inverse
inv <- MCM$getmInv()
if(!is.null(inv)) { # if a value is stored
message("Returning cached inverse...")
inv # print the inverse
}
} else { # calculate inverse
#first, cache original matrix
MCM$setmOrig(orig)
message("Calculating inverse...")
MCM$setmInv(orig) # pass local copy of original matrix for calculation
MCM$getmInv() # print inverse
}
}