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 (45 loc) · 1.75 KB
/
Copy pathcachematrix.R
File metadata and controls
59 lines (45 loc) · 1.75 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
# Templated after makeVector & cachevector from Roger Peng's R Programming course.
# https://class.coursera.org/rprog-003/human_grading/view/courses/972138/assessments/3/submissions
## makeCacheMatrix creates a special "vector", which is really a list containing a function to
## set the value of the vector
## get the value of the vector
## set the value of the solve function
## get the value of the solve function
## Set up a specia list of functions that allow caching results of a solve function.
makeCacheMatrix <- function(x = numeric()) {
# Ensure s isn't set locally.
s <- NULL
# Create the "set" function which pushes y into the parent environment and clears s.
set <- function(y) {
x <<- y
s <<- NULL
}
# Create the" get function
get <- function() x
# Create the "setsolve" function which sets s to the "solve" function in the parent env.
setsolve <- function(solve) s <<- solve
# Create the "getsolve" function which retreives s
getsolve <- function() s
# Create and return the cache list of functions (set, get, setsolve, getsolve).
list(set = set, get = get,
setsolve = setsolve,
getsolve = getsolve)
}
## Return a matrix that is the inverse of 'x'
cacheSolve <- function(x, ...) {
# Retreive the getsolve() function from the list.
s <- x$getsolve()
# Let us know if we're getting cached data (e.g. if s exists in the special list)
if(!is.null(s)) {
message("getting cached data")
return(s)
}
# Get the data from the list.
data <- x$get()
# Solve the matrix inversion
s <- solve(data, ...)
# Set the cached value back into the list.
x$setsolve(s)
# Return the value.
s
}