-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
71 lines (52 loc) · 1.96 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
## makeCacheMatrix creates a special matrix that can cache its inverse in a variable called inv
## It also provides nested functions to get and set the value of the cached variable
## set()
## (1) intializes a matrix by taking an argument 'y' and coercing it into a matrix 'x'
## (2) sets the inv of x to null
## get() returns x
## setinverse() populates inv with the value supplied in its argument 'inverse'
## getinverse() returns inv
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL
set <- function(y) {
x <<- y
inv <<- NULL
}
get <- function() x
setinverse <- function(inverse) inv <<- inverse
getinverse <- function() inv
list(set = set,
get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## The purpose of cacheSolve is to invert a matrix provided as an argument
## However, because we cache inverted matrices to preserve system resources,
## cacheSolve first calls getinverse() to see if we've got the values we want already
## If a value is present it is returned
## otherwise cacheSolve inverts the matrix and adds it to a cached value
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
# Please note that the funxtion calls below that are prefixed with 'x$' are nested functions in makeCacheMatrix
# This applies to the following functions called below:
# - getinverse()
# - get()
# - setinverse(inv)
# Check for a cached value
inv <- x$getinverse()
# If a cahced value is found:
if(!is.null(inv)) {
# (1) Notify the console the cached inverse is being returned
message("getting cached inverse")
# (2) Return it
return(inv)
}
# Otherwise:
# (1) Retrieve the matrix using makeCacheMatrix's get()
data <- x$get()
# (2) Invert the matrix
inv <- solve(data)
# (3) Set the cache value for the inverted matrix
x$setinverse(inv)
inv
}