forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
61 lines (47 loc) · 1.21 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
55
56
57
58
59
60
61
## Put comments here that give an overall description of what your
## functions do
## Write a short comment describing this function
# makeCacheMatrix create object that allow caching of inversion of given matrix.
# `get` method return stored matrix
# `set` method set new matrix
#
# m <- makeCacheMatrix(matrix(1:4, 2, 2))
# d <- m$get()
# m$set(matrix(5:8, 2, 2))
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL
xsaved <- x
set <- function(y) {
x <<- y
xsaved <<- y
inv <<- NULL
}
get <- function() { x }
setinv <- function(invn) {
inv <<- invn
xsaved <<- x
}
getinv <- function() { inv }
updated <- function() { ! identical(x, xsaved) }
list(set = set, get = get
, setinv = setinv, getinv = getinv, updated = updated)
}
## Write a short comment describing this function
# cacheSolve get object from makeCacheMatrix and calculate its inversion
# invm <- cacheSolve(m)
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
inv <- x$getinv()
if(!is.null(inv)) {
if(!x$updated()) {
message("getting cached data")
return(inv)
} else {
message("matrix was changed")
}
}
data <- x$get()
inv <- solve(data, ...)
x$setinv(inv)
inv
}