forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
46 lines (37 loc) · 1.57 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
## This pair of functions aid in the reduction of expensive matrix calculations.
## makeCacheMatrix creates a 'cache matrix' encapsulating the matrix data type while
## exposing functions to get/set the internal matrix and its inverse.
## The companion function cacheSolve takes advantage of the exposed functions to retrieve
## the inverted matrix if present before performing the computationally expensive inversion.
## Encapsulates a matrix, adding helper functions to get/set the matrix and its inverse
## Returns a list of function pointers
## Persists the original matrix and it's inverse (if set).
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL
# define the 'cacheMatrix' helper functions
set <- function(y) {
x <<- y
inv <<- NULL
}
get <-function() x
setinverse <- function(inverse) inv <<- inverse
getinverse <- function() inv
#return a list of pointers to our helper functions
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## Calculates the inverse of the matrix data encapsulated in the supplied "cache matrix"
## if the inverse has been calculated it uses the cached value, otherwise it calculates and
## saves the value.
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
cachedinverse <- x$getinverse()
if(!is.null(cachedinverse)) {
message("Using Cached Data - Bonus cpu cycle savings!")
return (cachedinverse)
}
cachedinverse <- solve(x$get())
x$setinverse(cachedinverse)
cachedinverse
}