Skip to content

Commit 6fb73f0

Browse files
committed
Initial implementation for cacheSolve and makeCacheMatrix
1 parent 7f657dd commit 6fb73f0

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

cachematrix.R

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,37 @@
33

44
## Write a short comment describing this function
55

6+
## TODO:
7+
# write guard clause against x != matrix
8+
# fix indentation to 4
69
makeCacheMatrix <- function(x = matrix()) {
7-
10+
inverse <- NULL
11+
set <- function(y) {
12+
x <<- y
13+
inverse <<- NULL
14+
}
15+
get <- function() x
16+
setInverse <- function(inverseMatrix) inverse <<- inverseMatrix
17+
getInverse <- function() inverse
18+
list(set = set, get = get,
19+
setInverse = setInverse,
20+
getInverse = getInverse)
821
}
922

1023

1124
## Write a short comment describing this function
1225

1326
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
27+
## Return a matrix that is the inverse of 'x'
28+
29+
inverse <- x$getInverse()
30+
if(!is.null(inverse)) {
31+
message("getting cached inverse matrix")
32+
return(inverse)
33+
}
34+
35+
data <- x$get()
36+
inverse <- solve(data, ...)
37+
x$setInverse(inverse)
38+
inverse
1539
}

0 commit comments

Comments
 (0)