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
81 lines (59 loc) · 2.57 KB
/
Copy pathcachematrix.R
File metadata and controls
81 lines (59 loc) · 2.57 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
78
79
80
81
# This program designed to demonstrate lexical scoping. It contains two
# functions, makeCacheMatrix, which will create a matrix object to cache its
# inverse and another, cacheSolve, that computes the inverse of the special
# matrix sent to it by the makeCacheMatrix function.
makeCacheMatrix <- function(x = matrix()) {
# This function creates a special matrix object that will be sent to cache
# its inverse. In addition, it will declare a number of functions
#
# Arguments:
# x: This variable represents the matrix defined by the user
cache <- NULL # initializes the cache variable.
getMatrix <- function(){
# This function returns the user defined matrix
#
# Returns:
# x: The user defined matrix
return(x)
}
setCache <- function(inverse) {
# This function stores the inverse of the matrix to the cache through
# lexical scoping
#
# Arguments:
# inverse: The inversed matrix
cache <<- inverse
}
getCache <- function() {
# This function is used to retrieve the cached inverse matrix
#
# Returns:
# cache: The cached inverse matrix
return(cache)
}
z = list(getMatrix = getMatrix, setCache = setCache, getCache = getCache)
# This list stores the actionable nested functions of makeCacheMatrix
cacheSolve(x, z) # Calls the cacheSolve function to invert the matrix
print(cache) # Prints the contents of the cache
cacheSolve(x, z) # Calls the cacheSolve again to test cache recovery
print(cache) # Prints the contents of the cache
}
cacheSolve <- function(x, z) {
# This function computes the inverse of the matrix provided to it. If the
# inverse already exists and the matrix has not changed, the function will
# retrieve the inverse from cache.
#
# Arguments:
# x: Contains the user defined matrix
# z: Contains the list of nested functions of makeCacheMatrix
cacheData <- z$getCache() # Retrieve the cache
data <- z$getMatrix() # Retrieve the previous matrix
if(!is.null(cacheData) && identical(x, data)) {
# Tests to see if the cache is not empty and if the matrix is the same.
# If so, it will return the cache
message("Duplicate matrix. Printing cached data")
return(cacheData)
}
cacheData <- solve(data) # Computes the new inverse
z$setCache(cacheData) # Caches the inverse
}