forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
61 lines (41 loc) · 1.61 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
## makeCacheMatrix starts savinf null default values for some variables.
# Then a few functions are defined inside for saving the input matrix and inverse,
# and for returning the input matrix and getting the cached values.
makeCacheMatrix <- function(in_mat = matrix()) {
#Save initial default Null values
saved_inv<-NULL
saved_mat<-NULL
#Function for saving Matrix and Inverse in cache
setinv<-function(y){
saved_mat <<- y
saved_inv <<- solve(y)
}
#self explanatory functions
get_input_matrix <- function() in_mat
get_saved_matrix <-function() saved_mat
getinv <-function() saved_inv
list(setinv = setinv, get_input_matrix = get_input_matrix,
get_saved_matrix=get_saved_matrix, getinv=getinv)
}
## cacheSolve starts retrieving the input and cached matrix.
# It compares both of them and if they are the same it retrieves the cached
# inverse. If they are dissimilar it means the matrix has changed so it calculates
# the inverse and saves it in cache.
cacheSolve <- function(x, ...) {
#retrieve input matrix and cached matrix
inputmat <- x$get_input_matrix()
savedmat <- x$get_saved_matrix()
#retrieve cached inverse
inv<- x$getinv()
#compare cached and input matrix, copy inverse when appropriate
if(!is.null(inv) && identical(inputmat, savedmat) ) {
message("The inverse of this matrix has been calculated.
Let's get the damn thing:")
return(inv)
}else{
message("The inverse of this matrix is not in cache.
I'll calculate it explicitly and save it.")
newinv<-x$setinv(inputmat)
newinv
}
}