File tree 1 file changed +25
-11
lines changed
1 file changed +25
-11
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
5
-
6
- makeCacheMatrix <- function (x = matrix ()) {
1
+ # This file defines two functions for solving a matrix with cache support.
7
2
3
+ # Returns a cachable object with set, get, set_inverse, get_inverse methods
4
+ makeCacheMatrix <- function (m = matrix ()) {
5
+ inverse_m <- NULL
6
+ set <- function (rhs ) {
7
+ m <<- rhs
8
+ inverse_m <<- NULL
9
+ }
10
+ get <- function () m
11
+ set_inverse <- function (rhs ) inverse_m <<- rhs
12
+ get_inverse <- function () inverse_m
13
+ list (set = set , get = get , set_inverse = set_inverse , get_inverse = get_inverse )
8
14
}
9
15
10
-
11
- # # Write a short comment describing this function
12
-
13
- cacheSolve <- function (x , ... ) {
14
- # # Return a matrix that is the inverse of 'x'
16
+ # Returns the inverse of a matrix.
17
+ # m is a cacheable matrix constructed by calling makeCacheMatrix
18
+ cacheSolve <- function (m , ... ) {
19
+ # # Return a matrix that is the inverse of 'x'
20
+ inverse_m <- m $ get_inverse()
21
+ if (! is.null(inverse_m ))
22
+ {
23
+ message(" getting cached data" )
24
+ return (inverse_m )
25
+ }
26
+ data <- m $ get()
27
+ m $ set_inverse( solve(data , ... ) )
28
+ m $ get_inverse()
15
29
}
You can’t perform that action at this time.
0 commit comments