File tree Expand file tree Collapse file tree 1 file changed +27
-6
lines changed Expand file tree Collapse file tree 1 file changed +27
-6
lines changed Original file line number Diff line number Diff line change 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.
3
4
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
5
6
6
7
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 )
8
17
}
9
18
10
19
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
12
22
13
23
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
15
36
}
You can’t perform that action at this time.
0 commit comments