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
62 lines (55 loc) · 1.63 KB
/
cachematrix.R
File metadata and controls
62 lines (55 loc) · 1.63 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
# USAGE
#
######## load functions to environment
# source(".../cachematrix.R")
#
######## generate a sample matrix
# rawMatrix <- rbind(c(1, -1/4), c(-1/4, 1))
#
######## call makeCacheMatrix to create an object that can store a matrix and its inverse
# cachedMatrix <- makeCacheMatrix(rawMatrix)
#
######## call cacheSolve and note that the inverse is returned
# cacheSolve(cachedMatrix)
#
######## call cacheSolve again and note that a cache of the inverse is returned
# cacheSolve(cachedMatrix)
#
######## validate that the returned inverse is valid
# cacheSolve(cachedMatrix) %*% rawMatrix
## accept an incoming matrix and define the following list of operations
#
#### get/set -- the matrix data
#### setinverse -- calculate the matrix inverse using the solve function and store in m
#### getinverse -- return m (the calculated inverse)
#
# return those operations in a list
makeCacheMatrix <- function(x = numeric()) {
m <- NULL
set <- function(y) {
x <<- y
m <<- NULL
}
get <- function() x
setinverse <- function(solve) m <<- solve
getinverse <- function() m
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## check whether the incoming matrix has already been solved
## if so, print a message and return that value
#
## if not, calculate the inverse using the solve function
## store the result in the cache (using setinverse) and return it
cacheSolve <- function(x, ...) {
m <- x$getinverse()
if(!is.null(m)) {
message("getting cached data")
return(m)
}
data <- x$get()
m <- solve(data, ...)
x$setinverse(m)
m
}