File tree Expand file tree Collapse file tree 1 file changed +28
-7
lines changed Expand file tree Collapse file tree 1 file changed +28
-7
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
3
-
4
- # # Write a short comment describing this function
1
+ # # This pair of functions allow the use of lexical scoping to hold state i.e cache
2
+ # # results
5
3
4
+ # # creates a special "matrix", which is really a list containing 4 function to
5
+ # # set and get the function values and set and get the inverse of the input matrix
6
6
makeCacheMatrix <- function (x = matrix ()) {
7
-
7
+ i <- NULL
8
+ set <- function (y ) {
9
+ x <<- y
10
+ i <<- NULL
11
+ }
12
+ get <- function () x
13
+ setinv <- function (inv ) i <<- inv
14
+ getinv <- function () i
15
+ list (set = set , get = get ,
16
+ setinv = setinv ,
17
+ getinv = getinv )
18
+
8
19
}
9
20
10
21
11
- # # Write a short comment describing this function
12
-
22
+ # # This function takes the output of the 'makeCacheMatrix' function as input
23
+ # # and either gets a cached value or "solves" the inverse and stores that in the cache
13
24
cacheSolve <- function (x , ... ) {
14
25
# # Return a matrix that is the inverse of 'x'
26
+ i <- x $ getinv()
27
+ if (! is.null(i )) {
28
+ message(" getting cached data" )
29
+ return (i )
30
+ }
31
+ data <- x $ get()
32
+ i <- solve(data ) # #%*% data
33
+ x $ setinv(i )
34
+ i
35
+
15
36
}
You can’t perform that action at this time.
0 commit comments