forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
53 lines (48 loc) · 1.68 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
## Functions to calculate the inverse of a given square matrix, store the result in
## cache and reuse this stored value if needed again.
##
## Usage: First assign makeCacheMatrix() on the input matrix to an object and then
## apply cacheSolve() to that object.
## Example:
## z <- makeCacheMatrix(matrix(c(1:4),2,2))
## cacheSolve(z)
##
## Input: An invertible matrix.
## Output: The inverse of the input (plus messages about cache usage).
## makeCacheMatrix() function creates a list with 4 functions for:
## - storing/modifying the input data and initializing the result - set()
## - getting the input data - get()
## - storing/modifying the result - setinverse()
## - getting the result - getinverse()
makeCacheMatrix <- function(x = numeric()) {
i <- NULL
set <- function(y) {
x <<- y
i <<- NULL
}
get <- function() x
setinverse <- function(mean) i <<- mean
getinverse <- function() i
## Returns the list composed of 4 functions
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## cacheSolve() function looks for the inverse matrix value in cache.
## If found, returns it with a message informing about cache retrieval.
## If not, calculates the inverse, stores it in the cache and returns it.
cacheSolve <- function(x, ...) {
## Tries to get the inverse from cache
i <- x$getinverse()
## If found the result in cache, returns it
if(!is.null(i)) {
message("getting cached data")
return(i)
}
## If not found, gets the input data, calculates its inverse, stores it in cache
## and returns it
data <- x$get()
i <- solve(data, ...)
x$setinverse(i)
i
}