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
47 lines (39 loc) · 1.17 KB
/
Copy pathcachematrix.R
File metadata and controls
47 lines (39 loc) · 1.17 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
#a set of utility functions to provide caching for matrix
#inversion operations
# Create a special matrix object that can
# cache its inverse
makeCacheMatrix <- function(x = matrix()) {
csolve <- NULL
set <- function(y) {
x <<- y
csolve <<- NULL
}
#returns matrix
get <- function() x
#use to set cached solution
setsolved <- function(s) csolve <<- s
#get uached solution
getsolved <- function() csolve
#return cache matrix as a list
list(set = set, get = get,
setsolved = setsolved,
getsolved = getsolved)
}
#computes the inverse of the special
#"matrix" returned by `makeCacheMatrix` above. If the inverse has
#already been calculated (and the matrix has not changed), then
#`cacheSolve` should retrieve the inverse from the cache.
cacheSolve <- function(x, ...) {
#look for cached value
csolve <- x$getsolved()
#if you found cached value, return it...
if(!is.null(csolve)) {
message("getting cached data")
return(csolve)
}
#otherwise calculate the data and cache it before returning
data <- x$get()
s <- solve(data, ...)
x$setsolved(s)
s
}