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
65 lines (61 loc) · 2.22 KB
/
cachematrix.R
File metadata and controls
65 lines (61 loc) · 2.22 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
## Caching the Inverse of a Matrix
##
## Matrix inversion is usually a costly computation. These functions
## expose a matrix inversion function called cacheSolve which will
## cache the inverse of a matrix so that it does not to be computed
## repeatedly.
##
## Usage:
## > mc <- makeCacheMatrix(matrix(...))
## > cacheSolve(mc)
## (Subsequent calls of cacheSolve(mc) will return the cached result)
##
## @date 2014-08-22
##
## Given a matrix, create and return a new matrix-like object that
## can cache its inverse.
##
## @param x a matrix which will be wrapped by the function
## @return a matrix-wrapping object which includes methods to
## get/set data and get/set the cached inverse
## @see cacheSolve
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL
# get() returns the matrix encapsulated by the object
get <- function() x
# set() clears both the data and the cached inverse
set <- function(y) {
x <<- y
inv <<- NULL
}
# getInverse() returns the cached inverse
getInverse <- function() inv
# setInverse() saves the inverse to the cache
setInverse <- function(inverse) inv <<- inverse
list(set = set,
get = get,
setInverse = setInverse,
getInverse = getInverse)
}
## Given a matrix object returned by makeCacheMatrix(), either returned
## the cached inverse or calculate the inverse and cache it.
##
## @param x a matrix object returned by makeCacheMatrix()
## @param ... any additional arguments to the solve() function
## @return the inverse of the matrix, which may have been calculated
## on the fly or returned from a cache
## @see makeCacheMatrix
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
## Return the inverse from the cache if it has been cached already
inverse <- x$getInverse()
if (!is.null(inverse)) {
message("getting cached data")
return(inverse)
}
# calculate the inverse, cache it, and return it
data <- x$get()
inverse <- solve(data, ...)
x$setInverse(inverse)
inverse
}