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
66 lines (52 loc) · 1.46 KB
/
cachematrix.R
File metadata and controls
66 lines (52 loc) · 1.46 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
### This file creates a cache matrix that computes the Inverse Matrix,
### and stores it somewhere in memory via lexical scoping. The solving
### function in this file is aware of these caching mechanisms, unlike
### the native solve() for matrices given by the standard library.
# makeCacheMatrix
# creates setters and getters for a matrix that caches its inverse matrix
# INPUTS
# -------------------
# x : matrix()
# OUTPUTS
# -------------------
# list of functions: get, set, setInverse, getInverse
makeCacheMatrix <- function(x = matrix()) {
# Cached Inverse object
Im <- NULL
# Setter
set <- function(y) {
x <<- y
Im <<- NULL # Setting should set the Inverse Matrix to NULL
}
# Getter
get <- function() x
# Set Inverse function
setInverse <- function(Inverse) Im <<- Inverse
# Get Inverse function
getInverse <- function() Im
# List primitive that has our setters and getters
list(set = set,
get = get,
getInverse = getInverse,
setInverse = setInverse)
}
# cacheSolve
# It is aware of the cacheMatrix caching mechanism and will take
# advantage of the stored inverse matrix in the computation.
# INPUTs
# -------------------
# x : cacheMatrix list
# OUTPUTS
# -------------------
# Im : Inverse Matrix
cacheSolve <- function(x, ...) {
Im <- x$getInverse()
if(!is.null(Im)) {
message("Using cached inverse...")
return(Im)
}
data <- x$get()
Im <- solve(data)
x$setInverse(Im)
Im
}