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
50 lines (47 loc) · 1.76 KB
/
cachematrix.R
File metadata and controls
50 lines (47 loc) · 1.76 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
## makeCacheMatrix and cacheSolve by Mark Smith
## General comment:
## It should be noted that this caching technique is only useful for caching
## one matrix which is computed repeatedly. Storing the matrix and inverse
## in a cache and then finding/retrieving the result from multiple stored
## results would make it much more useful in the real world but it is out
## of scope for this assignment.
## makeCacheMatrix creates an object to store the state of the cached matrix.
## An environment is created (x) which persists the object state by defining
## functions that use the environment (due to scoping rules) even though it
## is not the environment that they will be called from.
## This is what I know as a class definition in other languages, which we
## instantiate when we initialise it with an input matrix.
makeCacheMatrix <- function(x = matrix()) {
m <- NULL
set <- function(y) {
x <<- y
m <<- NULL
}
get <- function()
x
setinverse <- function(inv)
m <<- inv
getinverse <- function()
m
list(
set = set,
get = get,
setinverse = setinverse,
getinverse = getinverse
)
}
## cacheSolve operates by getting the supplied makeCacheMatrix objects and
## then checking if the inverse has previously been calculated. If not, it
## calculates, stores and returns the inverse, otherwise it returns the
## cached result.
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
}