forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
69 lines (51 loc) · 1.85 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
62
63
64
65
66
67
68
# The encoding of the script is CP936
# R version 3.1.1 (2014-07-10) -- "Sock it to Me"
# 20140813 uhuruqingcheng
# Couresra-[R Programming][rprog-006][Programming Assignment 2]
## This script including a pair of functions that will cache the inverse of
# a matrix.
## Usage Example
# m <- matrix(runif(n=16), 4, 4) # create a random matrix
# cm <- makeCacheMatrix(m) # create the cached matrix
# cacheSolve(cm) # solve for inverse (any subsequent call will return the cached value)
## This function creates a special "matrix" object that can cache its inverse.
# This special "matrix" is really a list containing a function to:
# set the value of the matrix
# get the value of the matrix
# set the value of the matrix inverse
# get the value of the matdix inverse
makeCacheMatrix <- function(x = matrix()) {
matrix_inv <- NULL
# set and get matrix data with initially empty cache
set <- function(y){
x <<- y
matrix_inv <<- NULL
}
get <- function() x
# set (i.e. cache) and get inverse
setinverse <- function(inverse) matrix_inv <<- inverse
getinverse <- function() matrix_inv
# return list of function
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## This function 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 the cachesolve should retrieve the inverse
# from the cache.
cacheSolve <- function(x, ...) {
# get the inverse and if cached (i.e. non-null) return that value
matrix_inv <- x$getinverse()
if(!is.null(matrix_inv)){
message("getting cached data")
return(matrix_inv)
}
# calculate the inverse
data <- x$get()
matrix_inv <- solve(data, ...)
# cache the inverse
x$setinverse(matrix_inv)
# return the inverse
matrix_inv
}