File tree Expand file tree Collapse file tree 1 file changed +25
-6
lines changed Expand file tree Collapse file tree 1 file changed +25
-6
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
3
-
4
- # # Write a short comment describing this function
1
+ # # Pair of function that compute/cache the inverse of a matrix.
5
2
3
+ # # This function creates a special "matrix" object that can cache its inverse.
6
4
makeCacheMatrix <- function (x = matrix ()) {
5
+ s <- NULL
6
+ setMatrix <- function (y ) {
7
+ x <<- y
8
+ s <<- NULL
9
+ }
10
+ getMatrix <- function () x
11
+ setSolve <- function (sol ) s <<- sol
12
+ getSolve <- function () s
13
+ list (setMatrix = setMatrix , getMatrix = getMatrix ,
14
+ setSolve = setSolve , getSolve = getSolve )
7
15
8
16
}
9
17
10
18
11
- # # Write a short comment describing this function
12
-
19
+ # # This function computes the inverse of the special "matrix" returned by
20
+ # # makeCacheMatrix above. If the inverse has already been calculated (and
21
+ # # the matrix has not changed), then cacheSolve should retrieve the inverse
22
+ # # from the cache.
13
23
cacheSolve <- function (x , ... ) {
14
24
# # Return a matrix that is the inverse of 'x'
25
+ s <- x $ getSolve()
26
+ if (! is.null(s )) {
27
+ message(" getting cached data" )
28
+ return (s )
29
+ }
30
+ data <- x $ getMatrix()
31
+ s <- solve(data , ... )
32
+ x $ setSolve(s )
33
+ s
15
34
}
You can’t perform that action at this time.
0 commit comments