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
84 lines (70 loc) · 2.8 KB
/
Copy pathcachematrix.R
File metadata and controls
84 lines (70 loc) · 2.8 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
75
76
77
78
79
80
81
82
83
84
## ----------------------------------------------------------------------------------
## makeCacheMatrix
## - parameters:
## - x: matrix object defaulting to an empty matrix if omitted
## - output:
## list object of functions for cacheable matrix operations
## - set / get: sets or gets the enclosed matrix
## - setInverse: sets an internal variable with the inverse of the enclosed matrix
## - getInverse: retrieves the inverse of the enclosed matrix
##
## - notes
## - the "enclosed" matrix is the matrix parameter initially passed as a parameter
## and bound via closure to the object created by makeCacheMatrix(matrix). This
## value is changed via the set(matrix) function.
## ----------------------------------------------------------------------------------
makeCacheMatrix <- function(x = matrix()) {
## lexically scoped variable caching inverted matrix result
m <- NULL
## update input matrix scoped by closure with new matrix value
## and reset the inverted matrix cache
set <- function(y){
x <<- y
m <<- NULL
}
## get input matrix scoped by closure
get <- function(){
x
}
## update cache with inverted matrix
setInverse <- function(im){
m <<- im
}
## get cached inverted matrix
getInverse <- function(){
m
}
## return a list object of functions bound to the input matrix via closure
list(set = set, get = get, setInverse = setInverse, getInverse = getInverse)
}
## ----------------------------------------------------------------------------------
## cacheSolve
## - parameters:
## - x: makeCacheMatrix list result object
## - output:
## inverse of parameter x matrix
##
## - notes
## - if x does not have a cached inverse, the inverse is computed via the solve
## function and stored in x via x's setInverse(im) function
## - currently assumes a square invertable matrix
## ----------------------------------------------------------------------------------
cacheSolve <- function(x, ...) {
## get the cached inverse if the matrix
tmp <- x$getInverse()
## if the cached inverse is NOT null, we indeed have the cached inverse
## so go ahead and return it
if (!is.null(tmp)) {
message("getting cached data")
return(tmp)
}
## we do not have the inverse cached yet
## get the matrix
tmp2 <- x$get()
## solve for the inverse of that matrix
tmp <- solve(tmp2, ...)
## cached the result
x$setInverse(tmp)
## and return the result
tmp
}