Skip to content

Commit 2dc5b9c

Browse files
committed
The Matrix is cached
1 parent 7f657dd commit 2dc5b9c

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

cachematrix.R

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,42 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
3-
4-
## Write a short comment describing this function
51

2+
# This is a generic cache object which has not changed
3+
# much from the original cacheVector. Infact it is generic
4+
# and can be simply a cacheObject. The logic of what is cached
5+
# is contained in cacheSolve
66
makeCacheMatrix <- function(x = matrix()) {
7+
solveCache <- NULL
8+
get <- function() x
9+
# Set the Matrix, remove any old solved data
10+
set <- function(y) {
11+
x <<- y
12+
solveCache <<- NULL
13+
x
14+
}
715

8-
}
16+
# Set the Solve
17+
setSolve <- function(y) {
18+
solveCache <<- y
19+
solveCache
20+
}
921

22+
getSolve <- function() solveCache
1023

11-
## Write a short comment describing this function
24+
list(get=get, set=set, getSolve=getSolve, setSolve=setSolve)
25+
}
1226

27+
28+
# This function contains the reposnbility of solving the inverse matrix itself
29+
# and updating the cache object with the result. If it is already solved
30+
# then simply return that value.
1331
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
32+
inverse <- x$getSolve()
33+
if (!is.null(inverse)) {
34+
message("Getting Cached Data")
35+
## Return a matrix that is the inverse of 'x'
36+
return(inverse)
37+
}
38+
39+
# Inverse must not yet be solved, lets solve and set it
40+
# Default return here from setSolve will returned the solved value
41+
x$setSolve(solve(x$get(), ...))
1542
}

0 commit comments

Comments
 (0)