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
43 lines (38 loc) · 2.06 KB
/
Copy pathcachematrix.R
File metadata and controls
43 lines (38 loc) · 2.06 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
## Coursera R-programming course assignment: Caching the Inverse of a Matrix
## Forked from https://github.com/rdpeng/ProgrammingAssignment2
## Assumption: Matrix is square and invertible (no error checking).
## Example code:
# test <- makeCacheMatrix(matrix(1:4,2,2))
# cacheSolve(test)
## Creates a special matrix object that can cache its inverse.
makeCacheMatrix <- function(x = matrix()) {
i <- NULL ## Initially set inverse(i) to null
set <- function(y) { ## Set matrix value from user input
x <<- y # Value assigned to enclosing environment
i <<- NULL # Inverse set to null in enclosing environment
}
get <- function() { ## Get matrix value - used in cacheSolve()
x
}
setinverse <- function(inverse) { ## Set value of inverse - returned from cacheSolve()
i <<- inverse # Inverse set in enclosing environment
}
getinverse <- function() { ## Get value of inverse - used in cacheSolve()
i
}
list(set = set, get = get, ## Return function list
setinverse = setinverse,
getinverse = getinverse)
}
## Retrieves inverse from cache if it exists, otherwise computes inverse and caches it.
cacheSolve <- function(x, ...) {
i <- x$getinverse()
if(!is.null(i)) { ## If cached inverse exists...
message("getting cached matrix") # a. Write message
return(i) # b. Return cached inverse
}
data <- x$get() ## Else if no cached inverse...
i <- solve(data, ...) # a. Compute inverse
x$setinverse(i) # b. Cache inverse
i # c. Return the result
}