forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
50 lines (40 loc) · 1.41 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
45
46
47
48
49
50
## Together these functions implement a "matrix" that caches
## its own inverse for future use.
## This function wraps matrix data, providing getter
## and setter functions for caching purposes.
makeCacheMatrix <- function(x = matrix()) {
m <- NULL
## this resets the stored inverse, and replaces the old x matrix
## with a new one
set <- function(y) {
x <<- y
m <<- NULL
}
get <- function() x
## this is called by the cacheSolve() function to set the calculated
## inverse
setinverse <- function(inverse) m <<- inverse
## and this gets the inverse if it has been set
getinverse <- function() m
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## This function checks the matrix object created above to
## see if its inverse is set, and sets the inverse if it is NULL.
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x' -
## if this hasn't been set already it will return NULL
m <- x$getinverse()
if(!is.null(m)) {
message("getting cached data")
return(m)
}
data <- x$get()
## Note: ginv() in the MASS package provides a generalized matrix
## inversion which will work on more types of matrices than solve()
## (although ginv() makes some assumptions you might not want to)
m <- solve(data, ...)
x$setinverse(m)
m
}