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
34 lines (30 loc) · 908 Bytes
/
cachematrix.R
File metadata and controls
34 lines (30 loc) · 908 Bytes
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
# This creates a pair of functions which cache the inverse
# of a matrix in order to conserve energy spent in its computation.
# It is assumed that the matrix is invertible
makeCacheMatrix <- function(x = matrix()) {
# Creates a 'matrix' object that can cache its inverse
inv <- NULL
# Define object methods
set <- function(y) {
x <<- y
inv <<- NULL
}
get <- function() x
setinv <- function(solve) inv <<- solve
getinv <- function() inv
# Create list of methods/'matrix' object
list(set = set, get = get, setinv = setinv, getinv = getinv)
}
cacheSolve <- function(x, ...) {
# Returns a matrix that is the inverse of 'x'
# 'x' is an object returned by makeCacheMatrix
inv <- x$getinv()
if(!is.null(inv)) {
message('Getting cached data')
return(inv)
}
data <- x$get()
inv <- solve(data, ...)
x$setinv(inv)
inv
}