File tree Expand file tree Collapse file tree 1 file changed +15
-6
lines changed
Expand file tree Collapse file tree 1 file changed +15
-6
lines changed Original file line number Diff line number Diff line change 1- # # Matrix inversion is usually a costly computation and their may be some
1+ # # Matrix inversion is usually a costly computation and there may be some
22# # benefit to caching the inverse of a matrix rather than compute it repeatedly.
33# # This two functions do exactly that. Create matrix that stores its inverse (using makeCacheMatrix)
44# # then get its inverse (using cacheSolve)
2424makeCacheMatrix <- function (mtx = matrix ()) {
2525 cachedInverse <- NULL
2626 set <- function (y ) {
27+ # changes initial matrix and removes old matrix cache
2728 mtx <<- y
2829 cachedInverse <<- NULL
2930 }
3031 get <- function () mtx
31- setInverse <- function (inv ) cachedInverse <<- inv
32- getInverse <- function () cachedInverse
33- list (set = set , get = get ,
34- setInverse = setInverse ,
35- getInverse = getInverse )
32+ setInverse <- function (inv ) {
33+ # sets inverse version of the matrix
34+ cachedInverse <<- inv
35+ }
36+ getInverse <- function () {
37+ # returns inverse of the matrix
38+ cachedInverse
39+ }
40+ list (set = set , get = get ,
41+ setInverse = setInverse ,
42+ getInverse = getInverse )
3643}
3744
3845
@@ -43,8 +50,10 @@ makeCacheMatrix <- function(mtx = matrix()) {
4350cacheSolve <- function (x , ... ) {
4451 inv <- x $ getInverse()
4552 if (! is.null(inv )) {
53+ # cached invers exists, return it
4654 return (inv )
4755 }
56+ # inverse is not cached yet, cache it and return
4857 mtx <- x $ get()
4958 inv <- solve(mtx , ... )
5059 x $ setInverse(inv )
You can’t perform that action at this time.
0 commit comments