forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
41 lines (36 loc) · 912 Bytes
/
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
## These functions implements invering (solving) matrix with caching
## This function take some matrix as an argument and return
## special "matrix" which can store cached inverse
makeCacheMatrix <- function(x = matrix()) {
s <- NULL
set <- function(y) {
x <<- y
s <<- NULL
}
get <- function() {
x
}
setSolve <- function(solve) {
s <<- solve
}
getSolve <- function() {
s
}
list(set = set, get = get, setSolve = setSolve, getSolve = getSolve)
}
## This function calculates inverse of matrix with caching result
## First argument is special "matrix" with internal cache
## which can be created by makeCacheMatrix function
## Other arguments are the same as for solve function
cacheSolve <- function(x, ...) {
s <- x$getSolve()
if (!is.null(s)) {
message("getting cache data")
return(s)
}
message("solving matrix")
data <- x$get()
s <- solve(data, ...)
x$setSolve(s)
s
}