forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
55 lines (51 loc) · 1.2 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
## these functions store a matrix and its inverse so that
## repeated calculation of the inverse can be avoided
## special "matrix" object that stores a matrix and its inverse
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL
set <- function(y) {
x <<- y
inv <<- NULL
}
get <- function() x
setinverse <- function(solve) inv <<- solve
getinverse <- function() inv
list(set = set,
get = get,
setinverse = setinverse,
getinverse = getinverse
)
}
## return the inverse of the matrix object,
## fetched from cache or calculated if not cached.
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
inv <- x$getinverse()
if(!is.null(inv)) {
message("getting cached data")
return(inv)
} else {
message("cached data not found, calculating...")
data <- x$get()
inv <- solve(data, ...)
x$setinverse(inv)
return(inv)
}
}
## example usage:
## > m <- makeCacheMatrix()
## > m$set(matrix(1:4, 2, 2))
## > m$get()
## [,1] [,2]
## [1,] 1 3
## [2,] 2 4
## > cacheSolve(m)
## cached data not found, calculating...
## [,1] [,2]
## [1,] -2 1.5
## [2,] 1 -0.5
## > cacheSolve(m)
## getting cached data
## [,1] [,2]
## [1,] -2 1.5
## [2,] 1 -0.5