Skip to content

Commit 4a7736e

Browse files
committed
functions for caching inverse implemtned
1 parent 7f657dd commit 4a7736e

File tree

1 file changed

+35
-15
lines changed

1 file changed

+35
-15
lines changed

cachematrix.R

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,35 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
3-
4-
## Write a short comment describing this function
5-
6-
makeCacheMatrix <- function(x = matrix()) {
7-
8-
}
9-
10-
11-
## Write a short comment describing this function
12-
13-
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
15-
}
1+
## Following functions serve as a cache for matrix inverse.
2+
3+
4+
## This function returns list of methods for accessing target matrix and
5+
## inverse result using closure mechanism
6+
7+
makeCacheMatrix <- function(x = matrix()) {
8+
inverse <- NULL
9+
set <- function(y) {
10+
x <<- y
11+
inverse <<- NULL
12+
}
13+
get <- function() x
14+
setinverse <- function(i) inverse <<- i
15+
getinverse <- function() inverse
16+
list(set = set, get = get,
17+
setinverse = setinverse,
18+
getinverse = getinverse)
19+
}
20+
21+
22+
## Function computes inverse of matrix if it wasn't computed before,
23+
## or returns cached result. X argument should be a list created by
24+
## makeCacheMatrix function
25+
26+
cacheSolve <- function(x, ...) {
27+
i <- x$getinverse()
28+
if(!is.null(i)) {
29+
return(i)
30+
}
31+
data <- x$get()
32+
i <- solve(data, ...)
33+
x$setinverse(i)
34+
i
35+
}

0 commit comments

Comments
 (0)