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
62 lines (52 loc) · 1.85 KB
/
cachematrix.R
File metadata and controls
62 lines (52 loc) · 1.85 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
##*****************************************************************************
##
## These functions work in conjunction to create a *special* matrix which
## includes a built-in caching mechanism for the inverse.
##
##*****************************************************************************
##*****************************************************************************
## Accepts a square matrix and returns it wrapped in caching functions.
##
## The return cache matrix will be accessible via its get() function.
##*****************************************************************************
makeCacheMatrix <- function(x = matrix()) {
## Error Check: make sure the matrix is square
nr = nrow(x)
nc = ncol(x)
if ( nr != nc ) {
stop("Provided matrix must be square")
}
i <- NULL
set <- function(y) {
x <<- y
i <<- NULL
}
get <- function() x
setInverse <- function(inverse) i <<- inverse
getInverse <- function() i
list(set = set, get = get,
setInverse = setInverse,
getInverse = getInverse)
}
##*****************************************************************************
## Computes the inverse of the provided cached matrix; if the inverse has
## already been calculated, then the value is retrieved from a cache to enhance
## performance.
##
## The inverse matrix is available via its getInverse() function.
##*****************************************************************************
cacheSolve <- function(x, ...) {
## Check the cache for the inverse
i <- x$getInverse()
## If available from the cache, simply return it
if(!is.null(i)) {
message("getting cached data")
return(i)
}
## Inverse has not yet been calculated, so calculate it now and store the value
## in the cache for all future access requests.
data <- x$get()
i <- solve(data, ...)
x$setInverse(i)
i
}