forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcachematrix.Rmd
More file actions
78 lines (59 loc) · 3.19 KB
/
cachematrix.Rmd
File metadata and controls
78 lines (59 loc) · 3.19 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
69
70
71
72
73
74
75
76
77
---
title: "Inverse Matrix"
output:
html_document:
keep_md: true
---
## 0 Introduction
This second programming assignment will require you to write an R function is able to cache potentially time-consuming computations. For example, taking the mean of a numeric vector is typically a fast operation. However, for a very long vector, it may take too long to compute the mean, especially if it has to be computed repeatedly (e.g. in a loop). If the contents of a vector are not changing, it may make sense to cache the value of the mean so that when we need it again, it can be looked up in the cache rather than recomputed. In this Programming Assignment will take advantage of the scoping rules of the R language and how they can be manipulated to preserve state inside of an R object.
## 1 Write Function `makeCacheMatrix` and `cacheSolve`
### 1.1 The function `makeCacheMatrix` creates a special "matrix" object that can cache its inverse. This function creates a special "vector", which is really a list containing a function to
- set the value of matrix
- get the value of matrix
- set the value of inverse
- get the value of inverse
### 1.2 The function `cacheSolve` 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.
### 1.3 Create N*N random matrix (possibly invertible) to do simple test. N is set large enough to show the power of caching.
## 2. Function Definition and Simple Test
```{r cachematrix}
library(knitr)
opts_chunk$set(echo = TRUE) ##Set environment
makeCacheMatrix <- function(x = matrix()) { ##creates a special object that can cache its inverse
inverse <- NULL ##Internal attribute
set <- function(y) {
x <<- y ##Use <<- to assignment value to an object in different environment
inverse <<- NULL
}
get <- function() x
setInverse <- function(inverseMatrix) inverse <<- inverseMatrix
getInverse <- function() inverse
list(set=set, get=get, setInverse=setInverse, getInverse=getInverse)
}
cacheSolve <- function(x, ...) { ##computes the inverse of the matrix returned by makeCacheMatrix()
inverse <- x$getInverse()
if (!is.null(inverse)) { ##Check if inverse matrix has already been calculated
message("getting cached data")
return(inverse)
}
dataMatrix <- x$get() ##Calculate if cache is empty
inverse <- solve(dataMatrix, ...)
x$setInverse(inverse)
inverse
}
##Now let's do simple test.
set.seed(9999)
N <- 500 ##Large enough
mat <- matrix(rnorm(N*N, mean=10, sd=5), nrow=N, ncol=N) ##Create N*N random matrix
temp <- makeCacheMatrix(mat) ##Assign mat into temp
##First trial
start <- Sys.time() ##Start time of execution
x1 <- cacheSolve(temp) ##Execution of inverse matrix
diff1 <- Sys.time() - start ##Duration of execution
diff1
##Second trial
start <- Sys.time()
x2 <- cacheSolve(temp)
diff2 <- Sys.time() - start
diff2
```
The total execution time of first trial is `r diff1`s. It is obvious that if we do not use `cacheSolve`, the execution time of second trial should also be `r diff1`s. However, if we take advantage of caching, the execution time is significantly reduced to `r diff2`s.