Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9d79b05

Browse files
committed
calculation of cached matrix
1 parent 7f657dd commit 9d79b05

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

cachematrix.R

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,36 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## These functions will allow you to cache expensive operation of
2+
## calculating matrix inverse. These calculations can be really expensive when the vector size is huge
3+
## We use lexical scoping of R to implement caching.
34

4-
## Write a short comment describing this function
5+
## This is a special matrix object which can be cacheable. It stores both the original and cached version of matrix
56

67
makeCacheMatrix <- function(x = matrix()) {
7-
8+
inverseMatrix <- NULL
9+
set <- function(y){
10+
x <<- y
11+
inverseMatrix <<- NULL
12+
}
13+
get <- function() x
14+
setInverse <- function(inverse) inverseMatrix <<- inverse
15+
getInverse <- function() inverseMatrix
16+
list(set = set, get = get, setInverse = setInverse, getInverse = getInverse)
817
}
918

1019

11-
## Write a short comment describing this function
20+
## This method first checks if inversed matrix is preesent in cache or not.
21+
## if it isn't present in cache then it performs the inversion of matrix and then returns the value
1222

1323
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
24+
## Return a matrix that is the inverse of 'x'
25+
26+
inverseMatrix <- x$getInverse()
27+
if(!is.null(inverseMatrix)){
28+
message("Using cached version of inverse matrix")
29+
return(inverseMatrix)
30+
}
31+
32+
matrix <- x$get()
33+
inverseMatrix <- solve(matrix,...)
34+
x$setInverse(inverseMatrix)
35+
inverseMatrix
1536
}

0 commit comments

Comments
 (0)