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
59 lines (54 loc) · 1.96 KB
/
cachematrix.R
File metadata and controls
59 lines (54 loc) · 1.96 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
51
52
53
54
55
56
57
58
59
## In order to reduce the computational load of solving for
## the inverse of a matrix, the following functions are
## used to create a special version of a matrix that is
## able to cache its inverse.
## makeCacheMatrix is responsible for creating an object
## that stores a matrix, and its inverse.
## get and set are added as getter and
## setter functions for the original matrix.
## getinverse and setinverse are added as getter and
## setter functions for the inverse matrix.
makeCacheMatrix <- function(x = matrix()) {
# Initialize the inverse attribute as NULL (empty)
inv <- NULL
# Create setter function that sets a new value for the
# primary matrix and empties the cache.
# Note that the "super assignment" operator is used to
# set the attributes on the cache matrix object rather
# than inside the scope of the setter function.
set <- function(y) {
x <<- y
inv <<- NULL
}
# Create getter function that retrieves the primary matrix
get <- function() x
# Create setter function for the inverse matrix
setinverse <- function(i) inv <<- i
# Create getter function that retrieves the inverse matrix
getinverse <- function() inv
# Return the set of functions as a list
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## cacheSolve accepts as its primary argument any matrix
## created by makeCacheMatrix.
## It returns the cached inverse if available,
## or calculates the inverse otherwise.
## Any further arguments are passed to the solve method
cacheSolve <- function(x, ...) {
# Get the cached inverse and return it if it exists
inv <- x$getinverse()
if(!is.null(inv)) {
message("getting cached data")
return(inv)
}
# Cache not set, so get the original matrix
data <- x$get()
# Solve it
inv <- solve(data, ...)
# Set the cache for next time
x$setinverse(inv)
# Return the inverse matrix
inv
}