This repository was archived by the owner on Jan 12, 2025. It is now read-only.
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
56 lines (45 loc) · 1.78 KB
/
Copy pathcachematrix.R
File metadata and controls
56 lines (45 loc) · 1.78 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
## This set of functions is to substitute the solve()
## function (the inverse of the matrix) with its
## cachable version.
## Coding standard is as recommended in the course.
## Excessive comments are for training purposes only.
## This function creates a special version of matrix
## object that can keep cached version of its inverse
makeCacheMatrix <- function(x = matrix()) {
# we're just creating the matrix, so the cached
# version doesn't exist yet
cachedSolve <- NULL
# getter for x
get <- function() x
# setter for x
set <- function(newX = NULL) {
x <<- newX # setting the variable
cachedSolve <<- NULL # and clearing the cache
}
# getter for cached inverse
getSolve <- function() cachedSolve
# setter for cached inverse
setSolve <- function(newSolve = NULL) cachedSolve <<- newSolve
# cacheMatrix is really a 4-tuple, containing
# the getters for x, and cached version of the
# inverse matrix
list(get=get, set=set, getSolve=getSolve, setSolve=setSolve)
}
## This function calculates the inverse of a special
## version of matrix object and save it in its cache.
## If the inverse has been already calculated, it returns
## the cached version.
cacheSolve <- function(x, ...) {
# attempt to get the cached version
cachedSolve <- x$getSolve()
# attempt successful: return the cached version
if (!is.null(cachedSolve)) {
message("getting cached data")
return(cachedSolve)
}
# attempt failed: recalculate, cache and return
message("recalculating")
cachedSolve <- solve(x$get(), ...)
x$setSolve(cachedSolve)
cachedSolve
}