forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
48 lines (43 loc) · 1.74 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
## These functions implement a cacheable invert operation for a matrix
## As this is a time consuming operation, we use the << operator to
## cache the inverted matrix and then serve up the cached copy using the
## cacheSolve() function if it is available.
## makeCacheMatrix takes a regular matrix and exposes getter and setter
## functions for the cached version of the matrix. It returns a list of
## functions that are usable with the cached matrix (get,set,getinverse,setinverse)
makeCacheMatrix <- function(x = matrix()) {
## m is the variable where the cache is stored
m <- null
## set() voids the cache when the underlying matrix is changed and stores
## the new matrix in x
set <- function (y) {
x <<- y
m <<- NULL
}
## get() returns the raw matrix
get <- function() x
## setinverse() stores the inverse into the cache variable m
setinverse <- function(inverse) m <<- inverse
## getinverse() returns the cached value
getinverse <- function() m
## return the list of function prototypes for this structure
list (set=set, get=get, getinverse=getinverse, setinverse=setinverse)
}
## This function sets and fetches the cached value from the makeCacheMatrix
## structure. If the value is already cached, it simply returnes the cached value,
## otherwise it computes the inverse using solve() and caches that value in
## the makeCacheMatrix structure.
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
## Check the cached value, if it is not null, return it.
m <- x$getinverse()
if (!is.null(m)) {
message("getting cached data")
return(m)
}
## Fetch the matrix and compute the inverse, store it, and return the inverse
data <- x$get()
m <- solve(data, ...)
x$setinverse(m)
m
}