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
64 lines (52 loc) · 2.46 KB
/
Copy pathcachematrix.R
File metadata and controls
64 lines (52 loc) · 2.46 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
60
61
62
63
64
## This code has a function to define a special matrix object that can
## cache its inverse value and a solver method to fetch the inverse value
## from the cache (if not present it will calculate the inverse value and
## stores in cache)
## Function : makeCacheMatrix
## Description: This fuction creates a special "Matrix" object
## that caches its inverse once computed. When the values
## are changed, cache is cleared. This special matrix
## object has four methods associated with it:
##
## 1. get() : returns the value of the matrix
## 2. set(x) : sets the matrix value as specified in x
## 3. getinv() : returns the cached inverse value of this matrix
## 4. setinv(i): caches the inverse value this matrix
##
## Usage e.g : 1. To initialize the object:
## a <- makeCacheMatrix(x*)
## x -> an optional regular matrix object
## 2. To set a new value
## a$set(x)
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL
set <- function(y) { # assigns a new value to matrix and
x <<- y # clears cache
inv <<- NULL
}
get <- function() x # returns value of matrix
setinv <- function(inv_matrix) inv <<- inv_matrix
# stores the computed inverse value in cache
getinv <- function() inv # returns cached inverse value
list(set = set, get = get,
setinv = setinv,
getinv = getinv)
}
## Function : cacheSolve
## Description: Returns a matrix that is the inverse of 'x'.
## This function first tries to fetch the inverse value of matrix
## from cache of special matrix object defined above. If cached
## value is not available it calculates the inverse and stores in cache
## Usage e.g : cacheSolve(x)
## x - special matrix object
cacheSolve <- function(x, ...) {
inv <- x$getinv() ## checks if inverse is already
if(!is.null(inv)) { ## available in the cache memory
message("getting cached data")
return(inv)
}
data <- x$get() ## if not available in cache
inv <- solve(data, ...) ## inverse is computed and cached
x$setinv(inv) ## for future use
inv ## returns the inverse matrix
}