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
73 lines (59 loc) · 2.15 KB
/
cachematrix.R
File metadata and controls
73 lines (59 loc) · 2.15 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
## The following functions allow you to create a cache store
## of a matrix and its inverse value, which you can use to
## repetitively extract the matrix's inverse value without
## running the expensive calculation each time
##
## Example usage:
## # create a cache object with your matrix
## matrixCache <- makeCacheMatrix$set(myMatrix)
## # get the inverse value
## cacheSolve(matrixCache)
## # change the matrix in the cache object
## matrixCache$set(newMatrix)
## This function creates a special "matrix" object that can
## cache its inverse
makeCacheMatrix <- function(cachedMatrix = matrix()) {
# set the default inverse value
matrixInverse <- NULL
# set the matrix and reset the inverse value
set <- function(matrix) {
# only necessary if the matrix has changed
if (!identical(matrix, cachedMatrix)) {
cachedMatrix <<- matrix
matrixInverse <<- NULL
}
}
# return the set matrix
get <- function() cachedMatrix
# set the inverse value
setInverse <- function(inverse) matrixInverse <<- inverse
# return the cached inverse value
getInverse <- function() matrixInverse
# set the functions as a list in makeCacheMatrix
list(set = set, get = get,
setInverse = setInverse,
getInverse = getInverse)
}
## This function computes the inverse of the special "matrix"
## returned by makeCacheMatrix above. If the inverse has already
## been calculated (and the matrix has not changed), then the
## cachesolve retrieves the inverse from the cache
cacheSolve <- function(matrixCache = makeCacheMatrix, ...) {
# check if the cache has an existing inverse value
matrixInverse <- matrixCache$getInverse()
# return the cached inverse value if there is one
if(!is.null(matrixInverse)) {
return(matrixInverse)
}
# get the cached matrix
cachedMatrix <- matrixCache$get()
if(is.null(cachedMatrix)) {
error("no cached matrix found")
}
# calculate the inverse value
matrixInverse <- solve(cachedMatrix, ...)
# cache the inverse value
matrixCache$setInverse(matrixInverse)
# return the inverse value
matrixInverse
}