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
141 lines (130 loc) · 4.74 KB
/
cachematrix.R
File metadata and controls
141 lines (130 loc) · 4.74 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# Assignment: Caching the Inverse of a Matrix
#-------------------------------------------------------------------------------------
# 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.
#-------------------------------------------------------------------------------------
# makeCacheMatrix
# This function creates a matrix object that can "cache" its inverse.
#
# Input: Square Matrix
#
# Functions:
# -setMatrix - Sets and stores a matrix
# -getMatrix - Gets and returns the stored value of the matrix
# -setInverse - Sets the inverse of the matrix in the "cache"
# of the given matrix
# -getInverse - Geta the cached inverse value of the supplied matrix
#
# Returns: list of functions
#
#-------------------------------------------------------------------------------------
makeCacheMatrix <- function(x = matrix()) {
# holds the cached value or NULL if nothing is cached
# initially there is nothing cached so set cache to NULL
cache <- NULL
# store a matrix
setMatrix <- function(newValue) {
x <<- newValue
# no longer needed, flush the cache variable
cache <<- NULL
}
# returns the stored matrix
getMatrix <- function() {
x
}
# cache the given argument
setInverse <- function(solve) {
cache <<- solve
}
# get the cached value
getInverse <- function() {
cache
}
# return a list. Each named element of the list is a function
list(setMatrix = setMatrix, getMatrix = getMatrix,
setInverse = setInverse, getInverse = getInverse)
}
#-------------------------------------------------------------------------------------
# cacheSolve
# This function returns the inverse of the matrix if available. The function works
# through the following steps:
# 1. It checks if the inverse has already been computed. If available
# it gets the result and skips the computation.
# 2. If not, it computes the inverse, sets the value in the cache via
# setinverse function in makeCacheMatrix.
# 3. Return the inverse matirx
#
# Returns: a matrix that is the inverse of 'x'
#
# --This function assumes that the matrix is always invertible--
#
#------------------------------------------------------------------------------------
cacheSolve <- function(x, ...) {
# get the cached value of the matrix if it is available
inverse <- x$getInverse()
# if a cached value exists return it
if(!is.null(inverse)) {
message("Accessing cached data")
return(inverse)
}
# otherwise get the provided matrix
data <- x$getMatrix()
# caclulate the inverse
inverse <- solve(data)
# sets the value in the cache via setInverse
x$setInverse(inverse)
# return the inverse
inverse
}
#-------------------------------------------------------------------------------------
# Test & verification
#-------------------------------------------------------------------------------------
#
# create a square matrix
#> sqrMtrx <- c(0, 0, 0, 0, 1,
#+ 1, 0, 0, 0, 0,
#+ 0, 1, 0, 0, 0,
#+ 0, 0, 1, 0, 0,
#+ 0, 0, 0, 1, 0)
#> sqrMtrx <- matrix(sqrMtrx, ncol=5, byrow=TRUE)
#> x<-makeCacheMatrix(sqrMtrx)
#> x$getMatrix()
# [,1] [,2] [,3] [,4] [,5]
#[1,] 0 0 0 0 1
#[2,] 1 0 0 0 0
#[3,] 0 1 0 0 0
#[4,] 0 0 1 0 0
#[5,] 0 0 0 1 0
#
# Use solve() to verify our results
#
#> solve(sqrMtrx)
# [,1] [,2] [,3] [,4] [,5]
#[1,] 0 1 0 0 0
#[2,] 0 0 1 0 0
#[3,] 0 0 0 1 0
#[4,] 0 0 0 0 1
#[5,] 1 0 0 0 0
#
# Not in the cache at this point
#> cacheSolve(x)
# [,1] [,2] [,3] [,4] [,5]
#[1,] 0 1 0 0 0
#[2,] 0 0 1 0 0
#[3,] 0 0 0 1 0
#[4,] 0 0 0 0 1
#[5,] 1 0 0 0 0
#
# The previous run loaded the matrix in the cache and should
# be accesible now
#> cacheSolve(x)
#Accessing cached data
# [,1] [,2] [,3] [,4] [,5]
#[1,] 0 1 0 0 0
#[2,] 0 0 1 0 0
#[3,] 0 0 0 1 0
#[4,] 0 0 0 0 1
#[5,] 1 0 0 0 0