Skip to content

Commit fbf7812

Browse files
author
Zhengyang
committed
implement the functions
1 parent 7f657dd commit fbf7812

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

cachematrix.R

+25-11
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
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.
72

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)
814
}
915

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()
1529
}

0 commit comments

Comments
 (0)