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
72 lines (60 loc) · 2.84 KB
/
cachematrix.R
File metadata and controls
72 lines (60 loc) · 2.84 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
## Below are a pair of functions that take a square invertible matrix of numbers, calculate it's inverse
## the first time, then caches and returns the result. On subsequent iterations that these functions take this
## matrix of numbers, its inverse will be retrieved from the cache rather than re-calculated before returning.
## The makeCacheMatrix() function creates 4 sub-functions that can set and get a matrix "x" (and its inverse),
## then returns these 4 functions as one list object. In use, you assign an instance of this function to an object
## with the input matrix as the argument.
makeCacheMatrix <- function(x = matrix()) {
# initialize the variable i
i <- NULL
# Subfunction that sets the input matrix to var "x"
set <- function(y) {
# Assign variables to the parent environment
x <<- y
i <<- NULL
}
# Subfunction that retrieves the input matrix "x"
get <- function() x
# Subfunction that sets the inverse matrix to var "i"
setinverse <- function(inverse) i <<- inverse
# Subfunction that retrieves the inverse matrix "i"
getinverse <- function() i
# Return a list containing all of the Subfunctions
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## The cacheSolve() function actually computes the inverse of the matrix "x" if it hasn't already been computed and
## cached. The object instance of makeCacheMatrix created becomes a list of the four functions returned from it,
## which in use becomes the argument in this catchSolve function. If the inverse has already been computed, then
## the cacheSolve function will retrieve the inverse from the cache.
cacheSolve <- function(x, ...) {
i <- x$getinverse()
# If i is not NULL, meaning it has already been computed, then return cached matrix
if(!is.null(i)) {
message("getting cached data")
return(i)
}
# Otherwise get the input matrix
data <- x$get()
# And compute the inverse
i <- solve(data, ...)
# Either way, now set the inverse matrix
x$setinverse(i)
#And return it
i
}
###############################################################################################
## Running and Testing ########################################################################
###############################################################################################
# a <- makeCacheMatrix(matrix(rnorm(9),3,3)) # an instance of makeCacheMatrix with an input matrix
# cacheSolve(a) # returns the inverse of the input matrix
###############################################################################################
# a <- makeCacheMatrix()
# a
# class(a)
# class(a$set)
# a$set(matrix(rnorm(9),3,3))
# a$get()
# cacheSolve(a)
# cacheSolve(a)