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
51 lines (38 loc) · 1.83 KB
/
Copy pathcachematrix.R
File metadata and controls
51 lines (38 loc) · 1.83 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
## Coursera - R Programming by Roger D. Peng
## Programming Assignment 2 - Peer Reviewed
## Utilising R, this assignment aims to using caching techniques to speed up CPU costly calculations, in this
## case calculations on matrices. This involves the use of two functions -
## 'makeCacheMatrix' This function creates a special "matrix" object that can cache its inverse
## 'cacheSolve' This function computes the inverse of the special "matrix" returned by makeCacheMatrix.
## If the inverse has already been calculated (and the matrix has not changed), then the
## cachesolve should retrieve the inverse from the cache.
## Objects 'x' will be the matrix and 'inv' its solved inverse matrix
## After creating the special matrix object, this function will cache its inverse.
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL
set <- function(y) {
x <<- y
inv <<- NULL
}
get <- function() x
set_inv <- function(solve) inv <<- solve
get_inv <- function() inv
list(set = set, get = get,
set_inv = set_inv,
get_inv = get_inv)
}
## This function will check to see if the solution to the inverse matrix 'x' has already been cached, if it has then
## will return the cached data, if not will go ahead and calculate the inverse, cache it and return it.
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
#Checks to see if the solution to the inverse of the matrix object x is already cached
inv <- x$get_inv()
if(!is.null(inv)) {
message("getting cached inverse matrix data")
return(inv)
}
data <- x$get()
inv <- solve(data, ...)
x$set_inv(inv)
inv
}