forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
73 lines (56 loc) · 2.01 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
## Very similar to the exfample functions of the mean,
## these two functions store (Cache) data it is passed
## while it solves (inverts) pass matrix's.
##
## This Allows us to keep data for future use so it doesn't
## always have to recalcualte when the calculations have
# already been done.
## makeCacheMatrix will store all matrix's that are passed to it for future use
## creating all needed shell functions to cache the data it gets
makeCacheMatrix <- function(x = matrix()) {
#first create an empty variable, we will store our data here
m <- NULL
##create a shell function, set, that will give us var X
## which can exists outside its scope
## and will clear m in case it has been modified
set <- function(y){
x <<- y
m <<- NULL
}
## create the shell function to get the matrix
get <- function() x
## create the shell function to store the inverse
## and make again use the <<- to allow cross-env access
setinverse <-function(solve) m <<- solve
## create the shell functionto get the inverse
getinverse <- function() m
## store this all in a list
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse
)
}
## CacheSolve does the actual work of inversion
## IF the inversion has not already been solved.
## If it has, then it simply returns the stroed data
## from the previous calculation
cacheSolve <- function(x, ...) {
#set m to equal the get inverse function (with x passed to it)
m <- x$getinverse()
##If there is currently data that matches, it will show a message
## telling you that it is simply receiving that data
## and then will display that
if(!is.null(m)){
message("getting cached data")
return(m)
}
##otherwise, creates a variable 'data' that runs the get function
#with the data it was passed
data <- x$get()
##The meat of this whole thing. Inverse the matrix!
m <- solve(data)
##send this to our set inverse function to store it for future use
x$setinverse(m)
## Return a matrix that is the inverse of 'x'
return(m)
}