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
32 lines (29 loc) · 1.05 KB
/
cachematrix.R
File metadata and controls
32 lines (29 loc) · 1.05 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
## Functions in this script can be used to cache matrix and its inverse
## NOTE: These function assumes that matrix is square matrix and invertible.
## Following function can be used to cache a matrix and its inverse
makeCacheMatrix <- function(invertibleMatrix = matrix()) {
inverse <- NULL
set <- function(y) {
invertibleMatrix <<- y
inverse <<- NULL
}
get <- function() invertibleMatrix
setInverse <- function(theInverse) inverse <<- theInverse
getInverse <- function() inverse
list(set = set, get = get,
setInverse = setInverse,
getInverse = getInverse)
}
## Following function can be used to optionally compute and cache inverse of matrix
## It also returns a matrix that is the inverse of 'x'
cacheSolve <- function(cachedInvertibleMatrix, ...) {
inverse <- cachedInvertibleMatrix$getInverse()
if(!is.null(inverse)) {
message("using cached inverse")
return(inverse)
}
invertibleMatrix <- cachedInvertibleMatrix$get()
inverse <- solve(invertibleMatrix)
cachedInvertibleMatrix$setInverse(inverse)
inverse
}