forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
73 lines (56 loc) · 2.23 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
## Using the function cacheSolve with makeCacheMatrix below
## takes a matrix as input, and produces the inverse of that matrix as output.
## An important additional capability of the functions below is that the matrix inverse
## is not re-calculated, if it already has been. To prevent this unneccessary step, the temporary
## storage technique of 'caching' is used.
# makeCacheMatrix creates a list that is a special vector of the functions needed to cache data properly.
# the functions stored in the list do the following: (1) set the original matrix value (2) get the original matrix value
# (3) set the inverse value that is calculated (4) get the inverse value that has been previously or just recently calculated
makeCacheMatrix <- function(x = matrix()) {
# the output inverse matrix is initilized to null
i <- NULL
# The 'cache'-ing part. Expands the scope to the parent environment.
set <- function(y) {
# stores the matrix
x <<- y
# Inverse is set to null when doing a new inverse calculation
i <<- NULL
}
# returns the value of the evaluated expression x (aka the inverse)
get <- function() x
# calculates the inverse of the matrix
setInverse <- function(solve) i <<- solve
# gets the inverse
getInverse <- function() i
# keeps all of the previous results for reference
list(set = set, get= get, setInverse = setInverse,
getInverse = getInverse)
}
# cacheSolve returns the inverse of the matrix of makeCacheMatrix.
# If the inverse = the original, then the inverse is not recalulated
# but the cached data is used
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
i <- x$getInverse()
if(!is.null(i)) {
message("getting cached data")
return (i)
}
# If the matrix is not the same, the new matrix value must be read
data <-x$get()
# and the new inverse is calculated
i <- solve(data, ...)
# and the new inverse is stored in the cached data
x$setInverse(i)
#output the inverse
i
}
# test 1
A <- matrix(c(2,3,3,4),nrow = 2, byrow = TRUE)
y <- makeCacheMatrix(A)
cacheSolve(y)
cacheSolve(makeCacheMatrix(A))
#test 2
mat<-matrix(c(1,4,9,0,-3,2,2,7,8),3,3)
y$set(mat)
cacheSolve(y)