forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
54 lines (39 loc) · 1.18 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
## creates augmented matrix with corrosponding getter / setter methods
## for its inverse
makeCacheMatrix <- function(x = matrix()) {
s <- NULL
# set matrix, reset cached value of inverse matrix
set <- function(y) {
x <<- y
s <<- NULL
}
# get original matrix
get <- function() x
# set inverse
setSolve <- function(solve) s <<- solve
# get inverse
getSolve <- function() s
list(set = set, get = get,
setSolve = setSolve,
getSolve = getSolve)
}
## methods returns the inverse of a matrix. Inverse is only calculated at the first
## function call. All following calls will return cached values
cacheSolve <- function(x, ...) {
solve <- x$getSolve()
# check whether cached values exists
# if non existend calculate inverse matrix
if(is.null(solve)){
# get original matrix
y <- x$get()
# calculate inverse
solve = solve(y)
# cache inverse
x$setSolve(solve)
}
else {
message("cached data loaded")
}
# return inverse
solve
}