Skip to content

Commit 00c0a04

Browse files
committed
implemented inverse caching
1 parent 7f657dd commit 00c0a04

File tree

1 file changed

+33
-8
lines changed

1 file changed

+33
-8
lines changed

cachematrix.R

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,40 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## object to contain matrix and it's
2+
## inverse (if computed)
33

4-
## Write a short comment describing this function
5-
6-
makeCacheMatrix <- function(x = matrix()) {
4+
makeCacheMatrix <- function(actual = matrix()) {
5+
inverse <- NULL
6+
set <- function(x) {
7+
actual <<- x
8+
inverse <<- NULL
9+
}
10+
get <- function() {
11+
actual
12+
}
713

14+
setInverse <- function(i) {
15+
inverse <<- i
16+
}
17+
getInverse <- function() {
18+
inverse
19+
}
20+
list(set = set, get = get,
21+
setInverse = setInverse,
22+
getInverse = getInverse)
823
}
924

10-
11-
## Write a short comment describing this function
25+
## method to get inverse. If not computed,
26+
## it will stor it in cache. If previously
27+
## computed, it will use cache
1228

1329
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
30+
inverse <- x$getInverse()
31+
32+
if (is.null(inverse)) {
33+
actual <- x$get()
34+
if (dim(actual)[1] == dim(actual)[2]) {
35+
inverse <- solve(actual)
36+
x$setInverse(inverse)
37+
}
38+
}
39+
inverse
1540
}

0 commit comments

Comments
 (0)