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
59 lines (53 loc) · 2.14 KB
/
cachematrix.R
File metadata and controls
59 lines (53 loc) · 2.14 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
## The functions created here work together to allow the user to cache the
## inverse of the matrix for later reuse. The first function creates a special
## matrix which can store the matrix itself as well as its calculated inverse.
## It basically returns a list of getters and setters for the matrix and the inverse
## the second function uses the result of the first to either lookup the inverse in
## the cache or calculate it and store it in the cache if it is not available
## This function creates a special "matrix", which is really a list containing
## a function to:
## 1. set the value of the matrix
## 2. get the value of the matrix
## 3. set the value of the inverse
## 4. get the value of the inverse
makeCacheMatrix <- function(x = matrix()) {
## preset the inverse to NULL
inverse <- NULL
## setter for the original matrix. This should reset the inverse to NULL
set <- function(y){
x<<-y
## reset the inverse to NULL to make sure we dont't use it if the matrix
## changes
inverse<<-NULL
}
## getter for the original matrix
get <- function() x
## setter for the inverse
setinverse <- function(calculatedInverse){
inverse<<-calculatedInverse
}
## getter for the inverse
getinverse <- function() inverse
list(set=set, get=get,
setinverse=setinverse,
getinverse=getinverse)
}
## This function calculates the inverse of the special "matrix" created by
## makeCacheMatrix. It first checks to see if the inverse has already been
## calculated. If so it gets the inverse from the cache. Otherwise it calculates
## the inverse and sets its value via the setinverse function
cacheSolve <- function(x, ...) {
## see if the inverse exits
inverse <- x$getinverse()
if (!is.null(inverse)){
## inverse exists. Write a message to that effect and return it
message("using cached inverse")
return(inverse)
}
data <- x$get()
## actually calculate the inverse
inverse <- solve(data)
## cache the newly calcuated inverse
x$setinverse(inverse)
inverse
}