forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
41 lines (32 loc) · 1.14 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
# This program contains two functions:
# 'makeCacheMatrix' which creates a special matrix object with a "cache"
# that records the result of its inversion (if called before) in the form
# of a list.
# This function creates a list object which contains a matrix as well as
# functions to handle a simple caching method for computing its inverse,
# through assignment to the global environment.
makeCacheMatrix <- function(x = matrix()) {
i <- NULL
list(
set = function(y) {x <<- y; i <<- NULL},
get = function() {x},
setinv = function(inv) i <<- inv,
getinv = function() {i}
)
}
# This function takes a "special" matrix object with a cached result of
# its inversion as argument, contained in a list as created by the
# makeCacheMatrix function and returns the result of inverting the matrix
# either by performing the computation (in case the result is not contained
# in the list cache) or by retrieving the result from the list cache.
cacheSolve <- function(x, ...) {
i <- x$getinv()
if(!is.null(i)) {
message("getting cached data")
return(i)
}
data <- x$get()
j <- solve(data, ...)
x$setinv(j)
j -> out
}