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
50 lines (41 loc) · 1.01 KB
/
cachematrix.R
File metadata and controls
50 lines (41 loc) · 1.01 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
## Programming Assignment 2 -- R Programming -JHU
## Demonstrate how effective chaching is done to speedup codes
## run this first to initialize
makeCacheMatrix <- function(x = matrix()) {
set(x)
}
## These are the functions to be used in the program
## function to define inv, the inverse
set <- function(y) {
x <<- y
inv <<- NULL
}
## have x handy
get <- function() {
x
}
## to transfer inv to the cache in this environment after it is calculated
## in cacheSolve
setinv <- function(solve) {
inv <<- solve
}
## to get inv from cache
getinv <- function() {
inv
}
## This is the caller function -- determines if a matrix calculation is truly
## needed or data is already available in the cache
## USAGE: cacheSolve(get) -- this is to make sure the same matrix x is
## our input
cacheSolve <- function(x) {
## Return a matrix that is the inverse of 'x'
inv <- getinv()
if(!is.null(inv)) {
message("getting cached data")
return(inv)
}
data <- x
inv <- solve(data, ...)
setinv(inv)
inv
}