forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
39 lines (34 loc) · 1.08 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
## These functions create an object to calculate the inverse of a matrix and
## cache its result.
## Define the object CacheMatrix with methods:
# set : set the matrix to solve
# get : get the matrix
# setsolve : set the inverted matrix (output of solve)
# getsovle : get the inverted matrix
makeCacheMatrix <- function(x = matrix()) {
im <- NULL
set <- function(y) {
x <<- y
im <<- NULL
}
get <- function() x
setsolve <- function(solve) im <<- solve
getsolve <- function() im
list(set = set, get = get,
setsolve = setsolve,
getsolve = getsolve)
}
## Calculates the inversion of matrix x, either returns the previously cached
# solution, or computes and caches a new one.
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
im <- x$getsolve()
if(!is.null(im)) {
message("getting cached data")
return(im)
}
data <- x$get()
im <- solve(data, ...)
x$setsolve(im)
im
}