forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
56 lines (46 loc) · 1.84 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
## These functions set up a cache for the inverse of a square matrix and return
## either the stored result, or the newly calculated inverse. Assume that the matrix supplied is always invertible.
## This function returns a labeled list of functions used to cache and retrieve the inverse of a matrix.
makeCacheMatrix <- function(x = numeric()) {
i <- NULL
set <- function(y) {
# set x to argument y and initializes i to NULL in parent (global) environment
x <<- y
i <<- NULL
}
# get returns value of x
get <- function() x
# setinverse sets i in makeCacheMatrix to inverse
setinverse <- function(inverse) i <<- inverse
# getinverse returns cached value of i
getinverse <- function() i
# return labeled list of functions
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## This function either retrieves the stored matrix, or calculates a new inverse matrix.
cacheSolve <- function(x, ...) {
# check to see if inverse is cached
i <- x$getinverse()
if(!is.null(i)) {
message("getting cached data")
# return the cached inverse if one exists
return(i)
}
# else call get to retrieve the argument, and store it in variable data
data <- x$get()
# no need to test if matrix is square since the assignment says
# to assume an invertible matrix, which is square by definition
# calculate the inverse matrix and store in i
i <- solve(data, ...)
if(data %*% i == 1) {
message("product of matrices is an identity matrix")
} else {
stop("product of matrices is not an identity matrix")
}
# store this inverse in the cache
x$setinverse(i)
# return the inverse of the matrix
i
}