forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
64 lines (46 loc) · 2.06 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
## The functions below allow the creation of a special matrix object, which can
## cache the inverse of the matrix held within the object once it has been calculated.
## Once the inverse has been cached, the inversion calcualtion is not repeated,
## rather the cached inverted matrix is simply retrieved and returned.
## makeCacheMatric takes a matrix as the only argument and makes a special matrix object,
## exposing a set of 4 functions to allow setting/getting of the matrix and a
## cached variable (which dosen't strictly have to be a matrix but it is in this case)
makeCacheMatrix <- function(mx = matrix()) {
## initialize inv within this function to NULL,
## ensures null returned if no cached item exists
inv <- NULL
## function that updates the input matrix and clears the cached item
set <- function(y){
mx <<- y
inv <<- NULL # sets cached inverse to null
}
## function that returns the matrix
get <- function() mx
## function that stores the cached item
setinv <- function(solved) inv <<- solved
## function that returns the cached item
getinv <- function() inv
## returns the functions above
list(set = set, get = get, setinv = setinv, getinv = getinv)
}
## cacheSolve takes the special matrix object created by makeCacheMatrix
## and calculates the inverse of the matrix and caches it back in the
## special matrix object, unless the inverse has already been cached,
## in which case it simply retrieves and returns the inverse
cacheSolve <- function(x, ...) {
## get the chached inverse (if it exists)
mx <- x$getinv()
## if the cached inverse exists, return it and end execution
if(!is.null(mx)){
message("using cached inverse matrix")
return(mx)
}
## get the original matrix out of the special matrix object
mat <- x$get()
## calculate the inverse of the matrix and store it in the
## special matrix object
mx <- solve(mat)
x$setinv(mx)
## Return the inverted matrix
mx
}