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
36 lines (29 loc) · 1.17 KB
/
cachematrix.R
File metadata and controls
36 lines (29 loc) · 1.17 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
## These functions will allow you to cache expensive operation of
## calculating matrix inverse. These calculations can be really expensive when the vector size is huge
## We use lexical scoping of R to implement caching.
## This is a special matrix object which can be cacheable. It stores both the original and cached version of matrix
makeCacheMatrix <- function(x = matrix()) {
inverseMatrix <- NULL
set <- function(y){
x <<- y
inverseMatrix <<- NULL
}
get <- function() x
setInverse <- function(inverse) inverseMatrix <<- inverse
getInverse <- function() inverseMatrix
list(set = set, get = get, setInverse = setInverse, getInverse = getInverse)
}
## This method first checks if inversed matrix is preesent in cache or not.
## if it isn't present in cache then it performs the inversion of matrix and then returns the value
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
inverseMatrix <- x$getInverse()
if(!is.null(inverseMatrix)){
message("Using cached version of inverse matrix")
return(inverseMatrix)
}
matrix <- x$get()
inverseMatrix <- solve(matrix,...)
x$setInverse(inverseMatrix)
inverseMatrix
}