File tree Expand file tree Collapse file tree 1 file changed +27
-7
lines changed Expand file tree Collapse file tree 1 file changed +27
-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
1
+ # # makeCacheMatrix creates a new matrix object that can store its own inverse
2
+ # # cacheSolve returnes cached inverse of matrix produced by makeCacheMatrix or
3
+ # # calculates and caches it if the inverse wasn't calculated before
3
4
4
- # # Write a short comment describing this function
5
5
6
+ # # create a special matrix that can store cache of its inverse
6
7
makeCacheMatrix <- function (x = matrix ()) {
7
-
8
+ inv <- NULL
9
+ set <- function (y ,... ) {
10
+ x <<- matrix (y ,... )
11
+ inv <<- NULL
12
+ }
13
+ get <- function () x
14
+ setinverse <- function (inverse ) inv <<- inverse
15
+ getinverse <- function () inv
16
+ list (set = set , get = get ,
17
+ setinverse = setinverse ,
18
+ getinverse = getinverse )
8
19
}
9
20
10
21
11
- # # Write a short comment describing this function
12
-
22
+ # # try to retrieve cached inverse matrix,
23
+ # # if not avaliable, then calculate, cache and return
13
24
cacheSolve <- function (x , ... ) {
14
- # # Return a matrix that is the inverse of 'x'
25
+ # # Return a matrix that is the inverse of 'x'
26
+ inv <- x $ getinverse()
27
+ if (! is.null(inv )) {
28
+ message(" getting cached data" )
29
+ return (inv )
30
+ }
31
+ matrix <- x $ get()
32
+ inv <- solve(matrix , ... )
33
+ x $ setinverse(inv )
34
+ inv
15
35
}
You can’t perform that action at this time.
0 commit comments