File tree Expand file tree Collapse file tree 1 file changed +29
-6
lines changed Expand file tree Collapse file tree 1 file changed +29
-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
+ # # Functions for caching inverse matrix calculation. This is solved with one
2
+ # # generator function which uses lexical scoping rules to store a cache for
3
+ # # inverse matrix. Before solving inverse matrix calculation, cache variable is
4
+ # # previously checked and if cache is different than NULL, cached value is
5
+ # # returned.
3
6
4
- # # Write a short comment describing this function
7
+ # # Generator function for managing matrix values and inverse matrix cache.
5
8
6
9
makeCacheMatrix <- function (x = matrix ()) {
7
-
10
+ inv <- NULL
11
+ set <- function (y ) {
12
+ x <<- y
13
+ inv <<- NULL
14
+ }
15
+ get <- function () x
16
+ setinverse <- function (inverse ) inv <<- inverse
17
+ getinverse <- function () inv
18
+ list (set = set , get = get ,
19
+ setinverse = setinverse ,
20
+ getinverse = getinverse )
8
21
}
9
22
10
23
11
- # # Write a short comment describing this function
24
+ # # Function to solve inverse matrix calculation, while making use of caching
25
+ # # features provided by generator function.
12
26
13
27
cacheSolve <- function (x , ... ) {
14
- # # Return a matrix that is the inverse of 'x'
28
+ # # Return a matrix that is the inverse of 'x'
29
+ inv <- x $ getinverse()
30
+ if (! is.null(inv )) {
31
+ message(" getting cached result" )
32
+ return (inv )
33
+ }
34
+ data <- x $ get()
35
+ inv <- solve(data , ... )
36
+ x $ setinverse(inv )
37
+ inv
15
38
}
You can’t perform that action at this time.
0 commit comments