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
70 lines (63 loc) · 2.04 KB
/
cachematrix.R
File metadata and controls
70 lines (63 loc) · 2.04 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
60
61
62
63
64
65
66
67
68
## Put comments here that give an overall description of what your
## functions do
## creat a pair of functions that in total can:
## 1. return the inverse of a matrix if it has been cached before
## 2. calculate and cache the inverse of a matrix if it has not been calculated
## Write a short comment describing this function
## return a list of functions that can
## 1. set the matrix value
## 2. return the matrix value
## 3. set the inverse matrix
## 4. return the inverse matrix
makeCacheMatrix <- function(x = matrix()) {
xi <- NULL #inverse of x, is NULL if not setted
## set the matrix using y, used if need to change the value of the matrix
set <- function(y) {
x <<- y
xi <<- NULL
}
## return the original matrix
get <- function() x
## if the inverse matrix is known, cache it
setinver <- function(inver) xi <<- inver
## return the inverse of the matrix
getinver <- function() xi
list(set = set, get = get,
setinver = setinver,
getinver = getinver)
}
## Write a short comment describing this function
## using the object from function makeCacheMatrix
## 1. return the inverse matrix if it is cached, and print notification
## 2. calculate and cache the inverse of the matrix if it is not cached before
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
xi <- x$getinver()
if(!is.null(xi)){
## cache not empty, using the cached value, print out message
message("getting cached data")
return(xi)
}
## cache is empty, the inverse has not been calculated before, calculate and cache
data <- x$get()
xi <- solve(data)
## cache the inverse
x$setinver(xi)
xi
}
## TEST EXAMPLE AND RESULT
## the first call of cacheSolve, the inverse has
## not been calculate, so it is calculated and cached
## the second call of cacheSolve, the cached value is returned
## x <- 1:4
## dim(x) <- c(2,2)
## y <- makeCacheMatrix(x)
## cacheSolve(y)
## [,1] [,2]
## [1,] -2 1.5
## [2,] 1 -0.5
## cacheSolve(y)
## getting cached data
## [,1] [,2]
## [1,] -2 1.5
## [2,] 1 -0.5