forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
55 lines (50 loc) · 1.62 KB
/
cachematrix.R
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
# The following pair of functions provide an API for caching solutions to matrix inverses
# in order to avoid computing it repeatedly.
# Example of usage:
#
# source("cachematrix.R")
#
# matrix <- replicate(2000, rnorm(2000)) # Create a large matrix
# m <- makeCacheMatrix(matrix) # Wrap matrix using makeCacheMatrix
# i <- cacheSolve(m) # Solves inverse for the first time
#
# i <- cacheSolve(m) # subsequent calls will use cache and output "getting cache solution"
makeCacheMatrix <- function(x = matrix()) {
# A matrix wrapper that can holds it's inverse.
# This wrapper can be used by cacheSolve function to solve the inverse or get it from the cache.
#
# Args:
# x: matrix for whom we want to solve it's invese
#
# Returns:
# A list with setters and getter for either the matrix or it's inverse.
i <- NULL
set <- function(y) {
x <<- y
i <<- NULL
}
get <- function() x
setInverse <- function(inverse) i <<- inverse
getInverse <- function() i
list(set = set, get = get, setInverse = setInverse, getInverse = getInverse)
}
cacheSolve <- function(x, ...) {
# Solves the inverse of 'x' by either using the solve functon or a cache.
# Uses the cached solution if it is available (and outputs "getting cache solution").
# If it's not cached, calculates the inverse and updates the cache.
#
# Args:
# x: matrix wrapper created by makeCacheMatrix function
#
# Returns:
# A matrix that is the inverse of 'x'
i <- x$getInverse()
if (!is.null(i)) {
message("getting cache solution")
return(i)
}
matrix <- x$get()
i <- solve(matrix, ...)
x$setInverse(i)
i
}