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
38 lines (33 loc) · 1.45 KB
/
Copy pathcachematrix.R
File metadata and controls
38 lines (33 loc) · 1.45 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
## Two functions to recieve a matrix, solve its inverse, and cache it for
## subsequent quick retrieval
## makeCacheMatrix receives a matrix and returns a list containing four
## functions to manipulate it, cache the inverse and retrieve it.
makeCacheMatrix <- function(x = matrix(),...) {
solvedm <- NULL ## Initiaize internal solved matrix
set <- function(y) { ## externally change the matrix
x <<- y
solvedm <<- NULL
}
get <- function() x ## return matrix
setinverse <- function(inverse) solvedm <<- inverse
## cache inverse to parent environment
getinverse <- function() solvedm ## return cached inverse
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## cacheSolve retrieves the cache's inverse from makeCacheMatrix. If no inverse
## is cached, it solves the inverse and caches it using makeCacheMatrix, before
## returning the inverse.
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
solvedm <- x$getinverse() ## Check cached inverse
if(!is.null(solvedm)) { ## if cached inverse exists, return it
message("getting cached data")
return(solvedm)
}
data <- x$get() ## if no cached inverse exists, get matrix and solve it
solvedm <- solve(data, ...)
x$setinverse(solvedm) ## call to cache solved inverse
solvedm ## return inverse
}