/
cachematrix.R
104 lines (94 loc) · 4.35 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
98
99
100
101
102
103
104
# ID = 2 / Submission 2 / FINAL
# Parent: c4f60ef / Commit: ebc44be47f343d5d3e9ee0308580e5b33495854d
# Parent: 12b3255 / Commit: 5fdd025b5cf13b609a5b4ba397bfbf11aedff5df
# ================================================================================
#
# Matrix inversion beyond 1000 X 1000 exceeded the capability of my laptop and
# took several/many minutes to compute. Requiring this calculating time, perhaps
# for many iterations in a loop, would drastically slow down computer analysis.
# Calculating the matrix inversion once and using that result many times would
# significantly speed up computer analysis involving large data sets. Multiple
# tests on a 1000 X 1000 matrix of pseudo random numbers consistenly suggested
# a time saving approximating 82.5% through the utilization of the caching process.
#
# FUNCTION 1
# ----------
# The makeCacheMatrix function creates a "matrix", "sets" and "gets" the value of
# the matrix and "sets" and "gets" the value of the inverse of this matrix.
#
makeCacheMatrix <- function(x = matrix()) {
inverse1 <- NULL # initialize inverse1 to NULL
set <- function(y) {
x <<- y # set matrix x
inverse1 <<- NULL # reinitialize inverse1 to NULL
}
get <- function() x # retrieve stored matrix x
setinverse <- function(inverse) inverse1 <<- inverse # set inverse of matrix to inverse1
getinverse <- function() inverse1 # get inverse1
list(set=set, get=get, # list internal methods
setinverse=setinverse,
getinverse=getinverse)
}
# ================================================================================
#
# FUNCTION 2
# ----------
# The cacheSolve function checks for matrix inversion in cache and, if present,
# skips the calculation. If the matrix inversion is not found, program flow drops
# down to calculate the matrix inversion and return the result as "inverse1".
#
cacheSolve <- function(x, ...) {
inverse1 <- x$getinverse() # get the cached value
if(!is.null(inverse1)) { # if cached value exists, return it
print(start.time1)
message("Retrieving Cached Inverse Matrix")
return(inverse1) # return previous cached value
}
message("Calculating Inverse Matrix")
print(start.time2)
data <- x$get() # get matrix
inverse1 <- solve(data) # calculate inverse matrix
x$setinverse(inverse1) # assign inverse matrix to inverse1
inverse1 # print matrix inverse1
}
# ================================================================================
#
# TESTING HYPOTHESIS:
# ------------------
#
# set.seed(192837465)
# r = rnorm(1000000)
# x = matrix(r, nrow=1000, ncol=1000)
x = rbind(c(2, 5), c(9, 4))
m = makeCacheMatrix(x)
message("Printing Original Matrix")
m$get()
# FIRST ITERATION
start.time2 = Sys.time()
cacheSolve(m)
duration2 = Sys.time() - start.time2
# SECOND ITERATION
start.time1 = Sys.time()
cacheSolve(m)
duration1 = Sys.time() - start.time1
time.saving = (duration2 - duration1)
message("CALCULATING TIME SAVING by RETRIEVING CACHED MATRIX")
message("Based on Calculations with a 1000 X 1000 Matrix of Pseudo Random Numbers")
#
message("Time Required to Calculate Matrix Inversion")
print(duration2)
#
message("Time Required to Retrieve Matrix Inversion from Cache")
print(duration1)
#
message("Time Saved by Retrieving Matrix Inversion from Cache")
print(time.saving)
#
message("Retrieving the matrix inversion from cache consistently reduced computing time by @ 82.5%.")
# message("Percentage of Time Saved by Retrieving Matrix Inversion from Cache")
# print((time.saving/duration1)*100)
# ------------------
# The values for the matrix inversion were confirmed using independent software.
# Testing for "time saving" was done on the 1000 X 1000 matrix of pseudo random numbers.
# Any matrix larger than 1000 X 1000 exceeded the capabilities of my laptop RAM.
# Testing on the 2 X 2 matrix yielded times too small to be used in calculations.