forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
83 lines (80 loc) · 2.78 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
## --------------------------------------------------------------------------
## Coursera - R Programming - Programming Assignment 2 - Peer Assessment
##
## This R script implements two functions, makeCacheMatrix and cacheSolve,
## which reduce the time needed to determine the inverse of a matrix in
## repeated calculations, by storing the result in a cache.
## --------------------------------------------------------------------------
##
## --------------------------------------------------------------------------
## makeCacheMatrix
##
## Description
## Creates a special "matrix" object that can cache its inverse.
##
## Usage
## makeCacheMatrix(x = matrix())
##
## Arguments
## x a matrix.
##
## Value
## Special "matrix" object, i.e., a list containing functions to:
## set the value of the "matrix",
## get the value of the "matrix",
## set the inverse of the "matrix",
## get the inverse of the "matrix".
##
## Examples
## myMatrix <- makeCacheMatrix() creates empty myMatrix
## myMatrix <- makeCacheMatrix(x) creates myMatrix initialized with x
## myMatrix$set(x) sets myMatrix data to x
## myMatrix$get() gets myMatrix data
## myMatrix$setInverse(i) sets myMatrix inverse to i
## myMatrix$getInverse() gets myMatrix inverse
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL
set <- function(y) {
x <<- y
inv <<- NULL ## NULL marks the matrix has changed
}
get <- function() x
setInverse <- function(inverse) inv <<- inverse
getInverse <- function() inv
list(set = set,
get = get,
setInverse = setInverse,
getInverse = getInverse)
}
## --------------------------------------------------------------------------
## cacheSolve
##
## Description
## Computes the inverse of the special "matrix" x created by makeCacheMatrix.
##
## Usage
## cacheSolve(x, ...)
##
## Arguments
## x a special "matrix" created by makeCacheMatrix.
## x must be a square invertible matrix.
## ... further arguments passed to or from other methods.
##
## Value
## If the inverse of x has already been calculated and the matrix has not
## changed, cacheSolve returns the inverse of x from the cache. Otherwise,
## cacheSolve calculates the inverse, stores the result in the cache, and
## returns it.
cacheSolve <- function(x, ...) {
inv <- x$getInverse()
## inv is NULL when the inverse has not been calculated yet or
## the matrix has changed.
if(!is.null(inv)) {
message("Getting cached inverse")
} else {
data <- x$get()
inv <- solve(data, ...)
x$setInverse(inv)
}
inv
}