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
55 lines (43 loc) · 1.38 KB
/
cachematrix.R
File metadata and controls
55 lines (43 loc) · 1.38 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
## Calculates, caches and recalls inverse matrix:
## First function "makeCacheMatrix" creates a "matrix", which actually is a
## list of functions.
## Second function "cacheSolve" recalls inverse matrix if available. If not,
## it calculates and stores inverse matrix.
## TODO: check google R code style
## creates a special "matrix" object
## that can cache its inverse:
## a list containing a function to
#
# 1. set the value of the matrix
# 2. get the value of the matrix
# 3. set the value of the inverse matrix
# 4. get the value of the inverse matrix
makeCacheMatrix <- function(x = matrix()) {
inverse.matrix <- NULL
setInput <- function(y) {
input.matrix <<- y
inverse.matrix <<- NULL
}
getInput <- function() x
setInverse <- function(inverse) inverse.matrix <<- inverse
getInverse <- function() inverse.matrix
list(setInput = setInput,
getInput = getInput,
setInverse = setInverse,
getInverse = getInverse)
}
## checks if inverse matrix is cached
## yes: retrieve from cache
## no: calculate inverse matrix
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
inverse.matrix <- x$getInverse()
if(!is.null(inverse.matrix)) {
message("getting cached data")
return(inverse.matrix)
}
data <- x$getInput()
inverse.matrix <- solve(data, ...)
x$setInverse(inverse.matrix)
inverse.matrix
}