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
78 lines (61 loc) · 2.2 KB
/
cachematrix.R
File metadata and controls
78 lines (61 loc) · 2.2 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
## Function: makeCacheMatrix
## Input: numeric matric - default to empty matrix
## Output: List of 4 functions
## Purpose: Creates an environment with a numeric matric and the associated
## functions required to effectively cache its inverse
## Function: set
## Input: numeric matrix
## Output: null
## Purpose: Allows other environments to change the data stored in
## environment and resets the cached inverse to null
## Function: get
## Input: None
## Output: numeric matrix
## Purpose: Allows data from an object created with makeCacheMatrix to be
## accessed by other functions
## Function: setInverse
## Input: Inverse of matrix stored as x in this environment
## Output: inverse (not used)
## Purpose: Allow other functions to store inverse matrix within the
## environment
## Function: getInverse
## Input: None
## Output: Inverse Matrix
## Purpose: Allow other functions to access inverse matrix within the
## environment
makeCacheMatrix <- function(x = matrix()) {
## Creating caching environment for first time, inverse hasn't been calc'ed
inverse <- NULL
set<- function (y){
x <<- y
## Invalidate any cached inverse values
inverse <<- NULL
}
get <- function() x
setInverse <- function (i) inverse <<- i
getInverse <- function () inverse
## return a list to allow functions within this environment
list(set = set, get = get, setInverse = setInverse, getInverse = getInverse)
}
## Function: cacheSolve
## Input: List of functions returned by makeCacheMatrix
## Output: inverse matrix
## Purpose: Checks if inverse has been calculated, if so, returns it.
## Otherwise, calculates the inverse, stores it in environment
## the object passed was declared in and returns the inverse.
cacheSolve <- function(x, ...) {
## get inverse from environment of x
inverse = x$getInverse()
## check if it has already been calculated
if (!is.null(inverse)){
return(inverse)
}
## if not, get the data stored in x
data <- x$get();
## calculate the inverse
inverse <- solve(data, ...)
## set the inverse object in x's envir. to the calculated value
x$setInverse(inverse)
## return the inverse
inverse
}