forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
54 lines (48 loc) · 2.09 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
51
52
53
## Put comments here that give an overall description of what your
## functions do
## `makeCacheMatrix`: This function creates a special "matrix" object
## that can cache its inverse.
makeCacheMatrix <- function(x = matrix()) {
## initialize the inversion cache matrix
m <- NULL
## define the set element
set <- function(y) {
## store the supplied matrix into the storage object assigned by the outer environment
x <<- y
## reinitialize the inversion cache matrix
m <<- NULL
}
## define the get element as a function that returns the matrix stored in the environment
get <- function() x
## define the setsolve element as a function that cache the value supplied into the inversion cache matrix
setsolve <- function(solve) m <<- solve
## define getsolve as a function that returns the inversion cache matrix
getsolve <- function() m
## set the list element returned to the outer environment to address the appropriate defined function in the makeCacheMatrix
## i.e. if a <- makeCacheMatrix(M), then a$get() == M, a$set(N) results in a$get() == N
## a$getsolve == M-1 (the inverse of M) and if a$setsolve(N) results in a$getsolve == N
list(set = set, get = get,
setsolve = setsolve,
getsolve = getsolve)
}
## `cacheSolve`: 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
## `cacheSolve` should retrieve the inverse from the cache.
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
m <- x$getsolve()
## check to see if the inverse matrix is cached
if(!is.null(m)) { ## if it is
message("getting cached data")
return(m) ## return the cached data
}
## if it is not cached, retrieve the original matrix using the $get function
data <- x$get()
## solve for the inverse and store in it m temporarily
m <- solve(data, ...)
## then store that inverted matrix into the cacheMatrix object supplied in the parameters (x)
x$setsolve(m)
## return the inverted matrix to the caller
m
}