forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
97 lines (80 loc) · 3.49 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#### R programming, Programming Assignment 2: Lexical scoping
##Caching the inverse of a matrix
# ------------------------------------------------------------------------------#
# To write an R function is able to cache potentially time-consuming computations.
# Matrix inversion is usually a costly computation and their may be some benefit
# to caching the inverse of a matrix rather than compute it repeatedly.
# Your assignment is to write a pair of functions that cache the inverse of
# a matrix:
# makeCacheMatrix: This function creates a special "matrix" object that can
# cache its inverse.
# cacheSolve: 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.
# ------------------------------------------------------------------------------#
###
## Function giving an object with remain variable that can perform result cached
## operation like matrix inversion.
##
## @param x (Should be a matrix or coercive matrix object) Matrix to convert
##
## @result an list of function manipulating the 'x' matrix given and its
## cached element
###
makeCacheMatrix <- function(x = matrix()) {
inv_m <- NULL; #remain variable. Will be use to store matrix inverse of 'x'
#Function to replace current matrix by a new given matrix 'y'
# (and reset the remain/cached variable)
set_function <- function(y) { x <<- y; inv_m <<- NULL; };
#Function giving current matrix manipulated for cached operation
get_function <- function() { x; };
#Function to store cached operation result and the remain variable
setcachedvariable <- function(result) { inv_m <<- result; };
#Function giving the remain variable
getcachedvariable <- function() { inv_m; };
list(set = set_function, get = get_function,
setmean = setcachedvariable, getmean = getcachedvariable) #return of makeCacheMatrix
}
###
## Function performing matrix inversion operation of object created by function
## makeCacheMatrix and cache result in the object.
##
## @param x special matrix (object created by makeCacheMatrix() )
## @param ... additional argument for solve function
##
## @result a matrix that is the inverse of 'x'
###
cacheSolve <- function(x, ...) {
m <- x$getmean(); #Get cached inverse of 'x' if exist
if(!is.null(m)) { #Do we calculate matrix inverse ?
message("getting cached data"); #no need
} else {
m <- solve(x$get(), ...);
x$setmean(m); #cache the result in the remain variable
}
return(m);
}
#################################################################################
# Give a invertible matrix of 'Nd' dimension
inversibleMatrix <- function(Nd=2000) {
set.seed(6463);
U <- matrix(rnorm(Nd^2, mean=1, sd=2), nrow=Nd);
V <- matrix(rnorm(Nd^2, mean=3, sd=1), nrow=Nd);
Di <- runif(Nd, min=.2, max=6.3);
X <- U %*% diag(sort(Di)) %*% t(V);
return(X);
}
testMatrix <- function(X, n=2) {
b<-data.frame(solve=0, cacheSolve=0);
Y<-makeCacheMatrix(X);
for(i in 1:n){
a<-Sys.time();
solve(X);
b[i,1]<-difftime(Sys.time(), a, units="secs");
a<-Sys.time();
cacheSolve(Y);
b[i,2]<-difftime(Sys.time(), a, units="secs");
}
return(b);
}