Repository navigation
Expand file tree
/
Copy pathcachematrix.R
More file actions
76 lines (66 loc) · 1.93 KB
/
Copy pathcachematrix.R
File metadata and controls
76 lines (66 loc) · 1.93 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
## Specialized Matrix Framework to cache inverse calculation
## Solution 1 - Following the Vector/Mean template
## from the assignment description
## Creates a specialized Matrix Object that can store
## a cache of inverse calculation
makeCacheMatrix <- function(x = matrix()) {
# variable to hold the cached data
inverse <- NULL
# function to update the matrix values (and invalidate cache)
set <- function(y) {
x <<- y
inverse <<- NULL
}
# function to get the matrix values
get <- function() x
# function to set the inverse cached value
set.inverse <- function(value) inverse <<- value
# function to set the inverse cached value
get.inverse <- function() inverse
# return attributes
list(
set = set, get = get,
set.inverse = set.inverse,
get.inverse = get.inverse
)
}
## Operates in a specialized Matrix Object that can
## cache the inverse calculation
cacheSolve <- function(x, ...) {
inverse <- x$get.inverse()
if (is.null(inverse)) {
# Cache missing. Calculate and Store
message("cache miss: no previous value found.")
data <- x$get()
inverse <- solve(data, ...)
x$set.inverse(inverse)
}
# return
inverse
}
## Solution 2 - My take, more object oriented
## Makes cacheSolve() almost useless
## (commented out because differs very much from the template)
# makeCacheMatrix <- function(x = matrix()) {
# inverse <- NULL
# set <- function(y) {
# x <<- y
# inverse <<- NULL
# }
# get <- function() x
#
# get.inverse <- function(...) {
# if (is.null(inverse)) {
# message("cache miss: no previous value found.")
# inverse <<- solve(x, ...)
# return(inverse)
# }
# inverse
# }
#
# list(set = set, get = get, get.inverse = get.inverse)
# }
#
# cacheSolve <- function(x, ...) {
# x$get.inverse(...)
# }