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
49 lines (44 loc) · 1.54 KB
/
cachematrix.R
File metadata and controls
49 lines (44 loc) · 1.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
## Put comments here that give an overall description of what your
## functions do
## function makeCacheMatrix returns a list of functions that provide
## cache capabilities to input variable x
makeCacheMatrix <- function(x = matrix()) {
# Define a cache variable that is empty
invCache <- NULL
# Define function to set the value of x and to clear the cache
set <- function(y) {
x <<- y
invCache <<- NULL
}
# Define the function to get the value of x, simply returning x
get <- function() x
# Define function to fill the cache
setinv <- function(inv) invCache <<- inv
# Define function to get the cache content
getinv <- function() invCache
# Return list of functions
list(set = set, get = get,
setinv = setinv,
getinv = getinv)
}
## Function cacheSolve returns the inverse of input matrix x (assumption
## that x is always invertible). The inverse is cached if not done so
## before, and if already cached this cache is returned directly and the
## inverse calculation is skipped
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
# get the cached inverse of x
inv <- x$getinv()
# If the obtained cached is filled, return the cache
if(!is.null(inv)) {
message("getting cached data")
return(inv)
}
# Apparently the cache was empty, now calculate the inverse
data <- x$get()
inv <- solve(data, ...)
# Store the calculated inverse in the cache
x$setinv(inv)
# Return the inverse of x
inv
}