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
65 lines (51 loc) · 1.92 KB
/
cachematrix.R
File metadata and controls
65 lines (51 loc) · 1.92 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
##Assignment 2
##Inverse Matrix Caching
##This function is able to cache large computations
##For example, inverse computation of large dimensional matrix
##In this function, we will take advantage of scoping rules of R language
##and to show how they can be manipulated to preserve state inside of R object.
##This function creates a special "vector", which is really a list containing
##a function to
##1. set the value of matrix
##2. get the value of matrix
##3. set the value of inverse
##4. get the value of inverse
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)
}
##This function calculates the inverse of matrix created with the above function
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 have a simple test
set.seed(10000)
N <- 999 ##Set matrix dimension, large enough to show power of caching
mat <- matrix(rnorm(N*N, mean=10, sd=5), nrow=N, ncol=N) ##Create N*N random matrix
temp <- makeCacheMatrix(mat)
##First trial
start = Sys.time()
cacheSolve(temp)
diff1 = Sys.time() - start
print(diff1)
##Second trial
start = Sys.time()
cacheSolve(temp)
diff2 = Sys.time() - start
print(diff2)
##Using caching, the execution time of the second trial is significantly reduced.