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
59 lines (46 loc) · 1.97 KB
/
Copy pathcachematrix.R
File metadata and controls
59 lines (46 loc) · 1.97 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
## Matrix inversion can be a costly computation. The functions defined
## in this file use caching to avoid repeated computations of the
## inverse for the same matrix.
## makeCacheMatrix creates a special data structure in which a matrix
## and its inverse are "stored".
makeCacheMatrix <- function(m = matrix()) {
# Variables in this scope are "shared" by the functions defined
# below and can therefore act as a common store. Variables 'm' (the
# formal parameter) and 'inverse' used to store the matrix and
# its inverse respectively. NULL used to indicate that a valid
# value hasn't been stored/cached yet.
inverse <- NULL
# Define functions to set (store) and get (retrieve) the matrix
# and its inverse
setMatrix <- function(newMatrix) {
# Update m of the outer scope
m <<- newMatrix
# If new matrix being stored then previously cached inverse
# no longer valid
inverse <<- NULL
}
getMatrix <- function() m
setInverse <- function(newInverse) inverse <<- newInverse
getInverse <- function() inverse
# Return the list of functions defined above
list(setMatrix = setMatrix, getMatrix = getMatrix,
setInverse = setInverse, getInverse = getInverse)
}
## cacheSolve is a more efficient implementation of inverse which
## caches its computation so that whenever the matrix doesn't change
## it retrieves the cached value rather than recomputing the inverse.
cacheSolve <- function(x, ...) {
## Input 'x' expected to be a "special" matrix of the form created
## by makeCacheMatrix
## Return the inverse of the matrix stored in 'x'
# Retrieve cached value of inverse
inverse <- x$getInverse()
# If no cached value then compute inverse and store it
if (is.null(inverse)) {
m <- x$getMatrix()
inverse <- solve(m, ...)
x$setInverse(inverse)
}
# Return the inverse
inverse
}