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
73 lines (61 loc) · 2.54 KB
/
cachematrix.R
File metadata and controls
73 lines (61 loc) · 2.54 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
## Basically, this works like the provided
## makeCacheMean()/cacheMean() example, only with a different
## function, namely solve() instead of mean()
## The following resource helped a lot in understanding this:
## https://github.com/lgreski/datasciencectacontent/
## blob/master/markdown/rprog-breakingDownMakeVector.md
## makeCacheMatrix() provides functions for storing
## the original matrix and the inverse. It is initialized with
## a matrix object. The functions and the data, i.e. the matrix,
## exist in the environment of makeCacheMatrix.
## By default, initialize the function with an empty matrix.
## This prevents errors when the get() function is called
makeCacheMatrix <- function(x = matrix()) {
i <- NULL ## Initialize the inverse
## Provide getters and setters
set <- function(y) {
x <<- y ## Assign the argument to the parent environment
i <<- NULL ## Initialize the inverse in the parent environment.
## Clears any cached value.
}
## Retrieve x from the parent environment
get <- function() x
## Assign argument to the inverse (i) in the parent environment
setinverse <- function(inverse) i <<- inverse
## Retrieve i from the parent environment
getinverse <- function() i
## Create a named list and return it to the parent environment
list(set = set, ## Give the name "set" to the set() function
get = get, ## Give the name "get" to the get() function
setinverse = setinverse, ## Give the name "setinverse" to the
## setinverse() function
getinverse = getinverse ## Give the name "getinverse" to the
## getinverse() function
)
## That way we can use the $ operator and
## don't have to access the functions like so: myMatrix[[2]]()
}
## cacheSolve() operates on the object that contains the
## makeCacheMatrix() functions and data.
## A regular matrix will not work!
## cacheSolve() checks whether makeCacheMatrix$getinverse()
## returns a matrix object; then it either
## - returns the cached result or
## - calculates it and stores it with makeCacheMatrix$setinverse()
cacheSolve <- function(x, ...) {
## Request the matrix that is the inverse of 'x'
i <- x$getinverse()
## If there is an actual inverse matrix there,
## return that.
if(!is.null(i)) {
message("getting cached data")
return(i)
}
## Otherwise, get the original matrix and solve it
data <- x$get()
i <- solve(data, ...)
## Then set the inverse...
x$setinverse(i)
## and return the inverse
i
}