forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcacheSolve.R
More file actions
43 lines (42 loc) · 1.76 KB
/
cacheSolve.R
File metadata and controls
43 lines (42 loc) · 1.76 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
# Purpose: Retrieves the inverse "i" of a square matrix,
# if the inverse is already cached, else it solves the inverse
# and caches the result in an existing list. This list is
# located in the parent environment from which cacheSolve was called.
#
# Note: cacheSolve.R and makeCacheMatrix.R work together; both
# must be sourced into the parent environment to achieve the purpose.
#
# The other source, makeCacheMatrix.R, defines the function
# makeCacheMatricx() and a list of functions in the parent
# enviroment which are called by cacheSolve.
#
# To achieve the purpose, the user should:
#
# 1) initialize the list of functions with myCacheMatrix :
#
# yourCM <- makeCacheMartix(yourmatrix)
#
# 2) Solve for the inverse:
#
# cacheSolve(yourCM)
#
# On subsequent calls of cacheSolve(yourCM), "getting cached inverse"
# is displayed, until data in yourCM is changed or removed.
# yourCM$set(yourNewMatrix) will update data, clear cache.
#
# Note: these functions assume a valid invertible matrix is used.
#
# ### prepped by WhitefishDontJump (aka J Raphael), 2014 & 2015
# repeating course as CTA
#######################################################################
cacheSolve <- function(x, ...) {
i <- x$getinv() # is the inverse cached?
if(!is.null(i)) { # if cached, then message
message ("getting cached inverse") # and
return(i) # exit, ouput cached result
}
data <- x$get() # implicit 'else', get data
i <- solve(data, ...) # solve for inverse
x$setinv(i) # store result in parent
i # output result
}