forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
71 lines (62 loc) · 2.12 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
## Functions written by owlice
## ___________________________________
## START General comments
##
## This file contains two functions; the first is makeCacheMatrix and the
## second is cacheSolve.
##
## makeCacheMatrix creates a matrix object that can cache its inverse
##
## cacheSolve computes the inverse of the matrix object returned by
## the function makeCacheMatrix if this has not already been computed. If
## the inverse has already been calculated and the matrix is unchanged,
## cachesolve retrieves the inverse from the cache
##
## END General comments
## ___________________________________
## ___________________________________
## START makeCacheMatrix general comments
##
## makeCacheMatrix creates a matrix object that can cache its inverse
##
## END makeCacheMatrix general comments
## ___________________________________
##
makeCacheMatrix <- function(x = matrix()) {
## initialize m to NULL
m <- NULL
## create set function
set <- function(y) {
x <<- y
m <<- NULL
}
## create get function
get <- function()x
## invert m
m <<- solve(x)
## set origMatrix as supervariable so can test for computation of inverse in cacheSolve
origMatrix <<- x
}
## ___________________________________
## START cacheSolve general comments
##
## cacheSolve tests to see whether the inverse of the matrix object returned
## by function makeCacheMatrix has already been computed; if so, it returns
## what it has cached. If the inverse has not been computed and the matrix is
## unchanged, this function computes, caches, and returns the inverse
##
## END cacheSolve general comments
## ___________________________________
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
## if the inverse of the matrix has been computed, grab info from cache
if(!is.null(m) && origMatrix == x) {
message("getting cached data")
return(m)
}
else{
## the inverse of the matrix has not been computed, so compute and return it
m <- solve(x)
m
}
}