forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcachematrix.R
More file actions
109 lines (97 loc) · 2.51 KB
/
Copy pathcachematrix.R
File metadata and controls
109 lines (97 loc) · 2.51 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
## These functions are used to create a special object that stores a matrix
## and cache's its inverse.
## makeCacheMatrix function creates a special vector, which is really a list
## containing functions to:
## 1. set the values of the matrix and initialize its cache (set)
## 2. get the values of the matrix (get)
## 3. set the inverse of the matrix (setInv)
## 4. get the inverse of the matrix (getInv)
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL
set <- function(y = matrix()) {
x <<- y
inv <<- NULL
}
get <- function() {
x
}
setInv <- function(inverse) {
inv <<- inverse
}
getInv <- function() {
inv
}
list(set = set,
get = get,
setInv = setInv,
getInv = getInv)
}
## cacheSolve function calculates the inverse of the special "matrix" created
## with makeCacheMatrix function. However it checks to see if the inverse has
## already been calculated. If so, it gets the inverse from the cache and
## skips the computation. Otherwise it calculates the inverse of the matrix
## and sets the value of the inverse in the cache via the setInv function.
cacheSolve <- function(x, ...) {
inv <- x$getInv()
if(!is.null(inv)) {
message("getting cached inverse")
return(inv)
}
mat <- x$get()
inv <- solve(mat, ...)
x$setInv(inv)
inv
}
## Usage: Example
## Matrix supplied should be invertible
## > x <- c(8,9,10,11)
## > mat1 <- matrix(x,2,2)
## > a <- makeCacheMatrix(mat1)
## > a$get()
## [,1] [,2]
## [1,] 8 10
## [2,] 9 11
## > a$getInv()
## NULL
## > cacheSolve(a)
## [,1] [,2]
## [1,] -5.5 5
## [2,] 4.5 -4
## > cacheSolve(a)
## getting cached inverse
## [,1] [,2]
## [1,] -5.5 5
## [2,] 4.5 -4
## > a$getInv()
## [,1] [,2]
## [1,] -5.5 5
## [2,] 4.5 -4
## > a$get()
## [,1] [,2]
## [1,] 8 10
## [2,] 9 11
## Supplying a new invertible matrix
## > y <- c(12,13,14,15)
## > mat2 <- matrix(y,2,2)
## > a$set(mat2)
## > a$get()
## [,1] [,2]
## [1,] 12 14
## [2,] 13 15
## > cacheSolve(a)
## [,1] [,2]
## [1,] -7.5 7
## [2,] 6.5 -6
## > cacheSolve(a)
## getting cached inverse
## [,1] [,2]
## [1,] -7.5 7
## [2,] 6.5 -6
## > a$getInv()
## [,1] [,2]
## [1,] -7.5 7
## [2,] 6.5 -6
## > a$get()
## [,1] [,2]
## [1,] 12 14
## [2,] 13 15