forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
66 lines (39 loc) · 1.77 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
## The 2 functions 'makeCacheMatrix' and 'cacheSolve' will operate on matrix and
## it's inverse. The function 'makeCachematrix' caches the matrix and it's inverse,
## so that it need not be computed again when the inverse is required.
## The function 'cacheSolve' returns the cached inverse. If the cache is not
## available, then it computes the inverse and caches it.
## This function takes a matrix as it's parameter and returns a list. The list
## contains all the operations that can be performed on a matrix(set, get, setinv, getinv).
makeCacheMatrix <- function(x = matrix()) {
#The matrix is intially not set. So, it's inverse it set to NULL.
inv <- NULL
#The 4 operations are defined here.
set <- function(mat) {
x <- mat
inv <- NULL
}
get <- function() x
setinv <- function(inverse) inv <- inverse
getinv <- function() inv
## The list of operations are returned here.
list( set = set, get = get, setinv = setinv, getinv = getinv )
}
## This function takes a list of operations that are returned by the 1st function.
## Then it returns the inverse of the matrix by either using the cached inverse,
## or by computing it dynamically.
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
inv <- x$getinv()
## Check if the inverse if NULL. If it is not, then it's already cached. So,
## simply return it.
if( !is.null(inv) ) {
message("getting cached inverse of the matrix")
return (inv)
}
## If it is not cached, the compute the inverse, cache it, and then return it.
mat <- x$get()
inv <- solve(mat,...)
x$setinv(inv)
inv
}