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
96 lines (65 loc) · 4.52 KB
/
cachematrix.R
File metadata and controls
96 lines (65 loc) · 4.52 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#The two functions below create a special matrix and solve the inverse of the matrix
#The first function produces the spacial matrix and allows the user four list options
#which may be performed on the matrix:
# 1) setMatrixValue which assigns the a value to the matrix;
# 2) getMatrix which returns the values stores in the matrix;
# 3) cacheInverseMatrix stores the inverse of a matrix in the cache; and
# 4) getInverseMatrix returns the inverse of the matrix.
#The second function cacheSolve solves the inverse of a matrix if the value is not already stored in cache.
#In the event the value is already in cache, the computation is skipped and the stored value is printed
##This function produces a list of four functions which can be called on a matrix.
#The functions make a cache matrix (a matrix stored in the cache of the system), return the matrix, cache the inverse of the matrix and print the inverse matrix
makeCacheMatrix <- function(x = matrix()) {
#creation of a variable to store the cache value of the matrix. This value is initially defined a NULL.
cacheValue <- NULL
#Option 1 in the list to be defined: this function makes a matrix
#this function stores the value of a matrix as input into the makeCashMatrix function.
setMatrixValue <- function(matrixValue){
#assigns the value of matrixValue to x. This value is useable in the parent environment of setMatrixValue.
x <<- matrixValue
#the matrix value, as stored in the cache, is set back to null. This is assigned in the parent environment.
cacheValue <<- NULL
}
#Option 2 in the list to be defined: this function retrieves the matrix defined
getMatrix <- function(){
#prints the matrix stored in makeCashMatrix input (x)
print(x)
}
#Option 3 in the list to be defined: stores the inverse of a matrix in the cache
cacheInverseMatrix <- function(y){
#stores the inverse of a matrix into cache which is assigned outside of the function
cacheValue <<- y
}
#Option 4 in the list to be defined: returns the cache inverse of the defined matrix
getInverseMatrix <- function(){
#returns the value of the cache matrix
return(cacheValue)
}
# returns a list of functions as defined in the above options
list(makeCacheMatrix = makeCacheMatrix, getMatrix = getMatrix, cacheInverseMatrix = cacheInverseMatrix, getInverseMatrix = getInverseMatrix)
#end of function makeCacheMatrix
}
##This function returns the inverse of a special square matrix as defined by the makeCacheMatrix function
cacheSolve <- function(z, ...) {
#gets the inverse of a the matrix
inverse <- z$getInverseMatrix()
#assesses whether a cache value exists for this matrix and returns it if it does (if invsere is not empty, then the inverse is returned)
if(!is.null(inverse)) {
#returns a message stating the cache value is being used
message("Returning cache data")
#returns the cache stored inverse matrix
return(inverse)
}
#if there is no value in the inverse, the function calculates the inverse of the matrix
else{
#gets the value of the matrix and stores it in a variable matrix
matrix <- z$getMatrix()
#stores the inverse of the matrix into the inverse variable
inverse <- solve(matrix)
#stores the value into cache matrix
z$cacheInverseMatrix(inverse)
}
#prints the output of the inverse matrix
print(inverse)
#end of cacheSolve function
}