forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcacheMatrixInverse.R
More file actions
61 lines (54 loc) · 2.29 KB
/
cacheMatrixInverse.R
File metadata and controls
61 lines (54 loc) · 2.29 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
# The following functions provide a means to cache the results of a matrix inversion.
# The purpose of this exercise is to take advantage of R properties to create essentially
# a static variable which contains the inverse of an existing matrix to avoid computational
# cost of inverting the matrix each time it is needed.
#
# The function is somewhat brittle. It does not test to ensure that the input matrix was
# square (nxn) or whether it is invertable, so it is subject to potential run-time errors
#
# Calling information:
#
# m <- matrix (rnorm(25,5,5)) # build an initial 5x5 test matrix
# mCached <- makeCachedMatrix(m) # creates the special cached version of the matrix
# and the getter/setter functions
# mInverse <- cacheSolve(m) # First call both creates and returns the inverse
# and creates the cached, static copy of the inverse
# mInverse2 <- cacheSolve(m) # Subsequent call returns the cached copy; it does
# have to re-invert the matrix
#
# makeCacheMatrix establishes the getter and setter functions for the matrix and the inverse
#
makeCacheMatrix <- function (x=matrix()) {
inv <- NULL
# set the initial cached matrix and inverse
set <- function (y) {
x <<- y
inv <<- NULL
}
# get the static value of the matrix
get <- function () x
# set the static value for the matrix inverse
setinv <- function(inverse) inv <<- inverse
# get the static value of the matrix inverse
getinv <- function () inv
# establish the list of return objects
list(set=set,get=get,setinv=setinv,getinv=getinv)
}
#
# cacheSolve returns the inversion of the special matrix managed by our makeCacheMatrix functions
#
cacheSolve <- function(x, ...) {
inv <- x$getinv() # attempt to get our cached inverted matrix
# if the matrix has been cached, return the cached version
if (!is.null(inv)) {
message("getting cached matrix")
return (inv)
}
# otherwise it is necessary to used the setinv to create the cached version
data <- x$get()
# use the solve function to invert
inv <- solve(data, ...)
# save the cached version
x$setinv (inv)
inv
}