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
71 lines (60 loc) · 2.14 KB
/
cachematrix.R
File metadata and controls
71 lines (60 loc) · 2.14 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
## cachematrix.R
#
# This module provides functions which allow a "cached matrix" to be
# created which memoizes (caches the computation of) the inverse of
# the matrix. Thus, repeated calls to "cacheSolve" return the same
# computed inverse unless and until the matrix stored therein is
# changed, upon which time, the inverse will be recomputed.
#
# The name "cacheSolve" (presumably) comes from "solve", which is the
# usual means to directly compute the inverse of a matrix in R.
#
# This implementation closely resembles the example code in the
# assignment, obviously.
## makeCacheMatrix(m)
#
# m: matrix
#
# returns: object(list) with methods to manipulate the object's state
#
# makeCacheMatrix is a constructor/factory which creates an object
# with various methods that allow one to cache a matrix in in and to
# retrieve that matrix's inverse. I'm going to use the same
# conventions for method names as the vector example.
makeCacheMatrix <- function(cachedMatrix = matrix()) {
cachedInversion <- NULL
set <- function(newMatrix) {
cachedMatrix <<- newMatrix
cachedInversion <<- NULL
}
get <- function() cachedMatrix
setInversion <- function(inversion) cachedInversion <<- inversion
getInversion <- function() cachedInversion
list(set = set, get = get,
setInversion = setInversion, getInversion = getInversion)
}
## cacheSolve(m)
#
# m: matrix
#
# returns: the inverse of the matrix
#
# cacheSolve produces the equivalent answer as "solve", but the
# computation of the inverse will happen only once for a specific
# matrix. (I.e., if the same matrix object, as returned by
# "makeCacheMatrix", is passed into this function repeatedly, the
# actual invocation of "solve" will only happen once.)
cacheSolve <- function(m, ...) {
i <- m$getInversion()
if (!is.null(i)) {
# It is unclear if the following message (a la the example
# code) is intended to be included in the submitted homework
# assignment. Thus, I will leave it visible.
message("getting cached data")
return(i)
}
rawMatrix <- m$get()
i <- solve(rawMatrix, ...)
m$setInversion(i)
i
}