forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
40 lines (35 loc) · 1.16 KB
/
cachematrix.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
## Put comments here that give an overall description of what your
## functions do
## The first function, makeCacheMatrix creates a special "matrix", which is really a list containing a function to
## * set the value of the matrix
## * get the value of the matrix
## * set the value of the inverse
## * get the value of the inverse
makeCacheMatrix <- function(x = matrix()) {
cachesolve <- NULL
set <- function(y) {
x <<- y
cachesolve <<- NULL
}
get <- function() x
setSolve <- function(s) cachesolve <<- s
getSolve <- function() cachesolve
list(set = set, get = get,
setSolve = setSolve,
getSolve = getSolve)
}
## The secound function, cacheSolve calculates the inverse of the special "matrix" created with the above function.
## * if there is a `cachesolve` just return it
## * otherwise solve and store the result to `cachesolve` and return it
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
cachesolve <- x$getSolve()
if(!is.null(cachesolve)) {
message("getting cached data")
return(cachesolve)
}
data <- x$get()
cachesolve <- solve(data, ...)
x$setSolve(cachesolve)
cachesolve
}