forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
67 lines (51 loc) · 1.82 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
# AUTHOR: Richard Hoska
# DESCRIPTION: Coursera R Programming - Week 3 Assignment 2
# Matrix inversion is usually a costly computation and there may be some benefit to
# caching the inverse of a matrix rather than computing it repeatedly (there are also
# alternatives to matrix inversion that we will not discuss here). Your assignment is to
# write a pair of functions that cache the inverse of a matrix.
# DATE: July 27 2014
## This function creates a special "matrix" object that can cache its inverse.
makeCacheMatrix <- function(x = matrix()) {
#--- CREATE variable to store the cached inverse matrix
inv <- NULL
#--- GETTER for matrix
get <- function() x
#--- SETTER for matrix
set <- function(y) {
x <<- y
inv <<- NULL
}
#--- SETTER for inverse
setinv <- function(inverse) inv <<- inverse
#--- GETTER for inverse
getinv <- function() inv
#--- RETURN matrix with newly defined functions
list(set = set, get = get, setinv = setinv, getinv = getinv)
}
## This function computes the inverse of the special "matrix" returned by makeCacheMatrix above.
## If the inverse has already been calculated (and the matrix has not changed), then cacheSolve
## should retrieve the inverse from the cache.
cacheSolve <- function(x, ...) {
#---
inv <- x$getinv()
#--- CHECK to see if inverse has already been calculated
if (!is.null(inv)) {
#--- OUTPUT a message to effect debugging
message("getting cached data")
#--- IF SO RETURN it
return(inv)
}
else
{
#--- OUTPUT a message to effect debugging
message("caching data")
# The inverse is not yet calculated, so we calculate it
data <- x$get()
inv <- solve(data, ...)
#--- CACHE the inverse
x$setinv(inv)
#--- RETURN inverse
return(inv)
}
}