forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
58 lines (46 loc) · 1.81 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
## Logan J Travis
## github.com/l0neJT
## 2014-04-23
## 'makeCacheMatrix' and 'cacheSolve' simplify data management for
## a primary matrix and its inversion by keeping both in memory,
## limiting access through a list of control functions, and by checking
## the cached matrices prior to solving for the inversion.
## store and manage a matrix along with its inversion
## keeps both in memory for retrieval without reprocessing
## returns a list of control functions (e.g. get, set, clear, etc.)
## NO DIRECT ACCESS to cached matrices
makeCacheMatrix <- function(x = matrix()) {
invX <- NULL
## store a new primary matrix and reset the inversion matrix
set <- function(y) {
x <<- y
invX <<- NULL
}
## return the primary matrix
get <- function() x
## reset both the primary and inversion matrices; for testing
clear <- function() set(matrix())
## store the inverstion of matrix 'x' to 'invX'
## calls 'solve' from the R base package
## returns an error if no solution possible
solveInv <- function() invX <<- solve(x)
## return inversion matrix
getInv <- function() invX
## reset inversion matrix to NULL; for testing
clearInv <- function() invX <<- NULL
## return the list of avaialble function calls
list(set = set, get = get, clear = clear, solveInv = solveInv,
getInv = getInv, clearInv = clearInv)
}
## take a 'makeCacheMatrix' object and return the inversion matrix
## checks memory for a non-NULL inversion matrix either returning the
## stored matrix or - if NULL - solving first then returning the matrix
cacheSolve <- function(x, ...) {
if(is.null(x$getInv())){
message("solving inverse matrix")
x$solveInv()
} else {
message("retrieving from cache")
}
x$getInv()
}